Example: marketing

Simulation Programming with Python - Northwestern …

Chapter 4 Simulation Programmingwith PythonThis chapter shows how simulations of some of the examples in Chap. 3 can beprogrammed using Python and the SimPy Simulation library[1]. The goals ofthe chapter are to introduce SimPy, and to hint at the experiment design andanalysis issues that will be covered in later chapters. While this chapter willgenerally follow the flow of Chap. 3, it will use the conventions and patternsenabled by the SimPy SimPy OverviewSimPy is an object-oriented, process-based discrete-event Simulation library forPython. It is open source and released under the M license. SimPy providesthe modeler with components of a Simulation model including processes, foractive components like customers, messages, and vehicles, and resources, forpassive components that form limited capacity congestion points like servers,checkout counters, and tunnels.

Simulation Programming with Python This chapter shows how simulations of some of the examples in Chap. 3 can be programmed using Python and the SimPy simulation library[1]. The goals of ... tasks such as statistics and plotting it is intended to be used along with other

Tags:

  Programming, Python, With, Simulation, Tasks, Simulation programming with python

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Simulation Programming with Python - Northwestern …

1 Chapter 4 Simulation Programmingwith PythonThis chapter shows how simulations of some of the examples in Chap. 3 can beprogrammed using Python and the SimPy Simulation library[1]. The goals ofthe chapter are to introduce SimPy, and to hint at the experiment design andanalysis issues that will be covered in later chapters. While this chapter willgenerally follow the flow of Chap. 3, it will use the conventions and patternsenabled by the SimPy SimPy OverviewSimPy is an object-oriented, process-based discrete-event Simulation library forPython. It is open source and released under the M license. SimPy providesthe modeler with components of a Simulation model including processes, foractive components like customers, messages, and vehicles, and resources, forpassive components that form limited capacity congestion points like servers,checkout counters, and tunnels.

2 It also provides monitor variables to aid ingathering statistics. Random variates are provided by the standard Pythonrandom module. Since SimPy itself is written in pure Python , it can also runon the Java Virtual Machine (Jython) and the .NET environment (IronPython).As of SimPy version , it works on all implementations of Python version up. Other documentation can be found at the SimPy website1and otherSimPy tutorials[2].SimPy and the Python language are each currently in flux, with users ofPython in the midst of a long transition from the Python series to while SimPy is expected to transition to version 3 which will involve changesin the library interface. Scientific and technical computing users such as mostsimulation modelers and analysts are generally staying with the Python se-1 4. Simulation Programming with Python ries as necessary software libraries are being ported and tested.

3 SimPy itselfsupports the Python series as of version In addition, SimPy is undergo-ing a major overhaul from SimPy to version This chapter and the codeon the website will assume use of Python and SimPy In the future,the book website will also include versions of the code based on SimPy as they and their supporting libraries are comes with data collection capabilities. But for other data analysistasks such as statistics and plotting it is intended to be used along with otherlibraries that make up the Python scientific computing ecosystem centered onNumpy and Scipy[3]. As such, it can easily be used with other Python packagesfor plotting (Matplotlib), GUI (WxPython, TKInter, PyQT, etc.), statistics( , statsmodels), and databases. This chapter will assume that youhave Numpy, Scipy3, Matplotlib4, and Statsmodels5installed. In addition thenetwork/graph library Networkx will be used in a network model, but it canbe skipped with no loss of continuity.

4 The easiest way to install Python alongwith its scientific libraries (including SimPy) is to install a scientifically orienteddistribution, such as Enthought Canopy6for Windows, Mac OS X, or Linux;or Python (x,y)7for Windows or Linux. If you are installing using a standardPython distribution, you can install SimPy by usingeasyinstallorpip. Notethe capitalization of SimPy install SimPyorpip install SimPyThe other required libraries can be installed in a similar manner. See thespecific library webpages for more chapter will assume that you have the Numpy, Scipy, Matplotlib, andStatsmodels libraries Random numbersThere are two groups of random-variate generations functions generally used,randomfrom the Python Standard Library and the random variate generatorsin A third source of random variate generators arethose included in PyGSL, the Python interface to the GNU Scientific Library( )Therandommodule of the Python Standard Library is based on the MersenneTwister as the core generator.

5 This generator is extensively tested and produces2 SIMPY OVERVIEW353-bit precision floats and a period of 219937 1. Some distributions included inthe therandommodule include uniform, triangular, Beta, Exponential, Gamma,Gaussian, Normal, Lognormal, and Weibull basic use of random variate generators in therandommodule is asfollows:1. Load therandommodule:import random2. Instantiate a generator:g = ()3. Set the (1234)4. Draw a random variate: A random value from 0 to 1 () A random value (float) from a to (a,b) A random integer from a to b (inclusive) (a, b) A random sample ofkitems from (population,k)TheScipymodule includes a larger list of random variate generators includ-ing over 80 continuous and 10 discrete random variable distributions. For eachdistribution, a number of functions are available: rvs: Random Variates generator, pdf: Probability Density Function, cdf: Cumulative Distribution Function, sf: Survival Function (1-CDF), ppf: Percent Point Function (Inverse of CDF), isf: Inverse Survival Function (Inverse of SF), stats: Return mean, variance, (Fisher s) skew, or (Fisher s) kurtosis, moment: non-central moments of the over 80 distributions are included, the parameters of the distributions arein a standardized form, with the parameters being the location, scale and shapeof the distribution.

6 The module documentation and a probability reference maybe consulted to relate the parameters to those that you may be more basic use of Scipy random number generators is as Load the Numpy and Scipy modules4 CHAPTER 4. Simulation Programming with Python import numpy as npimport scipy as sp2. Set the random number seed. Scipy uses the Numpy random number gen-erators so the Numpy seed function should be (1234)3. Instantiate the generator8. Some examples: Normal with mean 10 and standard deviation 4:norm1 = (loc = 10, scale = 4) Uniform from 0 to 10:unif1 = (loc = 0, scale = 10) Exponential with mean 1:expo1 = (scale = )4. Generate a random ().As you already know, the pseudorandom numbers we use to generate randomvariates in simulations are essentially a long list. Random-number streams arejust different starting places in this list, spaced far apart. The need for streamsis discussed in Chap.

7 7,but it is important to know that any stream is as goodas any other stream in a well-tested generator. In Python , the random numberstream used is set using the seed functions ( () ). SymPy componentsSimPy is built upon a special type of Python function calledgenerators[?].When a generator is called, the body of the function does not execute, rather,it returns an iterator object that wraps the body, local variables, and currentpoint of execution (initially the start of the function). This iterator then runsthe function body up to theyieldstatement, then returns the result of thefollowing SimPy, theyieldstatements are used to define the event is used for a process to either do something to itself ( the next cararrives); to request a resource, such as requesting a server; to release a resource,such as a server that is no longer needed; or to wait for another event.

8 [2]. yieldhold: used to indicate the passage of a certain amount of timewithin a process; yieldrequest: used to cause a process to join a queue for a given re-source (and start using it immediately if no other jobs are waiting for theresource);8this method is known in the documentation asfreezing a SIMPY OVERVIEW5 yieldrelease: used to indicate that the process is done using the givenresource, thus enabling the next thread in the queue, if any, to use theresource; yieldpassivate: used to have a process wait until awakened by someother based on a sequence of theseyieldgenerators along withsimulation a SimPy Simulation , the Simulation is initialized, then resources are de-fined. Next, processes are defined to generate entities, which in turn can gen-erate their own processes over the course of the Simulation . The processes arethen activated so that they generate other events and processes.

9 Monitors collectstatistics on entities, resources, or any other event which occurs in the SimPy, resources such as parking spaces are represented are three types of resources. AResourceis something whose units are required by aProcess. WhenaProcessis done with a unit of theResourceit releases the unit foranotherProcessto use. AProcessthat requires a unit of Resource whenall units are busy with other Processes can join a queue and wait for thenext unit to be available. ALevelis homogeneous undifferentiated material that can be producedor consumed by aProcess. An example of a level is gasoline in a tank ata gas station. AStoremodels the production or consumption of specific items of anytype. An example would be a storeroom that holds a variety of surgicalsupplies,The Simulation must also collect data for use in later calculating statisticson the performance of the system.

10 In SimPy, this is done through creating data within a Simulation is done through aMonitoror theMonitoris the more general version, this chapter will use that. Youcan go to the SimPy website for more information. TheMonitormakes ob-servations and records the value and the time of the observation, allowing forstatistics to be saved for analysis after the end of the Simulation . TheMon-itorcan also be included in the definition of anyResource(to be discussedin the main Simulation program) so statistics on queues and other resourcescan be collected as well. In theCarobject; theMonitorparkingtracks thevalue of the every point where the value , as cars enter and leave the parking the main part of the Simulation program, the Simulation object is defined,then resources, processes, and monitors are defined and activated as needed tostart the Simulation . In addition, the following function calls can be made:6 CHAPTER 4.


Related search queries