Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7ba209d

Browse files
committedJul 12, 2016
Add notebooks for priority queues and baulking functions
1 parent eeed875 commit 7ba209d

File tree

3 files changed

+694
-0
lines changed

3 files changed

+694
-0
lines changed
 

‎Baulking Functions - I.ipynb

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Baulking Functions - I"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"### This notebook will illustrate a simple deterministic example, designed to show the difference between baulking and queueing capacities"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"outputs": [],
24+
"source": [
25+
"import ciw"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 2,
31+
"metadata": {
32+
"collapsed": true
33+
},
34+
"outputs": [],
35+
"source": [
36+
"def my_baulking_function(n):\n",
37+
" if n < 3:\n",
38+
" return 0.0\n",
39+
" return 1.0"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 3,
45+
"metadata": {
46+
"collapsed": true
47+
},
48+
"outputs": [],
49+
"source": [
50+
"params_dict = {\n",
51+
" 'Arrival_distributions': [['Deterministic', 5.0], ['Deterministic', 23.0]],\n",
52+
" 'Service_distributions': [['Deterministic', 21.0], ['Deterministic', 1.5]],\n",
53+
" 'Transition_matrices': [[0.0, 0.0], [1.0, 0.0]],\n",
54+
" 'Number_of_servers': [1, 1],\n",
55+
" 'Baulking_functions': [my_baulking_function, None]\n",
56+
"}"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"### Now if we run this to time t=48, we will get the following chain of events:"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"| Time | Queue Node 1 | In Service Node 1 | Queue Node 2 | In Service Node 2 | Customer Finished a Service | Baulked Customers |\n",
71+
"|------|--------------|-------------------|--------------|-------------------|-----------------------------|-------------------|\n",
72+
"| 0 | | | | | | |\n",
73+
"| 5 | | 1 | | | | |\n",
74+
"| 10 | 2 | 1 | | | | |\n",
75+
"| 15 | 3, 2 | 1 | | | | |\n",
76+
"| 20 | 3, 2 | 1 | | | | 4 |\n",
77+
"| 23 | 3, 2 | 1 | | 5 | | 4 |\n",
78+
"| 24.5 | 5, 3, 2 | 1 | | | 5 | 4 |\n",
79+
"| 25 | 5, 3, 2 | 1 | | | 5 | 4, 6 |\n",
80+
"| 26 | 5, 3 | 2 | | | 5, 1 | 4, 6 |\n",
81+
"| 30 | 5, 3 | 2 | | | 5, 1 | 4, 6, 7 |\n",
82+
"| 35 | 5, 3 | 2 | | | 5, 1 | 4, 6, 7, 8 |\n",
83+
"| 40 | 5, 3 | 2 | | | 5, 1 | 4, 6, 7, 8, 9 |\n",
84+
"| 45 | 5, 3 | 2 | | | 5, 1 | 4, 6, 7, 8, 9, 10 |\n",
85+
"| 46 | 5, 3 | 2 | | 11 | 5, 1 | 4, 6, 7, 8, 9, 10 |\n",
86+
"| 47 | 5 | 3 | | 11 | 5, 1, 2 | 4, 6, 7, 8, 9, 10 |\n",
87+
"| 47.5 | 11, 5 | 3 | | | 5, 1, 2, 11 | 4, 6, 7, 8, 9, 10 |\n",
88+
"| 48 | | | | | | |"
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"### We would therefore expect the following results:"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 4,
101+
"metadata": {
102+
"collapsed": true
103+
},
104+
"outputs": [],
105+
"source": [
106+
"expected_baulking_dictionary = {1:{0:[20.0, 25.0, 30.0, 35.0, 40.0, 45.0]}, 2:{0:[]}}\n",
107+
"expected_ids_of_completed_customers = set([5, 1, 2, 11])\n",
108+
"expected_waits_of_completed_customers = set([0.0, 0.0, 0.0, 16])\n",
109+
"expected_arrival_dates_of_completed_customers = set([5.0, 10.0, 23.0, 46.0])\n",
110+
"expected_service_start_dates_of_completed_customers = set([5.0, 23.0, 26.0, 46.0])\n",
111+
"expected_service_end_dates_of_completed_customers = set([24.5, 26.0, 47.0, 47.5])"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"### Let's check:"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 5,
124+
"metadata": {
125+
"collapsed": false
126+
},
127+
"outputs": [],
128+
"source": [
129+
"N = ciw.create_network(params_dict)\n",
130+
"Q = ciw.Simulation(N)"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": 6,
136+
"metadata": {
137+
"collapsed": true
138+
},
139+
"outputs": [],
140+
"source": [
141+
"Q.simulate_until_max_time(48)\n",
142+
"recs = Q.get_all_records()"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 7,
148+
"metadata": {
149+
"collapsed": false
150+
},
151+
"outputs": [
152+
{
153+
"data": {
154+
"text/plain": [
155+
"True"
156+
]
157+
},
158+
"execution_count": 7,
159+
"metadata": {},
160+
"output_type": "execute_result"
161+
}
162+
],
163+
"source": [
164+
"Q.baulked_dict == expected_baulking_dictionary"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 8,
170+
"metadata": {
171+
"collapsed": false
172+
},
173+
"outputs": [
174+
{
175+
"data": {
176+
"text/plain": [
177+
"True"
178+
]
179+
},
180+
"execution_count": 8,
181+
"metadata": {},
182+
"output_type": "execute_result"
183+
}
184+
],
185+
"source": [
186+
"set([r.id_number for r in recs]) == expected_ids_of_completed_customers"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": 9,
192+
"metadata": {
193+
"collapsed": false
194+
},
195+
"outputs": [
196+
{
197+
"data": {
198+
"text/plain": [
199+
"True"
200+
]
201+
},
202+
"execution_count": 9,
203+
"metadata": {},
204+
"output_type": "execute_result"
205+
}
206+
],
207+
"source": [
208+
"set([r.waiting_time for r in recs]) == expected_waits_of_completed_customers"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": 10,
214+
"metadata": {
215+
"collapsed": false
216+
},
217+
"outputs": [
218+
{
219+
"data": {
220+
"text/plain": [
221+
"True"
222+
]
223+
},
224+
"execution_count": 10,
225+
"metadata": {},
226+
"output_type": "execute_result"
227+
}
228+
],
229+
"source": [
230+
"set([r.arrival_date for r in recs]) == expected_arrival_dates_of_completed_customers"
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": 11,
236+
"metadata": {
237+
"collapsed": false
238+
},
239+
"outputs": [
240+
{
241+
"data": {
242+
"text/plain": [
243+
"True"
244+
]
245+
},
246+
"execution_count": 11,
247+
"metadata": {},
248+
"output_type": "execute_result"
249+
}
250+
],
251+
"source": [
252+
"set([r.service_start_date for r in recs]) == expected_service_start_dates_of_completed_customers"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": 12,
258+
"metadata": {
259+
"collapsed": false
260+
},
261+
"outputs": [
262+
{
263+
"data": {
264+
"text/plain": [
265+
"True"
266+
]
267+
},
268+
"execution_count": 12,
269+
"metadata": {},
270+
"output_type": "execute_result"
271+
}
272+
],
273+
"source": [
274+
"set([r.service_end_date for r in recs]) == expected_service_end_dates_of_completed_customers"
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": null,
280+
"metadata": {
281+
"collapsed": true
282+
},
283+
"outputs": [],
284+
"source": []
285+
}
286+
],
287+
"metadata": {
288+
"kernelspec": {
289+
"display_name": "Python 3",
290+
"language": "python",
291+
"name": "python3"
292+
},
293+
"language_info": {
294+
"codemirror_mode": {
295+
"name": "ipython",
296+
"version": 3
297+
},
298+
"file_extension": ".py",
299+
"mimetype": "text/x-python",
300+
"name": "python",
301+
"nbconvert_exporter": "python",
302+
"pygments_lexer": "ipython3",
303+
"version": "3.5.1"
304+
}
305+
},
306+
"nbformat": 4,
307+
"nbformat_minor": 0
308+
}

‎Baulking Functions - II.ipynb

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Baulking Functions - II"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"### This example is more involved that Baulking Functions - I, and shows the flexibility of the baulking functions functionality"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 4,
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"outputs": [],
24+
"source": [
25+
"import ciw\n",
26+
"import math"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {
33+
"collapsed": true
34+
},
35+
"outputs": [],
36+
"source": [
37+
"def baulking_function_A(n):\n",
38+
" if n < 3:\n",
39+
" return 0.0\n",
40+
" if n < 7:\n",
41+
" return 0.5\n",
42+
" if n < 11:\n",
43+
" return 1.0"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 3,
49+
"metadata": {
50+
"collapsed": true
51+
},
52+
"outputs": [],
53+
"source": [
54+
"def baulking_function_B(n):\n",
55+
" return 1.0 - (1.0 / n)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 14,
61+
"metadata": {
62+
"collapsed": true
63+
},
64+
"outputs": [],
65+
"source": [
66+
"def baulking_function_C(n):\n",
67+
" if n < 10:\n",
68+
" return math.exp(-n)\n",
69+
" return 0.0"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"### Baulking functions can be as complicated as you like, as long as they take in a positive integer (number of customers at the node), and return a probability (a float between 0.0 and 1.0).\n",
77+
"### This example will show how different baulking functions can be assigned to different node - customer class pairs. We will even combine baulking with queueing capacities. Networks can be as complicated as you like... we could even have added priorities too, but let's stick with baulking and queue capacities for now."
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 15,
83+
"metadata": {
84+
"collapsed": false
85+
},
86+
"outputs": [],
87+
"source": [
88+
"params_dict = {\n",
89+
" 'Arrival_distributions': {'Class 0': [['Exponential', 8.0], ['Exponential', 4.0]],\n",
90+
" 'Class 1': [['Exponential', 6.0], ['Exponential', 5.0]]},\n",
91+
" 'Service_distributions': {'Class 0': [['Exponential', 10.0], ['Exponential', 7.0]],\n",
92+
" 'Class 1': [['Exponential', 6.0], ['Exponential', 4.0]]},\n",
93+
" 'Transition_matrices': {'Class 0': [[0.0, 0.3], [0.3, 0.3]],\n",
94+
" 'Class 1': [[0.2, 0.1], [0.0, 0.2]]},\n",
95+
" 'Number_of_servers': [2, 2],\n",
96+
" 'Baulking_functions': {'Class 0': [baulking_function_A, None],\n",
97+
" 'Class 1': [baulking_function_B, baulking_function_C]}\n",
98+
"}"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 16,
104+
"metadata": {
105+
"collapsed": false
106+
},
107+
"outputs": [],
108+
"source": [
109+
"N = ciw.create_network(params_dict)\n",
110+
"Q = ciw.Simulation(N)"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 17,
116+
"metadata": {
117+
"collapsed": false
118+
},
119+
"outputs": [
120+
{
121+
"ename": "ZeroDivisionError",
122+
"evalue": "float division by zero",
123+
"output_type": "error",
124+
"traceback": [
125+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
126+
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
127+
"\u001b[0;32m<ipython-input-17-d373bed3e80b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mQ\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msimulate_until_max_time\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
128+
"\u001b[0;32m/Users/geraintianpalmer/Documents/Ciw/ciw/simulation.py\u001b[0m in \u001b[0;36msimulate_until_max_time\u001b[0;34m(self, max_simulation_time)\u001b[0m\n\u001b[1;32m 245\u001b[0m \u001b[0mcurrent_time\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnext_active_node\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext_event_date\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 246\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mcurrent_time\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mmax_simulation_time\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 247\u001b[0;31m \u001b[0mnext_active_node\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhave_event\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 248\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtransitive_nodes\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 249\u001b[0m \u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate_next_event_date\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcurrent_time\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
129+
"\u001b[0;32m/Users/geraintianpalmer/Documents/Ciw/ciw/arrival_node.py\u001b[0m in \u001b[0;36mhave_event\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 71\u001b[0m priority_class)\n\u001b[1;32m 72\u001b[0m \u001b[0mnext_node\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msimulation\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtransitive_nodes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext_node\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 73\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrelease_individual\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_node\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnext_individual\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 74\u001b[0m self.event_dates_dict[self.next_node][\n\u001b[1;32m 75\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext_class\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mincrement_time\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
130+
"\u001b[0;32m/Users/geraintianpalmer/Documents/Ciw/ciw/arrival_node.py\u001b[0m in \u001b[0;36mrelease_individual\u001b[0;34m(self, next_node, next_individual)\u001b[0m\n\u001b[1;32m 123\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecord_rejection\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_node\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 124\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 125\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecide_baulk\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_node\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnext_individual\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 126\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msend_individual\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnext_node\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnext_individual\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
131+
"\u001b[0;32m/Users/geraintianpalmer/Documents/Ciw/ciw/arrival_node.py\u001b[0m in \u001b[0;36mdecide_baulk\u001b[0;34m(self, next_node, next_individual)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0mrnd_num\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 42\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mrnd_num\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mnext_node\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbaulking_functions\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext_class\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_node\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumber_of_individuals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 43\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecord_baulk\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext_node\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 44\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
132+
"\u001b[0;32m<ipython-input-3-9946f13f7a01>\u001b[0m in \u001b[0;36mbaulking_function_B\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbaulking_function_B\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1.0\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1.0\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
133+
"\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero"
134+
]
135+
}
136+
],
137+
"source": [
138+
"Q.simulate_until_max_time(100)"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": 18,
144+
"metadata": {
145+
"collapsed": false
146+
},
147+
"outputs": [
148+
{
149+
"data": {
150+
"text/plain": [
151+
"0.006737946999085467"
152+
]
153+
},
154+
"execution_count": 18,
155+
"metadata": {},
156+
"output_type": "execute_result"
157+
}
158+
],
159+
"source": [
160+
"math.exp(-5)"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {
167+
"collapsed": true
168+
},
169+
"outputs": [],
170+
"source": []
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {
176+
"collapsed": true
177+
},
178+
"outputs": [],
179+
"source": []
180+
}
181+
],
182+
"metadata": {
183+
"kernelspec": {
184+
"display_name": "Python 3",
185+
"language": "python",
186+
"name": "python3"
187+
},
188+
"language_info": {
189+
"codemirror_mode": {
190+
"name": "ipython",
191+
"version": 3
192+
},
193+
"file_extension": ".py",
194+
"mimetype": "text/x-python",
195+
"name": "python",
196+
"nbconvert_exporter": "python",
197+
"pygments_lexer": "ipython3",
198+
"version": "3.5.1"
199+
}
200+
},
201+
"nbformat": 4,
202+
"nbformat_minor": 0
203+
}

‎Priority Queues.ipynb

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Priority Queues"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"### Import Ciw"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {
21+
"collapsed": false
22+
},
23+
"outputs": [],
24+
"source": [
25+
"import ciw\n",
26+
"from random import seed\n",
27+
"from numpy import mean, random"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"### We will simulate an M/M/1 queue with two priority classes. The two classes will have the same service rate (1.0) but different arrival rates (0.2 and 0.6 respectively). The first class will have higher priority than the second. Let's build the parameters dictionary:"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 2,
40+
"metadata": {
41+
"collapsed": true
42+
},
43+
"outputs": [],
44+
"source": [
45+
"params_dict = {\n",
46+
" 'Arrival_distributions': {'Class 0': [['Exponential', 0.2]],\n",
47+
" 'Class 1': [['Exponential', 0.6]]},\n",
48+
" 'Service_distributions': {'Class 0': [['Exponential', 1.0]],\n",
49+
" 'Class 1': [['Exponential', 1.0]]},\n",
50+
" 'Transition_matrices': {'Class 0': [[0.0]],\n",
51+
" 'Class 1': [[0.0]]},\n",
52+
" 'Number_of_servers': [1],\n",
53+
" 'Priority_classes': {'Class 0': 0,\n",
54+
" 'Class 1': 1}\n",
55+
"\n",
56+
" }"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"### This corresponds to Example 4.3 in http://www.win.tue.nl/~iadan/que/h4.pdf, we shall compare the results obtained analytically to the results obtained using Ciw. Analytically we expect the average time in the system for those in Class 0 to be 2.0, and for those in Class 1 to be 6.0."
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"### We shall run 150 trials, running each trial for a maximum time of 500, and a warm-up time of 100 will be used."
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 3,
76+
"metadata": {
77+
"collapsed": true
78+
},
79+
"outputs": [],
80+
"source": [
81+
"throughput_class0 = []\n",
82+
"throughput_class1 = []"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 4,
88+
"metadata": {
89+
"collapsed": true
90+
},
91+
"outputs": [],
92+
"source": [
93+
"random.seed(20)\n",
94+
"seed(20)"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 6,
100+
"metadata": {
101+
"collapsed": false
102+
},
103+
"outputs": [],
104+
"source": [
105+
"for iteration in range(150):\n",
106+
" N = ciw.create_network(params_dict)\n",
107+
" Q = ciw.Simulation(N)\n",
108+
" Q.simulate_until_max_time(500)\n",
109+
" recs = Q.get_all_records()\n",
110+
" throughput_class0.append(mean([r.waiting_time + r.service_time for r in recs if r.customer_class==0 if r.arrival_date > 100]))\n",
111+
" throughput_class1.append(mean([r.waiting_time + r.service_time for r in recs if r.customer_class==1 if r.arrival_date > 100]))"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 7,
117+
"metadata": {
118+
"collapsed": false
119+
},
120+
"outputs": [
121+
{
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"1.98638760088\n"
126+
]
127+
}
128+
],
129+
"source": [
130+
"print(mean(throughput_class0))"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": 8,
136+
"metadata": {
137+
"collapsed": false
138+
},
139+
"outputs": [
140+
{
141+
"name": "stdout",
142+
"output_type": "stream",
143+
"text": [
144+
"5.91798349036\n"
145+
]
146+
}
147+
],
148+
"source": [
149+
"print(mean(throughput_class1))"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {
156+
"collapsed": true
157+
},
158+
"outputs": [],
159+
"source": []
160+
}
161+
],
162+
"metadata": {
163+
"kernelspec": {
164+
"display_name": "Python 3",
165+
"language": "python",
166+
"name": "python3"
167+
},
168+
"language_info": {
169+
"codemirror_mode": {
170+
"name": "ipython",
171+
"version": 3
172+
},
173+
"file_extension": ".py",
174+
"mimetype": "text/x-python",
175+
"name": "python",
176+
"nbconvert_exporter": "python",
177+
"pygments_lexer": "ipython3",
178+
"version": "3.5.1"
179+
}
180+
},
181+
"nbformat": 4,
182+
"nbformat_minor": 0
183+
}

0 commit comments

Comments
 (0)
Please sign in to comment.