1. Introduction to the SimPy package
Some dynamic systems can be characterized using discrete-event simulations, and while it is possible to create such models using standard Python, the SimPy package can be helpful.
2. What is SimPy?
SimPy is a process-based discrete-event simulation framework based on standard Python.
It uses generator functions to define processes and events. A generator is a particular type of function that is covered in one of the prerequisite courses. We will refresh its key properties in this video.
SimPy is scalable. Models can start with one generator and evolve to have multiple.
Each generator can contain multiple processes running in sequence or parallel.
3. Normal functions vs. generator functions
What are generators, and how do they differ from normal functions?
Normal functions use the "return" statement to return a value. Conversely, generator functions use the "yield" statement. This is the only difference in terms of syntax, but it translates into different behavior.
Notice on the right-hand side how we can add multiple "yield" statements. That's because, unlike normal functions that return a value and terminate, generators with "yield" statements return a first value, but instead of terminating the function, they suspend it, maintaining its internal state. This internal statement is reinstated when the generator is called again.
Because of this, executing the normal function on the left-hand side only prints "10", but executing the generator on the right-hand side prints both "20" and "12".
Let's now summarize the main SimPy syntax we'll be using.
4. Summarizing key SimPy methods
First there's the method dot-Environment to build SimPy Environment objects, which we are giving the name "env".
Second there's the method dot-process to add processes to the SimPy Environment using Generator Functions.
Third, we have the method dot-run to run the model.
Fourth, we have the method dot-timeout to clock-in the time a process takes to complete.
Fifth, we can use the dot-now method to get the current simulation time.
Let's walk through an example.
5. Traffic lights: Building a SimPy model
Imagine we want to build a discrete-event model to study the impact of introducing two new traffic lights in Toronto, Canada.
The first step is to import the SimPy package.
The second step is to create a generator to simulate the traffic lights. It uses the dot-timeout method to clock-in time between light switches (red, yellow and green) as prescribed by the argument "timestep". The other two arguments are "env", which is the SimPy environment and "name", which is the name given to each traffic light.
6. Traffic lights: Building a SimPy model
The third step is to build the SimPy environment.
The fourth step is to add the two traffic lights as parallel, independent processes to the environment, one named "Leslie St.", which switches colors every 15 seconds, and the other named "Arlington Av.", which switches colors every 30 seconds.
The fifth step is to run the model. In this case, we are running the model for 90 seconds. The time units are up to us to define, but they must be consistent throughout the model. Here the time units will be seconds.
Let's now put the code together and run it for ninety seconds.
7. Traffic lights: Model outputs
Running this model produces the following console output. Each line records a traffic light switch. There are three pieces of information per line: the time of the event, the traffic light identification, and the color of the light switch.
As prescribed in the model setup, the traffic light at Leslie St. switches colors every 15 seconds, while the one at Arlington Av. switches every 30 seconds.
8. Let's practice!
Now it's your turn to build a discrete-event simulation model using SimPy!