Simulating Real-World Processes in Python With SimPy (Summary)
In this course, you’ve learned how to build and run a simulation in Python using the simpy
framework. You’ve come to understand how systems have agents undergo processes, and how you can create virtual representations of those systems to fortify them against congestion and delay. While the type of simulation can vary, the overall execution is the same! You’ll be able to apply what you’ve learned here to a variety of different scenarios.
Now you can:
- Brainstorm a simulation algorithm step by step
- Create a virtual environment in Python with
simpy
- Define functions that represent agents and processes
- Change parameters of your simulation to find the optimal solution
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
Congratulations on completing this course on how to simulate processes using simpy
. In this set of lessons, you learned how to set up your development environment, how to define a simulation, how to set up the simulation environment, how to define the processes within that environment, how to run those processes within the simulation, how to calculate metrics from the simulation, a few ways on how to select parameters for the simulation, and then finally, you were able to experiment with that simulation. simpy
relies on understanding how to use yield
and generators in Python, so with a solid understanding of those and how simpy
works, you’re now free to try and model real-world processes with Python.
00:44 Some of these solutions can be very valuable in saving people time and money, so don’t be afraid to try new things out. You can make your simulations as simple or complex as needed.
00:55
And if you head to the simpy
documentation, there’s a couple of examples of some different processes that you can simulate, so that might be a good way to get started in using simpy
on your own.
philipwong37 on March 21, 2021
If there are multiple queues to get food before sitting down, how do I model the shortest queue to get food?
foolishhugo on June 5, 2022
I want to explore applying these simulations to software development processes
Zak Markos Stefanou on May 11, 2023
Thank you very much. That was really enlightening. However, I seem to have the following problem. I found that with 9 cashiers, 1 server, 1 usher, we can drop the time to 8min 4sec. (seed is same as in sample code).
I would expect that increasing the servers would either drop above time or leave it intact. Instead of this the average time increases. How is this possible? Where is the catch?
(venv) PS C:\Data\pythonProject\venv> py .\simulate.py
Input # of cashiers working: 9
Input # of servers working: 1
Inout # of ushers working: 1
Running simulation...
The average wait time is 8 minutes and 4 seconds.
(venv) PS C:\Data\pythonProject\venv> py .\simulate.py
Input # of cashiers working: 9
Input # of servers working: 2
Inout # of ushers working: 1
Running simulation...
The average wait time is 11 minutes and 31 seconds.
Firewing on Aug. 11, 2023
I wish to take this opportunity to highlight that the order in which the requests are made is important in determining the average wait time. In the lesson, the go_to_movies function has the usher request before the server request. (I would have thought the usher request should be called last. At least, that’s the case here in Australia. Might be different overseas).
To test what I mean, take out the randomness by having ‘Buy ticket’ in 2 minutes and always ‘Buy food’ in 3 minutes. Leave the usher at 3 seconds. Now if the simulation is run for 45 minutes and 1 cashier, 1 food server and 1 usher, the average wait time is 23 minutes and 56 seconds.
Now swap around the ‘with’ requests in the go_to_movies function by calling with.theater.cashier.request() and then with.theater.server.request() and then with.theater.usher.request() and finally rerun.
The average wait time comes out as is 23 minutes and 36 seconds for the same number of resources.
Become a Member to join the conversation.
gregorypierce on Nov. 4, 2020
This was actually pretty cool. Maybe I’ll simulate a fast food restaurant :)