SimPy Package: Managing the scheduling of events

Discrete Event Simulation in Python

Dr Diogo Costa

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

Managing event occurrences

  • The occurrence of events in real-world problems can be affected by several situations.

  • The most common situations involve:

    • conditional events
    • interruption of events
    • waiting for processes

These situations can be translated into a SimPy discrete-event model.

Discrete Event Simulation in Python

Conditional events

  • Certain events depend on the occurrence of other event
  • They are only triggered if a condition is met

Example

  • Customers arrive at a clothes shop

    def customer_arrivals(env, shop)
    
  • Looks for particular item

    env.timeout(search_time)
    
  • Buys it only if available
if shop.available[item]:

Generator: bringing all code together

def customer_arrivals(env, shop):

while True: yield env.timeout(search_time)
# Only buy item if available if shop.available[item]: env.process(shop_buyer(env, item, num_items, shop))
Discrete Event Simulation in Python

Interruption of events

Certain events may be interrupted due to:

  • Higher priority events taking place
  • Voluntary interruption (e.g., car charging)
  • Forced interruption (e.g., machine breaks)

Method

simpy_process_1.interrupt()

Example

Machine breaks periodically, interrupting the production line.

def break_machine(env):

# Break machine every now and then. while True: # Time between machine fails yield env.timeout(fail_time())
# Machine fail: interrupt process simpy_process_1.interrupt()
Discrete Event Simulation in Python

Waiting for processes

  • Some processes may have to wait for others to complete
  • This often happens because resources are often limited
  • Example: shared resources with capacity=5:
    simpy.Resource(env, capacity=5)
    
  • Thus, a process may have to wait if all resources are being used

Example: Petrol reserves in a gas station need to be replenish regularly

with gas_station.request() as request:

# Request one of the gas pumps yield request
# Once pump is available: pump fuel liters_request = 10 yield fuel_reserve.get(liters_request) # The "actual" refueling takes time refuel_duration = liters_request / refuel_speed
yield env.timeout(refuel_duration)
Discrete Event Simulation in Python

Concatenate events using bitwise operators

Bitwise operators

  • Perform bitwise calculations on integers
  • Expect two operands
    • Example: a & b, a >> b, a ^ b

In SimPy

  • Generates condition event
  • Between 2 events

Relevant bitwise operators for SimPy

  • Waits for both processes: __and__() or &
  • Waits for either process: __or__() or |
Discrete Event Simulation in Python

Concatenate events using bitwise operators

Example: Moviegoer waits in the ticket line until his/her turn or the movie sells out

with counter.request() as my_turn:

# Wait until turn or movie is sold out result = yield my_turn | counter.sold_out[movie]
# If tickets available if my_turn in result: theater.num_moviegoers[movie] += 1 return
# If tickets sold out if my_turn not in result: theater.num_renegers[movie] += 1 return
Discrete Event Simulation in Python

Let's practice!

Discrete Event Simulation in Python

Preparing Video For Download...