Building a discrete-event model with SimPy

Discrete Event Simulation in Python

Dr Diogo Costa

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

Before we build our model

  • Understand dynamic system

  • Define objective(s) of discrete-event model

  • Identify processes:

    • running in parallel
    • running in sequence
  • Identify appropriate SimPy resources (resource, container, store)

  • Define architecture of model:

    • Identify state variables
    • Workflow of model
    • Generators
Discrete Event Simulation in Python

The dynamic system and objectives of the model

Understand the dynamic system

  • What are the aspects that modulate its behavior?
  • What processes/events need to be included?

Objectives of the model

  • Often used to optimize systems
  • What are the aspects you want to optimize?

    • More customers
    • Faster processing times
    • Optimize resource allocation
  • State-variables to monitor these objectives

Discrete Event Simulation in Python

Identify the appropriate SimPy resources

Shared Resources

simpy.Resources(env, capacity=4)
  • Resources shared between processes

Examples

  • Tables at a restaurant
  • Machines in a factory
  • Seats at a theatre
  • Cashiers in a shop

Containers

simpy.Container(env, capacity, 
init=initial_capacity)
  • Resources behaves like a tank or basket

  • Add and remove quantity:

.put() and .get()

Examples

  • Fuel reserves in a gas station
  • Coal in a coal-fired power plant
Discrete Event Simulation in Python

Identify the appropriate SimPy resources

Store

  • Resource like a store with multiple items

  • Capacity is unlimited (default)

  • Capacity slots for storing arbitrary objects

  • Add and remove items:

.put() and .get()

Examples

  • Shops selling multiple items
  • Storage room
  • Workshop
  • Household
Discrete Event Simulation in Python

Concatenate events using bitwise operators

SimPy uses bitwise operators to concatenate two events

Types of operators in Python

  • Arithmetic, logical, and comparison operators

Bitwise operators

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

In SimPy

  • Bitwise operators generate condition event
  • Wait for both or one of two events/processes

Relevant bitwise operators for SimPy

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

Let's practice!

Discrete Event Simulation in Python

Preparing Video For Download...