-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncreasingServers.py
123 lines (93 loc) · 4.04 KB
/
IncreasingServers.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
import random
import simpy
import numpy as np
import matplotlib.pyplot as plt
GRAND_TOTAL_SERVER_TIME = []
GRAND_TOTAL_CHECKER_TIME = []
GRAND_TOTAL_WAIT_TIME = []
GRAND_TOTAL_SYSTEM_TIME = []
for numServers in range(1, 5):
random.seed(7)
ARRIVAL_RATE_LAMBDA = 50 # passengers per minute (lambda)
NUM_SERVERS = numServers # ID/boarding-pass queue servers
SERVICE_TIME = 0.75 # minutes per passenger at ID/boarding pass queue
averageServeTime = []
NUM_CHECKERS = 1 # personal-check queue checkers
MIN_SCAN_TIME = 0.5
MAX_SCAN_TIME = 1.0
averageCheckTime = []
SIM_RUN_TIME = 3000 # minutes to run the simulation
averageTotalWaitTime = []
averageSystemTime = []
class Airport(object):
def __init__(self, env):
self.env = env
self.serverQueue = simpy.Resource(env, capacity=NUM_SERVERS)
self.checkQueue = simpy.Resource(env, capacity=NUM_CHECKERS)
def serve(self): # boarding-pass/id check
yield self.env.timeout(SERVICE_TIME)
def check(self): # personal check
checkerTime = random.uniform(MIN_SCAN_TIME, MAX_SCAN_TIME)
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(ARRIVAL_RATE_LAMBDA):
env.process(passenger(env, 'Passenger %d' % i, airport))
# Create more passengers while the simulation is running
arrivalNumber = ARRIVAL_RATE_LAMBDA - 1
while True:
# time between successive arrivals (exponential distribution)
yield env.timeout(np.random.exponential(ARRIVAL_RATE_LAMBDA))
# arriving passengers
for i in range(ARRIVAL_RATE_LAMBDA):
arrivalNumber += 1
env.process(passenger(env, 'Passenger %d' % arrivalNumber, airport))
# Setup and start the simulation
random.seed(7)
env = simpy.Environment()
env.process(startSimulation(env))
env.run(until=SIM_RUN_TIME)
print("\n")
print('Number of Servers in id/boarding-pass queue:', NUM_SERVERS)
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))
plt.plot(range(1, 5), GRAND_TOTAL_SERVER_TIME)
plt.plot(range(1, 5), GRAND_TOTAL_CHECKER_TIME)
plt.plot(range(1, 5), GRAND_TOTAL_WAIT_TIME)
plt.plot(range(1, 5), GRAND_TOTAL_SYSTEM_TIME)
plt.xticks([1, 2, 3, 4])
plt.xlabel("Number of Servers")
plt.ylabel("Total Wait Time")
plt.title("Effects of Increasing Servers on Wait Times")
plt.legend(['Server Wait',
'Checker Wait',
'Total Wait',
'Time in System'],
loc='upper right')
plt.show()