Simulate Real-life Events in Python Using SimPy | by Khuyen Tran | Towards Data Science

As a restaurant manager, you want to approximate how much food your restaurant needs to prepare for tomorrow. You know how many customers come to your restaurant per day and the average time it takes to serve one customer. However, it is challenging to put all of these variables into one calculation.
Wouldn’t it be great if you can simulate this event using Python?
That is when SimPy comes in handy.
SimPy is a Python library that enables you to simulate real-life events. It can model active components such as customers, vehicles, or agents.
To install SimPy, type:
pip install simpy
In SimPy, the behaviors of active components such as customers or vehicles are modeled with processes. These processes live in an environment. They interact with the environment and with each other via events.
To understand these concepts better, let’s try to model a waiter (a process) who serves in a restaurant (an environment). Some basic things that waiters do are:
Taking orders from customers
Giving orders to the cooks
Serving food to customers
Here, the waiter is a process. The waiter lives in an env . The current simulation time is env.now . The env.timeout(duration) method models the time it takes to finish one activity.
Output:
Cool! We are able to simulate some basic things a waiter does. The outputs above make sense since it takes around:
5s to take the orders from the customers (take_order_duration = 5)
2s to give the orders to the cooks (give_order_duration = 2)
5s to serve food to the customers (serve_order_duration = 5)
Note that we don’t see “Start serving customers food at 31” because we only run the simulation for 30 s.
We can simulate customers using the same logic as above. However, since there is a limited number of customers that can be served in the restaurant, we will also model the restaurant with a limited capacity.
That could be done using thesimpy.Resource class. We will use simpy.Resource(env, capacity=2) to limit the number of customers to 2. This means new customers can get into the restaurant only when there are fewer than 2 customers in the restaurant.
Now we can specify the duration of each activity and simulate 5 customers. Note that the order of customers and their numbers are unrelated.
Output:
Cool! From the output, we can see that:
New customers come in at random times
If there are already 2 customers in the restaurant, new customers need to wait until an existing customer finish eating
Pretty cool, isn’t it?
As a manager, you think 10 items per option of food are enough to feed your customers. Let’s test this assumption by applying what we have learned so far.
We will simulate a restaurant that:
Only takes take-out orders
There is only one staff member
The customers are waiting in line to order food on the menu. The more items a customer orders, the longer the customer needs to wait.
The attributes of this restaurant are:
staff : a resource with the capacity 1 (the staff can only serve one customer at a time)
foods : options of food the restaurant offers
available : number of items per option of food
run_out : events when each option of food runs out
when_run_out : the time when each option of food runs out
rejected_customers : number of customers who leave the line because their food option runs out
We also want to create a customer who is waiting in line. A customer will:
Leave if there is not enough food left
Order food if there is enough food left
If there is no food left after the customer orders a particular kind of food, we will declare that the chosen food has run out.
Two new concepts in the code above are:
restaurant.staff.request : Request usage of the resource. In this case, the customer requests a service from the staff member.
restaurant.run_out[food].succeed : run_out is an event that can either succeed or fail. In this case, if the event run_out succeeds, it signals that the restaurant runs out of a food option.
There are new customers until the simulation reaches the time limit.
Now we have created the customers and the restaurant, let’s run the simulation. We will print messages when the restaurant runs out of a food option.
Output:
Cool! Imagine 1 second in SimPy is 1 minute in real life. If the simulation runs for 240 minutes (4 hours), and there are 10 items per food choice, then the restaurant will run out of each food choice in fewer than 240 minutes.
There is also a total of 22 customers leaving the restaurant hungry. Thus, if we want to feed all of our customers in 240 minutes, we might need to prepare more than 10 items per food option.
Note that the example above is only a simplified version of a real restaurant. To make the example closer to a real event in a restaurant, you might want to add more interactions.
You can also run the simulations more than once to get a better approximation of the number of minutes before an option of food runs out.
Check out the examples in SimPy’s documentation for other inspirations.
Congratulations! You have just learned how to use SimPy to simulate real-life events. I hope this will give you the motivation to simulate the events you are interested in.
The source code for this article can be found here:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: