-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm1queue.py
58 lines (42 loc) · 1.51 KB
/
mm1queue.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
#-----------------------------------------------------------------------
# mm1queue.py
#-----------------------------------------------------------------------
import sys
import stdlib.stddraw as stddraw
import stdlib.stdrandom as stdrandom
from linkedqueue import Queue
from histogram import Histogram
# Accept float command-line arguments lamb and mu. Simulate an
# M/M/1 queue with arrival rate lamb and service rate mu.
lamb = float(sys.argv[1]) # Arrival rate
mu = float(sys.argv[2]) # Service rate
histogram = Histogram(60 + 1)
queue = Queue()
stddraw.setCanvasSize(700, 500)
# Compute time of next arrival.
nextArrival = stdrandom.exp(lamb)
# Compute time of next completed service.
nextService = nextArrival + stdrandom.exp(mu)
# Simulate the M/M/1 queue.
while True:
# Next event is an arrival.
while nextArrival < nextService:
# Simulate an arrival
queue.enqueue(nextArrival)
nextArrival += stdrandom.exp(lamb)
# Next event is a service completion.
arrival = queue.dequeue()
wait = nextService - arrival
# Update the histogram.
stddraw.clear(stddraw.LIGHT_GRAY)
histogram.addDataPoint(min(60, int(round(wait))))
histogram.draw()
stddraw.show(20.0)
# Update the queue.
if queue.isEmpty():
nextService = nextArrival + stdrandom.exp(mu)
else:
nextService = nextService + stdrandom.exp(mu)
#-----------------------------------------------------------------------
# python mm1queue.py .167 .25
# python mm1queue.py .167 .20