-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
295 lines (238 loc) · 11 KB
/
models.py
File metadata and controls
295 lines (238 loc) · 11 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
from parameter import Params
from math import pow, factorial
from math import inf
from simulation import simulate, chart
###M/M/1
class MM1(Params):
"""
Represents an M/M/1 queuing system.
"""
def __init__(self, lumbda, mu):
"""
Initializes the M/M/1 system parameters.
"""
# Validate inputs
if lumbda >= mu:
raise ValueError("Arrival rate (lumbda) must be less than service rate (mu) for stability.")
super().__init__(lumbda, mu)
def findL(self):
"""Calculates and returns the average number of customers in the system (L)."""
return self.lumbda / (self.mu - self.lumbda)
def findLq(self):
"""Calculates and returns the average number of customers in the queue (Lq)."""
return (self.findL() * self.lumbda) / self.mu
def findW(self):
"""Calculates and returns the average time a customer spends in the system (W)."""
return 1 / (self.mu - self.lumbda)
def findWq(self):
"""Calculates and returns the average time a customer spends in the queue (Wq)."""
return (self.findW() * self.lumbda) / self.mu
def findPk(self, k):
"""Calculates and returns the probability of having k customers in the system (Pk)."""
return pow(self.findRu(), k) * (1 - self.findRu())
def findRu(self):
"""Calculates and returns the server utilization (Ru)."""
return self.lumbda / self.mu
def display(self):
print(f"L: {self.findL()}")
print(f"Lq: {self.findLq()}")
print(f"W: {self.findW()}")
print(f"Wq: {self.findWq()}")
print(f"Ru: {self.findRu()}")
####M/M/1/K
class MM1K(Params):
"""
Represents an M/M/1/K queuing system.
"""
def __init__(self, lumbda, mu, systemCapacity):
"""
Initializes the M/M/1/K system parameters.
"""
# Validate inputs
if lumbda >= mu:
raise ValueError("Arrival rate (lumbda) must be less than service rate (mu) for stability.")
if systemCapacity <= 0:
raise ValueError("System capacity must be a positive integer.")
super().__init__(lumbda, mu, systemCapacity=systemCapacity)
self._sc = systemCapacity
self._ru = self.findRu()
self._ruK = pow(self._ru, systemCapacity)
self._ruK1 = pow(self._ru, systemCapacity + 1)
def findL(self):
"""Calculates and returns the average number of customers in the system (L)."""
if self._ru == 1:
return self._sc / 2.0
else:
numerator = self._ru * (1 - (self._sc + 1) * self._ruK + self._sc * self._ruK1)
denominator = (1 - self._ru) * (1 - self._ruK1)
return numerator / denominator
def findLq(self):
"""Calculates and returns the average number of customers in the queue (Lq)."""
return self._findLambdaDash() * self.findWq()
def findW(self):
"""Calculates and returns the average time a customer spends in the system (W)."""
return self.findL() / self._findLambdaDash()
def findWq(self):
"""Calculates and returns the average time a customer spends in the queue (Wq)."""
return self.findW() - (1.0 / self.mu)
def findPk(self, k):
"""Calculates and returns the probability of having k customers in the system (Pk)."""
if self._ru == 1:
return 1.0 / (self._sc + 1.0)
else:
return self._ruK * ((1 - self._ru) / (1 - self._ruK1))
def _findLambdaDash(self):
"""Calculates and returns the effective arrival rate (lambda_dash)."""
return self.lumbda * (1 - self.findPk(self._sc))
def findRu(self):
"""Calculates and returns the server utilization (Ru)."""
return self.lumbda / self.mu
def display(self):
print(f"L: {self.findL()}")
print(f"Lq: {self.findLq()}")
print(f"W: {self.findW()}")
print(f"Wq: {self.findWq()}")
print(f"Ru: {self.findRu()}")
###M/M/C
class MMC(Params):
"""
Represents an M/M/c queuing system.
"""
def __init__(self, lumbda, mu, numberOfServers):
"""
Initializes the M/M/c system parameters.
"""
# Validate inputs
if lumbda >= mu * numberOfServers:
raise ValueError("Arrival rate (lumbda) must be less than service rate (mu) * number of servers (c) for stability.")
if numberOfServers <= 0:
raise ValueError("Number of servers must be a positive integer.")
super().__init__(lumbda, mu, numberOfServers=numberOfServers)
self.c = numberOfServers
self.findR = self.lumbda / self.mu
self.P0 = self.findP0()
self._Lq = self.findLq()
def findL(self):
"""Calculates and returns the average number of customers in the system (L)."""
return self._Lq + self.findR
def findLq(self):
"""Calculates and returns the average number of customers in the queue (Lq)."""
numerator = (pow(self.findR, self.c + 1) / self.c)
denominator = factorial(self.c) * (pow(1 - (self.findR / self.c), 2))
return (numerator / denominator) * self.P0
def findW(self):
"""Calculates and returns the average time a customer spends in the system (W)."""
return (self._Lq / self.lumbda) + (1 / self.mu)
def findWq(self):
"""Calculates and returns the average time a customer spends in the queue (Wq)."""
return self._Lq / self.lumbda
def findPk(self, k):
"""Calculates and returns the probability of having k customers in the system (Pk)."""
if k < self.c:
return (pow(self.lumbda, k) / (factorial(k) * pow(self.mu, k))) * self.P0
else:
return (pow(self.lumbda, k) / (pow(self.c, k - self.c) * factorial(self.c) * pow(self.mu, k))) * self.P0
def findRu(self):
"""Calculates and returns the server utilization (Ru)."""
return self.findR / self.c
def findP0(self):
"""Calculates and returns the probability of having 0 customers in the system (P0)."""
r = self.findR
ru = self.findRu()
c = self.c
first = 0
if ru < 1:
for i in range(c):
first += pow(r, i) / factorial(i)
return 1 / (first + ((c * pow(r, c)) / (factorial(c) * (c - r))))
else:
for i in range(c):
first += (1 / factorial(i)) * pow(r, i)
return 1 / (first + ((1 / factorial(c)) * pow(r, c) * ((c * self.mu) / (c * self.mu - self.lumbda))))
def display(self):
print(f"L: {self.findL()}")
print(f"Lq: {self.findLq()}")
print(f"W: {self.findW()}")
print(f"Wq: {self.findWq()}")
print(f"Ru: {self.findRu()}")
####M/M/C/K
class MMCK(Params):
"""
Represents an M/M/c/K queuing system.
"""
def __init__(self, lumbda, mu, numberOfServers, systemCapacity):
"""
Initializes the M/M/c/K system parameters.
"""
# Validate inputs
#if lumbda >= mu * numberOfServers:
#raise ValueError("Arrival rate (lumbda) must be less than service rate (mu) * number of servers (c) for stability.")
if numberOfServers <= 0:
raise ValueError("Number of servers must be a positive integer.")
if systemCapacity <= 0:
raise ValueError("System capacity must be a positive integer.")
super().__init__(lumbda, mu, numberOfServers=numberOfServers, systemCapacity=systemCapacity)
self.c = numberOfServers
self.sc = systemCapacity
self.findR = self.lumbda / self.mu
self.findp0 = self.findP0()
self.findLambdaDash = self.lumbda * (1 - self.findPk(self.sc))
def findL(self):
"""Calculates and returns the average number of customers in the system (L)."""
last = sum((self.c - i) * (pow(self.findR, i) / factorial(i)) for i in range(self.c))
return self.findLq() + self.c - self.findp0 * last
def findLq(self):
"""Calculates and returns the average number of customers in the queue (Lq)."""
ru = self.findRu()
numerator = ru * pow(self.findR, self.c) * self.findp0
denominator = factorial(self.c) * pow(1 - ru, 2)
blocking_factor = (1 - pow(ru, self.sc - self.c + 1) - (1 - ru) * (self.sc - self.c + 1) * pow(ru, self.sc - self.c))
return (numerator / denominator) * blocking_factor
def findW(self):
"""Calculates and returns the average time a customer spends in the system (W)."""
return self.findL() / self.findLambdaDash
def findWq(self):
"""Calculates and returns the average time a customer spends in the queue (Wq)."""
return self.findLq() / self.findLambdaDash
def findPk(self, n):
"""Calculates and returns the probability of having n customers in the system (Pk)."""
if n < self.c:
return (pow(self.findR, n) / factorial(n)) * self.findp0
return (pow(self.findR, n) / (pow(self.c, n - self.c) * factorial(self.c))) * self.findp0
def findP0(self):
"""Calculates and returns the probability of having 0 customers in the system (P0)."""
first = sum(pow(self.findR, i) / factorial(i) for i in range(self.c))
ru = self.findRu()
if ru != 1:
return 1 / (first + (pow(self.findR, self.c) / factorial(self.c)) * ((1 - pow(ru, self.sc - self.c + 1)) / (1 - ru)))
else:
return 1 / (first + (pow(self.findR, self.c) / factorial(self.c)) * (self.sc - self.c + 1))
def findRu(self):
"""Calculates and returns the server utilization (Ru)."""
return self.findR / self.c
def display(self):
print(f"L: {self.findL()}")
print(f"Lq: {self.findLq()}")
print(f"W: {self.findW()}")
print(f"Wq: {self.findWq()}")
print(f"Ru: {self.findRu()}")
def solution(lumbda, mu, numberOfServers=1, systemCapacity=inf):
if numberOfServers == 1:
if systemCapacity == inf or systemCapacity == 0:
MM1(lumbda, mu).display()
else:
MM1K(lumbda, mu, systemCapacity).display()
else:
if systemCapacity == inf:
MMC(lumbda, mu, numberOfServers).display()
else:
MMCK(lumbda, mu, numberOfServers, systemCapacity).display()
def ask_user():
arrival_rate = float(input("Enter the arrival rate (lambda): "))
service_rate = float(input("Enter the service rate (mu): "))
servers = int(input("Enter the Number of Servers (c):"))
capacity_input = input("Enter the system capacity (k) (leave empty for infinity): ")
capacity = inf if capacity_input == "" else int(capacity_input)
solution(arrival_rate, service_rate, servers, capacity)
simulate(arrival_rate, service_rate, 0)
chart()