Simpy: Simulation in Python: Roberto Solar Gallardo
Simpy: Simulation in Python: Roberto Solar Gallardo
Classes
Examples
References
December 6, 2012
Introduction
Classes
Examples
References
What is SimPy? object-oriented process-based discrete-event simulation language based on standard Python Features use of pythons generators
declare a function that behaves like an iterator a generator is resumable and can branch control elsewhere semi-coroutines
coroutines - functions that alternate execution with each other the exit/re-entry points are marked by pythons yield keywords
Introduction
Classes
Examples
References
Simpy programming
The scheduler a list of future events is maintained automatically the scheduler is started by the SimPy command simulate Elements of discrete-event models Active elements: entities (Process) Passive elements: resources (Resource, Level and Store) General Structure of a SimPy program dene one or more SimPy classes and objects call the function initialize create and activate at least one of the active entities start the simulation by calling simulate report results
Introduction
Classes
Examples
References
How it works
Simulation. t - Simpys clock Simulation. timestamps (heap) - event list (t,priority,instance)
1 2 3 4 5 6 7 8 9 10
class Cliente(Process): def visitar(self,tiempoDeServicio): print now(),self.name, ": He llegado" yield hold,self,tiempoDeServicio print now(),self.name, ": Ya me voy" initialize() c=Cliente(name="Roberto") activate(c,c.visitar(tiempoDeServicio=1.0),at=1.0) simulate(until=10.0)
Analysis
line 7: t=0 and timestamps=[] line 9: put c into timestamps (1.0,indened,c) line 4: put self into timestamps (now()+timehold,indened,self)
Introduction
Classes
Examples
References
Processes
Features model an entity with activities i.e. instances of some class that inherits from SimPys Process class each entity has a standard method - Process execution method Process execution method (PEM) species entities actions in detail each PEM runs in parallel with (and may interact with) the PEMs of other entities
Introduction
Classes
Examples
References
Resource facilities
simulates something to be queued for Resources model a service point with one or more identical service units Levels model a container of homogenous material which can be discrete (boxes, bricks, etc) or continuous (uid) Stores model a list of individual objects
Introduction
Classes
Examples
References
Monitors
collects simulation data for analysis, statistics, etc Monitors used to compile summary statistics such as waiting times and queue lengths simple averages and variances time-weighted averages histograms Tallys is an alternative to Monitor that does essentially the same job but uses less storage space at some cost of speed and exibility
Introduction
Classes
Examples
References
Introduction
Classes
Examples
References
Scheduling statements yield hold,self,T - reactivate self after T yield passivate,self - make self passive yield request,self,resource - request use of resource yield release,self,resource - give up use of resource yield put,self,level,N - level is increased N units yield get,self,level,N - level is decreased N units
Introduction
Classes
Examples
References
Cliente simple
from SimPy.Simulation import * class Cliente(Process): def visitar(self,tiempoDeServicio): print now(),self.name, ": He llegado" yield hold,self,tiempoDeServicio print now(),self.name, ": Ya me voy"
Output
1.0 Roberto : He llegado 2.0 Roberto : Ya me voy
Introduction
Classes
Examples
References
Output
7.92130825474 Roberto : He llegado class Cliente(Process): 8.92130825474 Roberto : Ya me voy def visitar(self,tiempoDeServicio): print now(),self.name,": He llegado" yield hold,self,tiempoDeServicio print now(),self.name,": Ya me voy" seed() initialize() c=Cliente(name="Roberto"); inicio=gammavariate(4,1); activate(c,c.visitar(tiempoDeServicio=1.0),at=inicio) simulate(until=10.0);
Introduction
Classes
Examples
References
Varios clientes
from SimPy.Simulation import * class Cliente(Process): def visitar(self,tiempoDeServicio): print now(),self.name, ": He llegado" yield hold,self,tiempoDeServicio print now(),self.name, ": Ya me voy"
Output
1.0 3.0 4.0 5.0 5.4 6.5 Roberto : He llegado Alonso : He llegado Roberto : Ya me voy Veronica : He llegado Alonso : Ya me voy Veronica : Ya me voy
Introduction
Classes
Examples
References
Muchos clientes
from SimPy.Simulation import * class Generador(Process): def generar(self,totalDeClientes,tasaDeLlegada): for i in range(totalDeClientes): c=Cliente(name="Cli-%02d"%(i)) activate(c,c.visitar(tiempoDeServicio=3.5)) yield hold,self,tasaDeLlegada
Output
Cli-00: Cli-01: Cli-00: Cli-02: Cli-01: Cli-03: Cli-02: Cli-04: Cli-03: Cli-04: He He Ya He Ya He Ya He Ya Ya llegado llegado me voy llegado me voy llegado me voy llegado me voy me voy
0.0 2.5 3.5 5.0 6.0 7.5 8.5 class Cliente(Process): 10.0 def visitar(self,tiempoDeServicio): 11.0 print "%2.1f %s: He llegado"%(now(),self.name) 13.5 yield hold,self,tiempoDeServicio print "%2.1f %s: Ya me voy"%(now(),self.name)
Introduction
Classes
Examples
References
Output
0.0 0.0 2.3 2.7 2.7 3.1 5.4 5.4 8.1 Cli-00: Cli-00: Cli-01: Cli-00: Cli-01: Cli-02: Cli-01: Cli-02: Cli-02: He T. He Ya T. He Ya T. Ya llegado Espera 0.0 llegado me voy Espera 0.4 llegado me voy Espera 2.3 me voy
class Cliente(Process): def visitar(self,tiempoDeServicio=0,recurso=None): llegada=now() print "%2.1f %s: He llegado"%(now(),self.name) yield request,self,recurso tiempoDeEspera=now()-llegada; print "%2.1f %s: Tiempo de espera %2.1f"%(now(),self.name,tiempoDeEspera) yield hold,self,tiempoDeServicio yield release,self,recurso print "%2.1f %s: Ya me voy"%(now(),self.name)
Introduction
Classes
Examples
References
El banco
from SimPy.Simulation import * from random import expovariate, seed MEDIA_TIEMPO_DE_SERVICIO=5.0 MEDIA_TIEMPO_DE_LLEGADA=2.5 class Generador(Process): def generar(self,totalDeClientes,recurso): for i in range(totalDeClientes): c=Cliente(name="Cliente-"+str(i)); activate(c,c.visitar(recurso=recurso)); tiempoDeLlegada=expovariate(1.0/MEDIA_TIEMPO_DE_LLEGADA); yield hold,self,tiempoDeLlegada class Cliente(Process): def visitar(self,recurso): llegada=now() yield request,self,recurso tiempoDeEspera=now()-llegada monitor.observe(tiempoDeEspera); tiempoDeServicio=expovariate(1.0/MEDIA_TIEMPO_DE_SERVICIO) yield hold,self,tiempoDeServicio yield release,self,recurso seed() initialize() monitor=Monitor(); banco=Resource(name="Banco",unitName="Cajero",capacity=2) generador=Generador(name="Generador") activate(generador,generador.generar(totalDeClientes=1000,recurso=banco)) simulate(until=2000) resultado=monitor.count(),monitor.mean() print "El tiempo promedio de espera de %d atenciones fue %f"%resultado
Introduction
Classes
Examples
References
SimPlot
Introduction
Classes
Examples
References
1 2
Introduction
Classes
Examples
References
THE END