Discrete Event Simulation in Python
Dr Diogo Costa
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
SimPy provides three types of resources:
resource
container
store
simpy.Resource()
simpy.Container()
simpy.Store()
Creating a resource
resource_1 = simpy.Resource(env, capacity=8)
capacity=8
)Requesting a resource
# Open resource request with resource_1.request() as request:
# Make resource request yield request
# Once resource is avaiable yield env.timeout(event_duration)
Variation of previous resource
resource_1 = simpy.PreemptiveResource(
env, capacity=1)
Some processes may have priority over others
Example: repairman
Usage similar to simpy.Resource()
Requesting resource with highest priority
with resource_1.request(priority=1) as
request:
yield request
yield env.timeout(process_duration)
Requesting resource with lower priority
with resource_1.request(priority=2) as
request:
yield request
yield env.timeout(process_duration)
Helpful to model production/consumption of homogeneous, undifferentiated bulk
May be continuous (like water) or discrete (like apples)
Example: Petrol reserves in a gas station.
tank_1 = simpy.Container(
env, tank_size, init=tank_size)
Add and remove from container/tank
yield tank_1.put(add_quantity)
yield tank_1.get(remove_quantity)
Get current quantity in tank
tank_1.level
Get total tank capacity
tank_1.capacity
Create a store
store_1 = simpy.Store(
env, capacity=2)
Similar to simpy.Container()
but allows storing different objects
In contrast to the rather abstract "amount" stored in containers
yield store_1.put("item_1")
yield store_1.get()
Get list of items available
store_1.items
Get store capacity
store_1.capacity
Example
Current situation: 1 cashier
simpy.Resource(env, capacity=1)
...
time: 23.7 min | Customer 30 > Finished
Scenarios simulated: n cashiers
simpy.Resource(env, capacity=2)
...
time: 13.2 min | Customer 30 > Finished
simpy.Resource(env, capacity=3)
...
time: 8.6 min | Customer 30 > Finished
simpy.Resource(env, capacity=4)
...
time: 8.6 min | Customer 30 > Finished
Discrete Event Simulation in Python