-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexmaple_sim_shortest_queue.py
More file actions
219 lines (164 loc) · 6.1 KB
/
exmaple_sim_shortest_queue.py
File metadata and controls
219 lines (164 loc) · 6.1 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
# Queueing system with control
# Decide stations can branch clients by using lambda functions. In the following model the clients are sent to one of two process stations depending on the queue lengths at the corresponding stations.
# Importing modules
# Generating integer pseudo random numbers
from random import randint
# Collecting results
import pandas as pd
# Simulation
from queuesim.stations import Source, Decide, DecideCondition, Process, Dispose
from queuesim import Simulator
from queuesim.random_dist import exp as exp_dist
# General model parameters
# Arrivals to be simulated
count = 100_000
# Inter-arrival times distribution
inter_arrival_time = exp_dist(50)
# Service times distribution
process_time = exp_dist(80)
# Fast service times distribution
process_time_fast = exp_dist(40)
# Number of operators per service station (by using two stations)
c = 1
# Prepare data collection
enq_dict = {}
en_dict = {}
# Two stations with individual queues, new clients choosing the queue by change
# Simulator
simulator = Simulator()
# Stations
source = Source(simulator, count, inter_arrival_time)
decide = Decide(simulator)
process1 = Process(simulator, process_time, c)
process2 = Process(simulator, process_time, c)
dispose = Dispose(simulator)
# Link stations
source.set_next(decide)
decide.add_next(process1, 1)
decide.add_next(process2, 1)
process1.set_next(dispose)
process2.set_next(dispose)
# Run simulation
simulator.run()
# Results
name = "2 queues, random"
enq = process1.statistic_queue_length.mean + process2.statistic_queue_length.mean
en = process1.statistic_wip.mean + process2.statistic_wip.mean
enq_dict[name] = enq
en_dict[name] = en
print("")
print("Two stations with individual queues, new clients choosing the queue by change")
print("E[NQ]=", round(enq, 2), sep="")
print("E[N]=", round(en, 2), sep="")
# Two stations with individual queues, new clients will enter the shortest queue
# Simulator
simulator = Simulator()
# Stations
source = Source(simulator, count, inter_arrival_time)
decide = DecideCondition(simulator)
process1 = Process(simulator, process_time, c)
process2 = Process(simulator, process_time, c)
dispose = Dispose(simulator)
def shortest_queue(client) -> int:
nq1 = process1.nq
nq2 = process2.nq
if nq1 < nq2: return 0
if nq1 > nq2: return 1
return randint(0, 1)
# Link stations
source.set_next(decide)
decide.set_condition(shortest_queue)
decide.add_next(process1)
decide.add_next(process2)
process1.set_next(dispose)
process2.set_next(dispose)
# Run simulation
simulator.run()
# Results
name = "2 queues, shortest"
enq = process1.statistic_queue_length.mean + process2.statistic_queue_length.mean
en = process1.statistic_wip.mean + process2.statistic_wip.mean
enq_dict[name] = enq
en_dict[name] = en
print("")
print("Two stations with individual queues, new clients will enter the shortest queue")
print("E[NQ]=", round(enq, 2), sep="")
print("E[N]=", round(en, 2), sep="")
# One service station (with a single queue) with two parallel operators at the station
# Simulator
simulator = Simulator()
# Stations
source = Source(simulator, count, inter_arrival_time)
process = Process(simulator, process_time, 2 * c)
dispose = Dispose(simulator)
# Link stations
source.set_next(process)
process.set_next(dispose)
# Run simulation
simulator.run()
# Results
name = "1 queue, 2 parallel operators"
enq = process.statistic_queue_length.mean
en = process.statistic_wip.mean
enq_dict[name] = enq
en_dict[name] = en
print("")
print("One service station (with a single queue) with two parallel operators at the station")
print("E[NQ]=", round(enq, 2), sep="")
print("E[N]=", round(en, 2), sep="")
# One service station (with a single queue) with batch processing
# Simulator
simulator = Simulator()
# Stations
source = Source(simulator, count, inter_arrival_time)
process = Process(simulator, process_time, c, b=2)
dispose = Dispose(simulator)
# Link stations
source.set_next(process)
process.set_next(dispose)
# Run simulation
simulator.run()
# Results
name = "1 queue, batch processing"
enq = process.statistic_queue_length.mean
en = process.statistic_wip.mean
enq_dict[name] = enq
en_dict[name] = en
print("")
print("One service station (with a single queue) with batch processing")
print("E[NQ]=", round(enq, 2), sep="")
print("E[N]=", round(en, 2), sep="")
# One service station (with a single queue) with a twice as fast operator as on the other models
# Simulator
simulator = Simulator()
# Stations
source = Source(simulator, count, inter_arrival_time)
process = Process(simulator, process_time_fast, c)
dispose = Dispose(simulator)
# Link stations
source.set_next(process)
process.set_next(dispose)
# Run simulation
simulator.run()
# Results
name = "1 queue, fast operator"
enq = process.statistic_queue_length.mean
en = process.statistic_wip.mean
enq_dict[name] = enq
en_dict[name] = en
print("")
print("One service station (with a single queue) with a twice as fast operator as on the other models")
print("E[NQ]=", round(enq, 2), sep="")
print("E[N]=", round(en, 2), sep="")
# Results of all models
results = pd.DataFrame({'E[NQ]': enq_dict, 'E[N]': en_dict})
print(results)
# All 5 models have the same arrival rate and the same operating capacity.
# In terms of queue lengths (or waiting time) the two queues with random selection is worst.
# Batch processing at a single station is a bit better, selecting the shortest of two queues is even more better.
# A single process station with a single fast operator is quite good. But a single process station with two operators is even better (in terms of queue length).
# Because in this case, if a client is already in process, if a second client arrives, this can be served immediately, too.
# In the case of the single, fast operator this second client would have to wait a short time.
# When considering the average number of clients in the system, the model with the fast operator is the best.
# In this model, the average waiting times are a bit longer than in the model with the two parallel operators at a single station.
# But because the processing times are significantly shorter, the average number of clients (waiting and in process) in the system is lower.