Setting Up the Simulation Environment
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
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.
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.
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
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.
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
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.
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
3 because it’s an exclusive, so that you’ll return a
1 or a
2. All right!
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.
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
And this will be between
5, so say
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.