SLIDE 1
Boosting Simulation Performance with Python Eran Friedman How to - - PowerPoint PPT Presentation
Boosting Simulation Performance with Python Eran Friedman How to - - PowerPoint PPT Presentation
Boosting Simulation Performance with Python Eran Friedman How to use Discrete-Event Simulation to run your system faster than real-time? Background About Me Eran Friedman Team lead @ Fabric Nowadays developing the Ground Robot
SLIDE 2
SLIDE 3
Background
SLIDE 4
About Me
- Eran Friedman
- Team lead @ Fabric
- Nowadays developing
the Ground Robot
SLIDE 5
Outline
- Simulations - why?
- How to use DES?
- How to use SimPy?
- What are the challenges?
- How to distribute?
SLIDE 6
Simulation
“An approximate imitation of the
- peration of a process or system ...”
- Wikipedia
SLIDE 7
Simulation
Orders Stock Motion
. . .
Backend
SLIDE 8
Simulation
Orders Stock Motion
. . .
Backend
SLIDE 9
Importance of Simulations
COVID-19
SLIDE 10
Importance of Simulations
Automated regression tests
SLIDE 11
Importance of Simulations
Analyze performance & compare algorithms
SLIDE 12
Importance of Simulations
Run in the cloud
SLIDE 13
Importance of Simulations
Verify warehouse layout
SLIDE 14
Importance of Simulations
Inject failures & improve robustness
SLIDE 15
Importance of Simulations
Simulate a large facility
SLIDE 16
Discrete-Event Simulation (DES)
- Operations are modeled as sequence of events
- Simulation jumps to the next event
- Simulation maintains its own clock
- Example: 2 m/s, 10 time-ticks/second
t=0 x=0cm t=0.1 x=20cm t=0.2 x=40cm
SLIDE 17
Discrete-Event Simulation (DES)
- Operations are modeled as sequence of events
- Simulation jumps to the next event
- Simulation maintains its own clock
- Example: 2 m/s, 10 time-ticks/second
t=0 x=0cm t=0.1 x=20cm t=0.2 x=40cm
SLIDE 18
Discrete-Event Simulation (DES)
- Operations are modeled as sequence of events
- Simulation jumps to the next event
- Simulation maintains its own clock
- Example: 2 m/s, 10 time-ticks/second
t=0 x=0cm t=0.1 x=20cm t=0.2 x=40cm
SLIDE 19
SimPy Library
- Discrete-event simulation (DES) framework
- Created in 2002
- MIT license
- Pure Python
- No dependencies
SLIDE 20
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue
SLIDE 21
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r1 t=0 r0 t=0
SLIDE 22
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r0 t=0 r1 t=0 Executing -
SLIDE 23
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r0 t=0 r1 t=0 Executing - r0 t=0.1
SLIDE 24
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r1 t=0 Executing - r0 t=0.1
SLIDE 25
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r0 t=0.1 Executing - r1 t=0
SLIDE 26
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r0 t=0.1 Executing - r1 t=0 r1 t=0.1
SLIDE 27
SimPy Overview
Environment t = 0 Processes: r0 r1 Event queue r0 t=0.1 Executing - r1 t=0.1
SLIDE 28
SimPy Overview
Environment t = 0.1 Processes: r0 r1 Event queue r1 t=0.1 Executing - r0 t=0.1
SLIDE 29
SimPy Example - Robot Race
- A robot’s speed is about 2-4 meters/second
SLIDE 30
SLIDE 31
SimPy Example - Robot Race
- All SimPy processes run in a single thread
- Parameters that affect performance:
○ Number of simulated components ○ Time tick granularity
- Can run in ‘real-time’ mode
SLIDE 32
Benefits of DES
- Accelerates development time and faster CI
SLIDE 33
Benefits of DES
- Accelerates development time and faster CI
- Realistic and deterministic simulation
SLIDE 34
Benefits of DES
- Accelerates development time and faster CI
- Realistic and deterministic simulation
- Simulate any date and time of the day
SLIDE 35
Multi-Threaded System
Orders Stock Motion
. . .
Backend
SLIDE 36
Time Leak - Event-Driven Component
- Not naturally tied to time
- SimPy supports event-driven processes
- Not suitable for multi-threaded systems
SLIDE 37
Time Leak - Event-Driven Component
- Not naturally tied to time
- SimPy supports event-driven processes
- Not suitable for multi-threaded systems
Solution: Inherit from Queue and create a SimPy process that joins on itself in each time tick
SLIDE 38
SLIDE 39
Implementation
- SimPy code runs in simulation only
- Can’t use the usual time-related functions.
Wrapping time-related functionality in our
- wn module
○ time.time() ○ time.sleep() ○ . . .
- Debugging - simulation timestamp in log
SLIDE 40
Distributed Simulation
SLIDE 41
Distributed Simulation
SLIDE 42
Distributed Simulation
create simpy process start local simpy do some work r e a d y
- nce all clients
are ready approve progress local simpy loop
SLIDE 43
Distributed Simulation
create simpy process start local simpy do some work r e a d y
- nce all clients
are ready approve progress local simpy loop sim time freezes
SLIDE 44
SLIDE 45
Summary
- Simulation is a powerful tool
- DES makes it more powerful
- is SimPle
- Time leak - synchronize all components time
- Easy to extend to a distributed simulation
SLIDE 46