Discrete Event Simulation in Python
Dr Diogo Costa
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
The occurrence of events in real-world problems can be affected by several situations.
The most common situations involve:
These situations can be translated into a SimPy discrete-event model.
Example
Customers arrive at a clothes shop
def customer_arrivals(env, shop)
Looks for particular item
env.timeout(search_time)
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))
Certain events may be interrupted due to:
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()
capacity=5
:simpy.Resource(env, capacity=5)
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)
Bitwise operators
a & b
, a >> b
, a ^ b
In SimPy
Relevant bitwise operators for SimPy
__and__()
or &
__or__()
or |
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