-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtutorial_5.py
More file actions
415 lines (329 loc) · 10.9 KB
/
Copy pathtutorial_5.py
File metadata and controls
415 lines (329 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from collections import deque
from heapq import heappop, heappush
import numpy as np
from scipy.stats import expon, uniform
np.random.seed(8)
ARRIVAL = 0
DEPARTURE = 1
ECONOMY = 0
BUSINESS = 1
class Job:
def __init__(self):
self.arrival_time = 0
self.service_time = 0
self.customer_type = ECONOMY
self.server_type = ECONOMY
self.departure_time = 0
self.queue_length_at_arrival = 0
def sojourn_time(self):
return self.departure_time - self.arrival_time
def waiting_time(self):
return self.sojourn_time() - self.service_time
def service_start(self):
return self.departure_time - self.service_time
def __repr__(self):
return f"{self.customer_type}, {self.server_type}, {self.arrival_time}, {self.service_time}, {self.service_start()}, {self.departure_time}\n"
def generate_jobs(F, G, p_business, num_jobs):
jobs = set()
time = 0
a = F.rvs(num_jobs)
b = G.rvs(num_jobs)
p = uniform(0, 1).rvs(num_jobs)
for n in range(num_jobs):
job = Job()
job.arrival_time = time + a[n]
job.service_time = b[n]
if p[n] < p_business:
job.customer_type = BUSINESS
else:
job.customer_type = ECONOMY
jobs.add(job)
time = job.arrival_time
return jobs
def generate_jobs_bad_implementation(F, G, p_business, num_jobs):
# the difference in performance is tremendous
for n in range(num_jobs):
job = Job()
job.arrival_time = time + F.rvs()
job.service_time = G.rvs()
if uniform(0, 1).rvs() < p_business:
job.customer_type = BUSINESS
else:
job.customer_type = ECONOMY
jobs.add(job)
time = job.arrival_time
return jobs
class GGc_with_business:
def __init__(self, c, jobs):
self.b = 1 # number of b servers
self.c = c # number of e servers
self.jobs = jobs
self.num_b_busy = 0
self.num_e_busy = 0
self.stack = []
self.b_queue = deque()
self.e_queue = deque()
self.fill_stack()
def fill_stack(self):
for job in sorted(self.jobs, key=lambda j: j.arrival_time):
heappush(self.stack, (job.arrival_time, job, ARRIVAL))
def handle_arrival(self, time, job):
if job.customer_type == BUSINESS:
job.queue_length_at_arrival = len(self.b_queue)
else:
job.queue_length_at_arrival = len(self.e_queue)
if job.customer_type == ECONOMY:
if self.num_e_busy < self.c:
job.server_type = ECONOMY
self.start_service(time, job)
elif self.num_b_busy < self.b:
job.server_type = BUSINESS
self.start_service(time, job)
else:
self.e_queue.append(job)
else: # business customer
if self.num_b_busy < self.b:
job.server_type = BUSINESS
self.start_service(time, job)
elif self.num_e_busy < self.c:
job.server_type = ECONOMY
self.start_service(time, job)
else:
self.b_queue.append(job)
def start_service(self, time, job):
if job.server_type == BUSINESS:
self.num_b_busy += 1
else:
self.num_e_busy += 1
job.departure_time = time + job.service_time
heappush(self.stack, (job.departure_time, job, DEPARTURE))
def pop_from_queue_set_server_and_start(self, time, queue, server_type):
next_job = queue.popleft()
next_job.server_type = server_type
self.start_service(time, next_job)
def handle_departure(self, time, job):
if job.server_type == BUSINESS:
self.num_b_busy -= 1
if self.b_queue:
self.pop_from_queue_set_server_and_start(time, self.b_queue, BUSINESS)
elif self.e_queue:
self.pop_from_queue_set_server_and_start(time, self.e_queue, BUSINESS)
else: # economy server free
self.num_e_busy -= 1
if self.e_queue:
self.pop_from_queue_set_server_and_start(time, self.e_queue, ECONOMY)
elif self.b_queue:
self.pop_from_queue_set_server_and_start(time, self.b_queue, ECONOMY)
def run(self):
time = 0
while self.stack: # not empty
time, job, epoch_type = heappop(self.stack)
if epoch_type == ARRIVAL:
self.handle_arrival(time, job)
else:
self.handle_departure(time, job)
def print_served_job(self):
for j in sorted(self.jobs, key=lambda j: j.arrival_time):
print(j)
def mean_waiting_time(self, customer_type=None):
if customer_type is None:
jobs = self.jobs
else:
jobs = set(j for j in self.jobs if j.customer_type == customer_type)
return sum(j.waiting_time() for j in jobs) / len(jobs)
def max_waiting_time(self, customer_type=None):
if customer_type is None:
return max(j.waiting_time() for j in self.jobs)
else:
return max(
j.waiting_time() for j in self.jobs if j.customer_type == customer_type
)
def sakasegawa(F, G, c):
labda = 1.0 / F.mean()
ES = G.mean()
rho = labda * ES / c
EWQ_1 = rho ** (np.sqrt(2 * (c + 1)) - 1) / (c * (1 - rho)) * ES
ca2 = F.var() * labda * labda
ce2 = G.var() / ES / ES
return (ca2 + ce2) / 2 * EWQ_1
# this time I worked according to a test driven procedure
def DD1_test_1():
# test with only business customers
c = 0
F = uniform(1, 0.0001)
G = expon(0.5, 0.0001)
p_business = 1
num_jobs = 5
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
ggc.print_served_job()
quit()
#DD1_test_1()
def DD1_test_2():
# test with only economy customers
c = 1
F = uniform(1, 0.0001)
G = expon(0.5, 0.0001)
p_business = 0
num_jobs = 5
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
ggc.print_served_job()
quit()
#DD1_test_2()
def DD1_test_3():
# test with only economy customers but only a business server
c = 0
F = uniform(1, 0.0001)
G = expon(0.5, 0.0001)
p_business = 0
num_jobs = 5
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
ggc.print_served_job()
quit()
#DD1_test_3()
def DD2_test_1():
# test with only economy customers and one e_server. As the b_server is always present, we must have 2 servers.
# assume that all jobs arrive at time 0, and have service time 1
c = 1
F = uniform(0, 0.0001)
G = expon(1, 0.0001)
p_business = 0
num_jobs = 10
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
ggc.print_served_job()
quit()
#DD2_test_1()
def mm1_test_1():
# test with only business customers but no e_server, very few jobs
c = 0
labda = 0.9
mu = 1
F = expon(scale=1.0 / labda)
G = expon(scale=1.0 / mu)
p_business = 1
num_jobs = 10
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
ggc.print_served_job()
quit()
#mm1_test_1()
def mm1_test_2():
# test with only economy customers but no e_server
c = 0
labda = 0.9
mu = 1
F = expon(scale=1.0 / labda)
G = expon(scale=1.0 / mu)
p_business = 0
print("theory: ", sakasegawa(F, G, c + 1)) # 1 for the business server
num_jobs = 100_000
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
print("mean waiting: ", ggc.mean_waiting_time())
quit()
#mm1_test_2()
def mm1_test_3():
# test with only business customers but no e_server
c = 0
labda = 0.9
mu = 1
F = expon(scale=1.0 / labda)
G = expon(scale=1.0 / mu)
p_business = 1
print("theory: ", sakasegawa(F, G, c + 1)) # 1 for the business server
num_jobs = 100_000
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
print("mean waiting: ", ggc.mean_waiting_time())
quit()
#mm1_test_3()
def mm2_test_1():
# test with only business customers and 1 e_server
c = 1
labda = 0.9
mu = 1
F = expon(scale=1.0 / labda)
G = expon(scale=1.0 / mu)
p_business = 1
print("theory: ", sakasegawa(F, G, c + 1)) # 1 for the business server
num_jobs = 100_000
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
# mind that Sakasegawa's result is an approximation for the M/M/c with c>1
print("mean waiting: ", ggc.mean_waiting_time())
quit()
#mm2_test_1()
def mm2_test_2():
# test with only economy customers and 1 e_server
c = 1
labda = 0.9
mu = 1
F = expon(scale=1.0 / labda)
G = expon(scale=1.0 / mu)
p_business = 0
print("theory: ", sakasegawa(F, G, c + 1)) # 1 for the business server
num_jobs = 100_000
jobs = generate_jobs(F, G, p_business, num_jobs)
ggc = GGc_with_business(c, jobs)
ggc.run()
print("mean waiting: ", ggc.mean_waiting_time())
quit()
#mm2_test_2()
# CASE ANALYSIS
import copy
def case_analysis(jobs, c):
# we need the same jobs for all cases, so that we can compare in a fair way.
b_jobs = set(copy.copy(j) for j in jobs if j.customer_type == BUSINESS)
e_jobs = set(copy.copy(j) for j in jobs if j.customer_type == ECONOMY)
# Case 1: each class its own server, no sharing
bus = GGc_with_business(0, b_jobs)
bus.run()
eco = GGc_with_business(c - 1, e_jobs)
eco.run()
# Case 2: sharing with business server
shared = GGc_with_business(c, jobs)
shared.run()
print("separate: bus mean", bus.mean_waiting_time())
print("shared: bus mean: ", shared.mean_waiting_time(BUSINESS))
print("separate: bus max", bus.max_waiting_time())
print("shared: bus max: ", shared.max_waiting_time(BUSINESS))
print("separate: eco mean", eco.mean_waiting_time())
print("shared: eco mean: ", shared.mean_waiting_time(ECONOMY))
print("separate: eco max", eco.max_waiting_time())
print("shared: eco max: ", shared.max_waiting_time(ECONOMY))
print("shared: all mean: ", shared.mean_waiting_time())
print("shared: all max: ", shared.max_waiting_time())
print()
def case1():
num_jobs = 300
labda = num_jobs / 90
F = expon(scale=1.0 / labda)
G = uniform(1, 2)
p_business = 0.1
c = 6
jobs = generate_jobs(F, G, p_business, num_jobs)
case_analysis(jobs, c)
quit()
#case1()
def case2():
num_jobs = 300
labda = num_jobs / 180
F = expon(scale=1.0 / labda)
G = uniform(1, 2)
p_business = 0.05
c = 5
jobs = generate_jobs(F, G, p_business, num_jobs)
case_analysis(jobs, c)
quit()
#case2()