-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
405 lines (341 loc) · 14.6 KB
/
Copy pathmain.py
File metadata and controls
405 lines (341 loc) · 14.6 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
from distributions import Distributions as dt
import random
import math
rush_hours = [(90, 210), (420, 540)]
total_min = 660
workers = [(0, 0, 0)]
clients = []
arrives = {}
on_kitchen = {}
times = {}
current_time = 0
cook_count = 0
def ppl_arrive(index, time):
arrives[index] = time
def in_range(x, a):
return a[0] <= x and x <= a[1]
def in_rush_hour(t):
return in_range(t, rush_hours[0]) or in_range(t, rush_hours[1])
def arrive_event(i, ta, ts1, ts2, ts3, \
total_time, cook_count, current_time, \
clients, workers, Ca, on_kitchen, testing_with_worker):
# if min([ta, ts1, ts2, ts3]) == ta:
current_time = ta
Ca += 1
r = random.randint(0, 1)
arrives[i] = (ta, r)
lt = current_time
if r:
# sandwish
ts = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts = current_time + dt.uniform(5, 8, random.random())
# print(f'-------------------> entre con {i}')#,{ts - current_time}, {current_time}, {ta}, {ts1}, {ts2}, {ts3}, {workers}, {cook_count}')
if in_rush_hour(current_time) and cook_count < 3 and testing_with_worker:
if clients:
# print(f'entre con {clients[0]}')
if workers[0] == 0:
clients.append(i)
item = clients.pop(0)
workers[0] = item
food = arrives[workers[0]][1]
if food:
# sandwish
ts = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts = current_time + dt.uniform(5, 8, random.random())
on_kitchen[workers[0]] = current_time
ts1 = ts
elif workers[1] == 0:
clients.append(i)
item = clients.pop(0)
workers[1] = item
food = arrives[workers[1]][1]
if food:
# sandwish
ts = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts = current_time + dt.uniform(5, 8, random.random())
on_kitchen[workers[1]] = current_time
ts2 = ts
elif workers[2] == 0:
clients.append(i)
item = clients.pop(0)
workers[2] = item
food = arrives[workers[2]][1]
if food:
# sandwish
ts = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts = current_time + dt.uniform(5, 8, random.random())
on_kitchen[workers[2]] = current_time
ts3 = ts
cook_count += 1
else:
# print(f'entre a 3 con {i}')
if workers[0] == 0:
workers[0] = i
ts1 = ts
elif workers[1] == 0:
workers[1] = i
ts2 = ts
elif workers[2] == 0:
workers[2] = i
ts3 = ts
on_kitchen[i] = ts
cook_count += 1
# print(f'entro {i}')
elif not in_rush_hour(current_time) and cook_count < 2 and testing_with_worker:
if workers[0] == 0:
workers[0] = i
ts1 = ts
elif workers[1] == 0:
workers[1] = i
ts2 = ts
cook_count += 1
on_kitchen[i] = ts
# print(f'entro {i}')
elif cook_count < 2 and not testing_with_worker:
if workers[0] == 0:
workers[0] = i
ts1 = ts
elif workers[1] == 0:
workers[1] = i
ts2 = ts
cook_count += 1
on_kitchen[i] = ts
# print(f'entro {i}')
else:
clients.append(i)
# print(f'le hice append a {i}')
# print(workers)
return True, Ca, ts1, ts2, ts3, cook_count, current_time, clients, workers, on_kitchen
def ts1_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients: list, leaves, on_kitchen):
# print(f'_______________________evento 1 sali con {workers[0]} {workers}{current_time} {ts1} {ts2} {ts3}')
Cp += 1
current_time = ts1
leaves[workers[0]] = current_time
if clients:
workers[0] = clients[0]
clients.pop(0)
food = arrives[workers[0]][1]
if food:
# sandwish
ts1 = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts1 = current_time + dt.uniform(5, 8, random.random())
on_kitchen[workers[0]] = current_time
else:
workers[0] = 0
ts1 = math.inf
cook_count -= 1
return True, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves
def ts2_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients: list, leaves, on_kitchen):
# print(f'_______________________evento 2 sali con {workers[1]} {workers} {current_time} {ts1} {ts2} {ts3}')
Cp += 1
current_time = ts2
leaves[workers[1]] = current_time
if clients:
workers[1] = clients[0]
clients.pop(0)
food = arrives[workers[1]][1]
if food:
# sandwish
ts2 = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts2 = current_time + dt.uniform(5, 8, random.random())
on_kitchen[workers[1]] = current_time
else:
workers[1] = 0
ts2 = math.inf
cook_count -= 1
return True, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves
return False, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves
def ts3_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients: list, leaves, in_rush_hour, on_kitchen):
# print(f'_______________________evento 3 sali con {workers[2]} {workers} {current_time} {ts1} {ts2} {ts3}')
Cp += 1
current_time = ts3
leaves[workers[2]] = current_time
if clients:
if in_rush_hour:
workers[2] = clients[0]
clients.pop(0)
food = arrives[workers[2]][1]
if food:
# sandwish
ts3 = current_time + dt.uniform(3, 5, random.random())
else:
#sushi
ts3 = current_time + dt.uniform(5, 8, random.random())
on_kitchen[workers[2]] = current_time
return True, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves
workers[2] = 0
ts3 = math.inf
cook_count -= 1
return True, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves
if __name__ == "__main__":
means = []
verbose = 0
for _ in range(1000):
withcooker = 0
withoutcooker = 0
### initializing the kitchen without an extra cooker
arrives = {}
leaves = {}
on_kitchen = {}
current_time = 0
ts1 = math.inf
ts2 = math.inf
ts3 = math.inf
Ca = 0
Cp = 0
i = 1
workers = [0, 0, 0]
cook_count = 0
clients = []
ta = current_time
testing_with_worker = 0
##############################################
current = 0
_ta = [0]
__ta = [-1]
while True:
if not in_rush_hour(current):
current += dt.exponential(1, random.random())
__ta.append(0)
else:
current += dt.exponential(4, random.random())
__ta.append(1)
if current > total_min:
break
_ta.append(current)
########################
while i != len(_ta):
m = min(_ta[i], ts1, ts2, ts3)
if m == _ta[i]:
status, Ca, ts1, ts2, ts3, cook_count, current_time, clients, workers, on_kitchen = \
arrive_event(i, _ta[i], ts1, ts2, ts3, \
total_min, cook_count, current_time, \
clients, workers, Ca, on_kitchen, testing_with_worker)
i += 1
elif m == ts1:
s1, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts1_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
elif m == ts2:
s2, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts2_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
else:
s3, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts3_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves, \
in_rush_hour(current_time), on_kitchen)
while clients or workers != [0, 0, 0]:
m = min(ts1, ts2, ts3)
if m == ts1:
s1, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts1_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
elif m == ts2:
s2, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts2_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
else:
s3, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts3_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves, \
in_rush_hour(current_time), on_kitchen)
for a in range(len(_ta) - 1):
on_kitchen[a + 1]
arrives[a + 1][0]
if on_kitchen[a + 1] - arrives[a + 1][0] >= 5:
withoutcooker += 1
### initializing the kitchen with an extra cooker
arrives = {}
leaves = {}
on_kitchen = {}
current_time = 0
ts1 = math.inf
ts2 = math.inf
ts3 = math.inf
Ca = 0
Cp = 0
i = 1
workers = [0, 0, 0]
cook_count = 0
clients = []
ta = current_time
lambda_ = random.randint(2, 5)
testing_with_worker = 1
while i != len(_ta):
m = min(_ta[i], ts1, ts2, ts3)
if m == _ta[i]:
# print(f'entre a arrive, {(_ta[i], ts1, ts2, ts3)} {workers}')
status, Ca, ts1, ts2, ts3, cook_count, current_time, clients, workers, on_kitchen = \
arrive_event(i, _ta[i], ts1, ts2, ts3, \
total_min, cook_count, current_time, \
clients, workers, Ca, on_kitchen, testing_with_worker)
i += 1
elif m == ts1:
# print(f'entre a ts1 {(_ta[i], ts1, ts2, ts3)} {workers}')
s1, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts1_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
elif m == ts2:
# print(f'entre a ts2 {(_ta[i], ts1, ts2, ts3)} {workers}')
s2, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts2_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
else:
# print(f'entre a ts3 {(_ta[i], ts1, ts2, ts3)} {workers}')
s3, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts3_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves, \
in_rush_hour(current_time), on_kitchen)
while clients or workers != [0, 0, 0]: #ts1 != math.inf and ts2 != math.inf and ts3 != math.inf:
m = min(ts1, ts2, ts3)
if m == ts1:
# print(f'entre a ts1 {(ts1, ts2, ts3)} {workers}')
s1, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts1_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
elif m == ts2:
# print(f'entre a ts2 {(ts1, ts2, ts3)} {workers}')
s2, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts2_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, \
leaves, on_kitchen)
else:
# print(f'entre a ts3 {(ts1, ts2, ts3)} {workers}')
s3, ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves = \
ts3_leaving_event(ts1, ts2, ts3, Cp, current_time, cook_count, workers, clients, leaves, \
in_rush_hour(current_time), on_kitchen)
for a in range(len(_ta) - 1):
try:
on_kitchen[a + 1]
except KeyError as e:
print(str(e), type(str(e)))
print('was it in rudh hour?', __ta[int(str(e))])
print(len(_ta))
print(on_kitchen.keys())
print(arrives.keys())
print(leaves.keys())
if on_kitchen[a + 1] - arrives[a + 1][0] >= 5:
withcooker += 1
if verbose:
print('number of customers who waited more than 5 minutes with one more cook during rush hour:', withcooker * 100 / (len(_ta)- 1), '%')
print('number of customers who waited more than 5 minutes without one more cook during rush hour:', withoutcooker * 100 / (len(_ta)- 1), '%')
print('total number of customers:', len(arrives))
rh = 0
nrh = 0
for i in __ta:
if i: rh +=1
else: nrh +=1
print('number of customers in rush hour:', rh)
print('number of customers in normal time:', nrh)
if len(arrives) != 0:
means.append((withoutcooker - withcooker) * 100 / len(arrives))
print('After finding the mean of all the percentages obtained from the simulations, the conclusion is that the mean is:', sum(means) / len(means))