-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_tandemqueue.py
193 lines (148 loc) · 6.37 KB
/
project_tandemqueue.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
# -*- coding: utf-8 -*-
"""project_tandemQueue.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kJRE7TNOJt8TeJzbYXTTTAbvUtq7SQ-T
"""
import numpy as np
np.random.seed(10)
class SSQ:
def __init__(self):
self.interarrivals= list(np.random.exponential(scale=1, size = 1000))
self.service_times= list(np.random.exponential(scale=1.43, size = 1000))
self.service_times1= list(np.random.exponential(scale=1.11, size = 1000))
# print(self.interarrivals)
# print(self.service_times)
# print(self.service_times1)
self.clock= 0.0
self.next_arrival=self.interarrivals.pop(0)
self.next_arrival1 = float('inf')
self.next_departure= float('inf')
self.next_departure1 = float('inf')
self.num_in_queue = 0
self.num_in_queue1 = 0
self.times_of_arrivalqueue = []
self.times_of_arrivalqueue1 = []
self.service_times_in_queue= []
self.service_times_in_queue1 = []
self.total_delay=0.0
self.num_of_delays= 0.0
self.server_status= 0
self.server_status1 = 0
self.last_event_time=0.0
self.last_event_time1=0.0
self.area_under_b = 0.0
self.area_under_b1 = 0.0
self.area_under_q = 0.0
self.area_under_q1 = 0.0
self.departure_time = []
def start(self):
while self.clock <=500:
self.simulate_next_event()
def simulate_next_event(self):
self.clock= min(self.next_arrival,self.next_departure,self.next_arrival1, self.next_departure1)
if self.clock == self.next_arrival or self.clock == self.next_departure:
self.area_under_b += (self.clock - self.last_event_time) * self.server_status
# print("Server status: ",self.server_status,"Area under b(t): ",self.area_under_b)
# print("server statues 2: ",self.server_status1)
if self.next_arrival <= self.next_departure:
self.arrival()
print("Arrival Clock at server 1:" +str(self.clock))
else:
print("Departure from server 1 at "+str(self.clock))
self.departure_time.append(self.next_departure)
self.departure()
if self.server_status1 == 0:
self.next_arrival1 = self.clock
else:
self.area_under_b1 +=(self.clock-self.last_event_time1) * self.server_status1
if self.next_arrival1 <= self.next_departure1:
self.arrival1()
print("Arrival Clock at server 2:" +str(self.clock))
else:
print("Departure from server 2 at "+str(self.clock))
self.departure_time.append(self.next_departure1)
self.departure1()
self.check_satisfiction()
self.area_under_q1 += (self.clock - self.last_event_time1) * self.num_in_queue1
# print("queue: ",self.num_in_queue1,"area: ",self.area_under_q1)
self.last_event_time1 = self.clock
self.area_under_q += (self.clock - self.last_event_time) * self.num_in_queue
self.last_event_time = self.clock
def arrival(self):
self.next_arrival= self.next_arrival+ self.interarrivals.pop(0)
if self.server_status==0: #Idle server
self.server_status= 1 #Make Busy
delay=0.0 #so delay is 0
self.total_delay += delay
self.num_of_delays +=1
#schedule next departure
self.next_departure = self.clock+ self.service_times.pop(0)
else: #Server BUSY
self.num_in_queue+=1
self.times_of_arrivalqueue.append(self.clock)
self.service_times_in_queue.append(self.service_times.pop(0))
def departure(self): ##depart from server
if self.num_in_queue==0: #if no customer in the queue
#make server IDLE
self.server_status= 0
#schedule next departure= infinity
self.next_departure= float('infinity')
else:
self.num_in_queue-=1
arrival= self.times_of_arrivalqueue.pop(0)
delay= self.clock- arrival
self.total_delay+=delay
self.num_of_delays+=1
self.next_departure = self.clock+ self.service_times_in_queue.pop(0)
#########################################################################
#arrival at server 2
def arrival1(self):
self.next_arrival1= self.next_arrival1+ self.next_departure
if self.server_status1==0: #server idle
self.server_status1= 1 #server BUSY
delay=0.0
self.total_delay += delay
self.num_of_delays +=1
self.next_departure1 = self.clock+ self.service_times1.pop(0)
else: #Server BUSY
self.num_in_queue1 +=1
self.times_of_arrivalqueue.append(self.clock)
self.service_times_in_queue.append(self.service_times.pop(0))
#departure form server 2
def departure1(self):
if self.num_in_queue1==0:
self.server_status1= 0
self.next_departure1= float('infinity')
else:
self.num_in_queue1-=1
arrival= self.times_of_arrivalqueue1.pop(0)
delay= self.clock- arrival
self.total_delay+=delay
self.num_of_delays+=1
self.next_departure1 = self.clock+ self.service_times_in_queue1.pop(0)
def check_satisfiction(self):
checker = np.random.choice(a=[0,1], p=[0.02, 0.98])
if checker == 0:
d_service_times= list(np.random.exponential(scale=1.43, size = 1))
d_service_times1 = list(np.random.exponential(scale=1.11, size = 1))
#Join at the tail of the queue
# self.times_of_arrivalqueue.append(self.clock)
# self.service_times_in_queue.append(d_service_times[-1])
# self.service_times1.append(d_service_times1[-1])
# self.num_in_queue +=1
# print("Customer at clock: ",self.clock," is disatisfied and Rejoin at the tail of the queue")
#Join at the head of the queue
self.times_of_arrivalqueue.insert(0,self.clock)
self.service_times_in_queue.insert(0,d_service_times[-1])
self.service_times1.insert(0,d_service_times1[-1])
self.num_in_queue +=1
print("Customer at clock: ",self.clock," is disatisfied and Rejoin at the head of the queue")
s = SSQ()
s.start()
print("__________________________________________________")
print("Average delay: ",(s.total_delay/s.num_of_delays))
print("Expected queue length server 1: ",(s.area_under_q/s.clock))
print("Expected queue length server 2: ",(s.area_under_q1/s.clock))
print("Utilization server 1: ",(s.area_under_b/s.clock),"(",(s.area_under_b/s.clock)*100,"%)")
print("Utilization server 2: ",(s.area_under_b1/s.clock),"(",(s.area_under_b1/s.clock)*100,"%)")