640 likes | 1.28k Views
Advanced Systems Simulation Capabilities in SimPy. Klaus G. Müller e-mail: kgmuller@xs4all.nl. Scope of presentation. SimPy ( Sim ulation in Py thon) in a nutshell Advanced capabilities of SimPy Tracing of simulation events – SimulationTrace module Data collection – class Monitor
E N D
Advanced Systems Simulation Capabilities in SimPy Klaus G. Müller e-mail: kgmuller@xs4all.nl
Scope of presentation • SimPy (Simulation in Python) in a nutshell • Advanced capabilities of SimPy • Tracing of simulation events – SimulationTrace module • Data collection – class Monitor • Plotting of results – SimPlot module • Simulation GUI – SimGUI module • Real time synchronization – SimulationRT module • Extensibility -- adding synchronization “verbs” • What users aredoing with SimPy
What Is SimPy? • A Discrete Event Simulation (DES) package • Process based • Object oriented • Modeled after Simula • Written in Python • The only DES package in Python • Open Source (GPL) • Homepage (download, documentation, tutorials): SimPy.SourceForge.Net
Semi-Coroutines Simulate Parallel Processes in SimPy initialize() activate(car,car.drive()) simulate(until=endtime) Generator attributes in Process instances . . . def drive(self): speed=50 yield hold,self,25 def fly(self): speed=800 yield hold,self,25 speed=200 yield hold,self,10 def drive(self): speed=50 yield hold,self,25
Basic SimPy API Entities class Process base class for processes class Resource base class for shared resources Verbs activate() activate a process reactivate() reactivate suspended process cancel() make a process passive interrupt() interrupt suspended process yield hold delay process for a period yield passivate make this process passive yield request request access to resource yield release give up resource
Model scenario • Messages arrive in a LAN at a rate of about 1/minute (calculated from 1000 data points collected) • They are processed by 2 expensive servers at 1 minute/message • To investigate:Could 4 cheaper, slower servers (2 min./message) do at least the same job? • Figures of merit: • Message delays • Nr of messages delayed
Run! Data file
SimulationTrace module: from Black Box to Glass Box • Insight into parallel processes and their interaction difficult • same problem occurs with simulation • Module SimulationTrace traces all events to show what is happening behind the scenes • Just replace from SimPy.Simulation import *withfrom SimPy.SimulationTrace import* • Trace output can be redirected to a file • Event types to be traced can be selected
Run! See trace
class Monitor for data collection and statistics • class Monitor (subclass of list) records time series of observed data values for a simulation variable • m = Monitor(name=‘’,ylab=‘y’,tlab=‘t’) • Attributes: • m.yseries() returns a list of the recorded data values. • m.tseries() returns a list of the recorded times. • m.total() returns the sum of the y values • m.count() returns the current number of observations. • m.mean() returns the simple average of the observations • m.var() returns the simple variance of the observations. • m.timeAverage() returns the time-average of the integrated y values,
Plotting results: The SimPlot module • Simulation needs data reduction • By statistics (see class Monitor) • By plotting • SimPlot is SimPy’s plotting module • Tk-based • Derived from Konrad Hinsen’s plotting module • Plots (x,y) data from Monitor instances and other lists • Plot types: • Line, bar, step, histogram, scatter • Basic and advanced API
Module SimGUI: SimPy’s Graphical User Interface • Satisfies need for simulation user GUI • Point-and-click • Simulation start • Parameter changes • Display/saving of results (raw and analyzed) • Model description • Satisfies need for easy development of simulation GUI • Tk-based simulation GUI framework
SimulationRT synchronizes simulation with real time • For simulation user interaction e.g. in game applications or animation, events should appear to be spaced in real time • SimulationRT allows tying simulation time to wall clock time • Example: simulate(real_time=True,relSpeed=0.2,until= …)runs1 simulation time unit in 5 real seconds • Module works much better under Windows than under Unix or Linux • Problem stems from time.clock( )
Extending SimPy:Adding new yield verbs • SimPy’s architecture and Python’s OO nature allow programmers easy addition of new scheduling or synchronization verbs • Example: • Addition of an unbounded buffer entity (class Bin) for producer/consumer synchronization • yield put,self,<Bin-instance>,<quantity>(non-blocking) for insertion into buffer • yield get,self,<Bin-instance>,<quantity>(blocking) for taking out from buffer
What are users doing with SimPy? • Teaching system simulation • Simulation of epidemics • Simulation of computer hardware • Simulation nuclear processing facilities in support of designing safeguards for nuclear non-proliferation • Visual simulation/animation which compares Personal Rapid Transit (PRT) and classic rapid transit systems like light rail or metro • Simulation of air space surveillance • Simulation of telescope management in observatory • . . . . • ? ? ? ?