-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
2623 lines (2068 loc) · 76.2 KB
/
webserver.py
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# libraries
# import matplotlib.pyplot as plt
# import matplotlib
# import numpy as np
# import pandas as pd
# import csv
# import seaborn as sns
# from matplotlib import colors
# from collections import OrderedDict
# from matplotlib.ticker import MaxNLocator
# from matplotlib import gridspec
# from time import sleep
# import sys
# import scipy.stats
# import json
import time
# import os
# from datetime import datetime
# import psutil
from math import floor, ceil, log, sqrt
from statistics import mean, stdev, median
# from queue import Queue as QU
import argparse
# Parsing command line arguments
parser = argparse.ArgumentParser(description="Web server")
parser.add_argument('-l', '--Lambda', type=float, metavar='', required=True, help='the parameter λ of the distribution of interarrival times')
parser.add_argument('-Kc', '--CPU_Queue', type=int, metavar='', required=True, help='the number K of customers that the CPU queue may hold')
parser.add_argument('-Ki', '--IO_Queue', type=int, metavar='', required=True, help='the number K of customers that the I/O queue may hold')
parser.add_argument('-C', '--Customers', type=int, metavar='', required=True, help='the number C of customers served before the program terminates')
parser.add_argument('-L', '--L', type=int, metavar='', required=True, help='an integer L such that L = 0 (runs service disciplines), L>0 (runs web server)')
parser.add_argument('-M', '--mode', type=int, metavar='', required=True, help='1 – FCFS, 2 – LCFS-NP, 3 – SJF-NP, 4 – Prio-NP, 5 – Prio-P')
# parser.add_argument('-i', '--iteration', type=int, metavar='', required=True, help='iteration i')
args = parser.parse_args()
### INPUT GLOBAL PARAMS ####
global LAMBDA
global K
global Kc
global Ki
global C
global L
global M
# global i
LAMBDA = args.Lambda
Kc = args.CPU_Queue
K = Kc
Ki = args.IO_Queue
C = args.Customers
L = args.L
M = args.mode
# i = args.iteration
global LOG
LOG = 0
global LISTTYPEP
LISTTYPEP = 0
print("*************************\nInput Parameters:\n")
print(" Lambda: {}\n Kcpu: {}\n Ki/o: {}\n C: {}\n".format(LAMBDA, Kc, Ki, C))
# DEFINED variables for ran0 function
IA = 16807
IM = 2147483647
AM = (1.0/IM)
IQ = 127773
IR = 2836
MASK = 123459876
def ran0(idum):
k = 0
ans = 0.0
idum = idum ^ MASK
k= int(idum/IQ)
idum=IA*(idum-k*IQ)-IR*k
if idum < 0:
idum+=IM
ans= AM*idum
idum = idum ^ MASK
return idum, ans;
def expdev(idum, alpha):
idum, dummy = ran0(idum)
idum, dummy = ran0(idum)
while dummy == 0.0:
idum, dummy=ran0(idum)
return idum, -log(dummy)/alpha
def serialize_dictionary(in_resd):
tmpd = {}
for k in list(in_resd.keys()):
if k in [-10, -20, -30, -40]:
tmpd[k] = in_resd[k]
else:
resd = in_resd[k]
tmp = {"event_type": resd.event_type,
"ID": resd.customer_ID,
"inter_arrival": resd.inter_arrival,
"arrival_time": resd.arrival_time,
"service_time": resd.service_time,
"departure_time": resd.departure_time,
"clock": resd.clock,
"served": resd.served,
"inqueue": resd.inqueue,
"queue_type": resd.queue_type,
"departure_generated": resd.departure_generated,
"vc": resd.visit_c,
"v1": resd.visit_1,
"v2": resd.visit_2,
"v3": resd.visit_3,
"wtc": resd.wt_c,
"wt1": resd.wt_1,
"wt2": resd.wt_2,
"wt3": resd.wt_3,
"in_system": resd.in_system,
"priority": resd.priority}
tmpd[k] = tmp
return tmpd
class NodeCustomer(object):
def __init__(self, event_type=None, customer_ID=None, inter_arrival=None, next_node=None):
self.event_type = event_type
self.customer_ID = customer_ID
self.inter_arrival = inter_arrival
self.arrival_time = 0
self.service_time = 0
self.departure_time = 0
self.clock = 0
self.served = 0
self.inqueue = False
self.queue_type = -1
self.departure_generated = -1
self.visit_c = 0
self.visit_1 = 0
self.visit_2 = 0
self.visit_3 = 0
self.wt_c = []
self.wt_1 = []
self.wt_2 = []
self.wt_3 = []
self.in_system = 0
self.priority = 0
self.next_node = next_node
def get_data(self):
return self.customer_ID
def get_id(self):
return self.customer_ID
def get_etype(self):
return self.event_type
def get_next(self):
return self.next_node
def set_next(self, new_next):
self.next_node = new_next
class LinkedList(object):
def __init__(self, head=None, event_type=None, clock=None):
self.head = head
self.list_size = 0
self.clock = clock
self.list_size = 0
def insert(self, new_node):
new_node.set_next(self.head)
self.head = new_node
self.list_size += 1
def get_time(self):
return self.head.clock
def get_size(self):
return self.list_size
def search(self, data):
current = self.head
found = False
while current and found is False:
if current.get_data() == data:
found = True
else:
current = current.get_next()
if current is None:
raise ValueError("Data not in list")
return current
def delete(self, data):
current = self.head
previous = None
found = False
while current and found is False:
if current.get_data() == data:
found = True
else:
previous = current
current = current.get_next()
if current is None:
raise ValueError("Data not in list")
if previous is None:
self.head = current.get_next()
self.list_size -= 1
else:
previous.set_next(current.get_next())
self.list_size -= 1
def print_list(self):
current = self.head
i = 0
print("PRINTING LIST OF SIZE:", self.list_size)
if current != None:
while 1:
print("{} PRINTLIST - ID: {}, Type: {}, Priority:{}, CLK:{}, AT: {}, ST: {}, DT: {}, CL: {}".format(\
i,current.customer_ID, current.event_type, current.priority, current.clock,\
current.arrival_time, current.service_time, current.departure_time, current.clock))
current = current.get_next()
if current == None:
break
i += 1
if i == self.list_size:
break
class LinkedList_P(object):
# global log
def __init__(self, head=None, event_type=None, clock=None):
self.head = NodeCustomer()
self.list_size = 0
self.clock = clock
self.list_size = 0
def insert(self, new_node):
# print(self.list_size, new_node.customer_ID)
if self.list_size == 0:
self.head.set_next(new_node)
new_node.set_next(None)
else:
if new_node.clock == 17.284573016828233:
print("FOUND:", new_node.customer_ID, new_node.event_type)
self.print_list()
new_node.set_next(self.head.get_next())
self.head.set_next(new_node)
self.list_size += 1
if LOG:
print("IN INSERT:", self.list_size)
self.print_list()
def get_time(self):
return self.head.clock
def get_size(self):
return self.list_size
def search(self, data):
current = self.head
found = False
while current and found is False:
if current.get_data() == data:
found = True
else:
current = current.get_next()
if current is None:
raise ValueError("Data not in list")
return current
def delete(self, cust):
current = self.head.get_next()
previous = None
found = False
while current != None:
if LOG:
print("del:" , current.get_id(), current.get_etype(), cust.customer_ID, cust.event_type)
if current.get_id() == cust.customer_ID and current.get_etype() == cust.event_type:
found = True
break
else:
previous = current
current = current.get_next()
if found == True:
if current != None and previous == None:
if LOG:
print("del 1")
self.head.set_next(current.get_next())
self.list_size -= 1
elif current != None and previous != None:
if LOG:
print("del 2")
previous.set_next(current.get_next())
self.list_size -= 1
elif current == None and previous != None:
if LOG:
print("del 3")
previous.set_next(None)
self.list_size -= 1
elif current == None and previous == None:
if LOG:
print("del 4 funny")
# previous.set_next(None)
# self.list_size -= 1
elif current is None and found == False:
print("RASISE")
raise ValueError("Data not in list")
# else:
# print("Fell Out")
def print_list(self):
if LOG:
current = self.head.get_next()
i = 0
if current == None:
print("PRINTLIST- LIST EMPTY")
if current != None:
while current != None:
print("{} PRINTLIST - ID: {}, Type: {}, Priority:{}, QType: {}, CLK:{}, AT: {}, ST: {}, DT: {}, CL: {}".format(\
i,current.customer_ID, current.event_type, current.priority, current.queue_type, current.clock,\
current.arrival_time, current.service_time, current.departure_time, current.clock))
current = current.get_next()
# if current == None:
# break
i += 1
# if i > 5:
# break
# Other Global Variables
global master_clock
global seed
global event_list
global task
global priority_seed
global seedIO
priority_seed = 500
seed = 5555
seedIO = 6666
# if M == 5:
# event_list = LinkedList_P()
# else:
# event_list = LinkedList()
event_list = LinkedList_P()
def assign_priority():
global priority_seed
priority_seed, rand_num = ran0(priority_seed)
if rand_num >= 0 and rand_num < 0.25:
return 1
if rand_num >= 0.25 and rand_num < 0.5:
return 2
if rand_num >= 0.5 and rand_num < 0.75:
return 3
if rand_num >= 0.75 and rand_num < 1.0:
return 4
def generate_arrival(customer_created, master_clock, mode=None):
global seed
customer_created += 1
seed, new_arrival_time = expdev(seed, LAMBDA)
new_customer = NodeCustomer("A", customer_created, new_arrival_time)
new_customer.inter_arrival = new_arrival_time
new_customer.arrival_time = master_clock + new_arrival_time
new_customer.clock = master_clock + new_arrival_time
if mode == 4 or mode == 5:
new_customer.priority = assign_priority()
return customer_created, new_customer
def generate_departure_time(customer, master_clock, M=None):
global seed
if customer.event_type == "D":
return customer
seed, new_serive_time = expdev(seed, 1)
customer.service_time = new_serive_time
customer.departure_time = new_serive_time + master_clock
customer.clock = new_serive_time + master_clock
customer.event_type = "D"
return customer
def sanatize_event_list():
global event_list
print("###SANITIZE:")
if event_list.list_size > 2:
current = event_list.head
sz = 1
while current != None:
tmp = current.get_next()
while tmp != None:
if tmp.customer_ID == current.customer_ID and tmp.event_type == current.event_type and tmp.clock == current.clock:
event_list.delete(tmp.customer_ID)
tmp = current.get_next()
else:
tmp = tmp.get_next()
current = current.get_next()
def add_event_to_list(customer):
global event_list
# print("---Insert")
event_list.insert(customer)
# event_list.print_list()
# sanatize_event_list()
def get_next_event_time():
global event_list
customer = event_list.head
if event_list.list_size == 1:
return customer.clock
if event_list.list_size > 1:
next_customer = customer.get_next()
if customer.clock < next_customer.clock:
return(customer.clock)
elif customer.clock >= next_customer.clock:
return next_customer.clock
def pop_event_from_list():
global event_list
global M
if LISTTYPEP == 0:
customer = event_list.head
else:
customer = event_list.head.get_next()
if event_list.list_size == 1:
if LISTTYPEP == 0:
event_list.delete(customer.customer_ID)
else:
event_list.delete(customer)
if event_list.list_size > 1:
next_customer = customer.get_next()
if customer.clock < next_customer.clock:
if LISTTYPEP == 0:
event_list.delete(customer.customer_ID)
else:
event_list.delete(customer)
return(customer)
elif customer.clock >= next_customer.clock:
if LISTTYPEP == 0:
event_list.delete(next_customer.customer_ID)
else:
event_list.delete(customer)
return next_customer
return customer
def pop_event_from_list_P():
global event_list
global M
if LISTTYPEP == 0:
customer = event_list.head
else:
customer = event_list.head.get_next()
if event_list.list_size == 1:
if LISTTYPEP == 0:
event_list.delete(customer.customer_ID)
else:
event_list.delete(customer)
if event_list.list_size > 1:
next_customer = customer.get_next()
if customer.clock < next_customer.clock:
if LISTTYPEP == 0:
event_list.delete(customer.customer_ID)
else:
event_list.delete(customer)
return(customer)
elif customer.clock >= next_customer.clock:
if LISTTYPEP == 0:
event_list.delete(next_customer.customer_ID)
else:
event_list.delete(customer)
return next_customer
return customer
def printDictionary(pDict):
keys = list(pDict.keys())
print("\n\nPrinting Dictionary. Total keys: ", len(keys))
for k in keys:
print("CID: {}, Type: {}, INA: {}, SERT: {}, DEPT: {}, CK: {}, INQ:{}, Served: {}".format(\
k, pDict[k].event_type, pDict[k].inter_arrival, pDict[k].service_time, \
pDict[k].departure_time, pDict[k].clock, pDict[k].inqueue, pDict[k].served))
def average_service_time_priority_NP(result_dict, C, priority):
service_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1 and result_dict[ID].priority == priority:
service_time_list.append(result_dict[ID].service_time)
return round(mean(service_time_list), 3)
def average_waiting_time_priority_NP(result_dict, C, priority):
waiting_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1 and result_dict[ID].priority == priority:
arrival_mcl = result_dict[ID].arrival_time - result_dict[ID].inter_arrival
service_mcl = result_dict[ID].departure_time #- result_dict[ID].service_time
tmp_waiting_time = service_mcl - result_dict[ID].arrival_time
waiting_time_list.append(tmp_waiting_time)
return round(mean(waiting_time_list), 3)
def average_waiting_time_priority_P(result_dict, C, priority):
waiting_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1 and result_dict[ID].priority == priority:
arrival_mcl = result_dict[ID].arrival_time - result_dict[ID].inter_arrival
service_mcl = result_dict[ID].departure_time #- result_dict[ID].service_time
tmp_waiting_time = service_mcl - result_dict[ID].arrival_time
waiting_time_list.append(tmp_waiting_time)
return round(mean(waiting_time_list), 3)
def average_service_time(result_dict, C):
service_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1:
service_time_list.append(result_dict[ID].service_time)
return round(mean(service_time_list), 3)
def average_waiting_time(result_dict, C):
waiting_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1:
arrival_mcl = result_dict[ID].arrival_time - result_dict[ID].inter_arrival
service_mcl = result_dict[ID].departure_time #- result_dict[ID].service_time
tmp_waiting_time = service_mcl - result_dict[ID].arrival_time
waiting_time_list.append(tmp_waiting_time)
return round(mean(waiting_time_list), 3)
def average_service_time_ci(result_dict, C):
service_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1:
service_time_list.append(result_dict[ID].service_time)
return mean_confidence_interval_self(service_time_list)
def average_waiting_time_ci(result_dict, C):
waiting_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1:
arrival_mcl = result_dict[ID].arrival_time - result_dict[ID].inter_arrival
service_mcl = result_dict[ID].departure_time #- result_dict[ID].service_time
tmp_waiting_time = service_mcl - result_dict[ID].arrival_time
waiting_time_list.append(tmp_waiting_time)
return mean_confidence_interval_self(waiting_time_list)
def average_waiting_time_sjf(result_dict, C):
print("\n\n")
waiting_time_list = []
for ID in list(result_dict.keys()):
if ID <= C:
if result_dict[ID].served == 1:
# arrival_mcl = result_dict[ID].arrival_time - result_dict[ID].inter_arrival
service_mcl = result_dict[ID].departure_time #- result_dict[ID].service_time
tmp_waiting_time = service_mcl - result_dict[ID].arrival_time
waiting_time_list.append(tmp_waiting_time)
customer_event = result_dict[ID]
# print("CLK: {}\t, ID: {}, Type: {}, IA: {}\t, AT: {}\t, ST: {}\t, DT: {}\t, DIFF: {}".format(\
# customer_event.clock, customer_event.customer_ID, customer_event.event_type,\
# customer_event.inter_arrival, customer_event.arrival_time, \
# customer_event.service_time, customer_event.departure_time, tmp_waiting_time) )
return round(mean(waiting_time_list), 3)
def average_waiting_time_lcfs(result_dict, C):
waiting_time_list = []
ID_list = list(result_dict.keys())
for i in range(0,len(ID_list)):
ID = ID_list[i]
if ID <= C:
if result_dict[ID].served == 1:
arrival_mcl = result_dict[ID].arrival_time - result_dict[ID].inter_arrival
service_mcl = result_dict[ID].departure_time #- result_dict[ID].service_time
tmp_waiting_time = service_mcl - result_dict[ID].arrival_time
# arrival_mcl = result_dict[ID].inter_arrival
# service_mcl = result_dict[ID].departure_time
# tmp_waiting_time = service_mcl - arrival_mcl - result_dict[ID].service_time
if tmp_waiting_time > 0:
# if 1:
waiting_time_list.append(tmp_waiting_time)
# print(waiting_time_list)
return round(mean(waiting_time_list), 3)
def print_stats_for_L_param(result_dict, C, L):
L = int(L)
ID_list = [L, L+1, L+10, L+11]
print("Values of L param:-")
for ID in ID_list:
if result_dict[ID].served == 1:
print("L = {}, Arrival Time: {}, Service Time: {}, Departure Time: {}, Customers in System: {}".format(\
ID, result_dict[ID].arrival_time, result_dict[ID].service_time, result_dict[ID].departure_time, result_dict[ID].in_system))
if result_dict[ID].served == -1:
print("L = {}, Arrival Time: {}, Service Time: {}, Departure Time: {}, Customers in System: {}".format(\
ID, result_dict[ID].arrival_time, result_dict[ID].service_time, result_dict[ID].arrival_time, result_dict[ID].in_system))
def get_sj_cid(queue):
ind = -1
if len(queue) == 0:
return ind
elif len(queue) == 1:
return 0
for i in range(1, len(queue)):
if queue[i].service_time > 0:
if queue[i].service_time >= queue[i-1].service_time:
ind = i-1
else:
ind = i
return ind
def get_cid_index(queue, customer):
ind = -1
if len(queue) == 0:
return ind
elif len(queue) == 1 and queue[0].customer_ID == customer.customer_ID:
return 0
for i in range(1, len(queue)):
if queue[i].customer_ID == customer.customer_ID:
return i
return ind
global queue_c
global queue_1
global queue_2
global queue_3
global queue_4
global customer_dropped_c
global customer_dropped_1
global customer_dropped_2
global customer_dropped_3
global customer_dropped_4
global customer_inqueue_c
global customer_inqueue_1
global customer_inqueue_2
global customer_inqueue_3
global customer_inqueue_4
global customer_sent_to_c
global customer_sent_to_1
global customer_sent_to_2
global customer_sent_to_3
global result_dictionary
def add_to_queue_priority_NP(customer_event, K):
global queue_1
global queue_2
global queue_3
global queue_4
global customer_dropped_1
global customer_dropped_2
global customer_dropped_3
global customer_dropped_4
global customer_inqueue_1
global customer_inqueue_2
global customer_inqueue_3
global customer_inqueue_4
global result_dictionary
K = floor(K/4)
if customer_event.priority == 1 and customer_inqueue_1 < K:
queue_1.append(customer_event)
customer_inqueue_1 += 1
elif customer_event.priority == 1 and customer_inqueue_1 >= K:
customer_dropped_1 += 1
customer_event.served = -1
result_dictionary[customer_event.customer_ID] = customer_event
if customer_event.priority == 2 and customer_inqueue_2 < K:
queue_2.append(customer_event)
customer_inqueue_2 += 1
elif customer_event.priority == 2 and customer_inqueue_2 >= K:
customer_dropped_2 += 1
customer_event.served = -1
result_dictionary[customer_event.customer_ID] = customer_event
if customer_event.priority == 3 and customer_inqueue_3 < K:
queue_3.append(customer_event)
customer_inqueue_3 += 1
elif customer_event.priority == 3 and customer_inqueue_3 >= K:
customer_dropped_3 += 1
customer_event.served = -1
result_dictionary[customer_event.customer_ID] = customer_event
if customer_event.priority == 4 and customer_inqueue_4 < K:
queue_4.append(customer_event)
customer_inqueue_4 += 1
elif customer_event.priority == 4 and customer_inqueue_4 >= K:
customer_dropped_4 += 1
customer_event.served = -1
result_dictionary[customer_event.customer_ID] = customer_event
def pop_from_queue_P(cust):
global queue_1
global queue_2
global queue_3
global queue_4
global customer_inqueue_1
global customer_inqueue_2
global customer_inqueue_3
global customer_inqueue_4
priority = cust.priority
# print(">> POPING Q: ID: {}, TYPE: {}, Priority:{}".format(cust.customer_ID, cust.event_type, priority))
# print("> {}, {}, {}, {}".format(customer_inqueue_1, customer_inqueue_2, customer_inqueue_3, customer_inqueue_4))
# if cust.event_type == "D":
# print("* Q EVENT type D. Nothing to pop.")
# return None
if priority == 1 and len(queue_1) > 0:
tmp_customer = queue_1.pop(0)
tmp_customer.service_time = cust.service_time
tmp_customer.departure_time = cust.departure_time
customer_inqueue_1 -= 1
# print("> {}, {}, {}, {}".format(customer_inqueue_1, customer_inqueue_2, customer_inqueue_3, customer_inqueue_4))
return tmp_customer
if priority == 2 and len(queue_2) > 0:
tmp_customer = queue_2.pop(0)
tmp_customer.service_time = cust.service_time
tmp_customer.departure_time = cust.departure_time
customer_inqueue_2 -= 1
# print("> {}, {}, {}, {}".format(customer_inqueue_1, customer_inqueue_2, customer_inqueue_3, customer_inqueue_4))
return tmp_customer
if priority == 3 and len(queue_3) > 0:
tmp_customer = queue_3.pop(0)
tmp_customer.service_time = cust.service_time
tmp_customer.departure_time = cust.departure_time
customer_inqueue_3 -= 1
# print("> {}, {}, {}, {}".format(customer_inqueue_1, customer_inqueue_2, customer_inqueue_3, customer_inqueue_4))
return tmp_customer
if priority == 4 and len(queue_4) > 0:
tmp_customer = queue_4.pop(0)
tmp_customer.service_time = cust.service_time
tmp_customer.departure_time = cust.departure_time
customer_inqueue_4 -= 1
# print("> {}, {}, {}, {}".format(customer_inqueue_1, customer_inqueue_2, customer_inqueue_3, customer_inqueue_4))
return tmp_customer
# print(">< {}, {}, {}, {}".format(customer_inqueue_1, customer_inqueue_2, customer_inqueue_3, customer_inqueue_4))
# if customer_inqueue_1 > 0:
# tmp_customer = queue_1.pop(0)
# customer_inqueue_1 -= 1
# return tmp_customer
# elif customer_inqueue_2 > 0:
# tmp_customer = queue_2.pop(0)
# customer_inqueue_2 -= 1
# return tmp_customer
# elif customer_inqueue_3 > 0:
# tmp_customer = queue_3.pop(0)
# customer_inqueue_3 -= 1
# return tmp_customer
# elif customer_inqueue_4 > 0:
# tmp_customer = queue_4.pop(0)
# customer_inqueue_4 -= 1
# return tmp_customer
def stop_service_and_enqueue(stop_cust, mcl, K):
global queue_1
global queue_2
global queue_3
global queue_4
global customer_dropped_1
global customer_dropped_2
global customer_dropped_3
global customer_dropped_4
global customer_inqueue_1
global customer_inqueue_2
global customer_inqueue_3
global customer_inqueue_4
global result_dictionary
K = floor(K/4)
stop_cust.service_time = stop_cust.departure_time - mcl
stop_cust.departure_time = mcl + stop_cust.service_time
stop_cust.clock = stop_cust.departure_time
stop_cust.event_type = "P"
if stop_cust.priority == 1 and customer_inqueue_1 < K:
queue_1.insert(0, stop_cust)
customer_inqueue_1 += 1
elif stop_cust.priority == 1 and customer_inqueue_1 >= K:
customer_dropped_1 += 1
stop_cust.served = -1
result_dictionary[stop_cust.customer_ID] = stop_cust
if stop_cust.priority == 2 and customer_inqueue_2 < K:
queue_2.insert(0, stop_cust)
customer_inqueue_2 += 1
elif stop_cust.priority == 2 and customer_inqueue_2 >= K:
customer_dropped_2 += 1
stop_cust.served = -1
result_dictionary[stop_cust.customer_ID] = stop_cust
if stop_cust.priority == 3 and customer_inqueue_3 < K:
queue_3.insert(0, stop_cust)
customer_inqueue_3 += 1
elif stop_cust.priority == 3 and customer_inqueue_3 >= K:
customer_dropped_3 += 1
stop_cust.served = -1
result_dictionary[stop_cust.customer_ID] = stop_cust
if stop_cust.priority == 4 and customer_inqueue_4 < K:
queue_4.insert(0, stop_cust)
customer_inqueue_4 += 1
elif stop_cust.priority == 4 and customer_inqueue_4 >= K:
customer_dropped_4 += 1
stop_cust.served = -1
result_dictionary[stop_cust.customer_ID] = stop_cust
def io_or_exit(cust):
global priority_seed
priority_seed, rand_num = ran0(priority_seed)
global Ki
global queue_1
global queue_2
global queue_3
global customer_dropped_1
global customer_dropped_2
global customer_dropped_3
global customer_inqueue_1
global customer_inqueue_2
global customer_inqueue_3
global customer_sent_to_1
global customer_sent_to_2
global customer_sent_to_3
global result_dictionary
io_e = 0
if rand_num >= 0 and rand_num < 0.7:
io_e = 0
if rand_num >= 0.7 and rand_num < 0.8:
io_e = 1
if rand_num >= 0.8 and rand_num < 0.9:
io_e = 2
if rand_num >= 0.9 and rand_num < 1.0:
io_e = 3
if LOG:
print("ro: {}, cid: {}, etype: {}".format(io_e, cust.customer_ID, cust.event_type))
if io_e == 1 and len(queue_1) < Ki:
cust.inqueue = True
cust.queue_type = 1
cust.event_type = "A1"
cust.visit_1 += 1
cust.arrival_time = cust.departure_time
cust.departure_generated = 0
customer_sent_to_1 += 1
queue_1.append(cust)
customer_inqueue_1 += 1
add_event_to_list(cust)
result_dictionary[cust.customer_ID] = cust
elif io_e == 1 and len(queue_1) >= Ki:
cust.inqueue = False
cust.queue_type = 1
cust.served = -1
cust.departure_time = 0
customer_dropped_1 += 1
result_dictionary[cust.customer_ID] = cust
if io_e == 2 and len(queue_2) < Ki:
cust.inqueue = True
cust.queue_type = 2
cust.event_type = "A2"
cust.visit_2 += 1
cust.arrival_time = cust.departure_time
cust.departure_generated = 0
customer_sent_to_2 += 1
queue_2.append(cust)
customer_inqueue_2 += 1
add_event_to_list(cust)
result_dictionary[cust.customer_ID] = cust
elif io_e == 2 and len(queue_2) >= Ki:
cust.inqueue = False
cust.queue_type = 2
cust.served = -1
cust.departure_time = 0
customer_dropped_2 += 1
result_dictionary[cust.customer_ID] = cust
if io_e == 3 and len(queue_3) < Ki:
cust.inqueue = True
cust.queue_type = 3
cust.event_type = "A3"
cust.visit_3 += 1
cust.arrival_time = cust.departure_time
cust.departure_generated = 0