| 1 |
============================================================= |
| 2 |
The Bank: Examples of SimPy Simulation |
| 3 |
============================================================= |
| 4 |
|
| 5 |
.. highlight:: python |
| 6 |
:linenothreshold: 5 |
| 7 |
|
| 8 |
.. index:: |
| 9 |
Monitors, Tallys, Processes,Resources, Levels, Stores |
| 10 |
|
| 11 |
|
| 12 |
Introduction |
| 13 |
------------------------------------- |
| 14 |
|
| 15 |
`SimPy`_ is used to develop a simple simulation of a bank with a |
| 16 |
number of tellers. This Python package provides *Processes* to model |
| 17 |
active components such as messages, customers, trucks, and planes. It |
| 18 |
has three classes to model facilities where congestion might occur: |
| 19 |
*Resources* for ordinary queues, *Levels* for the supply of quantities |
| 20 |
of material, and *Stores* for collections of individual items. Only |
| 21 |
examples of *Resources* are described here. It also provides |
| 22 |
*Monitors* and *Tallys* to record data like queue lengths and delay |
| 23 |
times and to calculate simple averages. It uses the standard Python |
| 24 |
random package to generate random numbers. |
| 25 |
|
| 26 |
Starting with SimPy 2.0 an object-oriented programmer's interface was |
| 27 |
added to the package. It is quite compatible with the current |
| 28 |
procedural approach which is used in the models described here. |
| 29 |
|
| 30 |
|
| 31 |
.. _`SimPy`: http://simpy.sourceforge.net/ |
| 32 |
|
| 33 |
|
| 34 |
SimPy can be obtained from: http://sourceforge.net/projects/simpy. |
| 35 |
The examples run with SimPy version 1.5 and later. This tutorial is |
| 36 |
best read with the SimPy Manual or Cheatsheet at your side for |
| 37 |
reference. |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
Before attempting to use SimPy you should be familiar with the Python_ |
| 42 |
language. In particular you should be able to use *classes*. Python is |
| 43 |
free and available for most machine types. You can find out more about |
| 44 |
it at the `Python web site`_. SimPy is compatible with Python version |
| 45 |
2.3 and later. |
| 46 |
|
| 47 |
.. _Python: http://www.Python.org |
| 48 |
.. _`Python web site`: http://www.Python.org |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
A single Customer |
| 53 |
------------------- |
| 54 |
|
| 55 |
In this tutorial we model a simple bank with customers arriving at |
| 56 |
random. We develop the model step-by-step, starting out simply, and |
| 57 |
producing a running program at each stage. The programs we develop are |
| 58 |
available without line numbers and ready to go, in the |
| 59 |
``bankprograms`` directory. Please copy them, run them and improve |
| 60 |
them - and in the tradition of open-source software suggest your |
| 61 |
modifications to the SimPy users list. Object-orented versions of all |
| 62 |
the models are included in the bankprobrams_OO sub directory. |
| 63 |
|
| 64 |
A simulation should always be developed to answer a specific question; |
| 65 |
in these models we investigate how changing the number of bank servers |
| 66 |
or tellers might affect the waiting time for customers. |
| 67 |
|
| 68 |
|
| 69 |
.. index:: bank 01 |
| 70 |
|
| 71 |
|
| 72 |
A Customer arriving at a fixed time |
| 73 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 |
|
| 75 |
|
| 76 |
We first model a single customer who arrives at the bank for a visit, |
| 77 |
looks around at the decor for a time and then leaves. There is no |
| 78 |
queueing. First we will assume his arrival time and the time he spends |
| 79 |
in the bank are fixed. |
| 80 |
|
| 81 |
We define a ``Customer`` class derived from the SimPy ``Process`` |
| 82 |
class. We create a ``Customer`` object, ``c`` who arrives at the bank |
| 83 |
at simulation time ``5.0`` and leaves after a fixed time of ``10.0`` |
| 84 |
minutes. |
| 85 |
|
| 86 |
Examine the following listing which is a complete runnable Python |
| 87 |
script, except for the line numbers. We use comments to divide the |
| 88 |
script up into sections. This makes for clarity later when the |
| 89 |
programs get more complicated. At line :an:`1` is a normal Python |
| 90 |
documentation string; :an:`2` imports the SimPy simulation code. |
| 91 |
|
| 92 |
.. index:: |
| 93 |
pair: PEM; Process Execution Method |
| 94 |
|
| 95 |
The ``Customer`` class definition at :an:`3` defines our customer class and has |
| 96 |
the required generator method (called ``visit`` :an:`4`) having a ``yield`` |
| 97 |
statement :an:`6`. Such a method is called a Process Execution Method (PEM) in |
| 98 |
SimPy. |
| 99 |
|
| 100 |
The customer's ``visit`` PEM at :an:`4`, models his activities. When he |
| 101 |
arrives (it will turn out to be a 'he' in this model), he will print out the |
| 102 |
simulation time, ``now()``, and his name at :an:`5`. The function ``now()`` |
| 103 |
can be used at any time in the simulation to find the current simulation time |
| 104 |
though it cannot be changed by the programmer. The customer's name will be set |
| 105 |
when the customer is created later in the script at :an:`10`. |
| 106 |
|
| 107 |
He then stays in the bank for a fixed simulation time ``timeInBank`` :an:`6`. |
| 108 |
This is achieved by the ``yield hold,self,timeInBank`` statement. This is the |
| 109 |
first of the special simulation commands that ``SimPy`` offers. |
| 110 |
|
| 111 |
After a simulation time of ``timeInBank``, the program's execution |
| 112 |
returns to the line after the ``yield`` statement at :an:`6`. The |
| 113 |
customer then prints out the current simulation time and his |
| 114 |
name at :an:`7`. This completes the declaration of the ``Customer`` class. |
| 115 |
|
| 116 |
The call ``initialize()`` at :an:`9` sets up the simulation |
| 117 |
system ready to receive ``activate`` calls. At :an:`10`, we create |
| 118 |
a customer, ``c``, with name ``Klaus``. All SimPy Processes have a |
| 119 |
``name`` attribute. We ``activate`` ``Klaus`` at :an:`11` |
| 120 |
specifying the object (``c``) to be activated, the call of the action |
| 121 |
routine (``c.visit(timeInBank = 10.0)``) and that it is to be activated |
| 122 |
at time 5 (``at = 5.0``). This will activate |
| 123 |
``Klaus`` exactly ``5`` minutes after the current time, in this case |
| 124 |
after the start of the simulation at ``0.0``. The call of an action |
| 125 |
routine such as ``c.visit`` can specify the values of arguments, here |
| 126 |
the ``timeInBank``. |
| 127 |
|
| 128 |
Finally the call of ``simulate(until=maxTime)`` at :an:`12` will |
| 129 |
start the simulation. This will run until the simulation time is |
| 130 |
``maxTime`` unless stopped beforehand either by the |
| 131 |
``stopSimulation()`` command or by running out of events to execute |
| 132 |
(as will happen here). ``maxTime`` was set to ``100.0`` at :an:`8`. |
| 133 |
|
| 134 |
|
| 135 |
.. |
| 136 |
Though we do not do it here, it is also possible to define an |
| 137 |
``__init__()`` method for a ``Process`` if you need to give the |
| 138 |
customer any attributes. Bear in mind that such an ``__init__`` |
| 139 |
method must first call ``Process.__init__(self)`` and can then |
| 140 |
initialize any instance variables needed. |
| 141 |
|
| 142 |
|
| 143 |
.. literalinclude:: bankprograms/bank01.py |
| 144 |
|
| 145 |
|
| 146 |
The short trace printed out by the ``print`` statements shows the |
| 147 |
result. The program finishes at simulation time ``15.0`` because there are |
| 148 |
no further events to be executed. At the end of the ``visit`` routine, |
| 149 |
the customer has no more actions and no other objects or customers are |
| 150 |
active. |
| 151 |
|
| 152 |
.. literalinclude:: bankprograms/bank01.out |
| 153 |
|
| 154 |
.. index:: random arrival, bank05 |
| 155 |
|
| 156 |
A Customer arriving at random |
| 157 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 158 |
|
| 159 |
Now we extend the model to allow our customer to arrive at a random |
| 160 |
simulated time though we will keep the time in the bank at 10.0, as |
| 161 |
before. |
| 162 |
|
| 163 |
The change occurs in line :an:`1` of the program and in lines :an:`2`, |
| 164 |
:an:`3`, and :an:`4`. In line :an:`1` we import from the standard |
| 165 |
Python ``random`` module to give us ``expovariate`` to generate the |
| 166 |
random time of arrival. We also import the ``seed`` function to |
| 167 |
initialize the random number stream to allow control of the random |
| 168 |
numbers. In line :an:`2` we provide an initial seed of ``99999``. An |
| 169 |
exponential random variate, ``t``, is generated in line :an:`3`. Note |
| 170 |
that the Python Random module's ``expovariate`` function uses the |
| 171 |
average rate (that is, ``1.0/mean``) as the argument. The generated |
| 172 |
random variate, ``t``, is used in line :an:`4` as the ``at`` argument |
| 173 |
to the ``activate`` call. |
| 174 |
|
| 175 |
|
| 176 |
.. literalinclude:: bankprograms/bank05.py |
| 177 |
|
| 178 |
|
| 179 |
The result is shown below. The customer now arrives at about |
| 180 |
0.64195, (or 10.58092 if you are not using Python 3). Changing the seed |
| 181 |
value would change that time. |
| 182 |
|
| 183 |
.. literalinclude:: bankprograms/bank05.out |
| 184 |
|
| 185 |
The display looks pretty untidy. In the next example I will try and |
| 186 |
make it tidier. |
| 187 |
|
| 188 |
If you are not using Python 3, you output may differ. |
| 189 |
The output for Python 2 is given in Appendix A. |
| 190 |
|
| 191 |
|
| 192 |
.. index:: bank02 |
| 193 |
|
| 194 |
More Customers |
| 195 |
------------------------------------- |
| 196 |
|
| 197 |
Our simulation does little so far. To consider a simulation with |
| 198 |
several customers we return to the simple deterministic model and add |
| 199 |
more ``Customers``. |
| 200 |
|
| 201 |
The program is almost as easy as the first example (`A Customer |
| 202 |
arriving at a fixed time`_). The main change is in lines |
| 203 |
:an:`4` to :an:`5` where we create, name, and activate three |
| 204 |
customers. We also increase the maximum simulation time to ``400`` |
| 205 |
(line :an:`3` and referred to in line :an:`6`). Observe that we need |
| 206 |
only one definition of the ``Customer`` class and create several |
| 207 |
objects of that class. These will act quite independently in this |
| 208 |
model. |
| 209 |
|
| 210 |
Each customer stays for a different ``timeinbank`` so, instead of |
| 211 |
setting a common value for this we set it for each customer. The |
| 212 |
customers are started at different times (using ``at=``). ``Tony's`` |
| 213 |
activation time occurs before ``Klaus's``, so ``Tony`` will arrive |
| 214 |
first even though his activation statement appears later in the |
| 215 |
script. |
| 216 |
|
| 217 |
As promised, the print statements have been changed to use Python |
| 218 |
string formatting (lines :an:`1` and :an:`2`). The statements look |
| 219 |
complicated but the output is much nicer. |
| 220 |
|
| 221 |
.. literalinclude:: bankprograms/bank02.py |
| 222 |
|
| 223 |
|
| 224 |
The trace produced by the program is shown below. Again the |
| 225 |
simulation finishes before the ``400.0`` specified in the ``simulate`` |
| 226 |
call as it has run out of events. |
| 227 |
|
| 228 |
.. literalinclude:: bankprograms/bank02.out |
| 229 |
|
| 230 |
.. ------------------------------------------------------------- |
| 231 |
|
| 232 |
.. index:: bank03 |
| 233 |
|
| 234 |
Many Customers |
| 235 |
~~~~~~~~~~~~~~~~~~~ |
| 236 |
|
| 237 |
|
| 238 |
Another change will allow us to have more customers. As it is |
| 239 |
tedious to give a specially chosen name to each one, we will |
| 240 |
call them ``Customer00, Customer01,...`` and use a separate |
| 241 |
``Source`` class to create and activate them. To make things clearer |
| 242 |
we do not use the random numbers in this model. |
| 243 |
|
| 244 |
.. index:: Source of entities |
| 245 |
|
| 246 |
.. 6 1 :an:`1` |
| 247 |
9 2 :an:`2` |
| 248 |
11 :an:`3` |
| 249 |
13 :an:`4` |
| 250 |
21 :an:`5` |
| 251 |
32 :an:`6` |
| 252 |
33 :an:`7` |
| 253 |
|
| 254 |
The following listing shows the new program. Lines :an:`1` to :an:`4` |
| 255 |
define a ``Source`` class. Its PEM, here called ``generate``, is |
| 256 |
defined in lines :an:`2` to :an:`4`. This PEM has a couple of arguments: |
| 257 |
the ``number`` of customers to be generated and the Time Between |
| 258 |
Arrivals, ``TBA``. It consists of a loop that creates a sequence |
| 259 |
of numbered ``Customers`` from ``0`` to ``(number-1)``, inclusive. We |
| 260 |
create a customer and give it a name in line :an:`3`. It is |
| 261 |
then activated at the current simulation time (the final argument of |
| 262 |
the ``activate`` statement is missing so that the default value of |
| 263 |
``now()`` is used as the time). We also specify how long the customer |
| 264 |
is to stay in the bank. To keep it simple, all customers stay |
| 265 |
exactly ``12`` minutes. After each new customer is activated, the |
| 266 |
``Source`` holds for a fixed time (``yield hold,self,TBA``) |
| 267 |
before creating the next one (line :an:`4`). |
| 268 |
|
| 269 |
A ``Source``, ``s``, is created in line :an:`5` and activated at line |
| 270 |
:an:`6` where the number of customers to be generated is set to |
| 271 |
``maxNumber = 5`` and the interval between customers to ``ARRint = |
| 272 |
10.0``. Once started at time ``0.0`` it creates customers at intervals |
| 273 |
and each customer then operates independently of the others: |
| 274 |
|
| 275 |
|
| 276 |
.. literalinclude:: bankprograms/bank03.py |
| 277 |
|
| 278 |
|
| 279 |
The output is: |
| 280 |
|
| 281 |
.. literalinclude:: bankprograms/bank03.out |
| 282 |
|
| 283 |
|
| 284 |
.. ------------------------------------------------------------- |
| 285 |
|
| 286 |
Many Random Customers |
| 287 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 288 |
|
| 289 |
.. index:: bank06 |
| 290 |
|
| 291 |
|
| 292 |
We now extend this model to allow arrivals at random. In simulation this |
| 293 |
is usually interpreted as meaning that the times between customer |
| 294 |
arrivals are distributed as exponential random variates. There is |
| 295 |
little change in our program, we use a ``Source`` object, as before. |
| 296 |
|
| 297 |
.. |
| 298 |
14 :an:`1` |
| 299 |
15 :an:`2` |
| 300 |
33 :an:`3` |
| 301 |
|
| 302 |
The exponential random variate is generated in line :an:`1` with |
| 303 |
``meanTBA`` as the mean Time Between Arrivals and used in line |
| 304 |
:an:`2`. Note that this parameter is not exactly intuitive. As already |
| 305 |
mentioned, the Python ``expovariate`` method uses the *rate* of |
| 306 |
arrivals as the parameter not the average interval between them. The |
| 307 |
exponential delay between two arrivals gives pseudo-random |
| 308 |
arrivals. In this model the first customer arrives at time ``0.0``. |
| 309 |
|
| 310 |
The ``seed`` method is called to initialize the random number stream |
| 311 |
in the ``model`` routine (line :an:`3`). It is possible to leave this |
| 312 |
call out but if we wish to do serious comparisons of systems, we must |
| 313 |
have control over the random variates and therefore control over the |
| 314 |
seeds. Then we can run identical models with different seeds or |
| 315 |
different models with identical seeds. We provide the seeds as |
| 316 |
control parameters of the run. Here a seed is assigned in line :an:`3` |
| 317 |
but it is clear it could have been read in or manually entered on an |
| 318 |
input form. |
| 319 |
|
| 320 |
|
| 321 |
.. literalinclude:: bankprograms/bank06.py |
| 322 |
|
| 323 |
with the following output: |
| 324 |
|
| 325 |
.. literalinclude:: bankprograms/bank06.out |
| 326 |
|
| 327 |
.. --------------------------------------------------------------- |
| 328 |
|
| 329 |
.. index:: |
| 330 |
pair: Resource; queue |
| 331 |
|
| 332 |
A Service counter |
| 333 |
------------------ |
| 334 |
|
| 335 |
So far, the model has been more like an art gallery, the customers |
| 336 |
entering, looking around, and leaving. Now they are going to require |
| 337 |
service from the bank clerk. We extend the model to include a service |
| 338 |
counter which will be modelled as an object of SimPy's ``Resource`` |
| 339 |
class with a single resource unit. The actions of a ``Resource`` are |
| 340 |
simple: a customer ``requests`` a unit of the resource (a clerk). If |
| 341 |
one is free he gets service (and removes the unit). If there is no |
| 342 |
free clerk the customer joins the queue (managed by the resource |
| 343 |
object) until it is their turn to be served. As each customer |
| 344 |
completes service and ``releases`` the unit, the clerk can start |
| 345 |
serving the next in line. |
| 346 |
|
| 347 |
|
| 348 |
.. --------------------------------------------------------------- |
| 349 |
|
| 350 |
.. index:: bank07, Service counter |
| 351 |
|
| 352 |
One Service counter |
| 353 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 354 |
|
| 355 |
.. |
| 356 |
14 :an:`1` |
| 357 |
22 :an:`2` |
| 358 |
25 :an:`3` |
| 359 |
26 :an:`4` |
| 360 |
28 :an:`5` |
| 361 |
29 :an:`6` |
| 362 |
35 :an:`7` |
| 363 |
38 :an:`8` |
| 364 |
45 :an:`9` |
| 365 |
|
| 366 |
The service counter is created as a ``Resource`` (``k``) in |
| 367 |
line :an:`8`. This is provided as an argument to the ``Source`` (line :an:`9`) |
| 368 |
which, in turn, provides it to each customer it creates and activates |
| 369 |
(line :an:`1`). |
| 370 |
|
| 371 |
The actions involving the service counter, ``k``, in the customer's |
| 372 |
PEM are: |
| 373 |
|
| 374 |
- the ``yield request`` statement in line :an:`3`. If the server is |
| 375 |
free then the customer can start service immediately and the code |
| 376 |
moves on to line :an:`4`. If the server is busy, the customer is |
| 377 |
automatically queued by the Resource. When it eventually comes |
| 378 |
available the PEM moves on to line :an:`4`. |
| 379 |
|
| 380 |
- the ``yield hold`` statement in line :an:`5` where the operation of |
| 381 |
the service counter is modelled. Here the service time is a fixed |
| 382 |
``timeInBank``. During this period the customer is being served. |
| 383 |
|
| 384 |
- the ``yield release`` statement in line :an:`6`. The current |
| 385 |
customer completes service and the service counter becomes available |
| 386 |
for any remaining customers in the queue. |
| 387 |
|
| 388 |
Observe that the service counter is used with the pattern (``yield |
| 389 |
request..``; ``yield hold..``; ``yield release..``). |
| 390 |
|
| 391 |
To show the effect of the service counter on the activities of the |
| 392 |
customers, I have added line :an:`2` to record when the customer |
| 393 |
arrived and line :an:`4` to record the time between arrival in the |
| 394 |
bank and starting service. Line :an:`4` is *after* the ``yield |
| 395 |
request`` command and will be reached only when the request is |
| 396 |
satisfied. It is *before* the ``yield hold`` that corresponds to the |
| 397 |
start of service. The variable ``wait`` will record how long the |
| 398 |
customer waited and will be 0 if he received service at once. This |
| 399 |
technique of saving the arrival time in a variable is common. So the |
| 400 |
``print`` statement also prints out how long the customer waited in |
| 401 |
the bank before starting service. |
| 402 |
|
| 403 |
|
| 404 |
.. literalinclude:: bankprograms/bank07.py |
| 405 |
|
| 406 |
Examining the trace we see that the first, and last, customers get instant |
| 407 |
service but the others have to wait. We still only have five customers |
| 408 |
(line :an:`4`) so we cannot draw general conclusions. |
| 409 |
|
| 410 |
.. literalinclude:: bankprograms/bank07.out |
| 411 |
|
| 412 |
.. index:: |
| 413 |
Resource, Random service time, bank08 |
| 414 |
pair: M/M/1; queue |
| 415 |
|
| 416 |
.. --------------------------------------------------------------- |
| 417 |
|
| 418 |
A server with a random service time |
| 419 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 420 |
|
| 421 |
This is a simple change to the model in that we retain the single |
| 422 |
service counter but make the customer service time a random |
| 423 |
variable. As is traditional in the study of simple queues we first |
| 424 |
assume an exponential service time and set the mean to ``timeInBank``. |
| 425 |
|
| 426 |
.. |
| 427 |
26 :an:`1` |
| 428 |
27 :an:`2` |
| 429 |
33 :an:`3` |
| 430 |
37 :an:`4` |
| 431 |
|
| 432 |
The service time random variable, ``tib``, is generated in line |
| 433 |
:an:`1` and used in line :an:`2`. The argument to be used in the call |
| 434 |
of ``expovariate`` is not the mean of the distribution, |
| 435 |
``timeInBank``, but is the rate ``1/timeInBank``. |
| 436 |
|
| 437 |
We have also collected together a number of constants by defining a |
| 438 |
number of appropriate variables and giving them values. These are in |
| 439 |
lines :an:`3` to :an:`4`. |
| 440 |
|
| 441 |
.. literalinclude:: bankprograms/bank08.py |
| 442 |
|
| 443 |
And the output: |
| 444 |
|
| 445 |
.. literalinclude:: bankprograms/bank08.out |
| 446 |
|
| 447 |
This model with random arrivals and exponential service times is an |
| 448 |
example of an M/M/1 queue and could rather easily be solved |
| 449 |
analytically to calculate the steady-state mean waiting time and other |
| 450 |
operating characteristics. (But not so easily solved for its transient |
| 451 |
behavior.) |
| 452 |
|
| 453 |
.. --------------------------------------------------------------- |
| 454 |
|
| 455 |
Several Service Counters |
| 456 |
------------------------------------- |
| 457 |
|
| 458 |
|
| 459 |
When we introduce several counters we must decide on a queue |
| 460 |
discipline. Are customers going to make one queue or are they going to |
| 461 |
form separate queues in front of each counter? Then there are |
| 462 |
complications - will they be allowed to switch lines (jockey)? We |
| 463 |
first consider a single queue with several counters and later consider |
| 464 |
separate isolated queues. We will not look at jockeying. |
| 465 |
|
| 466 |
.. --------------------------------------------------------------- |
| 467 |
|
| 468 |
.. index:: Resource, several counters, bank09 |
| 469 |
|
| 470 |
Several Counters but a Single Queue |
| 471 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 472 |
|
| 473 |
|
| 474 |
Here we model a bank whose customers arrive randomly and are to be |
| 475 |
served at a group of counters, taking a random time for service, where |
| 476 |
we assume that waiting customers form a single first-in first-out |
| 477 |
queue. |
| 478 |
|
| 479 |
.. |
| 480 |
42 :an:`1` |
| 481 |
|
| 482 |
The *only* difference between this model and the single-server model |
| 483 |
is in line :an:`1`. We have provided two counters by increasing the |
| 484 |
capacity of the ``counter`` resource to 2. These *units* of the |
| 485 |
resource correspond to the two counters. Because both clerks cannot be |
| 486 |
called ``Karen``, we have used a general name of ``Clerk``. |
| 487 |
|
| 488 |
.. literalinclude:: bankprograms/bank09.py |
| 489 |
|
| 490 |
The waiting times in this model are very different from those for the |
| 491 |
single service counter. For example, none of the customers had to |
| 492 |
wait. But, again, we have observed too few customers to draw general |
| 493 |
conclusions. |
| 494 |
|
| 495 |
.. literalinclude:: bankprograms/bank09.out |
| 496 |
|
| 497 |
.. --------------------------------------------------------------- |
| 498 |
.. index:: |
| 499 |
pair: Several queues; Resource |
| 500 |
single: bank10 |
| 501 |
|
| 502 |
Several Counters with individual queues |
| 503 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 504 |
|
| 505 |
.. |
| 506 |
17 :an:`1` |
| 507 |
19 :an:`2` |
| 508 |
28 :an:`3` |
| 509 |
29 :an:`4` |
| 510 |
30 :an:`5` |
| 511 |
32 :an:`6` |
| 512 |
56 :an:`7` |
| 513 |
|
| 514 |
|
| 515 |
Each counter now has its own queue. The programming is more |
| 516 |
complicated because the customer has to decide which one to |
| 517 |
join. The obvious technique is to make each counter a separate |
| 518 |
resource and it is useful to make a list of resource objects (line |
| 519 |
:an:`7`). |
| 520 |
|
| 521 |
In practice, a customer will join the shortest queue. So we define |
| 522 |
a Python function, ``NoInSystem(R)`` (lines :an:`1` to :an:`2`) to |
| 523 |
return the sum of the number waiting and the number being served for |
| 524 |
a particular counter, ``R``. This function is used in line :an:`3` to |
| 525 |
list the numbers at each counter. It is then easy to find which |
| 526 |
counter the arriving customer should join. We have also modified the |
| 527 |
trace printout, line :an:`4` to display the state of the system when |
| 528 |
the customer arrives. We choose the shortest queue in lines |
| 529 |
:an:`5` to :an:`6` (using the variable ``choice``). |
| 530 |
|
| 531 |
The rest of the program is the same as before. |
| 532 |
|
| 533 |
.. literalinclude:: bankprograms/bank10.py |
| 534 |
|
| 535 |
The results show how the customers choose the counter with the |
| 536 |
smallest number. Unlucky ``Customer03`` who joins the wrong queue has |
| 537 |
to wait until ``Customer01`` finishes before his service can be |
| 538 |
started. There are, however, too few arrivals in these runs, limited |
| 539 |
as they are to five customers, to draw any general conclusions about |
| 540 |
the relative efficiencies of the two systems. |
| 541 |
|
| 542 |
.. literalinclude:: bankprograms/bank10.out |
| 543 |
|
| 544 |
.. --------------------------------------------------------------- |
| 545 |
|
| 546 |
.. index:: Monitors, Gathering statistics, statistics |
| 547 |
|
| 548 |
Monitors and Gathering Statistics |
| 549 |
------------------------------------- |
| 550 |
|
| 551 |
The traces of output that have been displayed so far are valuable for |
| 552 |
checking that the simulation is operating correctly but would become |
| 553 |
too much if we simulate a whole day. We do need to get results from |
| 554 |
our simulation to answer the original questions. What, then, is the |
| 555 |
best way to summarize the results? |
| 556 |
|
| 557 |
One way is to analyze the traces elsewhere, piping the trace output, |
| 558 |
or a modified version of it, into a *real* statistical program such as |
| 559 |
*R* for statistical analysis, or into a file for later examination by |
| 560 |
a spreadsheet. We do not have space to examine this thoroughly here. |
| 561 |
Another way of presenting the results is to provide graphical |
| 562 |
output. |
| 563 |
|
| 564 |
SimPy offers an easy way to gather a few simple statistics such as |
| 565 |
averages: the ``Monitor`` and ``Tally`` classes. The ``Monitor`` |
| 566 |
records the values of chosen variables as time series (but see the |
| 567 |
comments in `Final Remarks`_). |
| 568 |
|
| 569 |
|
| 570 |
.. ------------------------------------------------------------- |
| 571 |
|
| 572 |
.. index:: |
| 573 |
pair: Monitored; queue |
| 574 |
single: bank11 |
| 575 |
|
| 576 |
The Bank with a Monitor |
| 577 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 578 |
|
| 579 |
We now demonstrate a ``Monitor`` that records the average waiting |
| 580 |
times for our customers. We return to the system with random arrivals, |
| 581 |
random service times and a single queue and remove the old trace |
| 582 |
statements. In practice, we would make the printouts controlled by a |
| 583 |
variable, say, ``TRACE`` which is set in the experimental data (or |
| 584 |
read in as a program option - but that is a different story). This |
| 585 |
would aid in debugging and would not complicate the data analysis. We |
| 586 |
will run the simulations for many more arrivals. |
| 587 |
|
| 588 |
.. |
| 589 |
24 :an:`1` |
| 590 |
42 :an:`2` |
| 591 |
45 :an:`3` |
| 592 |
51 :an:`4` |
| 593 |
|
| 594 |
|
| 595 |
A Monitor, ``wM``, is created in line :an:`2`. It ``observes`` and |
| 596 |
records the waiting time mentioned in line :an:`1`. We run |
| 597 |
``maxNumber=50`` customers (in the call of ``generate`` in line |
| 598 |
:an:`3`) and have increased ``maxTime`` to ``1000`` minutes. Brief |
| 599 |
statistics are given by the Monitor methods ``count()`` and ``mean()`` |
| 600 |
in line :an:`4`. |
| 601 |
|
| 602 |
.. literalinclude:: bankprograms/bank11.py |
| 603 |
|
| 604 |
|
| 605 |
The average waiting time for 50 customers in this 2-counter system is |
| 606 |
more reliable (i.e., less subject to random simulation effects) than |
| 607 |
the times we measured before but it is still not sufficiently reliable |
| 608 |
for real-world decisions. We should also replicate the runs using |
| 609 |
different random number seeds. The result of this run (using Python 3.2) is: |
| 610 |
|
| 611 |
.. literalinclude:: bankprograms/bank11.out |
| 612 |
|
| 613 |
Result for Python 2.x is given in Appendix A. |
| 614 |
|
| 615 |
.. ------------------------------------------------------------- |
| 616 |
|
| 617 |
.. index:: |
| 618 |
single: Multiple runs, replications, bank12 |
| 619 |
single: Random Number Seed |
| 620 |
pair: model; function |
| 621 |
|
| 622 |
Multiple runs |
| 623 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 624 |
|
| 625 |
To get a number of independent measurements we must replicate the runs |
| 626 |
using different random number seeds. Each replication must be |
| 627 |
independent of previous ones so the Monitor and Resources must be |
| 628 |
redefined for each run. We can no longer allow them to be global |
| 629 |
objects as we have before. |
| 630 |
|
| 631 |
.. |
| 632 |
13 :an:`1` |
| 633 |
40 :an:`2` |
| 634 |
43 :an:`3` |
| 635 |
48 :an:`4` |
| 636 |
50 :an:`5` |
| 637 |
54 :an:`6` |
| 638 |
57 :an:`7` |
| 639 |
|
| 640 |
We will define a function, ``model`` with a parameter ``runSeed`` so |
| 641 |
that the random number seed can be different for different runs (lines |
| 642 |
:an:`2` to :an:`5`). The contents of the function are the same as the |
| 643 |
``Model/Experiment`` section in the previous program except for one |
| 644 |
vital change. |
| 645 |
|
| 646 |
This is required since the Monitor, ``wM``, is defined inside the |
| 647 |
``model`` function (line :an:`3`). A customer can no longer refer to |
| 648 |
it. In the spirit of quality computer programming we will pass ``wM`` |
| 649 |
as a function argument. Unfortunately we have to do this in two steps, |
| 650 |
first to the ``Source`` (line :an:`4`) and then from the ``Source`` to |
| 651 |
the ``Customer`` (line :an:`1`). |
| 652 |
|
| 653 |
``model()`` is run for four different random-number seeds to get a set |
| 654 |
of replications (lines :an:`6` to :an:`7`). |
| 655 |
|
| 656 |
.. literalinclude:: bankprograms/bank12.py |
| 657 |
|
| 658 |
The results show some variation. Remember, though, that the system is still |
| 659 |
only operating for 50 customers so the system may not be in |
| 660 |
steady-state. |
| 661 |
|
| 662 |
.. literalinclude:: bankprograms/bank12.out |
| 663 |
|
| 664 |
|
| 665 |
.. index:: |
| 666 |
GUI input, Graphical Output,Statistical Output |
| 667 |
Priorities and Reneging,Other forms of Resource Facilities |
| 668 |
Advanced synchronization/scheduling commands |
| 669 |
|
| 670 |
|
| 671 |
Final Remarks |
| 672 |
------------------------------------- |
| 673 |
|
| 674 |
This introduction is too long and the examples are getting |
| 675 |
longer. There is much more to say about simulation with *SimPy* but no |
| 676 |
space. I finish with a list of topics for further study: |
| 677 |
|
| 678 |
* **GUI input**. Graphical input of simulation parameters could be an |
| 679 |
advantage in some cases. *SimPy* allows this and programs using |
| 680 |
these facilities have been developed (see, for example, program |
| 681 |
``MM1.py`` in the examples in the *SimPy* distribution) |
| 682 |
|
| 683 |
* **Graphical Output**. Similarly, graphical output of results can |
| 684 |
also be of value, not least in debugging simulation programs and |
| 685 |
checking for steady-state conditions. SimPlot is useful here. |
| 686 |
|
| 687 |
* **Statistical Output**. The ``Monitor`` class is useful in |
| 688 |
presenting results but more powerful methods of analysis are often |
| 689 |
needed. One solution is to output a trace and read that into a |
| 690 |
large-scale statistical system such as *R*. |
| 691 |
|
| 692 |
* **Priorities and Reneging in queues**. *SimPy* allows processes to |
| 693 |
request units of resources under a priority queue discipline |
| 694 |
(preemptive or not). It also allows processes to renege from a queue. |
| 695 |
|
| 696 |
* **Other forms of Resource Facilities**. *SimPy* has two other |
| 697 |
resource structures: ``Levels`` to hold bulk commodities, and |
| 698 |
``Stores`` to contain an inventory of different object types. |
| 699 |
|
| 700 |
* **Advanced synchronization/scheduling commands**. *SimPy* allows |
| 701 |
process synchronization by events and signals. |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
Acknowledgements |
| 706 |
------------------------------------- |
| 707 |
|
| 708 |
I thank Klaus Muller, Bob Helmbold, Mukhlis Matti and other developers |
| 709 |
and users of SimPy for improving this document by sending their |
| 710 |
comments. I would be grateful for further suggestions or |
| 711 |
corrections. Please send them to: *vignaux* at |
| 712 |
*users.sourceforge.net*. |
| 713 |
|
| 714 |
References |
| 715 |
------------------------------------- |
| 716 |
|
| 717 |
- Python website: http://www.Python.org |
| 718 |
|
| 719 |
- SimPy website: http://sourceforge.net/projects/simpy |
| 720 |
|
| 721 |
|
| 722 |
Appendix A |
| 723 |
------------------------------------- |
| 724 |
|
| 725 |
With Python 3 the definition of expovariate changed. In some cases |
| 726 |
this was back ported to some distributions of Python 2.7. |
| 727 |
Because of this the output for the bank programs varies. This section |
| 728 |
just contains the older output. |
| 729 |
|
| 730 |
**A Customer arriving at a fixed time** |
| 731 |
|
| 732 |
.. literalinclude:: bankprograms/python2_out/bank01.out |
| 733 |
|
| 734 |
**A Customer arriving at random** |
| 735 |
|
| 736 |
.. literalinclude:: bankprograms/python2_out/bank05.out |
| 737 |
|
| 738 |
**More Customers** |
| 739 |
|
| 740 |
.. literalinclude:: bankprograms/python2_out/bank02.out |
| 741 |
|
| 742 |
**Many Customers** |
| 743 |
|
| 744 |
.. literalinclude:: bankprograms/python2_out/bank03.out |
| 745 |
|
| 746 |
**Many Random Customers** |
| 747 |
|
| 748 |
.. literalinclude:: bankprograms/python2_out/bank06.out |
| 749 |
|
| 750 |
**One Service Counter** |
| 751 |
|
| 752 |
.. literalinclude:: bankprograms/python2_out/bank07.out |
| 753 |
|
| 754 |
**A server with a random service time** |
| 755 |
|
| 756 |
.. literalinclude:: bankprograms/python2_out/bank08.out |
| 757 |
|
| 758 |
**Several Counters but a Single Queue** |
| 759 |
|
| 760 |
.. literalinclude:: bankprograms/python2_out/bank09.out |
| 761 |
|
| 762 |
**Several Counters with individual queues** |
| 763 |
|
| 764 |
.. literalinclude:: bankprograms/python2_out/bank10.out |
| 765 |
|
| 766 |
**The Bank with a Monitor** |
| 767 |
|
| 768 |
.. literalinclude:: bankprograms/python2_out/bank11.out |
| 769 |
|
| 770 |
**Multiple runs** |
| 771 |
|
| 772 |
.. literalinclude:: bankprograms/python2_out/bank12.out |
| 773 |
|
| 774 |
|
| 775 |
.. |
| 776 |
------------------------------------------------------ |
| 777 |
|
| 778 |
|
| 779 |
.. |
| 780 |
Local Variables: |
| 781 |
mode: rst |
| 782 |
indent-tabs-mode: nil |
| 783 |
sentence-end-double-space: t |
| 784 |
fill-column: 70 |
| 785 |
End: |