Introduction Discrete-Event Simulation Using SimPy
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Introduction Discrete-Event Simulation Using SimPy

  • 526 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
526
On Slideshare
526
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
23
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang Introduction to Discrete-Event Simulation jjhuang Using SimPyWhat isSimulation andWhen to Use it?What isDiscrete-Event Chun-Chieh HuangSimulation? jjhuangExample toIllustrate WorldViews DSP and Algorithm Design DepartmentIntroduction to Metanoia Communications Inc.SimPy <jiunjie.huang@gmail.com>SimPy ExampleReferences June 9, 2012
  • 2. OutlineIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang 1 What is Simulation and When to Use it? jjhuangWhat isSimulation and 2 What is Discrete-Event Simulation?When to Use it?What isDiscrete-Event 3 Example to Illustrate World ViewsSimulation?Example toIllustrate WorldViews 4 Introduction to SimPyIntroduction toSimPySimPy Example 5 SimPy ExampleReferences
  • 3. What is Simulation and When to Use it?Introduction toDiscrete-Event Simulation Using SimPy Simulation Chun-Chieh a computer program that creates a virtual environment Huang jjhuang in order to study physical problemsWhat isSimulation andWhen to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 4. What is Simulation and When to Use it?Introduction toDiscrete-Event Simulation Using SimPy Simulation Chun-Chieh a computer program that creates a virtual environment Huang jjhuang in order to study physical problemsWhat is When to use simulationSimulation andWhen to Use it? hard to do real experiment, e.g. battle field, or banking systemWhat isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 5. What is Simulation and When to Use it?Introduction toDiscrete-Event Simulation Using SimPy Simulation Chun-Chieh a computer program that creates a virtual environment Huang jjhuang in order to study physical problemsWhat is When to use simulationSimulation andWhen to Use it? hard to do real experiment, e.g. battle field, or banking systemWhat isDiscrete-Event cheaper to do simulation,Simulation? e.g. RTL simulation for IC design,Example toIllustrate World or highway/freeway route planningViewsIntroduction toSimPySimPy ExampleReferences
  • 6. What is Simulation and When to Use it?Introduction toDiscrete-Event Simulation Using SimPy Simulation Chun-Chieh a computer program that creates a virtual environment Huang jjhuang in order to study physical problemsWhat is When to use simulationSimulation andWhen to Use it? hard to do real experiment, e.g. battle field, or banking systemWhat isDiscrete-Event cheaper to do simulation,Simulation? e.g. RTL simulation for IC design,Example toIllustrate World or highway/freeway route planningViews analyzing bottleneck of current workflowIntroduction toSimPySimPy ExampleReferences
  • 7. What is Simulation and When to Use it?Introduction toDiscrete-Event Simulation Using SimPy Simulation Chun-Chieh a computer program that creates a virtual environment Huang jjhuang in order to study physical problemsWhat is When to use simulationSimulation andWhen to Use it? hard to do real experiment, e.g. battle field, or banking systemWhat isDiscrete-Event cheaper to do simulation,Simulation? e.g. RTL simulation for IC design,Example toIllustrate World or highway/freeway route planningViews analyzing bottleneck of current workflowIntroduction toSimPy When not to use simulationSimPy Example more expensive to do simulation,References e.g. simple harmonic motion
  • 8. What is Simulation and When to Use it?Introduction toDiscrete-Event Simulation Using SimPy Simulation Chun-Chieh a computer program that creates a virtual environment Huang jjhuang in order to study physical problemsWhat is When to use simulationSimulation andWhen to Use it? hard to do real experiment, e.g. battle field, or banking systemWhat isDiscrete-Event cheaper to do simulation,Simulation? e.g. RTL simulation for IC design,Example toIllustrate World or highway/freeway route planningViews analyzing bottleneck of current workflowIntroduction toSimPy When not to use simulationSimPy Example more expensive to do simulation,References e.g. simple harmonic motion problems that can be analyzed by pencil and paper
  • 9. Categories of SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Continuous or discrete Huang jjhuang state variable is continuous, e.g. weather systems state variable is discrete, e.g. number of customersWhat isSimulation andWhen to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 10. Categories of SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Continuous or discrete Huang jjhuang state variable is continuous, e.g. weather systems state variable is discrete, e.g. number of customersWhat isSimulation and Static or dynamicWhen to Use it? Static: represents a system at a particular point of timeWhat isDiscrete-Event representation of time is unnecessary [1]Simulation? sometimes called Monte-Carlo simulation [2]Example to Dynamic: represents systems as they change over timeIllustrate WorldViews e.g. banking system from 9:00 AM to 5:00 PMIntroduction toSimPySimPy ExampleReferences
  • 11. Categories of SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Continuous or discrete Huang jjhuang state variable is continuous, e.g. weather systems state variable is discrete, e.g. number of customersWhat isSimulation and Static or dynamicWhen to Use it? Static: represents a system at a particular point of timeWhat isDiscrete-Event representation of time is unnecessary [1]Simulation? sometimes called Monte-Carlo simulation [2]Example to Dynamic: represents systems as they change over timeIllustrate WorldViews e.g. banking system from 9:00 AM to 5:00 PMIntroduction toSimPy Deterministic or stochasticSimPy Example Deterministic: contains no random variableReferences Stochastic: has one or more random variables
  • 12. What is Discrete-Event Simulation?Introduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuang Discrete-Event Simulation is DiscreteWhat isSimulation and DynamicWhen to Use it? StochasticWhat isDiscrete-Event Simulation for queueing in a post office is DESSimulation?Example to Mostly, but not limited to, queueing systemsIllustrate WorldViews factory work flowIntroduction to freeway traffic simulationSimPy network traffic simulationSimPy ExampleReferences
  • 13. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Activity-oriented Huang jjhuang fixed increment of time time-consumingWhat isSimulation andWhen to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 14. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Activity-oriented Huang jjhuang fixed increment of time time-consumingWhat isSimulation and Event-orientedWhen to Use it? on each event, generate next event andWhat isDiscrete-Event put into event queue and sortSimulation? simulation time advances to next closest eventExample to faster than activity-orientedIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 15. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Activity-oriented Huang jjhuang fixed increment of time time-consumingWhat isSimulation and Event-orientedWhen to Use it? on each event, generate next event andWhat isDiscrete-Event put into event queue and sortSimulation? simulation time advances to next closest eventExample to faster than activity-orientedIllustrate WorldViews Process-orientedIntroduction toSimPy abstract one object into a processSimPy Example easier to maintain in the endReferences
  • 16. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Activity-oriented Huang jjhuang fixed increment of time time-consumingWhat isSimulation and Event-orientedWhen to Use it? on each event, generate next event andWhat isDiscrete-Event put into event queue and sortSimulation? simulation time advances to next closest eventExample to faster than activity-orientedIllustrate WorldViews Process-orientedIntroduction toSimPy abstract one object into a processSimPy Example easier to maintain in the endReferences SimPy belongs here!
  • 17. Example to Illustrate World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuangWhat isSimulation and Simulating a post office with only one clerkWhen to Use it? Customers come in at random timeWhat isDiscrete-Event and wait if the clerk is already servingSimulation?Example to Clerk serves each customer for a random period of timeIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 18. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Activity-oriented Huang jjhuang fixed increment of timeWhat is time-consumingSimulation andWhen to Use it? Event-orientedWhat is on each event, generate next event andDiscrete-EventSimulation? put into event queue and sortExample to simulation time advances to next closest eventIllustrate World faster than activity-orientedViewsIntroduction to Process-orientedSimPy abstract one object into a processSimPy Example easier to maintain in the endReferences
  • 19. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? Simulation starts at 2.0!What isDiscrete-Event 3Simulation? Gen 1st arr at 2.6Example toIllustrate World 2Views Check events: 1st arr at 2.6, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 20. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 1st arr at 2.6, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 21. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 1st arr at 2.6, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 22. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 1st arr at 2.6, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 23. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 1st arr at 2.6, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 24. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 1st arr at 2.6, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 25. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 1st arr at 2.6, process nowIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 26. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? First arrival!What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 27. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang Calculate service time, end at 4.0What is 4Simulation andWhen to Use it? Calculate next arrival at 3.5What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 28. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 29. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 30. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 31. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 32. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 33. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 34. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 35. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 36. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: 2nd arrival at 3.5, process nowIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 37. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? Second arrival!What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 38. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? Calculate next arrival at ...What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 39. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: service end at 4.0, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 40. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: service end at 4.0, no actionIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 41. Activity-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2Views Check events: service end at 4.0, no action...Introduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 42. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Activity-oriented Huang jjhuang fixed increment of time time-consumingWhat isSimulation andWhen to Use it? Event-orientedWhat is on each event, generate next event andDiscrete-EventSimulation? put into event queue and sortExample to simulation time advances to next closest eventIllustrate World faster than activity-orientedViewsIntroduction to Process-orientedSimPy abstract one object into a processSimPy Example easier to maintain in the endReferences
  • 43. Event-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy 2.6: 1st arr Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? Simulation starts!What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 44. Event-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy 2.6: 1st arr Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? First arrival!What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 45. Event-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy 3.5: 2nd arr 4.0: 1st arr end Chun-Chieh Huang 5 jjhuang Calculate service time, end at 4.0What is 4Simulation andWhen to Use it? Calculate next arrival at 3.5What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 46. Event-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy 3.5: 2nd arr 4.0: 1st arr end Chun-Chieh Huang 5 jjhuangWhat is 4Simulation andWhen to Use it? Second arrival!What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 47. Event-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy 4.0: 1st arr end Chun-Chieh Huang 5 jjhuangWhat isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 48. Event-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy 4.0: 1st arr end Chun-Chieh Huang 5 jjhuang End service of 1st arrival.What isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation?Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 49. Discrete-Event Simulation World ViewsIntroduction toDiscrete-Event Simulation Using SimPy Activity-oriented Chun-Chieh Huang fixed increment of time jjhuang time-consumingWhat is Event-orientedSimulation andWhen to Use it? on each event, generate next event andWhat is put into event queue and sortDiscrete-EventSimulation? simulation time advances to next closest eventExample to faster than activity-orientedIllustrate WorldViews Process-orientedIntroduction to abstract one object into a processSimPy Arrival process for customers, or ASimPy Example Clerk process, or SReferences easier to maintain in the end
  • 50. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuang Arrival is an infinite loop of the followingWhat is Calculate next arrival timeSimulation andWhen to Use it? Sleep until next arrivalWhat is Add customer into queueDiscrete-EventSimulation? Clerk is an infinite loop of the followingExample to Sleep until waken up by customersIllustrate WorldViews Serve the customer on waken upIntroduction to until there is no customer in lineSimPySimPy ExampleReferences
  • 51. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A: Gen 1st arr at 2.6What is 4Simulation andWhen to Use it? Simulation starts!What isDiscrete-Event 3Simulation? S:Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 52. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A: Add customer to queueWhat is 4Simulation andWhen to Use it? First arrival!What isDiscrete-Event 3Simulation? S:Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 53. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A: Calc next arr at 3.5What isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation? S:Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 54. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A:What isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation? S: Serv, calc serv T at 4.0Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 55. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A: Add customer to queueWhat is 4Simulation andWhen to Use it? Second arrival!What isDiscrete-Event 3Simulation? S:Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 56. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A: Calc next arr at ...What is 4Simulation andWhen to Use it? Calc next arrival at ...What isDiscrete-Event 3Simulation? S:Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 57. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A:What isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation? S: 1st arr end serv. Call next.Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 58. Process-Oriented Discrete-Event SimulationIntroduction toDiscrete-Event Simulation 6 Using SimPy Chun-Chieh Huang 5 jjhuang A:What isSimulation and 4When to Use it?What isDiscrete-Event 3Simulation? S: Serv 2nd arr. Calc end time at ...Example toIllustrate World 2ViewsIntroduction toSimPy 1SimPy ExampleReferences 0 0 1 2 3 4 5 6 7 8 9
  • 59. Implementing Discrete-Event SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Use your own C/C++ implementation Huang jjhuang takes time to write simulation engine and algorithm code hard to debug when both are not verifiedWhat isSimulation and not very convincingWhen to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 60. Implementing Discrete-Event SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Use your own C/C++ implementation Huang jjhuang takes time to write simulation engine and algorithm code hard to debug when both are not verifiedWhat isSimulation and not very convincingWhen to Use it?What is Use generalized simulation library, or languageDiscrete-EventSimulation? SIMULA programming language C++SIM or JavaSIM [3]Example toIllustrate World SimEvents in Simulink/MATLABViews SimPy [4]Introduction toSimPySimPy ExampleReferences
  • 61. Implementing Discrete-Event SimulationIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Use your own C/C++ implementation Huang jjhuang takes time to write simulation engine and algorithm code hard to debug when both are not verifiedWhat isSimulation and not very convincingWhen to Use it?What is Use generalized simulation library, or languageDiscrete-EventSimulation? SIMULA programming language C++SIM or JavaSIM [3]Example toIllustrate World SimEvents in Simulink/MATLABViews SimPy [4]Introduction toSimPy Use special purpose simulation packagesSimPy Example ns-3 for network simulation [5]References
  • 62. Introduction to SimPyIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Uses Python for modeling Huang jjhuang Python is a scripting language like MATLAB, but faster! Python is free!What isSimulation and Python is very easy to write and beautiful!When to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 63. Introduction to SimPyIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Uses Python for modeling Huang jjhuang Python is a scripting language like MATLAB, but faster! Python is free!What isSimulation and Python is very easy to write and beautiful!When to Use it?What is Process-oriented Discrete-Event Simulation LanguageDiscrete-Event is easier to write modelSimulation? provides proven event manager implementationExample toIllustrate World lets you focus on algorithm codeViewsIntroduction toSimPySimPy ExampleReferences
  • 64. Introduction to SimPyIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Uses Python for modeling Huang jjhuang Python is a scripting language like MATLAB, but faster! Python is free!What isSimulation and Python is very easy to write and beautiful!When to Use it?What is Process-oriented Discrete-Event Simulation LanguageDiscrete-Event is easier to write modelSimulation? provides proven event manager implementationExample toIllustrate World lets you focus on algorithm codeViewsIntroduction to Uses coroutine to suspend/resume processSimPy guarantees order of executionSimPy Example cannot run on parallel machine for a single simulationReferences
  • 65. SimPy Terminology: ClassesIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuang ProcessWhat is simulates an entity which evolves in time, e.g.Simulation andWhen to Use it? a clerk that serves customersWhat is refered to as thread in [6]Discrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 66. SimPy Terminology: ClassesIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuang ProcessWhat is simulates an entity which evolves in time, e.g.Simulation andWhen to Use it? a clerk that serves customersWhat is refered to as thread in [6]Discrete-EventSimulation? ResourceExample to simulates something to be requested andIllustrate WorldViews queued if not available nowIntroduction to e.g. a wash machineSimPySimPy ExampleReferences
  • 67. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuangWhat isSimulation andWhen to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 68. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuang simulate() starts the simulationWhat isSimulation andWhen to Use it?What isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 69. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuang simulate() starts the simulationWhat is yield hold put current thread into suspension for a certainSimulation andWhen to Use it? amount of timeWhat isDiscrete-EventSimulation?Example toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 70. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuang simulate() starts the simulationWhat is yield hold put current thread into suspension for a certainSimulation andWhen to Use it? amount of timeWhat is yield passivate put current thread into suspension andDiscrete-EventSimulation? wait until awakened by some other threadExample toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 71. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuang simulate() starts the simulationWhat is yield hold put current thread into suspension for a certainSimulation andWhen to Use it? amount of timeWhat is yield passivate put current thread into suspension andDiscrete-EventSimulation? wait until awakened by some other threadExample toIllustrate World reactivate() awakes a previously-passivated threadViewsIntroduction toSimPySimPy ExampleReferences
  • 72. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuang simulate() starts the simulationWhat is yield hold put current thread into suspension for a certainSimulation andWhen to Use it? amount of timeWhat is yield passivate put current thread into suspension andDiscrete-EventSimulation? wait until awakened by some other threadExample toIllustrate World reactivate() awakes a previously-passivated threadViews cancel() cancels all the events associated with aIntroduction toSimPy previously-passivated threadSimPy ExampleReferences
  • 73. SimPy Terimnology: FunctionsIntroduction toDiscrete-Event Simulation activate() used to mark a thread as runnable when it is first Using SimPy created Chun-Chieh Huang jjhuang simulate() starts the simulationWhat is yield hold put current thread into suspension for a certainSimulation andWhen to Use it? amount of timeWhat is yield passivate put current thread into suspension andDiscrete-EventSimulation? wait until awakened by some other threadExample toIllustrate World reactivate() awakes a previously-passivated threadViews cancel() cancels all the events associated with aIntroduction toSimPy previously-passivated threadSimPy Example yield request requests for a given resourceReferences yield release used to indicate that current thread no longer need the given resource
  • 74. SimPy ExampleIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Scenario Huang jjhuang A post office with only one clerk. Customer arrival is a poisson process,What isSimulation and i.e. inter-arrival time is exponential distribution.When to Use it? Service time is also a poisson process.What isDiscrete-Event Classical M /M /1 problem in queueing theorySimulation? ProcessExample toIllustrate World ArrivalViews ClerkIntroduction toSimPy Queue is managed by ourselvesSimPy Example So that we can extend to M /M /m later, i.e. m clerksReferences
  • 75. Arrival Pseudo CodeIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuangWhat isSimulation and Arrival is an infinite loop of the followingWhen to Use it? Calculate next arrival timeWhat isDiscrete-Event Sleep until next arrivalSimulation? Add customer into queueExample toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 76. Arrival ProcessIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang 1 class A r r i v a l C l a s s ( Process ) : jjhuang 2 " " " An a r r i v a l maintenance c l a s s " " " 3 ArrivalRate = 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 i m eWhat is 4 def _ _ i n i t _ _ ( s e l f , name ) :Simulation and 5 Process . _ _ i n i t _ _ ( s e l f )When to Use it? 6 s e l f . name = name 7What is 8 def Run ( s e l f ) :Discrete-Event 9 while True :Simulation? 10 I n t e r A r r i v a l T i m e = G. Rnd . e x p o v a r i a t e ( A r r i v a l C l a s s . A r r i v a l R a t e ) 11 y i e l d hold , s e l f , I n t e r A r r i v a l T i m eExample to 12 C = Customer ( )Illustrate World 13 C l e r k C l a s s . Queue . append (C) # a customer a r r i v e sViews 14 G. NumCustomers += 1Introduction to 15 i f ClerkClass . I d l e != [ ] : # I s t h e r e any c l e r k i d l e ?SimPy 16 r e a c t i v a t e ( C l e r k C l a s s . I d l e [ 0 ] ) # Yes , wake him / her upSimPy ExampleReferences
  • 77. Clerk Pseudo CodeIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuangWhat isSimulation and Clerk is an infinite loop of the followingWhen to Use it? Sleep until waken up by customersWhat isDiscrete-Event Serve the customer on waken upSimulation? until there is no customer in lineExample toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences
  • 78. Clerk ProcessIntroduction toDiscrete-Event 1 class C l e r k C l a s s ( Process ) : Simulation 2 " " " A s e r v e r process , o r c l e r k i n t h e p o s t o f f i c e " " " Using SimPy 3 ServiceRate = 1 / 1 . 2 # r e c i p r o c a l o f mean s e r v i c e t i m e 4 MaxQueueLength = 0 Chun-Chieh 5 Queue = [ ] # customer queue Huang 6 Idle = [ ] # idle clerk l i s t jjhuang 7 Busy = [ ] # busy c l e r k l i s t 8 NumDone = 0 # No . o f customers being s e r v i c e dWhat is 9 def _ _ i n i t _ _ ( s e l f ) :Simulation and 10 Process . _ _ i n i t _ _ ( s e l f )When to Use it? 11 C l e r k C l a s s . I d l e . append ( s e l f ) # I n i t i a l l y idle 12 def Run ( s e l f ) :What is 13 while True :Discrete-Event 14 y i e l d passivate , s e l f # w a i t u n t i l awaken by customersSimulation? 15 C l e r k C l a s s . I d l e . remove ( s e l f ) 16 C l e r k C l a s s . Busy . append ( s e l f ) # going t o be busyExample to 17 while C l e r k C l a s s . Queue ! = [ ] :Illustrate World 18 i f l e n ( C l e r k C l a s s . Queue ) > C l e r k C l a s s . MaxQueueLength :Views 19 C l e r k C l a s s . MaxQueueLength = l e n ( C l e r k C l a s s . Queue ) 20 C = C l e r k C l a s s . Queue . pop ( ) # c a l l n e x t customer i n l i n eIntroduction to 21 # S t a r t s e r v i c e t h e customerSimPy 22 ServiceTime = G. Rnd . e x p o v a r i a t e ( C l e r k C l a s s . ServiceRate )SimPy Example 23 y i e l d hold , s e l f , ServiceTime # s t a r t t o serve 24 C . endService ( ) # end o f s e r v i c eReferences 25 G. T o t a l W a i t i n g T i m e += now ( ) − C . A r r i v a l T i m e 26 C l e r k C l a s s . NumDone += 1 27 del C # customer i s gone 28 C l e r k C l a s s . Busy . remove ( s e l f ) 29 C l e r k C l a s s . I d l e . append ( s e l f )
  • 79. Simulation Main LoopIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang 1 i f DEBUG: jjhuang 2 from SimPy . S i m u l a t i o n T r a c e import ∗ 3 else :What is 4 from SimPy . S i m u l a t i o n import ∗Simulation and 5When to Use it? 6 def main ( ) : 7 initialize () # I n i t i a l i z e s simulatorWhat is 8 A = A r r i v a l C l a s s ( name= " A r r i v a l " )Discrete-Event 9 a c t i v a t e ( A , A . Run ( ) ) # Activate a r r i v a lSimulation? 10 11 S = C l e r k C l a s s ( name= " C l e r k " )Example to 12 a c t i v a t e ( S , S . Run ( ) ) # Activate clerkIllustrate World 13Views 14 s i m u l a t e ( u n t i l =MaxSimTime ) # S t a r t to simulate ! 15Introduction to 16 i f __name__ == ’ __main__ ’ :SimPy 17 main ( )SimPy ExampleReferences
  • 80. Concluding RemarksIntroduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuangWhat is Simulation is a powerful tool to study physical problems atSimulation andWhen to Use it? cheaper cost.What isDiscrete-Event SimPy provides process-oriendted DES frameworkSimulation? to write simulation easily and reasonably fast.Example toIllustrate World And SimPy is free!ViewsIntroduction toSimPySimPy ExampleReferences
  • 81. ReferencesIntroduction toDiscrete-Event Simulation Using SimPy [1] R. E. Nance, “A history of discrete event simulation Chun-Chieh programming languages,” in The second ACM SIGPLAN Huang jjhuang conference on History of programming languages, ser. HOPL-II. New York, NY, USA: ACM, 1993, pp. 149–175.What isSimulation and [Online]. Available: http://doi.acm.org/10.1145/154766.155368When to Use it?What is [2] J. Banks, J. S. Carson, B. L. Nelson, and D. M. Nicol,Discrete-EventSimulation? Discrete-Event System Simulation (5th Edition). PrenticeExample to Hall, 2009.Illustrate WorldViews [3] C++SIM and JavaSim, “http://javasim.codehaus.org/.”Introduction toSimPy [4] S. S. Package, “http://simpy.sourceforge.net/.”SimPy Example [5] ns-3 Network Simulator, “http://www.nsnam.org/.”References [6] N. Matloff, “A discrete-event simulation course based on the simpy language,” Davis, 2006.
  • 82. Introduction toDiscrete-Event Simulation Using SimPy Chun-Chieh Huang jjhuangWhat isSimulation andWhen to Use it?What isDiscrete-EventSimulation? Q&AExample toIllustrate WorldViewsIntroduction toSimPySimPy ExampleReferences