Link Search Menu Expand Document

Simulating a bus stop with simpy

In this example, we will simulate a bus stop where people and buses arrive using the simpy library. This package allows you to simulate components such as vehicles or people and their interactions through events.

Let’s explore an example of simulating a bus stop. We have two processes:

  • 🧍🏼 People: Individuals who arrive at the stop randomly with the intention of taking the bus.
  • 🚌 Bus: A bus that arrives at the stop and picks up people at fixed intervals with some randomness.

This type of simulation can be useful for determining the appropriate size of buses and their frequencies, ensuring that people wait as little as possible without wasting resources.

For instance, if 10 people arrive per minute and a bus runs every hour with only 20 seats, the bus stop will become overcrowded, leading to dissatisfied customers. We aim to avoid this scenario.

We begin by importing the necessary modules.

import simpy
import random

Next, we define a BusStop class with two processes. One simulates people arriving at the bus stop, and the other simulates the bus arriving and picking up people.

class BusStop:
    def __init__(self, env, bus_capacity):
        self.people = 0
        self.env = env
        self.bus_capacity = bus_capacity

    def person_arrives(self):
        while True:
            arrive = random.randint(0, 10)
            print(f"[PERSON] Arrived {arrive} in the minute {self.env.now}")
            self.people += arrive
            yield self.env.timeout(1)

    def bus_arrives(self):
        while True:
            take_away = min(self.bus_capacity, self.people)
            self.people -= take_away
            print(f"[BUS] Took away: {take_away} and {self.people} remain")
            yield self.env.timeout(random.uniform(5, 10))

Let’s break it down:

  • 🧍🏼 person_arrives: Every minute, a random number of people between 0 and 10 arrive at the stop.
  • 🚌 bus_arrives: A bus arrives randomly between 5 and 10 minutes, with a normal distribution. Each time it arrives, it picks up as many people as its capacity allows.

Now, we create the env environment and run the simulation for 120 minutes.

env = simpy.Environment()
stop = BusStop(env, bus_capacity=50)

env.process(stop.person_arrives())
env.process(stop.bus_arrives())
env.run(until=120)

With these parameters, there are almost never people waiting for the bus. However, if we reduce the bus_capacity to 30, the bus stop becomes overcrowded, and more people are left waiting. The bus offer can’t keep up with the demand.

These types of simulations are very useful for planning bus or subway lines. For a given demand, we can estimate:

  • πŸ“ The size of the bus, measured in person capacity.
  • ⏳ The frequency of the bus, measured in time.

This approach can also be applied to simulations of virus propagation, financial markets, or traffic. We encourage you to explore all that simpy has to offer.

✏️ Exercises:

  • Use matplotlib to plot the number of people waiting at the stop over time. Modify the BusStop class if necessary.
  • Come up with a table that shows the number of people waiting at the stop for different bus capacities and frequencies. As this is random, use averages.