| 1 |
.. Tutorials |
| 2 |
|
| 3 |
.. .. image:: images/sm_SimPy_Logo.png |
| 4 |
:align: left |
| 5 |
|
| 6 |
============================================================= |
| 7 |
The Bank Tutorial part 2: More examples of SimPy Simulation |
| 8 |
============================================================= |
| 9 |
|
| 10 |
.. --------------------------------------------------- |
| 11 |
.. TO DO |
| 12 |
add example of using SimpyTrace |
| 13 |
Reneging due to an event |
| 14 |
Interrupts - YES |
| 15 |
SimEvents? |
| 16 |
|
| 17 |
.. --------------------------------------------------- |
| 18 |
|
| 19 |
:Author: G A Vignaux |
| 20 |
:Date: 2011 December |
| 21 |
:SimPy release: |release| |
| 22 |
:Python-Version: 2.6 and later |
| 23 |
|
| 24 |
|
| 25 |
.. highlight:: python |
| 26 |
:linenothreshold: 5 |
| 27 |
|
| 28 |
|
| 29 |
.. .. contents:: Table Of Contents |
| 30 |
:depth: 2 |
| 31 |
|
| 32 |
.. |
| 33 |
1 Introduction |
| 34 |
2 Priority Customers |
| 35 |
2.1 Priority Customers without preemption |
| 36 |
2.2 Priority Customers with preemption |
| 37 |
3 Balking and Reneging Customers |
| 38 |
3.1 Balking Customers |
| 39 |
3.2 Reneging (or abandoning) Customers |
| 40 |
4 Processes |
| 41 |
4.1 Interrupting a Process. |
| 42 |
4.2 ``waituntil`` the Bank door opens |
| 43 |
4.3 Wait for the doorman to give a signal: ``waitevent`` |
| 44 |
5 Monitors |
| 45 |
5.1 Plotting a Histogram of Monitor results |
| 46 |
5.2 Monitoring a Resource |
| 47 |
5.3 Plotting from Resource Monitors |
| 48 |
6 Acknowledgements |
| 49 |
7 References |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
.. .. sectnum:: |
| 54 |
:depth: 2 |
| 55 |
|
| 56 |
.. raw:: latex |
| 57 |
|
| 58 |
\newpage |
| 59 |
|
| 60 |
Introduction |
| 61 |
------------------------------------- |
| 62 |
|
| 63 |
The first Bank tutorial, The Bank, developed and explained a |
| 64 |
series of simulation models of a simple bank using SimPy_. In various |
| 65 |
models, customers arrived randomly, queued up to be served at one or |
| 66 |
several counters, modelled using the Resource class, and, in one case, |
| 67 |
could choose the shortest among several queues. It demonstrated the |
| 68 |
use of the Monitor class to record delays and showed how a ``model()`` |
| 69 |
mainline for the simulation was convenient to execute replications of |
| 70 |
simulation runs. |
| 71 |
|
| 72 |
In this extension to The Bank, I provide more examples of SimPy |
| 73 |
facilities for which there was no room and for some that were |
| 74 |
developed since it was written. These facilities are generally more |
| 75 |
complicated than those introduced before. They include queueing with priority, |
| 76 |
possibly with preemption, reneging, plotting, interrupting, waiting |
| 77 |
until a condition occurs (``waituntil``) and waiting for events to |
| 78 |
occur. |
| 79 |
|
| 80 |
.. _SimPy: http://simpy.sourceforge.net/ |
| 81 |
|
| 82 |
The programs are available without line numbers and ready to go, in |
| 83 |
directory ``bankprograms``. Some have trace statements for |
| 84 |
demonstration purposes, others produce graphical output to the |
| 85 |
screen. Let me encourage you to run them and modify them for yourself |
| 86 |
|
| 87 |
SimPy itself can be obtained from: http://simpy.sourceforge.net/. It |
| 88 |
is compatible with Python version 2.6 onwards. The examples in this |
| 89 |
documentation run with SimPy version 1.5 and later. |
| 90 |
|
| 91 |
This tutorial should be read with the SimPy Manual or Cheatsheet at |
| 92 |
your side for reference. |
| 93 |
|
| 94 |
|
| 95 |
.. raw:: latex |
| 96 |
|
| 97 |
\newpage |
| 98 |
|
| 99 |
Priority Customers |
| 100 |
------------------- |
| 101 |
|
| 102 |
In many situations there is a system of priority service. Those |
| 103 |
customers with high priority are served first, those with low priority |
| 104 |
must wait. In some cases, preemptive priority will even allow a |
| 105 |
high-priority customer to interrupt the service of one with a lower |
| 106 |
priority. |
| 107 |
|
| 108 |
SimPy implements priority requests with an extra numerical priority |
| 109 |
argument in the ``yield request`` command, higher values meaning |
| 110 |
higher priority. For this to operate, the requested Resource must have |
| 111 |
been defined with ``qType=PriorityQ``. |
| 112 |
|
| 113 |
Priority Customers without preemption |
| 114 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 115 |
|
| 116 |
.. index:: priority |
| 117 |
|
| 118 |
In the first example, we modify the program with random arrivals, one |
| 119 |
counter, and a fixed service time (like ``bank07.py`` in The Bank |
| 120 |
tutorial) to process a high priority customer. Warning: the ``seed()`` |
| 121 |
value has been changed to ``98989`` to make the story more exciting. |
| 122 |
|
| 123 |
The modifications are to the definition of the ``counter`` where we |
| 124 |
change the ``qType`` and to the ``yield request`` command in the |
| 125 |
``visit`` PEM of the customer. We also need to provide each customer |
| 126 |
with a priority. Since the default is ``priority=0`` this is easy for |
| 127 |
most of them. |
| 128 |
|
| 129 |
To observe the priority in action, while all other customers have the |
| 130 |
default priority of 0, in lines 46 to 48 we create and |
| 131 |
activate one special customer, ``Guido``, with priority 100 who |
| 132 |
arrives at time ``23.0`` (line 48). This is to ensure that he arrives |
| 133 |
after ``Customer03``. |
| 134 |
|
| 135 |
The ``visit`` customer method has a new parameter, ``P=0`` (line |
| 136 |
21) which allows us to set the customer priority. |
| 137 |
|
| 138 |
In lines 38 to 38 ``counter`` is defined with ``qType=PriorityQ`` so |
| 139 |
that we can request it with priority (line 26) using the |
| 140 |
statement ``yield request,self,counter,P`` |
| 141 |
|
| 142 |
In line 24 we print out the number of customers waiting when each |
| 143 |
customer arrives. |
| 144 |
|
| 145 |
|
| 146 |
.. literalinclude:: bankprograms/bank20.py |
| 147 |
|
| 148 |
The resulting output is as follows. The number of customers in the |
| 149 |
queue just as each arrives is displayed in the trace. That count does |
| 150 |
not include any customer in service. |
| 151 |
|
| 152 |
.. literalinclude:: bankprograms/bank20.out |
| 153 |
|
| 154 |
.. index:: random arrival, bank20 |
| 155 |
|
| 156 |
Reading carefully one can see that when ``Guido`` arrives |
| 157 |
``Customer00`` has been served and left at ``12.000``), ``Customer01`` |
| 158 |
is in service and two (customers 02 and 03) are queueing. ``Guido`` |
| 159 |
has priority over those waiting and is served before them at |
| 160 |
``33.162``. When ``Guido`` leaves at ``45.162``, ``Customer02`` starts |
| 161 |
service. |
| 162 |
|
| 163 |
|
| 164 |
Priority Customers with preemption |
| 165 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 166 |
|
| 167 |
.. index:: |
| 168 |
single: priority: preemption |
| 169 |
single: bank23 |
| 170 |
|
| 171 |
Now we allow ``Guido`` to have preemptive priority. He will displace |
| 172 |
any customer in service when he arrives. That customer will resume |
| 173 |
when ``Guido`` finishes (unless higher priority customers |
| 174 |
intervene). It requires only a change to one line of the program, |
| 175 |
adding the argument, ``preemptable=True`` to the ``Resource`` |
| 176 |
statement in line 38. |
| 177 |
|
| 178 |
.. literalinclude:: bankprograms/bank23.py |
| 179 |
|
| 180 |
|
| 181 |
Though ``Guido`` arrives at the same time, ``23.000``, he no longer |
| 182 |
has to wait and immediately goes into service, displacing the |
| 183 |
incumbent, ``Customer01``. That customer had already completed |
| 184 |
``23.000-12.000 = 11.000`` minutes of his service. When ``Guido`` |
| 185 |
finishes at ``35.000``, ``Customer01`` resumes service and takes |
| 186 |
``36.000-35.000 = 1.000`` minutes to finish. His total service time |
| 187 |
is the same as before (``12.000`` minutes). |
| 188 |
|
| 189 |
.. literalinclude:: bankprograms/bank23.out |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
.. raw:: latex |
| 194 |
|
| 195 |
\newpage |
| 196 |
|
| 197 |
Balking and Reneging Customers |
| 198 |
-------------------------------- |
| 199 |
|
| 200 |
.. index:: balking, reneging, abandoning (reneging) |
| 201 |
|
| 202 |
|
| 203 |
Balking occurs when a customer refuses to join a queue if it is too |
| 204 |
long. Reneging (or, better, abandonment) occurs if an impatient |
| 205 |
customer gives up while still waiting and before being served. |
| 206 |
|
| 207 |
Balking Customers |
| 208 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 209 |
|
| 210 |
.. index:: |
| 211 |
single: balking |
| 212 |
single: bank24 |
| 213 |
|
| 214 |
Another term for a system with balking customers is one where "blocked |
| 215 |
customers" are "cleared", termed by engineers a BCC system. This is |
| 216 |
very convenient analytically in queueing theory and formulae developed |
| 217 |
using this assumption are used extensively for planning communication |
| 218 |
systems. The easiest case is when no queueing is allowed. |
| 219 |
|
| 220 |
As an example let us investigate a BCC system with a single server but |
| 221 |
the waiting space is limited. We will estimate the rate of balking |
| 222 |
when the maximum number in the queue is set to 1. On arrival into the |
| 223 |
system the customer must first check to see if there is room. We will |
| 224 |
need the number of customers in the system or waiting. We could keep a |
| 225 |
count, incrementing when a customer joins the queue or, since we have |
| 226 |
a Resource, use the length of the Resource's ``waitQ``. Choosing the |
| 227 |
latter we test (on line 25). If there is not enough room, we |
| 228 |
balk, incrementing a Class variable ``Customer.numBalking`` at line |
| 229 |
34 to get the total number balking during the run. |
| 230 |
|
| 231 |
|
| 232 |
.. literalinclude:: bankprograms/bank24.py |
| 233 |
|
| 234 |
|
| 235 |
The resulting output for a run of this program showing balking |
| 236 |
occurring is given below: |
| 237 |
|
| 238 |
.. literalinclude:: bankprograms/bank24.out |
| 239 |
|
| 240 |
|
| 241 |
When ``Customer02`` arrives, numbers 00 is already in service and 01 |
| 242 |
is waiting. There is no room so 02 balks. By the vagaries of |
| 243 |
exponential random numbers, 00 takes a very long time to serve (55.0607 |
| 244 |
minutes) so the first one to find room is number 07 at 73.0765. |
| 245 |
|
| 246 |
|
| 247 |
Reneging (or abandoning) Customers |
| 248 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 249 |
|
| 250 |
Often in practice an impatient customer will leave the queue before |
| 251 |
being served. SimPy can model this *reneging* behaviour using a |
| 252 |
*compound yield statement*. In such a statement there are two |
| 253 |
yield clauses. An example is:: |
| 254 |
|
| 255 |
yield (request,self,counter),(hold,self,maxWaitTime) |
| 256 |
|
| 257 |
The first tuple of this statement is the usual ``yield request``, |
| 258 |
asking for a unit of ``counter`` Resource. The process will either get |
| 259 |
the unit immediately or be queued by the Resource. The second tuple is |
| 260 |
a reneging clause which has the same syntax as a ``yield hold``. The |
| 261 |
requesting process will renege if the wait exceeds ``maxWaitTime``. |
| 262 |
|
| 263 |
There is a complication, though. The requesting PEM must discover what |
| 264 |
actually happened. Did the process get the resource or did it |
| 265 |
renege? This involves a *mandatory* test of ``self.acquired(``\ |
| 266 |
*resource*\ |
| 267 |
``)``. In our example, this test is in line 26. |
| 268 |
|
| 269 |
.. literalinclude:: bankprograms/bank21.py |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
.. literalinclude:: bankprograms/bank21.out |
| 274 |
|
| 275 |
|
| 276 |
``Customer01`` arrives after 00 but has only 12 minutes |
| 277 |
patience. After that time in the queue (at time 14.166) he abandons |
| 278 |
the queue to leave 02 to take his place. 03 also abandons. 04 finds an |
| 279 |
empty system and takes the server without having to wait. |
| 280 |
|
| 281 |
.. ================================================================== |
| 282 |
|
| 283 |
.. raw:: latex |
| 284 |
|
| 285 |
\newpage |
| 286 |
|
| 287 |
Processes |
| 288 |
--------------------- |
| 289 |
|
| 290 |
In some simulations it is valuable for one SimPy Process to interrupt |
| 291 |
another. This can only be done when the *victim* is "active"; that is |
| 292 |
when it has an event scheduled for it. It must be executing a ``yield |
| 293 |
hold`` statement. |
| 294 |
|
| 295 |
A process waiting for a resource (after a ``yield request`` |
| 296 |
statement) is passive and cannot be interrupted by another. Instead |
| 297 |
the ``yield waituntil`` and ``yield waitevent`` facilities have been |
| 298 |
introduced to allow processes to wait for conditions set by other |
| 299 |
processes. |
| 300 |
|
| 301 |
Interrupting a Process. |
| 302 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 303 |
|
| 304 |
``Klaus`` goes into the bank to talk to the manager. For clarity we |
| 305 |
ignore the counters and other customers. During his conversation his |
| 306 |
cellphone rings. When he finishes the call he continues the |
| 307 |
conversation. |
| 308 |
|
| 309 |
In this example, ``call`` is an object of the ``Call`` Process class |
| 310 |
whose only purpose is to make the cellphone ring after a delay, |
| 311 |
``timeOfCall``, an argument to its ``ring`` PEM (line 26). |
| 312 |
|
| 313 |
``klaus``, a ``Customer``, is interrupted by the call (line 29). |
| 314 |
He is in the middle of a ``yield hold`` (line 12). When he exits |
| 315 |
from that command it is as if he went into a trance when talking to |
| 316 |
the bank manager. He suddenly wakes up and must check (line 13) |
| 317 |
to see whether has finished his conversation (if there was no call) or |
| 318 |
has been interrupted. |
| 319 |
|
| 320 |
If ``self.interrupted()`` is ``False`` he was not interrupted and |
| 321 |
leaves the bank (line 21) normally. If it is ``True``, he was |
| 322 |
interrupted by the call, remembers how much conversation he has left |
| 323 |
(line 14), resets the interrupt (line 15) and then deals |
| 324 |
with the call. When he finishes (line 19) he can resume the |
| 325 |
conversation, with, now we assume, a thoroughly irritated bank manager |
| 326 |
v(line 20). |
| 327 |
|
| 328 |
|
| 329 |
.. literalinclude:: bankprograms/bank22.py |
| 330 |
|
| 331 |
|
| 332 |
.. literalinclude:: bankprograms/bank22.out |
| 333 |
|
| 334 |
|
| 335 |
As this has no random numbers the results are reasonably clear: the |
| 336 |
interrupting call occurs at 9.0. It takes ``klaus`` 3 minutes to |
| 337 |
listen to the message and he resumes the conversation with the bank |
| 338 |
manager at 12.0. His total time of conversation is 9.0 + 11.0 = 20.0 |
| 339 |
minutes as it would have been if the interrupt had not occurred. |
| 340 |
|
| 341 |
|
| 342 |
``waituntil`` the Bank door opens |
| 343 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 344 |
|
| 345 |
Customers arrive at random, some of them getting to the bank before |
| 346 |
the door is opened by a doorman. They wait for the door to be opened |
| 347 |
and then rush in and queue to be served. |
| 348 |
|
| 349 |
This model uses the ``waituntil`` yield command. In the program listing |
| 350 |
the door is initially closed (line 7) and a function to test if |
| 351 |
it is open is defined at line 8. |
| 352 |
|
| 353 |
The ``Doorman`` class is defined starting at line 11 and the single |
| 354 |
``doorman`` is created and activated at at lines 65 and 66. The |
| 355 |
doorman waits for an average 10 minutes (label 16) and then |
| 356 |
opens the door. |
| 357 |
|
| 358 |
The ``Customer`` class is defined at 29 and a new customer prints out |
| 359 |
``Here I am`` on arrival. If the door is still closed, he adds ``but |
| 360 |
the door is shut`` and settles down to wait (line 40), using the |
| 361 |
``yield waituntil`` command. When the door is opened by the doorman the |
| 362 |
``dooropen`` state is changed and the customer (and all others waiting |
| 363 |
for the door) proceed. A customer arriving when the door is open will |
| 364 |
not be delayed. |
| 365 |
|
| 366 |
|
| 367 |
.. literalinclude:: bankprograms/bank14.py |
| 368 |
|
| 369 |
|
| 370 |
An output run for this programs shows how the first three customers |
| 371 |
have to wait until the door is opened. |
| 372 |
|
| 373 |
.. literalinclude:: bankprograms/bank14.out |
| 374 |
|
| 375 |
|
| 376 |
Wait for the doorman to give a signal: ``waitevent`` |
| 377 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 378 |
|
| 379 |
Customers arrive at random, some of them getting to the bank before |
| 380 |
the door is open. This is controlled by an automatic machine called |
| 381 |
the doorman which opens the door only at intervals of 30 minutes (it |
| 382 |
is a very secure bank). The customers wait for the door to be opened |
| 383 |
and all those waiting enter and proceed to the counter. The door is |
| 384 |
closed behind them. |
| 385 |
|
| 386 |
This model uses the ``yield waitevent`` command which requires a |
| 387 |
``SimEvent`` to be defined (line 7). The ``Doorman`` class is defined |
| 388 |
at line 8 and the ``doorman`` is created and activated at at labels |
| 389 |
61 and 62. The doorman waits for a fixed time (label |
| 390 |
13) and then tells the customers that the door is open. This is |
| 391 |
achieved on line 14 by signalling the ``dooropen`` event. |
| 392 |
|
| 393 |
The ``Customer`` class is defined at 25 and in its PEM, when a |
| 394 |
customer arrives, he prints out ``Here I am``. If the door is still |
| 395 |
closed, he adds `"but the door is shut`` and settles down to wait for |
| 396 |
the door to be opened using the ``yield waitevent`` command (line |
| 397 |
35). When the door is opened by the doorman (that is, he sends |
| 398 |
the ``dooropen.signal()`` the customer and any others waiting may |
| 399 |
proceed. |
| 400 |
|
| 401 |
|
| 402 |
.. literalinclude:: bankprograms/bank13.py |
| 403 |
|
| 404 |
|
| 405 |
An output run for this programs shows how the first three customers |
| 406 |
have to wait until the door is opened. |
| 407 |
|
| 408 |
.. literalinclude:: bankprograms/bank13.out |
| 409 |
|
| 410 |
|
| 411 |
.. ================================================================== |
| 412 |
|
| 413 |
.. raw:: latex |
| 414 |
|
| 415 |
\newpage |
| 416 |
|
| 417 |
|
| 418 |
Monitors |
| 419 |
------------- |
| 420 |
|
| 421 |
Monitors (and Tallys) are used to track and record values in a |
| 422 |
simulation. They store a list of [time,value] pairs, one pair being |
| 423 |
added whenever the ``observe`` method is called. A particularly |
| 424 |
useful characteristic is that they continue to exist after the |
| 425 |
simulation has been completed. Thus further analysis of the results |
| 426 |
can be carried out. |
| 427 |
|
| 428 |
|
| 429 |
Monitors have a set of simple statistical methods such as ``mean`` and |
| 430 |
``var`` to calculate the average and variance of the observed values |
| 431 |
-- useful in estimating the mean delay, for example. |
| 432 |
|
| 433 |
They also have the ``timeAverage`` method that calculates the |
| 434 |
time-weighted average of the recorded values. It determines the total |
| 435 |
area under the time~value graph and divides by the total time. This is |
| 436 |
useful for estimating the average number of customers in the bank, for |
| 437 |
example. There is an *important caveat* in using this method. To |
| 438 |
estimate the correct time average you must certainly ``observe`` the |
| 439 |
value (say the number of customers in the system) whenever it changes |
| 440 |
(as well as at any other time you wish) but, and this is important, |
| 441 |
observing the *new* value. The *old* value was recorded earlier. In |
| 442 |
practice this means that if we wish to observe a changing value, |
| 443 |
``n``, using the Monitor, ``Mon``, we must keep to the the following |
| 444 |
pattern:: |
| 445 |
|
| 446 |
n = n+1 |
| 447 |
Mon.observe(n,now()) |
| 448 |
|
| 449 |
Thus you make the change (not only increases) and *then* observe the new |
| 450 |
value. Of course the simulation time ``now()`` has not changed between |
| 451 |
the two statements. |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
Plotting a Histogram of Monitor results |
| 456 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 457 |
|
| 458 |
A Monitor can construct a histogram from its data using the |
| 459 |
``histogram`` method. In this model we monitor the time in the system |
| 460 |
for the customers. This is calculated for each customer in line |
| 461 |
29, using the arrival time saved in line 19. We create the |
| 462 |
Monitor, ``Mon``, at line 37 and the times are ``observed`` at line |
| 463 |
30. |
| 464 |
|
| 465 |
The histogram is constructed from the Monitor, after the simulation |
| 466 |
has finished, at line 55. The SimPy SimPlot package allows |
| 467 |
simple plotting of results from simulations. Here we use the SimPlot |
| 468 |
``plotHistogram`` method. The plotting routines appear in lines |
| 469 |
57-61. The ``plotHistogram`` call is in line 58. |
| 470 |
|
| 471 |
.. literalinclude:: bankprograms/bank17.py |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
Monitoring a Resource |
| 477 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 478 |
|
| 479 |
Now consider observing the number of customers waiting or executing in |
| 480 |
a Resource. Because of the need to ``observe`` the value after the |
| 481 |
change but at the same simulation instant, it is impossible to use the |
| 482 |
length of the Resource's ``waitQ`` directly with a Monitor defined |
| 483 |
outside the Resource. Instead Resources can be set up with built-in |
| 484 |
Monitors. |
| 485 |
|
| 486 |
Here is an example using a Monitored Resource. We intend to observe |
| 487 |
the average number waiting and active in the ``counter`` |
| 488 |
resource. ``counter`` is defined at line 32 and we have set |
| 489 |
``monitored=True``. This establishes two Monitors: ``waitMon``, to |
| 490 |
record changes in the numbers waiting and ``actMon`` to record changes |
| 491 |
in the numbers active in the ``counter``. We need make no further |
| 492 |
change to the operation of the program as monitoring is then |
| 493 |
automatic. No ``observe`` calls are necessary. |
| 494 |
|
| 495 |
At the end of the run in the ``model`` function, we calculate the |
| 496 |
``timeAverage`` of both ``waitMon`` and ``actMon`` and return them |
| 497 |
from the ``model`` call (line 45). These can then be printed at |
| 498 |
the end of the program (line 49). |
| 499 |
|
| 500 |
.. literalinclude:: bankprograms/bank15.py |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
Plotting from Resource Monitors |
| 506 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 507 |
|
| 508 |
Like all Monitors, ``waitMon`` and ``actMon`` in a monitored Resource |
| 509 |
contain information that enables us to graph the output. Alternative |
| 510 |
plotting packages can be used; here we use the simple ``SimPy.SimPlot`` |
| 511 |
package just to graph the number of customers waiting for the |
| 512 |
counter. The program is a simple modification of the one that uses a |
| 513 |
monitored Resource. |
| 514 |
|
| 515 |
The SimPlot package is imported at line 3. No major changes are |
| 516 |
made to the main part of the program except that I commented out the |
| 517 |
print statements. The changes occur in the ``model`` routine from lines |
| 518 |
37 to 44. The simulation now generates and processes 20 |
| 519 |
customers (line 43). ``model`` does not return a value but the |
| 520 |
Monitors of the ``counter`` Resource still exist when the simulation has |
| 521 |
terminated. |
| 522 |
|
| 523 |
The additional plotting actions take place in lines 50 to |
| 524 |
53. Line 51-52 construct a step plot and graphs the |
| 525 |
number in the waiting queue as a function of time. ``waitMon`` is |
| 526 |
primarily a list of *[time,value]* pairs which the ``plotStep`` method |
| 527 |
of the SimPlot object, ``plt`` uses without change. On running the |
| 528 |
program the graph is plotted; the user has to terminate the plotting |
| 529 |
``mainloop`` on the screen. |
| 530 |
|
| 531 |
|
| 532 |
.. literalinclude:: bankprograms/bank16.py |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
.. Some of the following links need changing |
| 538 |
|
| 539 |
.. _`SimPy`: http://simpy.sourceforge.net/ |
| 540 |
.. _Python: http://www.Python.org |
| 541 |
.. _`Python web site`: http://www.Python.org |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
Acknowledgements |
| 546 |
------------------------------ |
| 547 |
|
| 548 |
I thank Klaus Muller, Bob Helmbold, Mukhlis Matti and the other |
| 549 |
developers and users of SimPy for improving this document by sending |
| 550 |
their comments. I would be grateful for any further corrections or |
| 551 |
suggestions. Please send them to: *vignaux* at |
| 552 |
*users.sourceforge.net*. |
| 553 |
|
| 554 |
|
| 555 |
References |
| 556 |
------------------------------------- |
| 557 |
|
| 558 |
- Python website: http://www.Python.org |
| 559 |
|
| 560 |
- SimPy homepage: http://simpy.sourceforge.net/ |
| 561 |
|
| 562 |
- The Bank: |
| 563 |
|
| 564 |
|
| 565 |
.. |
| 566 |
------------------------------------------------------ |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
.. |
| 571 |
Local Variables: |
| 572 |
mode: rst |
| 573 |
indent-tabs-mode: nil |
| 574 |
sentence-end-double-space: t |
| 575 |
fill-column: 70 |
| 576 |
End: |
| 577 |
|