Adding Final Touches to the Simulation
Now that you’ve defined all that you need to define for the simulation to work, it’s time to make the main function to actually run the simulation. Here, you’re going to collect user input, run the simulation, and then print the results to the terminal. So define a
You’re going to have some setup here. I’m going to set a random seed here to
42, and if you set the same thing here, you’ll get the same results that I do.
And you’ll say
num_cashiers (number of cashiers),
num_servers (number of servers), and
num_ushers (number of ushers), is going to equal
get_user_input() to use that function that you just defined.
Now to run the simulation, we’re going to make a new
env, which is going to be a
env.process(). You’re going to run the theater, so pass in the
num_servers, and the
num_ushers to get all the employees in there. And then to run the environment, you’ll do
env.run() and then say
And what this is referring to is the number of minutes that you run the simulation. Now, you won’t actually have to wait 90 minutes, but it will be
90 minutes within the environment for the simulation.
Okay! So now that the simulation is completed, you will want to view the results, so the
mins and the
secs are going to equal
calculate_wait_times(), so let me get rid of this. And there we go,
calculate_wait_times(). And you’ll pass in the
01:49 And then from here, you can go ahead and print
"Running simulation..." And you can do some f-string formatting, so after a newline (
f"The average wait time is " and then pass in the
"minutes", and then pass it in the
02:14 And let me make sure I add some commas here
so that the
print() doesn’t have an issue. And I’ll actually go up here—and yeah, I forgot commas here as well.
Okay. So the last thing you’ll want to do is make a
if __name__ == '__main__':, like so, and then you’ll call
main(). All right!
So if you want to go ahead and finally test this out, let’s go ahead and run this with the default parameters. So I’m going to say
python simulate.py and—oh! I’ve got a
02:52 Looks like an extra period on line 45. So let’s go up to line 45, and there it is. Let’s get rid of that. Save it,
and let’s try that again. Okay! So the
# of cashiers working: let’s just say the default of
1 server and
And what you should end up with is
43 minutes and 38 seconds, which—if you go back to the goal of 10 minutes or less—is way off. So there’s some work to do here.
But your simulation is working, and you just tested it out for
90 minutes and it took less than a second to run. Okay! So that’s great. In the next lesson, you’re going to start experimenting with these parameters and seeing if you can get the wait times down to below 10 minutes, as that’s what the goal is.
@c e Can you be more specific as to why you can’t run the code in Colab?
Please, note that the
simpy module doesn’t come pre-installed with the Colab environment out of the box. However, you can fix that by executing the following shell command in your notebook cell:
!pip install simpy
(Don’t forget to include the exclamation mark!)
Become a Member to join the conversation.
c e on March 7, 2021
Hi Dear Real Python Team, Thank you for this video series. As a real beginner who needs to build a simulation, when I try the code in Colab, unfortunately I am not able to call it like you do as python simulate.py. Could you share some tips or a link with me to learn from there?