-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathQueueSim.py
520 lines (490 loc) · 21.3 KB
/
QueueSim.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
'''
Basic M/M/s queueing module
Brief description:
Contains EventQueue class which can create an m/m/s queueing system instance,
and simulate with graphics
Detailed description:
Classes contained:
Event:
Basically there are two event types, arrival and departure, this class
can defines arrival and departure events
attributes:
number: event number, each arrival/departure has a
unique event number, corresponding
arrival-departure pairs have the same event
number
type: int
eventType: either arrival or departure
arrival and departure are global constants
type: int
eventTime: occurance time of the event
type: float
serverNumber: server that serves the corresponding customer
of the event (arrival/departure)
type: int
name: name of the event
type: string
methods:
__init__(self,eventType,number,eventTime,server=None):
constructor of the class
input descriptions are same as in the
attributes section, server input is optional
__gt__(self,other): returns True if self.eventTime >
other.eventTime
__ge__(self,other): returns True if self.eventTime >=
other.eventTime
__lt__(self,other): returns True if self.eventTime <
other.eventTime
__le__(self,other): returns True if self.eventTime <=
other.eventTime
__eq__(self,other): returns True if self.eventTime ==
other.eventTime
__ne__(self,other): returns True if self.eventTime !=
other.eventTime
Customer:
A very basic class that defines customer types. It only has __init__()
method that initializes attributes.
attributes:
entryTime: arrival time of the customer
type: float
serviceTime: service time of the customer
type: float
number number of the customer
type: int
methods:
__init__(self,entryTime,serviceTime,customerNumber):
constructor of the class, inputs are as in the
attributes section
EventQueue:
This class holds the attributes and methods for the simulatio of M/M/s
queueing system
attributes:
seed: seed used for random module
IAT: mean of inter-arrival times between two
consecutive arrivals, inter-arrival times
are assumed to be exponentially distributed
type: float
ST: mean of service time. Service times are
assumed to be exponentially distributed
type: float
currentTime: value that shows time
type: float
server: list that holds the status of the
servers, BUSY if busy, IDLE if not
type: list
sqList list of queues, contains 1 queue if
queueing_mode == 'single', server_num queues
if queueing_mode in ('random', 'shortest')
type: list
eventTable: list that holds the events, events are sorted
with respect to their occurance time
type: list
waitingTime: dictionary that holds the waiting times of the
customers in the queue, keys are customer
numbers (Event numbers)
type: dict
TIS: time spent in system for each customer,
it is dictionary type and keys are customer
numbers
type: dict
serviceTime: service time of the customer, dictionary type,
keys are customer numbers
type: dict
eventCounter: arrival event counter, increases by 1 with
each arrival
type: int
customerCounter: departure event counter, increases by 1 with
each departure
type: int
queueing_mode: determines the queue mode, set by set_mode()
if mode == 'single' there is single queue,
if mode == 'random', then the number of queues
equals the number of servers and new customers
join a random queue according to distribution
self.pi
if mode == 'shortest', then the number of queues
equals the number of servers and new customers
join the shortest queue
type: string
graphics_mode: graphical mode, if graphics_mode == 'off', then
no graphics, graphics_mode == 'on', then
simulate with graphics
type: string
server_num: number of servers
queue_num: number of queues
methods:
__init__(self, seedInput, IAT, ST, pi, server_num,
queueing_mode, graphics_mode):
constructor of the class, seedInput is the
seed of simulation, IAT and ST are as in
attributes section
set_mode(self, server_num, queueing_mode, graphics_mode):
server_num is a positive integer
queueing_mode is either 'single', 'random',
or 'shortest'
graphics_mode is either string 'off' or
'on'
which_queue(self): when an arrival occurs this methods is called
if there is an available server this method
returns to the tuple of (queue,server) where
queue represents the queue that the customer
should join and server is one of the available
servers
if there is no available server, server value
returned is None
process_event(self,event):
processes event given and updates sqList and
eventTable accordingly
simulate(self, simulationLength):
simulates the system for simulationLength time
units
print_stat(self): print statistics to stdout
add_event(self,event): adds event to the eventTable
get_event(self,event): gets the first event in the event table
draw_screen(self): draws the status of the system to the screen
display_init(self): initializes pygame related parameters
Global Constants
Event Types
ARRIVE = 0
DEPART = 1
Server States
IDLE = 0
BUSY = 1
Created on Feb 9, 2012
Edited on Feb 11, 2013
'''
__version__ = '1.0.0'
__author__ = 'Aykut Bulut, Ted Ralphs ([email protected],[email protected])'
__license__ = 'MIT'
__maintainer__ = 'Aykut Bulut'
__email__ = '[email protected]'
__title__ = 'M/M/s queueing system'
from random import seed, expovariate
from math import sqrt, pow
import pygame
from pygame.locals import QUIT
from coinor.blimpy import Queue, PriorityQueue
from random import random
# Event Types
ARRIVE = 0
DEPART = 1
# Server States
IDLE = 0
BUSY = 1
class Event(object):
'''
Basically there are two event types, arrival and departure, this class can
generate instances of arrival and departure events. See the file
documentation for description of attributes.
'''
def __init__(self, eventType, eventNumber, eventTime, serverNumber = None):
'''
Constructor of the class. Input descriptions are same as in the
module (up) documentation, server input is optional.
'''
self.number = eventNumber
self.eventType = eventType
self.eventTime = eventTime
self.serverNumber = serverNumber
if self.eventType == ARRIVE:
self.name = 'arrival'+str(self.number)
elif self.eventType == DEPART:
self.name = 'departure'+str(self.number)
else:
print "unknown event type"
def __gt__(self, other):
'''
Returns True if self.eventTime > other.eventTime
'''
return self.eventTime > other.eventTime
def __ge__(self, other):
'''
Returns True if self.eventTime >= other.eventTime
'''
return self.eventTime >= other.eventTime
def __lt__(self, other):
'''
Returns True if self.eventTime < other.eventTime
'''
return self.eventTime < other.eventTime
def __le__(self, other):
'''
Returns True if self.eventTime <= other.eventTime
'''
return self.eventTime <= other.eventTime
def __eq__(self, other):
'''
returns True if self.eventTime == other.eventTime
'''
return self.eventTime == other.eventTime
def __ne__(self, other):
'''
Returns True if self.eventTime != other.eventTime
'''
return self.eventTime != other.eventTime
class Customer(object):
'''
Customer class. A basic class with only constructor method and 3
attributes.
'''
def __init__(self, entryTime, serviceTime, customerNumber):
'''
Initializes class attributes. See module documentation for explanation
of attributes.
'''
self.entryTime = entryTime
self.serviceTime = serviceTime
self.number = customerNumber
class EventQueue(object):
'''
This class holds the attributes and methods for the simulation of M/M/s
queueing system
'''
def __init__(self, seedInput = 0, IAT = 3, ST = 8, pi = None,
server_num = 3, queueing_mode = 'shortest',
graphics_mode = 'off'):
'''
Constructor of the class, sets initial values for class attributes
Post: self.ii, self.seed, self.IAT, self.ST, self.currentTime,
self.eventTable, self.waitingTime, self.TIS, self.serviceTime,
self.eventCounter, self.customerCounter, self.server, self.sqList
self.pi
'''
self.ii = 0
seed(seedInput)
self.seed = seedInput
self.IAT = IAT
self.ST = ST
self.currentTime = 0.0
self.eventTable = PriorityQueue()
# waiting time of customers
self.waitingTime = {}
# time in system for each customer
self.TIS = {}
# service time for the corresponding customer
self.serviceTime = {}
self.eventCounter = 0
self.customerCounter = 0
self.set_mode(server_num, queueing_mode, graphics_mode)
self.server = [IDLE for i in range(self.server_num)]
self.sqList = [Queue() for i in range(self.queue_num)]
# Equal probabilities by default
if pi == None:
pi = [1/float(self.queue_num) for i in range(self.queue_num)]
#Ensure the probabilities sum to one
pi[self.queue_num - 1] = 1 - sum(pi[:self.queue_num-1])
self.pi = pi
# Add first arrival event
self.add_event(ARRIVE, self.currentTime)
def set_mode(self, server_num = None, queueing_mode = None,
graphics_mode = None):
'''
server_num is a positive integer queueing_mode is either 'single',
'random', or 'shortest' graphics_mode is either string 'off' or 'on'
'''
if queueing_mode:
self.queueing_mode = queueing_mode
if server_num:
self.server_num = server_num
if self.queueing_mode != 'single':
self.queue_num = self.server_num
else:
self.queue_num = 1
if graphics_mode:
self.graphics_mode = graphics_mode
if graphics_mode == 'on' and self.server_num > 50:
print 'Only visualizing first 50 servers'
def process_event(self, event):
'''
processes event given and updates sqList and eventTable accordingly
'''
if event.eventType == ARRIVE:
whichQueue, whichServer = self.which_queue()
serviceTime = expovariate(1.0/self.ST)
if whichServer == None:
self.sqList[whichQueue].enqueue(Customer(self.currentTime,
serviceTime,
self.customerCounter))
else:
self.waitingTime[self.customerCounter] = 0
self.serviceTime[self.customerCounter] = serviceTime
self.add_event(DEPART, self.currentTime + serviceTime,
whichServer)
self.customerCounter += 1
self.add_event(ARRIVE, self.currentTime+expovariate(1.0/self.IAT))
elif event.eventType == DEPART:
if self.queue_num == 1:
q = self.sqList[0]
else:
q = self.sqList[event.serverNumber]
if not q.isEmpty():
customer = q.dequeue()
self.waitingTime[customer.number] = (self.currentTime -
customer.entryTime)
self.serviceTime[customer.number] = customer.serviceTime
self.add_event(DEPART, self.currentTime + customer.serviceTime,
event.serverNumber)
else:
self.server[event.serverNumber] = IDLE
else:
print "Unknown event type"
def which_queue(self):
'''
When an arrival occurs this methods is called if there is an available
server this method returns to the tuple of (queue,server) where queue
represents the queue that the customer should join and server is one of
the available servers if there is no available server, server value
returned is None
'''
# Single server policy
if self.queueing_mode == 'single':
if self.sqList[0].isEmpty():
for i in range(self.server_num):
if self.server[i] == IDLE:
self.server[i] = BUSY
return 0, i
else:
return 0, None
return 0, None
# COMPLETE CHOOSING RANDOM QUEUE
elif self.queueing_mode == 'random':
pass
# COMPLETE CHOOSING SHORTEST QUEUE
elif self.queueing_mode == 'shortest':
pass
def simulate(self, simulationLength):
'''
Simulates the system for simulationLength time units
'''
pgEventType = None
if self.graphics_mode == 'on':
self.display_init()
while self.currentTime < simulationLength and pgEventType != QUIT:
if self.graphics_mode == 'on':
for pgEvent in pygame.event.get():
pgEventType = pgEvent.type
self.clock.tick(self.framerate)
self.draw_screen()
self.screen.blit(self.background, (0,0))
pygame.display.flip()
event = self.get_event()
self.process_event(event)
def print_stat(self):
'''
Print statistics to stdout.
'''
for k in self.waitingTime:
self.TIS[k] = self.waitingTime[k] + self.serviceTime[k]
n1 = len(self.waitingTime)
n2 = len(self.serviceTime)
n3 = len(self.TIS)
av1 = sum(self.waitingTime.values())/n1
av2 = sum(self.serviceTime.values())/n2
av3 = sum(self.TIS.values())/n3
stdev1 = sqrt(sum([pow(self.waitingTime[i]-av1,2) for i in self.waitingTime])/(n1-1))
stdev2 = sqrt(sum([pow(self.serviceTime[i]-av2,2) for i in self.serviceTime])/(n2-1))
stdev3 = sqrt(sum([pow(self.TIS[i]-av3,2) for i in self.TIS])/(n3-1))
print '\n'
print 'Seed: ', self.seed
print 'Mode', self.queueing_mode
print 'Simulation ended at ', self.currentTime
print '=========================\t STATISTICS',
print ' \t ========================='
print '\t\t\tObservations\tAverage\t\tStDev'
print 'Waiting Time\t\t', n1, '\t\t', av1, '\t', stdev1
print 'Service Time\t\t', n2, '\t\t', av2, '\t', stdev2
print 'Time is System\t\t', n3, '\t\t', av3, '\t', stdev3
def add_event(self, eventType, eventTime, serverNumber = None):
'''
Adds event to the eventTable
'''
self.eventCounter += 1
self.eventTable.push(Event(eventType, self.eventCounter,
eventTime, serverNumber))
def get_event(self):
'''
Gets the first event in the event table
'''
e = self.eventTable.pop()
self.currentTime = e.eventTime
return e
def draw_screen(self):
'''
Draws the status of the system to the screen
'''
# Draw queue and server rectangles
for i in range(min(self.server_num, 50)):
pygame.draw.rect(self.background, self.colors[self.server[i]],
self.rec[i])
# Draw customer rectangles
if self.queueing_mode == 'single':
nrInQ = self.sqList[0].size()
for i in range(46):
rect = (690-i*15,305,self.cDimension[0],self.cDimension[1])
if i<nrInQ:
pygame.draw.rect(self.background,self.colors['c'],rect)
else:
pygame.draw.rect(self.background,self.colors['bg'],rect)
else:
for j in range(min(self.server_num, 50)):
nrInQ = self.sqList[j].size()
for i in range(46):
rect = (690-i*15,self.server_position[j],
self.cDimension[0], self.cDimension[1])
if i<nrInQ:
pygame.draw.rect(self.background,self.colors['c'],rect)
else:
pygame.draw.rect(self.background,self.colors['bg'],rect)
def display_init(self):
'''
Initializes pygame related parameters.
'''
pygame.init()
# pygame display parameters
self.cDimension = (10, 10)
self.sDimension = (10,10)
server_spacing = 550/min(self.server_num, 50)
padding = (600-(min(self.server_num, 50) - 1)*server_spacing)/2
self.server_position = [padding + i*server_spacing
for i in range(min(self.server_num, 50))]
self.rec = [(740, self.server_position[i], self.sDimension[0],
self.sDimension[1])
for i in range(min(self.server_num, 50))]
self.framerate = 100
self.colors = {'bg':(0,0,0),
'c':(0,0,200),
False:(0,255,0),
True:(255,0,0)}
# end of pygame display parameters
self.screenDimension = (800,600)
self.screen = pygame.display.set_mode(self.screenDimension)
pygame.display.set_caption(__title__)
self.background = self.screen.convert()
self.clock = pygame.time.Clock()
if __name__ == '__main__':
length = 1000
seedInput = 1
server_num = 11
IAT = 1
ST = 11
queueing_modes = ['single', 'shortest', 'random']
pi_vecs = [None, [.5, .05, .05, .05, .05, .05, .05, .05, .05, .05, .05]]
eq = EventQueue(seedInput = seedInput, IAT = IAT, ST = ST, pi = None,
server_num = server_num, queueing_mode = 'single',
graphics_mode = 'off')
#eq.simulate(length)
#eq.print_stat()
for mode in queueing_modes:
if mode == 'random':
for pi in pi_vecs:
eq = EventQueue(seedInput = seedInput, IAT = IAT, ST = ST, pi = pi,
server_num = server_num, queueing_mode = mode,
graphics_mode = 'off')
eq.simulate(length)
eq.print_stat()
else:
eq = EventQueue(seedInput = seedInput, IAT = IAT, ST = ST, pi = None,
server_num = server_num, queueing_mode = mode,
graphics_mode = 'off')
eq.simulate(length)
eq.print_stat()