boosting simulation performance with python

46
Boosting Simulation Performance with Python Eran Friedman

Upload: others

Post on 05-May-2022

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Boosting Simulation Performance with Python

Boosting Simulation Performance with Python

Eran Friedman

Page 2: Boosting Simulation Performance with Python

How to use Discrete-Event Simulation

to run your system faster than

real-time?

Page 4: Boosting Simulation Performance with Python

About Me● Eran Friedman

● Team lead @ Fabric

● Nowadays developing

the Ground Robot

Page 5: Boosting Simulation Performance with Python

Outline● Simulations - why?

● How to use DES?

● How to use SimPy?

● What are the challenges?

● How to distribute?

Page 6: Boosting Simulation Performance with Python

Simulation

“An approximate imitation of the

operation of a process or system ...”

- Wikipedia

Page 7: Boosting Simulation Performance with Python

Simulation

Orders Stock Motion . . .

Backend

Page 8: Boosting Simulation Performance with Python

Simulation

Orders Stock Motion . . .

Backend

Page 9: Boosting Simulation Performance with Python

Importance of SimulationsCOVID-19

Page 10: Boosting Simulation Performance with Python

Importance of SimulationsAutomated regression tests

Page 11: Boosting Simulation Performance with Python

Importance of SimulationsAnalyze performance & compare algorithms

Page 12: Boosting Simulation Performance with Python

Importance of SimulationsRun in the cloud

Page 13: Boosting Simulation Performance with Python

Importance of SimulationsVerify warehouse layout

Page 14: Boosting Simulation Performance with Python

Importance of SimulationsInject failures & improve robustness

Page 15: Boosting Simulation Performance with Python

Importance of SimulationsSimulate a large facility

Page 16: Boosting Simulation Performance with Python

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

Page 17: Boosting Simulation Performance with Python

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

Page 18: Boosting Simulation Performance with Python

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

Page 19: Boosting Simulation Performance with Python

SimPy Library● Discrete-event simulation (DES) framework

● Created in 2002

● MIT license

● Pure Python

● No dependencies

Page 20: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

Page 21: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r1

t=0

r0

t=0

Page 22: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r0

t=0

r1

t=0

Executing -

Page 23: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r0

t=0

r1

t=0

Executing -

r0

t=0.1

Page 24: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r1

t=0

Executing -

r0

t=0.1

Page 25: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r0

t=0.1

Executing -

r1

t=0

Page 26: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r0

t=0.1

Executing -

r1

t=0

r1

t=0.1

Page 27: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0

Processes:

r0 r1

Event queue

r0

t=0.1

Executing -

r1

t=0.1

Page 28: Boosting Simulation Performance with Python

SimPy Overview

Environment

t = 0.1

Processes:

r0 r1

Event queue

r1

t=0.1

Executing -

r0

t=0.1

Page 29: Boosting Simulation Performance with Python

SimPy Example - Robot Race● A robot’s speed is about 2-4 meters/second

Page 30: Boosting Simulation Performance with Python
Page 31: Boosting Simulation Performance with Python

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

Page 32: Boosting Simulation Performance with Python

Benefits of DES● Accelerates development time and faster CI

Page 33: Boosting Simulation Performance with Python

Benefits of DES● Accelerates development time and faster CI

● Realistic and deterministic simulation

Page 34: Boosting Simulation Performance with Python

Benefits of DES● Accelerates development time and faster CI

● Realistic and deterministic simulation

● Simulate any date and time of the day

Page 35: Boosting Simulation Performance with Python

Multi-Threaded System

Orders Stock Motion . . .

Backend

Page 36: Boosting Simulation Performance with Python

Time Leak - Event-Driven Component● Not naturally tied to time

● SimPy supports event-driven processes

● Not suitable for multi-threaded systems

Page 37: Boosting Simulation Performance with Python

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

Page 38: Boosting Simulation Performance with Python
Page 39: Boosting Simulation Performance with Python

Implementation● SimPy code runs in simulation only

● Can’t use the usual time-related functions.

Wrapping time-related functionality in our

own module

○ time.time()

○ time.sleep()

○ . . .

● Debugging - simulation timestamp in log

Page 40: Boosting Simulation Performance with Python

Distributed Simulation

Page 41: Boosting Simulation Performance with Python

Distributed Simulation

Page 42: Boosting Simulation Performance with Python

Distributed Simulation

create simpy process

start local simpy

do some work

ready

once all clients

are ready

approve

progress local simpy

loop

Page 43: Boosting Simulation Performance with Python

Distributed Simulation

create simpy process

start local simpy

do some work

ready

once all clients

are ready

approve

progress local simpy

loop

sim time freezes

Page 44: Boosting Simulation Performance with Python
Page 45: Boosting Simulation Performance with Python

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

Page 46: Boosting Simulation Performance with Python

Thank You!