I wanted to expand on my series of SimPy examples for supply chain and manufacturing simulation. In this article I show how you can model a parking lot in Python, using SimPy. SimPy is a discrete-event simulation modeling library in Python. Other SimPy examples and also R-based parking lot simmer simulation models have already been shared on this blog in the past. Here are some examples:
- Link: Parking lot simulator with simmer in R
- Link: Truck haulage simulation animation in Python
- Link: Thungela pedestrian and traffic simulation study for a new parking facility
The model discussed here could also be used to model other similar systems, for example a process buffer storage with minimum dwell times (cooling processes, hardening processes, and similar) and its slot/compartment utilization.
Benefit of parking lot simulations
Parking lot simulation models are invaluable tools in supply chain management, offering a multitude of benefits. These models facilitate the efficient allocation of resources, such as space and labor, optimizing their usage. They aid in capacity planning, enabling well-informed decisions regarding facility expansion or reduction. Traffic flow analysis enhances the overall movement of vehicles within distribution centers, reducing congestion and wait times. Efficient queue management ensures goods are loaded and unloaded promptly.
Furthermore, parking lot simulations help identify opportunities for cost reduction by minimizing idle time and enhancing operational efficiency. Performance measurement across various supply chain components supports benchmarking and goal setting. These models enable the exploration of “what-if” scenarios, allowing testing and assessment of layout, staffing, and process changes. They also aid in proactive risk mitigation by identifying bottlenecks and vulnerabilities in parking lot operations.
In addition, they serve as valuable tools for employee training and skill development, providing a controlled environment for learning parking lot and loading operations. Supporting sustainability initiatives, parking lot simulations optimize routes and reduce environmental impact. These simulations generate data-driven insights that inform strategic decision-making, and they promote a culture of continuous improvement by facilitating the testing of new strategies for ongoing optimization. Integrating parking lot simulations into supply chain management can lead to cost savings, efficiency gains, and overall performance improvements in the supply chain.
A SimPy parking lot simulation model
Below is a Python example, also available on Github, that consumes simpy for modeling a parking lot. The parking lot is modelled as a Container with a defined capacity. The capacity models the number of parking slots on the lot.
A SimPy process is implemented into the parking lot class for modeling car arrivals. This process goes on for a defined number of car arrivals. Each car arrives after a randomly distributed inter-arrival time. It then occupies an available slot for a randomly distributed amount of time. After that, the parking slot is released.
import simpy
import random
# modeling framework
class ParkingLot:
env :simpy.Environment
capacity :int
spots :simpy.resources.container.Container
def __init__(self,
env :simpy.Environment,
capacity :int
):
""" constructor """
self.env = env
self.capacity = capacity
self.spots = simpy.resources.container.Container(env, capacity, init=capacity)
def car_arrival(self,
car_id :int
) -> None:
"""
implement simpy process;
models car arrivals in the parking lot, occupying a slot for a randomly distributed duration
"""
print(f"Car {car_id} arrives at {self.env.now}")
yield self.spots.get(1)
print(f"Car {car_id} parks at {self.env.now}")
yield self.env.timeout(random.randint(1, 5))
print(f"Car {car_id} leaves at {self.env.now}")
yield self.spots.put(1)
def main(self,
cararrivals :int,
interarrivaltime_min :int,
interarrivaltime_max :int
) -> None:
""" implements simpy process for main parking lot simulation run """
for car in range(1, cararrivals+1):
t = random.randint(interarrivaltime_min, interarrivaltime_max)
yield self.env.timeout(t)
self.env.process(self.car_arrival(car))
# setup model
env = simpy.Environment()
parking_lot = ParkingLot(env, capacity= 10)
# setup simulation run itself
env.process(parking_lot.main(cararrivals= 10, interarrivaltime_min= 1, interarrivaltime_max= 5))
# run the model
env.run()
Running this model for 10 car arrivals, for a parking lot with 10 parking slots, generates the following output:
Car 2 arrives at 4
Car 2 parks at 4
Car 1 arrives at 2
Car 1 parks at 2
Car 1 leaves at 3
Car 3 arrives at 7
Car 3 parks at 7
Car 4 arrives at 8
Car 4 parks at 8
Car 2 leaves at 9
Car 5 arrives at 9
Car 5 parks at 9
Car 4 leaves at 10
Car 6 arrives at 11
Car 6 parks at 11
Car 3 leaves at 12
Car 7 arrives at 12
Car 7 parks at 12
Car 5 leaves at 13
Car 6 leaves at 13
Car 8 arrives at 16
Car 8 parks at 16
Car 7 leaves at 17
Car 8 leaves at 17
Car 9 arrives at 20
Car 9 parks at 20
Car 10 arrives at 22
Car 10 parks at 22
Car 10 leaves at 24
Car 9 leaves at 25
In upcoming tutorials I will show how to integrate custom statistics into this simulation model, and lastly, how to animate it.
Concluding remarks on SimPy parking lot simulation modeling
In this article I developed a simple base model for a parking lot, using SimPy in Python. I will use this model in upcoming tutorials to analyze car arrival processes and parking lot capacity utilization over time, and I will animate slot utilization over time. I will furthermore detail the model and implement parking lot zones and travel distance minimization strategies.
If you want to learn more about SimPy and discrete-event simulation in general you can consider studying the following SCDA blog posts and contributions:
- Link: Barge transport SimPy simulation
- Link: Assembly line simulation and line balancing
- Link: End-to-end poultry supply chain simulation
Data scientist focusing on simulation, optimization and modeling in R, SQL, VBA and Python
Leave a Reply