Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Setting Up the Simulation Environment

00:00 It’s now time to start defining the simulation, beginning with the environment. First, declare your imports at the top of simulate.py. You’re going to want to import simpy, random, and statistics.

00:16 As you’re interested in the wait times of the moviegoers, create an empty list to hold the wait times as they get to their seats. Once all the wait times have been appended here, you’ll be able to perform some statistics on this list to determine if you have the proper number of employees or not.

00:32 Now it’s time to make the environment class. This will be the theater as a whole, so we can start defining that class now. class Theater(), extend from object, and then start making the constructor.

00:46 def __init__(), which will have self, and you’ll also pass it in an environment, which will be the simpy environment. So you’ll set that as a property now, so say self.env—for environment—is env.

01:02 Continuing with the constructor, it’s time to set some parameters for the simulation. One thing that you can control is the number of cashiers available to sell tickets to the moviegoers. When you set this property, you’ll define it as a simpy.Resource. So go ahead and inside the constructor, add num_cashiers (number of cashiers). And then make a property that’ll be .cashier. So this is going to be a simpy.Resource, which will contain the env and the num_cashiers that you pass in.

01:34 Resources in simpy are parts of the environment that are limited, so while a resource is being used, it’s not available until that process completes.

01:43 Now, you may notice in here that there’s no process defined, so you can go ahead and do that now. Add another method to the Theater class called .purchase_ticket(), and this is going to take self and then a moviegoer.

01:58 Now, since you don’t know exactly how long it will take to sell a ticket to a customer, you can estimate it using previous data. The manager reports that on average selling a ticket takes between one and two minutes to complete.

02:10 You can model this using a timeout method from the environment, which will trigger an event once a certain amount of time has passed. So set this method to yield a self.env.timeout(), and then in here, you can say random and then just pick a random integer between 1 and 2—so put 3 because it’s an exclusive, so that you’ll return a 1 or a 2. All right!

02:36 So now when you create a Theater instance, you’ll have a number of cashiers and you’ll have the ability to purchase tickets. If you’re looking closely, you may notice that there’s no link between this .purchase_ticket() process and the .cashier resource up here, but that will be handled by the moviegoer, which you’ll learn about later. Now since the cashier is only one step in this movie theater, you’re going to need ushers and servers to check tickets and sell food, respectively.

03:03 So go back to the constructor and add some additional parameters. There’s going to be a num_servers (number of servers) and a num_ushers (number of ushers).

03:15 And like before, you’ll set these properties. So just copy and paste and you’ll have .servers

03:26 and your .ushers.

03:31 And then over here, pass in the num_servers. And then the num_ushers. Now, like the .cashier, they’re going to have their own methods.

03:42 So define some additional methods like .check_ticket(), which will take self and moviegoer.

03:50 And from the manager, you learn that ushers can check tickets in about three seconds, so here you can yield self and then an .env.timeout().

04:00 And because simpy is working in minutes right now, you can just say 3 / 60 to account for 3 seconds.

04:08 All right. And then now define .sell_food(), which will be self and the moviegoer. And this, the manager says it’ll take somewhere between one and five minutes to complete an order. So from here, you’ll yield self.env.timeout(), and then like before in the .cashier, you can say random.randint().

04:29 And this will be between 1 and 5, so say 1 and 6. Awesome! Now that you’ve defined your resources and the actions that they can take, it’s time to set up the code for the moviegoer to go through these processes and use these resources. You’ll see those in the next lesson.

Become a Member to join the conversation.