SimPy: Simulating Real-World Processes With Python
The real world is full of systems, like airports and highways, that frequently experience congestion and delay. When these systems are not optimized, their inefficiency can lead to countless unhappy customers and hours of wasted time. In this tutorial, you’ll learn how to use Python’s simpy framework to create virtual simulations that will help you solve problems like these.
In this tutorial, you’ll learn how to:
- Use a simulation to model a real-world process
- Create a step-by-step algorithm to approximate a complex system
- Design and run a real-world simulation in Python with
simpy
In this tutorial, you’ll create a simulation for a local movie theater. Your goal is to provide the manager with a script to help find the optimal number of employees to have on staff. You can download the source code for this script by clicking on the link below:
Download Code: Click here to download the code you’ll use to learn about SimPy in this tutorial.
What Simulation Is
A simulation is a representation of a real-world system. One can use mathematical or computational models of this system to study how it works, or what happens when parts of it are changed. Simulations are used in airports, restaurants, mechanics, government agencies, and many other systems where poor resource allocation can lead to congestion, customer dissatisfaction, and critical transportation delays.
A system can be any environment where things happen. Examples of real-world systems include car washes, banks, manufacturing plants, airports, post offices, call centers, and more. These systems have agents that undergo processes within them. For instance:
- A car wash will have cars go through the washing process.
- An airport will have passengers go through the security check process.
- A call center will have customers go through the process of speaking with a telemarketer.
This relationship is summed up in the table below:
| System | Agent | Process |
|---|---|---|
| Car wash | Car | Wash |
| Airport | Passenger | Security check |
| Call center | Customer | Speak with a telemarketer |
Understanding the processes that agents go through within a system is an important component of logistical planning, especially for large-scale organizations. For example, an airport can see passenger wait times at a security checkpoint skyrocket if there aren’t enough workers that day. Similarly, time-sensitive mail can be delayed by days (or even weeks) if it isn’t routed properly.
These instances of congestion can have real-life consequences on time and money, so it’s important to be able to model these processes beforehand. This gives you an idea of where the system might run into problems and how resources should be allocated ahead of time to solve those problems in the most efficient way possible.
How Simulation Works
In Python, you can use the simpy framework for event simulation. First, take a quick look at how a simulated process would run in Python. Below is a code snippet from a simulation of a security checkpoint system. The following three lines of code set up the environment, pass all necessary functions, and run the simulation:
# Set up the environment env = simpy.Environment() # Assume you've defined checkpoint_run() beforehand env.process(checkpoint_run(env, num_booths, check_time, passenger_arrival)) # Let's go! env.run(until=10) The first line of code above establishes the environment. You’ll do this by assigning simpy.Environment() to the desired variable. Here, it’s simply named env. This tells simpy to create an environment object named env that will manage the simulation time and move the simulation through each subsequent time step.
Once you have your environment established, you’ll pass in all of the variables that will act as your parameters. These are the things you can vary to see how the system will react to changes. For this security checkpoint system, you’re using the following parameters:
env: the environment object to schedule and process eventsnum_booths: the number of ID check boothscheck_time:the length of time it takes to check a passenger’s IDpassenger_arrival: the rate at which passengers arrive at the queue
Then, it’s time to run the simulation! You can do this by calling env.run() and specifying how long you want the simulation to run for. The simulation runs in minutes, so this sample code will run the simulation for 10 real-time minutes.
Note: Don’t worry! You won’t have to wait 10 actual minutes for the simulation to finish. Because simulation gives you a virtual look at a real-time process, those 10 minutes will pass in mere seconds on the computer.
To recap, here are the three steps to running a simulation in Python:
- Establish the environment.
- Pass in the parameters.
- Run the simulation.
But there’s a lot more going on underneath the hood! You’ll need to understand how to choose those parameters, and you’ll have to define all the functions that will be called when the simulation is run.
Let’s get started!
Read the full article at https://realpython.com/simpy-simulating-with-python/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
原文:https://www.a8q9.cn/en/article/simpy-simulating-real-world-processes-with-python-a32091.html