Date post: | 19-May-2015 |
Category: |
Technology |
Upload: | albert-huang |
View: | 418 times |
Download: | 4 times |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
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
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
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
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
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
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
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
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
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
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!
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
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
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
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
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
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
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
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
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
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
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!
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 ...
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
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
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...
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
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!
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!
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
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!
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
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.
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
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
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!
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:
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:
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
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!
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 ...
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.
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 ...
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
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]
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]
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!
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
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
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]
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
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
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
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
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
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
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
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
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
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
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
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
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 )
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 ( )
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!
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.
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