Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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
Download

Sample Code (.zip)

1.6 KB
Download

Course Slides (.pdf)

187.2 KB
Avatar image for gregorypierce

gregorypierce on Nov. 4, 2020

This was actually pretty cool. Maybe I’ll simulate a fast food restaurant :)

Avatar image for philipwong37

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?

Avatar image for foolishhugo

foolishhugo on June 5, 2022

I want to explore applying these simulations to software development processes

Avatar image for Zak Markos Stefanou

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.
Avatar image for Firewing

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.