Introduction Discrete-Event Simulation Using SimPy

82
Introduction to Discrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuang What is Simulation and When to Use it? What is Discrete-Event Simulation? Example to Illustrate World Views Introduction to SimPy SimPy Example References Introduction to Discrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuang DSP and Algorithm Design Department Metanoia Communications Inc. <[email protected]> June 9, 2012

description

This was slides for the talk I gave in PyCon Taiwan 2012.

Transcript of Introduction Discrete-Event Simulation Using SimPy

Page 1: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Introduction to Discrete-Event SimulationUsing SimPy

Chun-Chieh Huangjjhuang

DSP and Algorithm Design DepartmentMetanoia Communications Inc.<[email protected]>

June 9, 2012

Page 2: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Outline

1 What is Simulation and When to Use it?

2 What is Discrete-Event Simulation?

3 Example to Illustrate World Views

4 Introduction to SimPy

5 SimPy Example

Page 3: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Simulation and When to Use it?

Simulationa computer program that creates a virtual environmentin order to study physical problems

Page 4: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Simulation and When to Use it?

Simulationa computer program that creates a virtual environmentin order to study physical problems

When to use simulationhard to do real experiment,e.g. battle field, or banking system

Page 5: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Simulation and When to Use it?

Simulationa computer program that creates a virtual environmentin order to study physical problems

When to use simulationhard to do real experiment,e.g. battle field, or banking systemcheaper to do simulation,e.g. RTL simulation for IC design,or highway/freeway route planning

Page 6: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Simulation and When to Use it?

Simulationa computer program that creates a virtual environmentin order to study physical problems

When to use simulationhard to do real experiment,e.g. battle field, or banking systemcheaper to do simulation,e.g. RTL simulation for IC design,or highway/freeway route planninganalyzing bottleneck of current workflow

Page 7: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Simulation and When to Use it?

Simulationa computer program that creates a virtual environmentin order to study physical problems

When to use simulationhard to do real experiment,e.g. battle field, or banking systemcheaper to do simulation,e.g. RTL simulation for IC design,or highway/freeway route planninganalyzing bottleneck of current workflow

When not to use simulationmore expensive to do simulation,e.g. simple harmonic motion

Page 8: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Simulation and When to Use it?

Simulationa computer program that creates a virtual environmentin order to study physical problems

When to use simulationhard to do real experiment,e.g. battle field, or banking systemcheaper to do simulation,e.g. RTL simulation for IC design,or highway/freeway route planninganalyzing bottleneck of current workflow

When not to use simulationmore expensive to do simulation,e.g. simple harmonic motionproblems that can be analyzed by pencil and paper

Page 9: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Categories of Simulation

Continuous or discretestate variable is continuous, e.g. weather systemsstate variable is discrete, e.g. number of customers

Page 10: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Categories of Simulation

Continuous or discretestate variable is continuous, e.g. weather systemsstate variable is discrete, e.g. number of customers

Static or dynamicStatic: represents a system at a particular point of time

representation of time is unnecessary [1]sometimes called Monte-Carlo simulation [2]

Dynamic: represents systems as they change over time

e.g. banking system from 9:00 AM to 5:00 PM

Page 11: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Categories of Simulation

Continuous or discretestate variable is continuous, e.g. weather systemsstate variable is discrete, e.g. number of customers

Static or dynamicStatic: represents a system at a particular point of time

representation of time is unnecessary [1]sometimes called Monte-Carlo simulation [2]

Dynamic: represents systems as they change over time

e.g. banking system from 9:00 AM to 5:00 PM

Deterministic or stochasticDeterministic: contains no random variableStochastic: has one or more random variables

Page 12: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

What is Discrete-Event Simulation?

Discrete-Event Simulation isDiscreteDynamicStochastic

Simulation for queueing in a post office is DES

Mostly, but not limited to, queueing systemsfactory work flowfreeway traffic simulationnetwork traffic simulation

Page 13: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Page 14: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Event-orientedon each event, generate next event andput into event queue and sortsimulation time advances to next closest eventfaster than activity-oriented

Page 15: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Event-orientedon each event, generate next event andput into event queue and sortsimulation time advances to next closest eventfaster than activity-oriented

Process-orientedabstract one object into a processeasier to maintain in the end

Page 16: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Event-orientedon each event, generate next event andput into event queue and sortsimulation time advances to next closest eventfaster than activity-oriented

Process-orientedabstract one object into a processeasier to maintain in the endSimPy belongs here!

Page 17: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Example to Illustrate World Views

Simulating a post office with only one clerk

Customers come in at random timeand wait if the clerk is already serving

Clerk serves each customer for a random period of time

Page 18: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Event-orientedon each event, generate next event andput into event queue and sortsimulation time advances to next closest eventfaster than activity-oriented

Process-orientedabstract one object into a processeasier to maintain in the end

Page 19: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Simulation starts at 2.0!

Gen 1st arr at 2.6

Check events: 1st arr at 2.6, no action

Page 20: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 1st arr at 2.6, no action

Page 21: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 1st arr at 2.6, no action

Page 22: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 1st arr at 2.6, no action

Page 23: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 1st arr at 2.6, no action

Page 24: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 1st arr at 2.6, no action

Page 25: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 1st arr at 2.6, process now

Page 26: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

First arrival!

Page 27: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Calculate next arrival at 3.5

Calculate service time, end at 4.0

Page 28: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 29: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 30: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 31: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 32: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 33: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 34: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 35: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, no action

Page 36: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: 2nd arrival at 3.5, process now

Page 37: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Second arrival!

Page 38: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Calculate next arrival at ...

Page 39: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: service end at 4.0, no action

Page 40: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: service end at 4.0, no action

Page 41: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Activity-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Check events: service end at 4.0, no action...

Page 42: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Event-orientedon each event, generate next event andput into event queue and sortsimulation time advances to next closest eventfaster than activity-oriented

Process-orientedabstract one object into a processeasier to maintain in the end

Page 43: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Event-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

62.6: 1st arr

Simulation starts!

Page 44: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Event-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

62.6: 1st arr

First arrival!

Page 45: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Event-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

Calculate next arrival at 3.5

Calculate service time, end at 4.0

3.5: 2nd arr 4.0: 1st arr end

Page 46: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Event-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

63.5: 2nd arr 4.0: 1st arr end

Second arrival!

Page 47: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Event-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

64.0: 1st arr end

Page 48: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Event-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

64.0: 1st arr end

End service of 1st arrival.

Page 49: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Discrete-Event Simulation World Views

Activity-orientedfixed increment of timetime-consuming

Event-orientedon each event, generate next event andput into event queue and sortsimulation time advances to next closest eventfaster than activity-oriented

Process-orientedabstract one object into a process

Arrival process for customers, or AClerk process, or S

easier to maintain in the end

Page 50: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

Arrival is an infinite loop of the followingCalculate next arrival timeSleep until next arrivalAdd customer into queue

Clerk is an infinite loop of the followingSleep until waken up by customersServe the customer on waken upuntil there is no customer in line

Page 51: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A: Gen 1st arr at 2.6

S:

Simulation starts!

Page 52: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

First arrival!

A: Add customer to queue

S:

Page 53: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A: Calc next arr at 3.5

S:

Page 54: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A:

S: Serv, calc serv T at 4.0

Page 55: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A: Add customer to queue

S:

Second arrival!

Page 56: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A: Calc next arr at ...

S:

Calc next arrival at ...

Page 57: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A:

S: 1st arr end serv. Call next.

Page 58: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Process-Oriented Discrete-Event Simulation

0 1 2 3 4 5 6 7 8 90

1

2

3

4

5

6

A:

S: Serv 2nd arr. Calc end time at ...

Page 59: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Implementing Discrete-Event Simulation

Use your own C/C++ implementationtakes time to write simulation engine and algorithm codehard to debug when both are not verifiednot very convincing

Page 60: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Implementing Discrete-Event Simulation

Use your own C/C++ implementationtakes time to write simulation engine and algorithm codehard to debug when both are not verifiednot very convincing

Use generalized simulation library, or languageSIMULA programming languageC++SIM or JavaSIM [3]SimEvents in Simulink/MATLABSimPy [4]

Page 61: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Implementing Discrete-Event Simulation

Use your own C/C++ implementationtakes time to write simulation engine and algorithm codehard to debug when both are not verifiednot very convincing

Use generalized simulation library, or languageSIMULA programming languageC++SIM or JavaSIM [3]SimEvents in Simulink/MATLABSimPy [4]

Use special purpose simulation packagesns-3 for network simulation [5]

Page 62: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Introduction to SimPy

Uses Python for modelingPython is a scripting language like MATLAB, but faster!Python is free!Python is very easy to write and beautiful!

Page 63: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Introduction to SimPy

Uses Python for modelingPython is a scripting language like MATLAB, but faster!Python is free!Python is very easy to write and beautiful!

Process-oriented Discrete-Event Simulation Languageis easier to write modelprovides proven event manager implementationlets you focus on algorithm code

Page 64: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Introduction to SimPy

Uses Python for modelingPython is a scripting language like MATLAB, but faster!Python is free!Python is very easy to write and beautiful!

Process-oriented Discrete-Event Simulation Languageis easier to write modelprovides proven event manager implementationlets you focus on algorithm code

Uses coroutine to suspend/resume processguarantees order of executioncannot run on parallel machine for a single simulation

Page 65: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terminology: Classes

Processsimulates an entity which evolves in time, e.g.a clerk that serves customersrefered to as thread in [6]

Page 66: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terminology: Classes

Processsimulates an entity which evolves in time, e.g.a clerk that serves customersrefered to as thread in [6]

Resourcesimulates something to be requested andqueued if not available nowe.g. a wash machine

Page 67: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

Page 68: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

simulate() starts the simulation

Page 69: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

simulate() starts the simulation

yield hold put current thread into suspension for a certainamount of time

Page 70: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

simulate() starts the simulation

yield hold put current thread into suspension for a certainamount of time

yield passivate put current thread into suspension andwait until awakened by some other thread

Page 71: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

simulate() starts the simulation

yield hold put current thread into suspension for a certainamount of time

yield passivate put current thread into suspension andwait until awakened by some other thread

reactivate() awakes a previously-passivated thread

Page 72: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

simulate() starts the simulation

yield hold put current thread into suspension for a certainamount of time

yield passivate put current thread into suspension andwait until awakened by some other thread

reactivate() awakes a previously-passivated thread

cancel() cancels all the events associated with apreviously-passivated thread

Page 73: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Terimnology: Functions

activate() used to mark a thread as runnable when it is firstcreated

simulate() starts the simulation

yield hold put current thread into suspension for a certainamount of time

yield passivate put current thread into suspension andwait until awakened by some other thread

reactivate() awakes a previously-passivated thread

cancel() cancels all the events associated with apreviously-passivated thread

yield request requests for a given resource

yield release used to indicate that current thread no longer needthe given resource

Page 74: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

SimPy Example

ScenarioA post office with only one clerk.Customer arrival is a poisson process,i.e. inter-arrival time is exponential distribution.Service time is also a poisson process.Classical M/M/1 problem in queueing theory

ProcessArrivalClerk

Queue is managed by ourselvesSo that we can extend to M/M/m later, i.e. m clerks

Page 75: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Arrival Pseudo Code

Arrival is an infinite loop of the followingCalculate next arrival timeSleep until next arrivalAdd customer into queue

Page 76: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Arrival Process

1 class Ar r i va lC lass ( Process ) :2 " " " An a r r i v a l maintenance class " " "3 Ar r i va lRa te = 1 /1 .0 # r e c i p r o c a l o f mean i n t e r a r r i v a l t ime4 def __ in i t__ ( se l f , name ) :5 Process . __ in i t__ ( s e l f )6 s e l f .name = name78 def Run( s e l f ) :9 while True :

10 I n t e r A r r i v a l T i m e = G. Rnd . expovar ia te ( A r r i va lC lass . A r r i va lRa te )11 y i e l d hold , se l f , I n t e r A r r i v a l T i m e12 C = Customer ( )13 ClerkClass . Queue . append (C) # a customer a r r i v e s14 G. NumCustomers += 115 i f ClerkClass . I d l e != [ ] : # Is there any c l e r k i d l e ?16 r e a c t i v a t e ( ClerkClass . I d l e [ 0 ] ) # Yes , wake him / her up

Page 77: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Clerk Pseudo Code

Clerk is an infinite loop of the followingSleep until waken up by customersServe the customer on waken upuntil there is no customer in line

Page 78: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Clerk Process

1 class ClerkClass ( Process ) :2 " " " A server process , or c l e r k i n the post o f f i c e " " "3 ServiceRate = 1 /1 .2 # r e c i p r o c a l o f mean se rv i ce t ime4 MaxQueueLength = 05 Queue = [ ] # customer queue6 I d l e = [ ] # i d l e c l e r k l i s t7 Busy = [ ] # busy c l e r k l i s t8 NumDone = 0 # No . of customers being serviced9 def __ in i t__ ( s e l f ) :

10 Process . __ in i t__ ( s e l f )11 ClerkClass . I d l e . append ( s e l f ) # I n i t i a l l y i d l e12 def Run( s e l f ) :13 while True :14 y i e l d passivate , s e l f # wa i t u n t i l awaken by customers15 ClerkClass . I d l e . remove ( s e l f )16 ClerkClass . Busy . append( s e l f ) # going to be busy17 while ClerkClass . Queue != [ ] :18 i f len ( ClerkClass . Queue ) > ClerkClass . MaxQueueLength :19 ClerkClass . MaxQueueLength = len ( ClerkClass . Queue )20 C = ClerkClass . Queue . pop ( ) # c a l l next customer in l i n e21 # S t a r t se rv i ce the customer22 ServiceTime = G. Rnd . expovar ia te ( ClerkClass . ServiceRate )23 y i e l d hold , se l f , ServiceTime # s t a r t to serve24 C. endService ( ) # end of se rv i ce25 G. Tota lWai t ingTime += now ( ) − C. Ar r i va lT ime26 ClerkClass .NumDone += 127 del C # customer i s gone28 ClerkClass . Busy . remove ( s e l f )29 ClerkClass . I d l e . append( s e l f )

Page 79: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Simulation Main Loop

1 i f DEBUG:2 from SimPy . Simulat ionTrace import ∗

3 else :4 from SimPy . Simulat ion import ∗

56 def main ( ) :7 i n i t i a l i z e ( ) # I n i t i a l i z e s s imu la to r8 A = Ar r i va lC lass (name=" A r r i v a l " )9 a c t i v a t e (A ,A. Run ( ) ) # Act i va te a r r i v a l

1011 S = ClerkClass (name=" Clerk " )12 a c t i v a t e (S ,S. Run ( ) ) # Act i va te c l e r k1314 simulate ( u n t i l =MaxSimTime) # S t a r t to simulate !1516 i f __name__ == ’ __main__ ’ :17 main ( )

Page 80: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Concluding Remarks

Simulation is a powerful tool to study physical problems atcheaper cost.

SimPy provides process-oriendted DES frameworkto write simulation easily and reasonably fast.

And SimPy is free!

Page 81: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

References

[1] R. E. Nance, “A history of discrete event simulationprogramming languages,” in The second ACM SIGPLANconference on History of programming languages, ser.HOPL-II. New York, NY, USA: ACM, 1993, pp. 149–175.[Online]. Available: http://doi.acm.org/10.1145/154766.155368

[2] J. Banks, J. S. Carson, B. L. Nelson, and D. M. Nicol,Discrete-Event System Simulation (5th Edition). PrenticeHall, 2009.

[3] C++SIM and JavaSim, “http://javasim.codehaus.org/.”

[4] S. S. Package, “http://simpy.sourceforge.net/.”

[5] ns-3 Network Simulator, “http://www.nsnam.org/.”

[6] N. Matloff, “A discrete-event simulation course based on thesimpy language,” Davis, 2006.

Page 82: Introduction Discrete-Event Simulation Using SimPy

Introduction toDiscrete-Event

SimulationUsing SimPy

Chun-ChiehHuangjjhuang

What isSimulation andWhen to Use it?

What isDiscrete-EventSimulation?

Example toIllustrate WorldViews

Introduction toSimPy

SimPy Example

References

Q & A