SimPy package: Types of resources

Discrete Event Simulation in Python

Dr Diogo Costa

Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific

Types of resources available

  • SimPy provides three types of resources:

  • resource

  • container
  • store
Discrete Event Simulation in Python

Types of resources available

simpy.Resource()

  • Share resources between processes
  • Example: tables in a restaurant

simpy.Container()

  • Resource for sharing homogeneous matter between processes
  • Quantities can be discrete (e.g., apples) or continuous (e.g., water)
  • Example: Water reservoir

simpy.Store()

  • Store a possibly unlimited amount of objects
  • Example: supermarket
Discrete Event Simulation in Python

Method: simpy.Resources()

Creating a resource

resource_1 = simpy.Resource(env, capacity=8)
  • Used when we have a limited resource capacity (e.g., capacity=8)
  • Used to limit the number of processes accessing a resource concurrently
  • Process needs to request usage right to the resource
    • If all resources are being used, requests are enqueued
  • When processes releases the resource, pending requests will be triggered

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)
Discrete Event Simulation in Python

Resources: priorities and preemption

  • Variation of previous resource

    resource_1 = simpy.PreemptiveResource(
    env, capacity=1)
    
  • Some processes may have priority over others

  • Example: repairman

    • Priority 1: repair broken machines
    • Priority 2 or less: machine maintenance
  • 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)
    
Discrete Event Simulation in Python

Method: simpy.Container()

  • 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.

  • Cars decrease reserves while refill trucks increase it
  • Creating a container
    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
    
Discrete Event Simulation in Python

Method: simpy.Store()

  • 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

  • Add and remove to storage
yield store_1.put("item_1")
yield store_1.get()
  • Get list of items available

    store_1.items
    
  • Get store capacity

    store_1.capacity
    
Discrete Event Simulation in Python

How can SimPy resources inform business decisions?

Example

  • Supermarket with 1 cashier
  • Owner wants to know if worth investing in more cashiers
  • Will more cashiers reduce waiting time?

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

Let's practice!

Discrete Event Simulation in Python

Preparing Video For Download...