Skip to content

Commit 96328bc

Browse files
authored
Refactor simulate.py
- remove `arrival_times` global variable - replace `departure_times` global variable with `wait_times` - move assignment of `arrival_time = env.now` inside `go_to_movies()` - rename `calculate_wait_time()` to `get_average_wait_time()` - import statistics and use statistics.mean() to calculate average wait time
1 parent 67b8508 commit 96328bc

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

simulation-with-simpy/simulate.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import simpy
1010
import random
11+
import statistics
1112

12-
arrival_times = []
13-
departure_times = []
13+
wait_times = []
1414

1515

1616
class Theater(object):
@@ -31,6 +31,9 @@ def sell_food(self, moviegoer):
3131

3232

3333
def go_to_movies(env, moviegoer, theater):
34+
# Moviegoer arrives at the theater
35+
arrival_time = env.now
36+
3437
with theater.cashier.request() as request:
3538
yield request
3639
yield env.process(theater.purchase_ticket(moviegoer))
@@ -39,42 +42,31 @@ def go_to_movies(env, moviegoer, theater):
3942
yield request
4043
yield env.process(theater.check_ticket(moviegoer))
4144

42-
get_food = [0, 1]
43-
if random.choice(get_food) == 1:
45+
if random.choice([True, False]):
4446
with theater.server.request() as request:
4547
yield request
4648
yield env.process(theater.sell_food(moviegoer))
4749

48-
departure_time = env.now
49-
departure_times.append(departure_time)
50+
# Moviegoer heads into the theater
51+
wait_times.append(env.now - arrival_time)
5052

5153

5254
def run_theater(env, num_cashiers, num_servers, num_ushers):
5355
theater = Theater(env, num_cashiers, num_servers, num_ushers)
5456

5557
for moviegoer in range(3):
5658
env.process(go_to_movies(env, moviegoer, theater))
57-
arrival_time = env.now
58-
arrival_times.append(arrival_time)
5959

6060
while True:
6161
yield env.timeout(0.20) # Wait a bit before generating a new person
6262

6363
moviegoer += 1
6464
env.process(go_to_movies(env, moviegoer, theater))
65-
arrival_time = env.now
66-
arrival_times.append(arrival_time)
67-
68-
69-
def calculate_wait_time(arrival_times, departure_times):
70-
total_wait = []
71-
total_seated = len(departure_times)
7265

73-
for i in range(total_seated):
74-
total_wait.append(departure_times[i] - arrival_times[i])
7566

67+
def get_average_wait_time(wait_times):
68+
average_wait = statistics.mean(wait_times)
7669
# Pretty print the results
77-
average_wait = sum(total_wait) / total_seated
7870
minutes, frac_minutes = divmod(average_wait, 1)
7971
seconds = frac_minutes * 60
8072
return round(minutes), round(seconds)
@@ -89,7 +81,7 @@ def get_user_input():
8981
params = [int(x) for x in params]
9082
else:
9183
print(
92-
"Could not parse input. The simulation will use default values:",
84+
"Could not parse input. Simulation will use default values:",
9385
"\n1 cashier, 1 server, 1 usher.",
9486
)
9587
params = [1, 1, 1]
@@ -107,7 +99,7 @@ def main():
10799
env.run(until=90)
108100

109101
# View the results
110-
mins, secs = calculate_wait_time(arrival_times, departure_times)
102+
mins, secs = get_average_wait_time(wait_times)
111103
print(
112104
"Running simulation...",
113105
f"\nThe average wait time is {mins} minutes and {secs} seconds.",

0 commit comments

Comments
 (0)