Skip to content

Commit

Permalink
Removed more lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
djordon committed Jul 2, 2016
1 parent b459417 commit e62dc4a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
21 changes: 16 additions & 5 deletions examples/example_grocery_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import queueing_tool as qt
import functools

import numpy as np
import queueing_tool as qt


# Make an adjacency list
adja_list = {0: {1: {}}, 1: {k: {} for k in range(2, 22)}}
Expand All @@ -16,15 +19,23 @@
q_classes = {0: qt.NullQueue, 1: qt.QueueServer, 2: qt.QueueServer}

# Define the parameters for each of the queues
rate = lambda t: 25 + 350 * np.sin(np.pi * t / 2)**2
arr_f = lambda t: qt.poisson_random_measure(t, rate, 375)
ser_f = lambda t: t + np.random.exponential(0.2 / 2.5)
def rate(t):
return 25 + 350 * np.sin(np.pi * t / 2)**2

def ser_f(t):
return t + np.random.exponential(0.2 / 2.5)

def identity(t):
return t

arr_f = functools.partial(qt.poisson_random_measure, rate=rate, rate_max=375)


# Make a mapping between the edge types and the parameters used to make those
# queues. If a particular parameter is not given then th defaults are used.
q_args = {
1: {'arrival_f': arr_f,
'service_f': lambda t: t,
'service_f': identity,
'AgentFactory': qt.GreedyAgent},
2: {'num_servers': 1,
'service_f': ser_f}
Expand Down
13 changes: 7 additions & 6 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,10 @@ def test_QueueNetwork_greedy_routing(self):
rho = np.random.uniform(0.75, 1)
nSe = np.random.randint(1, 10)
mu = lam / (3 * rho * nSe)
arr = lambda t: t + np.random.exponential(1 / lam)
ser = lambda t: t + np.random.exponential(1 / mu)

def arr(t): return t + np.random.exponential(1 / lam)
def ser(t): return t + np.random.exponential(1 / mu)
def ser_id(t): return t

adj = {
0 : {1: {'edge_type': 1}},
Expand All @@ -277,7 +279,7 @@ def test_QueueNetwork_greedy_routing(self):
arg = {
1: {
'arrival_f': arr,
'service_f': lambda t: t,
'service_f': ser_id,
'AgentFactory': qt.GreedyAgent
},
2: {
Expand All @@ -286,15 +288,15 @@ def test_QueueNetwork_greedy_routing(self):
}
}

qn = qt.QueueNetwork(g, q_classes=qcl, q_args=arg)
qn = qt.QueueNetwork(g, q_classes=qcl, q_args=arg)
qn.initialize(edges=(0, 1))
qn.max_agents = 5000

num_events = 1000
ans = np.zeros(num_events, bool)
e01 = qn.g.edge_index[(0, 1)]
edg = qn.edge2queue[e01].edge
c = 0
c = 0

while c < num_events:
qn.simulate(n=1)
Expand Down Expand Up @@ -325,7 +327,6 @@ def test_QueueNetwork_initialize_Error(self):
self.qn.initialize(edge_type=1)

def test_QueueNetwork_initialization(self):

# Single edge index
k = np.random.randint(0, self.qn.nE)
self.qn.clear()
Expand Down
35 changes: 20 additions & 15 deletions tests/test_queue_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import unittest

import networkx as nx
Expand Down Expand Up @@ -70,9 +71,10 @@ def test_QueueServer_copy(self):

def test_QueueServer_active_cap(self):

r = lambda t: 2 + np.sin(t)
arr = lambda t: qt.poisson_random_measure(t, r, 3)
q = qt.QueueServer(active_cap=1000, arrival_f=arr, seed=12)
def r(t): return 2 + np.sin(t)
arr = functools.partial(qt.poisson_random_measure, rate=r, rate_max=3)

q = qt.QueueServer(active_cap=1000, arrival_f=arr, seed=12)
q.set_active()
q.simulate(n=3000)

Expand All @@ -84,8 +86,9 @@ def test_QueueServer_accounting(self):

nSe = np.random.randint(1, 10)
mu = self.lam / (self.rho * nSe)
arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.exponential(1 / mu)

def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.exponential(1 / mu)

q = qt.QueueServer(num_servers=nSe, arrival_f=arr, service_f=ser)
q.set_active()
Expand Down Expand Up @@ -115,11 +118,12 @@ def test_QueueServer_deactivate(self):
def test_QueueServer_simulation(self):

nSe = np.random.randint(1, 10)
mu = self.lam / (self.rho * nSe)
arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.exponential(1 / mu)
mu = self.lam / (self.rho * nSe)

def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.exponential(1 / mu)

q = qt.QueueServer(num_servers=nSe, arrival_f=arr, service_f=ser)
q = qt.QueueServer(num_servers=nSe, arrival_f=arr, service_f=ser)
q.set_active()
num_events = 5000

Expand Down Expand Up @@ -152,11 +156,12 @@ def test_QueueServer_simulation(self):
def test_LossQueue_accounting(self):

nSe = np.random.randint(1, 10)
mu = self.lam / (self.rho * nSe)
arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.exponential(1 / mu)
mu = self.lam / (self.rho * nSe)

def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.exponential(1 / mu)

q = qt.LossQueue(num_servers=nSe, arrival_f=arr, service_f=ser)
q = qt.LossQueue(num_servers=nSe, arrival_f=arr, service_f=ser)
q.set_active()
num_events = 15000

Expand All @@ -180,8 +185,8 @@ def test_LossQueue_blocking(self):
k = np.random.randint(5, 15)
scl = 1 / (mu * k)

arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.gamma(k, scl)
def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.gamma(k, scl)

q = qt.LossQueue(num_servers=nSe, arrival_f=arr, service_f=ser)
q.set_active()
Expand Down
21 changes: 11 additions & 10 deletions tests/test_statistical_properties.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import math
import os
import unittest
Expand Down Expand Up @@ -30,10 +31,10 @@ def setUp(self):
def test_Markovian_QueueServer(self):

nSe = np.random.randint(1, 10)
mu = self.lam / (self.rho * nSe)
mu = self.lam / (self.rho * nSe)

arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.exponential(1 / mu)
def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.exponential(1 / mu)

q = qt.QueueServer(num_servers=nSe, arrival_f=arr, service_f=ser)
n = 50000
Expand Down Expand Up @@ -71,10 +72,10 @@ def test_Markovian_QueueServer(self):
def test_QueueServer_Littleslaw(self):

nSe = np.random.randint(1, 10)
mu = self.lam / (self.rho * nSe)
mu = self.lam / (self.rho * nSe)

arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.exponential(1 / mu)
def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.exponential(1 / mu)

q = qt.QueueServer(num_servers=nSe, arrival_f=arr, service_f=ser)
n = 500000
Expand All @@ -100,8 +101,8 @@ def test_LossQueue_blocking(self):
k = np.random.randint(5, 15)
scl = 1 / (mu * k)

arr = lambda t: t + np.random.exponential(1 / self.lam)
ser = lambda t: t + np.random.gamma(k, scl)
def arr(t): return t + np.random.exponential(1 / self.lam)
def ser(t): return t + np.random.gamma(k, scl)

q2 = qt.LossQueue(num_servers=nSe, arrival_f=arr, service_f=ser)
q2.set_active()
Expand Down Expand Up @@ -136,8 +137,8 @@ def test_poisson_random_measure(self):
# random variables.
# This test should fail some percentage of the time

rate = lambda t: 0.5 + 4 * np.sin(np.pi * t / 12)**2
arr_f = lambda t: qt.poisson_random_measure(t, rate, 4.5)
def rate(t): return 0.5 + 4 * np.sin(np.pi * t / 12)**2
arr_f = functools.partial(qt.poisson_random_measure, rate=rate, rate_max=4.5)

nSamp = 15000
nArr = 1000
Expand Down

0 comments on commit e62dc4a

Please sign in to comment.