| 1 |
|
| 2 |
|
| 3 |
============================== |
| 4 |
Simulation with Event Stepping |
| 5 |
============================== |
| 6 |
|
| 7 |
|
| 8 |
:Authors: - Klaus Muller <[email protected]> |
| 9 |
- Tony Vignaux <[email protected]> |
| 10 |
:SimPy release: |release| |
| 11 |
:Web-site: http://simpy.sourceforge.net/ |
| 12 |
:Python-Version: 2.6+ |
| 13 |
:Date: December 2011 |
| 14 |
|
| 15 |
.. contents:: Contents |
| 16 |
:depth: 2 |
| 17 |
|
| 18 |
This manual describes **SimulationStep**, a SimPy module which supports |
| 19 |
stepping through a simulation model event by event. |
| 20 |
|
| 21 |
Introduction |
| 22 |
============ |
| 23 |
|
| 24 |
SimulationStep can assist with debugging models, interacting with them on |
| 25 |
an event-by-event basis, getting event-by-event output from a model (e.g. |
| 26 |
for plotting purposes), etc. |
| 27 |
|
| 28 |
SimulationStep is a derivative of the Simulation module. Over and above |
| 29 |
the capabilities provided by Simulation, SimulationStep supports stepping |
| 30 |
through a simulation model event by event. It caters for: |
| 31 |
|
| 32 |
- running a simulation model, with calling a user-defined procedure after every event, |
| 33 |
- running a simulation model one event at a time by repeated calls, |
| 34 |
- starting and stopping the event stepping mode under program control. |
| 35 |
|
| 36 |
SimulationStep overview |
| 37 |
======================= |
| 38 |
|
| 39 |
Here is a simple program which shows basic event stepping capabilities:: |
| 40 |
|
| 41 |
## Stepping1.py |
| 42 |
from __future__ import generators |
| 43 |
from SimPy.SimulationStep import * # (1) |
| 44 |
|
| 45 |
def callbackTimeTrace(): # (2) |
| 46 |
"""Prints event times |
| 47 |
""" |
| 48 |
print "at time=%s"%now() |
| 49 |
|
| 50 |
class Man(Process): |
| 51 |
def walk(self): |
| 52 |
print "got up" |
| 53 |
yield hold,self,1 |
| 54 |
print "got to door" |
| 55 |
yield hold,self,10 |
| 56 |
print "got to mail box" |
| 57 |
yield hold,self,10 |
| 58 |
print "got home again" |
| 59 |
|
| 60 |
#trace event times |
| 61 |
initialize() |
| 62 |
otto=Man() |
| 63 |
activate(otto,otto.walk()) |
| 64 |
startStepping() # (3) |
| 65 |
simulate(callback=callbackTimeTrace,until=100) # (4) |
| 66 |
|
| 67 |
A trivial simulation model, but with event stepping: |
| 68 |
|
| 69 |
(1) import the stepping version of Simulation |
| 70 |
(2) define a procedure which gets called after every event |
| 71 |
(3) switch into event stepping mode |
| 72 |
(4) run the model with event callback to the procedure defined at (2); ``simulate`` in SimulationStep has an extra named parameter, ``callback``. |
| 73 |
|
| 74 |
Running it produces this output:: |
| 75 |
|
| 76 |
got up |
| 77 |
at time=0 |
| 78 |
got to door |
| 79 |
at time=1 |
| 80 |
got to mail box |
| 81 |
at time=11 |
| 82 |
got home again |
| 83 |
at time=21 |
| 84 |
at time=21 |
| 85 |
|
| 86 |
The callback outputs the simulation time after every event. |
| 87 |
|
| 88 |
Here is another example, the same model, but now with the user getting control back after every |
| 89 |
event:: |
| 90 |
|
| 91 |
## Stepping2.py |
| 92 |
from __future__ import generators |
| 93 |
from SimPy.SimulationStep import * |
| 94 |
|
| 95 |
def callbackUserControl(): |
| 96 |
"""Allows user to control stepping |
| 97 |
""" |
| 98 |
a=raw_input("[Time=%s] Select one: End run (e), Continue stepping (s), Run to end (r)= "%now()) |
| 99 |
if a=="e": |
| 100 |
stopSimulation() |
| 101 |
elif a=="s": |
| 102 |
return |
| 103 |
else: |
| 104 |
stopStepping() |
| 105 |
|
| 106 |
class Man(Process): |
| 107 |
def walk(self): |
| 108 |
print "got up" |
| 109 |
yield hold,self,1 |
| 110 |
print "got to door" |
| 111 |
yield hold,self,10 |
| 112 |
print "got to mail box" |
| 113 |
yield hold,self,10 |
| 114 |
print "got home again" |
| 115 |
#allow user control |
| 116 |
initialize() |
| 117 |
otto=Man() |
| 118 |
activate(otto,otto.walk()) |
| 119 |
startStepping() |
| 120 |
simulate(callback=callbackUserControl,until=100) |
| 121 |
|
| 122 |
Its interactive output looks like this:: |
| 123 |
|
| 124 |
got up |
| 125 |
[Time=0] Select one: End run (e), Continue stepping (s), Run to end (r)= s |
| 126 |
got to door |
| 127 |
[Time=1] Select one: End run (e), Continue stepping (s), Run to end (r)= s |
| 128 |
got to mail box |
| 129 |
[Time=11] Select one: End run (e), Continue stepping (s), Run to end (r)= s |
| 130 |
got home again |
| 131 |
[Time=21] Select one: End run (e), Continue stepping (s), Run to end (r)= s |
| 132 |
[Time=21] Select one: End run (e), Continue stepping (s), Run to end (r)= s |
| 133 |
|
| 134 |
or this (the user stopped stepping mode at time=1):: |
| 135 |
|
| 136 |
got up |
| 137 |
[Time=0] Select one: End run (e), Continue stepping (s), Run to end (r)= s |
| 138 |
got to door |
| 139 |
[Time=1] Select one: End run (e), Continue stepping (s), Run to end (r)= r |
| 140 |
got to mail box |
| 141 |
got home again |
| 142 |
|
| 143 |
If one wants to run a tested/debugged model full speed, i.e. without stepping, |
| 144 |
one can write a program as follows:: |
| 145 |
|
| 146 |
## Stepping2Fast.py |
| 147 |
from __future__ import generators |
| 148 |
if __debug__: |
| 149 |
from SimPy.SimulationStep import * |
| 150 |
else: |
| 151 |
from SimPy.Simulation import * |
| 152 |
|
| 153 |
def callbackUserControl(): |
| 154 |
"""Allows user to control stepping |
| 155 |
""" |
| 156 |
if __debug__: |
| 157 |
a=raw_input("[Time=%s] Select one: End run (e), Continue stepping (s),\ |
| 158 |
Run to end (r)= "%now()) |
| 159 |
if a=="e": |
| 160 |
stopSimulation() |
| 161 |
elif a=="s": |
| 162 |
return |
| 163 |
else: |
| 164 |
stopStepping() |
| 165 |
|
| 166 |
class Man(Process): |
| 167 |
def walk(self): |
| 168 |
print "got up" |
| 169 |
yield hold,self,1 |
| 170 |
print "got to door" |
| 171 |
yield hold,self,10 |
| 172 |
print "got to mail box" |
| 173 |
yield hold,self,10 |
| 174 |
print "got home again" |
| 175 |
#allow user control if debugging |
| 176 |
initialize() |
| 177 |
otto=Man() |
| 178 |
activate(otto,otto.walk()) |
| 179 |
if __debug__: |
| 180 |
startStepping() |
| 181 |
simulate(callback=callbackUserControl,until=100) |
| 182 |
else: |
| 183 |
simulate(until=100) |
| 184 |
|
| 185 |
If one runs this with the Python command line option '-O', any |
| 186 |
statement starting with ``if __debug__:`` is ignored/skipped by the |
| 187 |
Python interpreter. |
| 188 |
|
| 189 |
The SimulationStep API |
| 190 |
====================== |
| 191 |
|
| 192 |
Structure |
| 193 |
--------- |
| 194 |
Basically, SimulationStep has the same API as Simulation, but with |
| 195 |
the following additions and changes:: |
| 196 |
|
| 197 |
def startStepping() **new** |
| 198 |
def stopStepping() **new** |
| 199 |
def simulate() **changed** |
| 200 |
def simulateStep() **new** |
| 201 |
|
| 202 |
**startStepping** |
| 203 |
------------------ |
| 204 |
|
| 205 |
Starts the event-stepping. |
| 206 |
|
| 207 |
Call: |
| 208 |
|
| 209 |
**startStepping()** |
| 210 |
|
| 211 |
Mandatory parameters: |
| 212 |
None. |
| 213 |
|
| 214 |
Optional parameters: |
| 215 |
None |
| 216 |
|
| 217 |
Return value: |
| 218 |
None. |
| 219 |
|
| 220 |
**stopStepping** |
| 221 |
------------------ |
| 222 |
Stops event-stepping. |
| 223 |
|
| 224 |
Call: |
| 225 |
**stopStepping()** |
| 226 |
|
| 227 |
Mandatory parameters: |
| 228 |
None |
| 229 |
|
| 230 |
Optional parameters: |
| 231 |
None |
| 232 |
|
| 233 |
Return value: |
| 234 |
None |
| 235 |
|
| 236 |
**simulate** |
| 237 |
---------------- |
| 238 |
Runs a simulation with callback to a user-defined function after each event, if stepping is turned on. |
| 239 |
By default, stepping is switched off. |
| 240 |
|
| 241 |
Call: |
| 242 |
**simulate(callback=<proc>,until=<endtime>)** |
| 243 |
|
| 244 |
Mandatory parameters: |
| 245 |
None |
| 246 |
|
| 247 |
Optional parameters: |
| 248 |
- **until = 0**: the simulation time until which the simulation is to run (positive floating point or integer number) |
| 249 |
- **callback = lambda:None**: the function to be called after every event (function reference) |
| 250 |
|
| 251 |
Return value: |
| 252 |
The simulation status at exit (string) |
| 253 |
|
| 254 |
**simulateStep** |
| 255 |
---------------- |
| 256 |
Runs a simulation for one event, with (optional) callback to a user-defined function |
| 257 |
after the event, if stepping is turned on. By default, stepping is switched off. |
| 258 |
Thus, to execute the model to completion, *simulateStep* must be called repeatedly. |
| 259 |
|
| 260 |
**Note: it is not yet clear to the developers whether this part of the API offers any advantages |
| 261 |
or capabilities over and above the *simulate* function. The survival of this function |
| 262 |
in future versions depends on the feedback from the user community.** |
| 263 |
|
| 264 |
Call: |
| 265 |
**simulateStep(callback=<proc>,until=<endtime>)** |
| 266 |
|
| 267 |
Mandatory parameters: |
| 268 |
None |
| 269 |
|
| 270 |
Optional parameters: |
| 271 |
- **until = 0**: the simulation time until which the simulation is to run (positive floating point or integer number) |
| 272 |
- **callback = lambda:None**: the function to be called after every event (function reference) |
| 273 |
|
| 274 |
Return value: |
| 275 |
The tuple **(simulation status at exit (string),<resumability flag>)**. |
| 276 |
<resumability flag> can have one of two string values: **"resumable"** if there |
| 277 |
are more events to be executed, and **"notResumable"** if all events have been exhausted |
| 278 |
or an error has occurred. *simulateStep* should normally only be called if |
| 279 |
"resumable" is returned. |
| 280 |
|
| 281 |
|
| 282 |
|