-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariableLambdaCap2.py
126 lines (96 loc) · 4.13 KB
/
VariableLambdaCap2.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
import random
import simpy
import matplotlib.pyplot as plt
import numpy as np
random.seed(7)
# Grand totals hold the results at each
# iteration of the for loop
GRAND_TOTAL_SERVER_TIME = []
GRAND_TOTAL_CHECKER_TIME = []
GRAND_TOTAL_WAIT_TIME = []
GRAND_TOTAL_SYSTEM_TIME = []
for lmdba in range(5, 50, 5):
arrivalRateLambda = lmdba # passengers per minute (lambda)
numServers = 2 # ID/boarding-pass queue servers
serviceTime = 0.75 # minutes per passenger at ID/boarding pass queue
numCheckers = 2 # personal-check queue checkers
minScanTime = 0.5
maxScanTime = 1.0
simRunTime = 3000 # minutes to run the simulation
averageCheckTime = []
averageServeTime = []
averageTotalWaitTime = []
averageSystemTime = []
class Airport(object):
def __init__(self, env):
self.env = env
self.serverQueue = simpy.Resource(env, capacity=numServers)
self.checkQueue = simpy.Resource(env, capacity=numCheckers)
def serve(self): # boarding-pass/id check
yield self.env.timeout(serviceTime)
def check(self): # personal check
checkerTime = random.uniform(minScanTime, maxScanTime)
yield self.env.timeout(checkerTime)
def passenger(env, name, airport):
# passenger arrives
enterSystemTime = env.now
# passenger enters ID/boarding-pass queue
with airport.serverQueue.request() as requestForServer:
startWaitingForServer = env.now
yield requestForServer
waitTimeForServer = env.now - startWaitingForServer
averageServeTime.append(waitTimeForServer)
yield env.process(airport.serve())
# passenger enters personal-check queue
with airport.checkQueue.request() as requestForChecker:
startWaitingForChecker = env.now
yield requestForChecker
waitTimeForChecker = env.now - startWaitingForChecker
averageCheckTime.append(waitTimeForChecker)
yield env.process(airport.check())
averageTotalWaitTime.append(waitTimeForServer + waitTimeForChecker)
averageSystemTime.append(env.now - enterSystemTime)
def startSimulation(env):
airport = Airport(env)
# Create initial passengers at time 0.0
for i in range(arrivalRateLambda):
env.process(passenger(env, 'Passenger %d' % i, airport))
# Create more passengers while the simulation is running
arrivalNumber = arrivalRateLambda - 1
while True:
# time between successive arrivals (exponential distribution)
yield env.timeout(np.random.exponential(arrivalRateLambda))
# arriving passengers
for i in range(arrivalRateLambda):
arrivalNumber += 1
env.process(passenger(env, 'Passenger %d' % arrivalNumber, airport))
# Setup and start the simulation
env = simpy.Environment()
env.process(startSimulation(env))
env.run(until=simRunTime)
print("\n")
print('Number of Servers in id/boarding-pass queue:', numServers)
print('Number of Checkers in personal-check queue:', numCheckers)
print('Avg wait in serve queue:', np.mean(averageServeTime))
print('Avg wait in check queue:', np.mean(averageCheckTime))
print('Avg total wait time:', np.mean(averageTotalWaitTime))
print('Avg time in system:', np.mean(averageSystemTime))
GRAND_TOTAL_WAIT_TIME.append(np.mean(averageTotalWaitTime))
GRAND_TOTAL_CHECKER_TIME.append(np.mean(averageCheckTime))
GRAND_TOTAL_SERVER_TIME.append(np.mean(averageServeTime))
GRAND_TOTAL_SYSTEM_TIME.append(np.mean(averageSystemTime))
# Plot the results
plt.plot(range(5, 50, 5), GRAND_TOTAL_SERVER_TIME)
plt.plot(range(5, 50, 5), GRAND_TOTAL_CHECKER_TIME)
plt.plot(range(5, 50, 5), GRAND_TOTAL_WAIT_TIME)
plt.plot(range(5, 50, 5), GRAND_TOTAL_SYSTEM_TIME)
plt.xticks(range(5, 50, 5))
plt.xlabel("Lambda")
plt.ylabel("Total Wait Time")
plt.title("Variable Lambda, Capacity 2")
plt.legend(['Server Wait',
'Checker Wait',
'Total Wait',
'Time in System'],
loc='lower right')
plt.show()