# The Bank (Object Oriented version)

## Introduction

SimPy is used to develop a simple simulation of a bank with a number of tellers. This Python package provides Processes to model active components such as messages, customers, trucks, and planes. It has three classes to model facilities where congestion might occur: Resources for ordinary queues, Levels for the supply of quantities of material, and Stores for collections of individual items. Only examples of Resources are described here. It also provides Monitors and Tallys to record data like queue lengths and delay times and to calculate simple averages. It uses the standard Python random package to generate random numbers.

Starting with SimPy 2.0 an object-oriented programmer’s interface was added to the package and it is this version that is described here. It is quite compatible with the procedural approach. The object-oriented interface, however, can support the process of developing and extending a simulation model better than the procedural approach.

SimPy can be obtained from: http://sourceforge.net/projects/simpy. This tutorial is best read with the SimPy Manual and CheatsheetOO at your side for reference.

Before attempting to use SimPy you should be familiar with the Python language. In particular you should be able to use classes. Python is free and available for most machine types. You can find out more about it at the Python web site. SimPy is compatible with Python version 2.3 and later.

## A single Customer

In this tutorial we model a simple bank with customers arriving at random. We develop the model step-by-step, starting out simply, and producing a running program at each stage. The programs we develop are available without line numbers and ready to go, in the `bankprograms_OO` directory. Please copy them, run them and improve them - and in the tradition of open-source software suggest your modifications to the SimPy users list. Object-orented versions of all the models are included in the same directory.

A simulation should always be developed to answer a specific question; in these models we investigate how changing the number of bank servers or tellers might affect the waiting time for customers.

### A Customer arriving at a fixed time

We first model a single customer who arrives at the bank for a visit, looks around at the decor for a time and then leaves. There is no queueing. First we will assume his arrival time and the time he spends in the bank are fixed.

Examine the following listing which is a complete runnable Python script, except for the line numbers. We use comments to divide the script up into sections. This makes for clarity later when the programs get more complicated. Line 1 is a normal Python documentation string; line 2 makes available the SimPy constructs needed for this model: the `Simulation` class, the `Process` class, and the `hold` verb.

We define a `Customer` class derived from the SimPy `Process` class. We create a `Customer` object, `c` who arrives at the bank at simulation time `5.0` and leaves after a fixed time of `10.0` minutes. The `Customer` class definition, lines 5-12, defines our customer class and has the required generator method (called `visit`) (line 9) having a `yield` statement (line 11)). Such a method is called a Process Execution Method (PEM) in SimPy.

The customer’s `visit` PEM, lines 9-12, models his activities. When he arrives (it will turn out to be a ‘he’ in this model), he will print out the simulation time, `self.sim.now()`, and his name (line 10). `self.sim` is a reference to the `BankModel` simulation object where this customer exists. Every `Process` instance is linked to the simulation in which it is created by assigning to its `sim` parameter when it is created (see line 19). The method `now()` can be used at any time in the simulation to find the current simulation time though it cannot be changed by the programmer. The customer’s name will be set when the customer is created later in the script (line 19).

He then stays in the bank for a fixed simulation time `timeInBank` (line 11). This is achieved by the `yield hold,self,timeInBank` statement. This is the first of the special simulation commands that `SimPy` offers.

After a simulation time of `timeInBank`, the program’s execution returns to the line after the `yield` statement, line 12. The customer then prints out the current simulation time and his name. This completes the declaration of the `Customer` class.

Though we do not do it here, it is also possible to define an `__init__()` method for a `Process` if you need to give the customer any attributes. Bear in mind that such an `__init__` method must first call `Process.__init__(self)` and can then initialize any instance variables needed.

Lines 6 to 21 define a class `BankModel`, composing a model of a bank from the `Simulation` class, a `Customer` class and the global experiment data. The definition `class BankModel(Simulation)` gives an instance of a `BankModel` all the attributes of class `Simulation`. (In OO terms, `BankModel` inherits from `Simulation`.) Any instance of `BankModel` is a `Simulation` instance. This gives a `BankModel` its own event list and thus its own time axis. Also, it allows a `BankModel` instance to activate processes and to start the execution of a simulation on its time axis.

Lines 17 to 21 define a `run` method; when called, it results in the execution of a `BankModel` instance, i.e. the performance of a simulation experiment. Line 18 initializes this simulation, i.e. it creates a new event list. L.19 creates a `Customer` object. The parameter assignment `sim = self` ties the customer instance to this and only this simulation. The customer does not exist outside this simulation. L.20 activates the customer’s `visit` process (PEM). Finally the call of `simulate(until=maxTime)` in line 24 starts the simulation. It will run until the simulation time is `maxTime` unless stopped beforehand either by the `stopSimulation()` command or by running out of events to execute (as will happen here). `maxTime` was set to `100.0` in line 25.

Note

If model classes like the``BankModel`` are to be given any other attributes, they must have an `__init__` method in which these attributes are assigned with the syntax `self.attrib1 = . . .`. Such an `__init__` method must first call `Simulation.__init__(self)` to also initialize the `Simulation` class from which the model inherits.

The simulation model is executed by line 32. `BankModel()` constructs the model, and `.run()` executes it. This is just a short form of:

    bM = BankModel()
    bM.run()

     1""" bank01_OO: The single non-random Customer """
     2from SimPy.Simulation import Simulation, Process, hold
     3
     4
     5## Model components -----------------------------
     6class Customer(Process):
     7    """ Customer arrives, looks around and leaves """
     8
     9    def visit(self, timeInBank):
    10        print("%2.1f %s  Here I am" % (self.sim.now(), self.name))
    11        yield hold, self, timeInBank
    12        print("%2.1f %s  I must leave" % (self.sim.now(), self.name))
    13
    14
    15## Model ----------------------------------------
    16class BankModel(Simulation):
    17    def run(self):
    18        """ PEM """
    19        c = Customer(name="Klaus", sim=self)
    20        self.activate(c, c.visit(timeInBank), at=tArrival)
    21        self.simulate(until=maxTime)
    22
    23## Experiment data ------------------------------
    24
    25maxTime = 100.0     # minutes
    26timeInBank = 10.0   # minutes
    27tArrival = 5.0      # minutes
    28
    29## Experiment -----------------------------------
    30
    31mymodel = BankModel()
    32mymodel.run()

The short trace printed out by the `print` statements shows the result. The program finishes at simulation time `15.0` because there are no further events to be executed. At the end of the `visit` routine, the customer has no more actions and no other objects or customers are active.

    5.0 Klaus  Here I am
    15.0 Klaus  I must leave

### A Customer arriving at random

Now we extend the model to allow our customer to arrive at a random simulated time though we will keep the time in the bank at 10.0, as before.

The change occurs in line 3 of the program and in lines 19, 21, 23, 31 and 35. In line 3 we import from the standard Python `random` module to give us `expovariate` to generate the random time of arrival. We also import the `seed` function to initialize the random number stream to allow control of the random numbers. The `run` method is given a parameter `aseed` for the initial seed (line 19) .In line 31 we provide an initial seed of `99999`. An exponential random variate, `t`, is generated in line 23. Note that the Python Random module’s `expovariate` function uses the rate (here, `1.0/tMeanArrival`) as the argument. The generated random variate, `t`, is used in line 24 as the `at` argument to the `activate` call. `tMeanArrival` is assigned a value of `5.0` minutes at line 31.

In line 35, the `BankModel` entity is generated and its `run` function called with parameter assignment `aseed=seedVal`.

     1""" bank05_OO: The single Random Customer """
     2from SimPy.Simulation import Simulation, Process, hold
     3from random import expovariate, seed
     4
     5## Model components ------------------------
     6
     7class Customer(Process):
     8    """ Customer arrives at a random time,
     9        looks around  and then leaves """
    10
    11    def visit(self, timeInBank):
    12        print("%f %s Here I am" % (self.sim.now(), self.name))
    13        yield hold, self, timeInBank
    14        print("%f %s I must leave" % (self.sim.now(), self.name))
    15
    16## Model -----------------------------------
    17
    18class BankModel(Simulation):
    19    def run(self, aseed):
    20        """ PEM """
    21        seed(aseed)
    22        c = Customer(name="Klaus", sim=self)
    23        t = expovariate(1.0 / tMeanArrival)
    24        self.activate(c, c.visit(timeInBank), at=t)
    25        self.simulate(until=maxTime)
    26
    27## Experiment data -------------------------
    28
    29maxTime = 100.0     # minutes
    30timeInBank = 10.0   # minutes
    31tMeanArrival = 5.0  # minutes
    32seedVal = 99999
    33
    34## Experiment ------------------------------
    35
    36mymodel = BankModel()
    37mymodel.run(aseed=seedVal)

The result is shown below. The customer now arrives at time 10.5809. Changing the seed value would change that time.

    0.641954 Klaus Here I am
    10.641954 Klaus I must leave

The display looks pretty untidy. In the next example I will try and make it tidier.

## More Customers

Our simulation does little so far. To consider a simulation with several customers we return to the simple deterministic model and add more `Customers`.

The program is almost as easy as the first example (A Customer arriving at a fixed time). The main change is in lines 19-24 where we create, name, and activate three customers. We also increase the maximum simulation time to `400` (line 29 and referred to in line 25). Observe that we need only one definition of the `Customer` class and create several objects of that class. These will act quite independently in this model.

Each customer stays for a different `timeInBank` so, instead of setting a common value for this we set it for each customer. The customers are started at different times (using `at=`). `Tony's` activation time occurs before `Klaus's`, so `Tony` will arrive first even though his activation statement appears later in the script.

As promised, the print statements have been changed to use Python string formatting (lines 10 and 12). The statements look complicated but the output is much nicer.

     1""" bank02_OO: More Customers """
     2from SimPy.Simulation import Simulation, Process, hold
     3
     4
     5## Model components ------------------------
     6class Customer(Process):
     7    """ Customer arrives, looks around and leaves """
     8
     9    def visit(self, timeInBank=0):
    10        print("%7.4f %s: Here I am" % (self.sim.now(), self.name))
    11        yield hold, self, timeInBank
    12        print("%7.4f %s: I must leave" % (self.sim.now(), self.name))
    13
    14
    15## Model -----------------------------------
    16class BankModel(Simulation):
    17    def run(self):
    18        """ PEM """
    19        c1 = Customer(name="Klaus", sim=self)
    20        self.activate(c1, c1.visit(timeInBank=10.0), at=5.0)
    21        c2 = Customer(name="Tony", sim=self)
    22        self.activate(c2, c2.visit(timeInBank=7.0), at=2.0)
    23        c3 = Customer(name="Evelyn", sim=self)
    24        self.activate(c3, c3.visit(timeInBank=20.0), at=12.0)
    25        self.simulate(until=maxTime)
    26
    27## Experiment data -------------------------
    28
    29maxTime = 400.0  # minutes
    30
    31## Experiment ------------------------------
    32
    33mymodel = BankModel()
    34mymodel.run()
    35

The trace produced by the program is shown below. Again the simulation finishes before the `400.0` specified in the `simulate` call.

    1 2.0000 Tony: Here I am
    2 5.0000 Klaus: Here I am
    3 9.0000 Tony: I must leave
    412.0000 Evelyn: Here I am
    515.0000 Klaus: I must leave
    632.0000 Evelyn: I must leave

### Many Customers

Another change will allow us to have more customers. As it is tedious to give a specially chosen name to each one, we will call them `Customer00, Customer01,...` and use a separate `Source` class to create and activate them. To make things clearer we do not use the random numbers in this model.

The following listing shows the new program. Lines 6-13 define a `Source` class. Its PEM, here called `generate`, is defined in lines 9-13. This PEM has a couple of arguments: the `number` of customers to be generated and the Time Between Arrivals, `TBA`. It consists of a loop that creates a stream of numbered `Customers` from `0` to `(number-1)`, inclusive. We create a customer and give it a name in line 11. The parameter assignment `sim = self.sim` ties the customers to the `BankModel` to which the `Source` belongs. The customer is then activated at the current simulation time (the final argument of the `activate` statement is missing so that the default value of `self.sim.now()`, the current simulation time for the instance of `BankModel`, is used as the time; here, it is `0.0`). We also specify how long the customer is to stay in the bank. To keep it simple, all customers stay exactly `12` minutes. After each new customer is activated, the `Source` holds for a fixed time (`yield hold,self,TBA`) before creating the next one (line 13).

`class BankModel(Simulation)` (line 24) provides a `run` method which executes this model consisting of a customer source and the global data. As `BankModel` inherits from `Simulation`, it has its own event list which gets initialized as empty in line 26.

A `Source`, `s`, is created in line 27 and activated at line 28 where the number of customers to be generated is set to `maxNumber = 5` and the interval between customers to `ARRint = 10.0`. The parameter assignment `sim = self` links the `Source` process to this `BankModel` instance. Once started at time `0.0`, `s` creates customers at intervals and each customer then operates independently of the others.

In line 40, a `BankModel` object is created and its `run` method executed:

     1""" bank03_OO: Many non-random Customers """
     2from SimPy.Simulation import Simulation, Process, hold
     3
     4
     5## Model components ------------------------
     6class Source(Process):
     7    """ Source generates customers regularly """
     8
     9    def generate(self, number, TBA):
    10        for i in range(number):
    11            c = Customer(name="Customer%02d" % (i), sim=self.sim)
    12            self.sim.activate(c, c.visit(timeInBank=12.0))
    13            yield hold, self, TBA
    14
    15
    16class Customer(Process):
    17    """ Customer arrives, looks around and leaves """
    18
    19    def visit(self, timeInBank):
    20        print("%7.4f %s: Here I am" % (self.sim.now(), self.name))
    21        yield hold, self, timeInBank
    22        print("%7.4f %s: I must leave" % (self.sim.now(), self.name))
    23
    24
    25## Model -----------------------------------
    26class BankModel(Simulation):
    27    def run(self):
    28        """ PEM """
    29        s = Source(sim=self)
    30        self.activate(s, s.generate(number=maxNumber,
    31                      TBA=ARRint), at=0.0)
    32        self.simulate(until=maxTime)
    33
    34## Experiment data -------------------------
    35
    36maxNumber = 5
    37maxTime = 400.0  # minutes
    38ARRint = 10.0    # time between arrivals, minutes
    39
    40## Experiment ------------------------------
    41
    42mymodel = BankModel()
    43mymodel.run()

The output is:

     1 0.0000 Customer00: Here I am
     210.0000 Customer01: Here I am
     312.0000 Customer00: I must leave
     420.0000 Customer02: Here I am
     522.0000 Customer01: I must leave
     630.0000 Customer03: Here I am
     732.0000 Customer02: I must leave
     840.0000 Customer04: Here I am
     942.0000 Customer03: I must leave
    1052.0000 Customer04: I must leave

### Many Random Customers

We now extend this model to allow arrivals at random. In simulation this is usually interpreted as meaning that the times between customer arrivals are distributed as exponential random variates. There is little change in our program, we use a `Source` object, as before.

The exponential random variate is generated in line 14 with `meanTBA` as the mean Time Between Arrivals and used in line 15. Note that this parameter is not exactly intuitive. As already mentioned, the Python `expovariate` method uses the rate of arrivals as the parameter not the average interval between them. The exponential delay between two arrivals gives pseudo-random arrivals. In this model the first customer arrives at time `0.0`.

The `seed` method is called to initialize the random number stream in the `run` routine of `BankModel` (line 30). It uses the value provided by parameter `aseed`. It is possible to leave this call out but if we wish to do serious comparisons of systems, we must have control over the random variates and therefore control over the seeds. Then we can run identical models with different seeds or different models with identical seeds. We provide the seeds as control parameters of the run. Here a seed is assigned in line 41 but it is clear it could have been read in or manually entered on an input form.

The `BankModel` is generated in line 45 and its `run` method called with the seed value as parameter.

     1""" bank06: Many Random Customers """
     2from SimPy.Simulation import Simulation, Process, hold
     3from random import expovariate, seed
     4
     5
     6## Model components ------------------------
     7class Source(Process):
     8    """ Source generates customers at random """
     9
    10    def generate(self, number, meanTBA):
    11        for i in range(number):
    12            c = Customer(name = "Customer%02d" % (i), sim=self.sim)
    13            self.sim.activate(c, c.visit(timeInBank=12.0))
    14            t = expovariate(1.0 / meanTBA)
    15            yield hold, self, t
    16
    17class Customer(Process):
    18    """ Customer arrives, looks round and leaves """
    19
    20    def visit(self, timeInBank=0):
    21        print("%7.4f %s: Here I am" % (self.sim.now(), self.name))
    22        yield hold, self, timeInBank
    23        print("%7.4f %s: I must leave" % (self.sim.now(), self.name))
    24
    25
    26## Model -----------------------------------
    27class BankModel(Simulation):
    28    def run(self, aseed):
    29        """ PEM """
    30        seed(aseed)
    31        s = Source(name='Source', sim=self)
    32        self.activate(s, s.generate(number=maxNumber,
    33                      meanTBA=ARRint), at=0.0)
    34        self.simulate(until=maxTime)
    35
    36## Experiment data -------------------------
    37
    38maxNumber = 5
    39maxTime = 400.0  # minutes
    40ARRint = 10.0    # mean arrival interval, minutes
    41seedVal = 99999
    42
    43## Experiment ------------------------------
    44
    45mymodel = BankModel()
    46mymodel.run(aseed=seedVal)
    47

This generates the following output:

     1 0.0000 Customer00: Here I am
     2 1.2839 Customer01: Here I am
     3 4.9842 Customer02: Here I am
     412.0000 Customer00: I must leave
     513.2839 Customer01: I must leave
     616.9842 Customer02: I must leave
     735.5432 Customer03: Here I am
     847.5432 Customer03: I must leave
     948.9918 Customer04: Here I am
    1060.9918 Customer04: I must leave

## A Service counter

So far, the model bank has been more like an art gallery, the customers entering, looking around, and leaving. Now they are going to require service from the bank clerk. We extend the model to include a service counter which will be modelled as an object of SimPy’s `Resource` class with a single resource unit. The actions of a `Resource` are simple: a customer `requests` a unit of the resource (a clerk). If one is free he gets service (and removes the unit, i.e., makes it busy). If there is no free clerk the customer joins the queue (managed by the resource object) until it is his turn to be served. As each customer completes service and `releases` the unit, the clerk automatically starts serving the next in line. This is done by reactivating that customer’s process where it had been blocked.

### One Service counter

As this model is built with the `Resource` class from `SimPy.Simulation`, it and the related `request` and `release` verbs are mported, in addition to the imports made in the previous programs (line 2).

The service counter is created as a `Resource` attribute `self.k` of the `BankModel` (line 39). The resource exists in the `BankModel`, and this is indicated by the parameter assignment `sim = self`. The `Source` PEM `generate` can access this attribute by `self.sim.k`, its `BankModel`’s resource attribute (line 14).

The actions involving the `Counter` referred to by the parameter `res` in the customer’s PEM are:

- the `yield request` statement in line 25. If the server is free then the customer can start service immediately and the code moves on to line 26. If the server is busy, the customer is automatically queued by the Resource. When it eventually comes available the PEM moves on to line 26.

- the `yield hold` statement in line 28 where the operation of the service counter is modelled. Here the service time is a fixed `timeInBank`. During this period the customer is being served and the resource (the counter) is busy.

- the `yield release` statement in line 29. The current customer completes service and the service counter becomes available for any remaining customers in the queue.

Observe that the service counter is used with the pattern (`yield request..`; `yield hold..`; `yield release..`).

To show the effect of the service counter on the activities of the customers, I have added line 22 to record when the customer arrived and line 26 to record the time between arrival in the bank and starting service. Line 26 is after the `yield request` command and will be reached only when the request is satisfied. It is before the `yield hold` that corresponds to the start of service. The variable `wait` will record how long the customer waited and will be 0 if he received service at once. This technique of saving the arrival time in a variable is common. So the `print` statement also prints out how long the customer waited in the bank before starting service.

     1""" bank07_OO: One Counter,random arrivals """
     2from SimPy.Simulation import Simulation, Process, hold, Resource, request, release
     3from random import expovariate, seed
     4
     5## Model components ------------------------
     6
     7class Source(Process):
     8    """ Source generates customers randomly """
     9
    10    def generate(self, number, meanTBA):
    11        for i in range(number):
    12            c = Customer(name="Customer%02d" % (i), sim=self.sim)
    13            self.sim.activate(c, c.visit(timeInBank=12.0,
    14                               res=self.sim.k))
    15            t = expovariate(1.0 / meanTBA)
    16            yield hold, self, t
    17
    18
    19class Customer(Process):
    20    """ Customer arrives, is served and  leaves """
    21
    22    def visit(self, timeInBank=0, res=None):
    23        arrive = self.sim.now()       # arrival time
    24        print("%8.3f %s: Here I am" % (self.sim.now(), self.name))
    25
    26        yield request, self, res
    27        wait = self.sim.now() - arrive  # waiting time
    28        print("%8.3f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
    29        yield hold, self, timeInBank
    30        yield release, self, res
    31
    32        print("%8.3f %s: Finished" % (self.sim.now(), self.name))
    33
    34## Model -----------------------------------
    35
    36class BankModel(Simulation):
    37    def run(self, aseed):
    38        """ PEM """
    39        seed(aseed)
    40        self.k = Resource(name="Counter", unitName="Clerk", sim=self)
    41        s = Source('Source', sim=self)
    42        self.activate(s, s.generate(number=maxNumber, meanTBA=ARRint), at=0.0)
    43        self.simulate(until=maxTime)
    44
    45## Experiment data -------------------------
    46
    47maxNumber = 5
    48maxTime = 400.0  # minutes
    49ARRint = 10.0    # mean, minutes
    50seedVal = 99999
    51
    52## Experiment ------------------------------
    53
    54mymodel = BankModel()
    55mymodel.run(aseed=seedVal)

Examining the trace we see that the first two customers get instant service but the others have to wait. We still only have five customers (line 44) so we cannot draw general conclusions.

     1   0.000 Customer00: Here I am
     2   0.000 Customer00: Waited  0.000
     3   1.284 Customer01: Here I am
     4   4.984 Customer02: Here I am
     5  12.000 Customer00: Finished
     6  12.000 Customer01: Waited 10.716
     7  24.000 Customer01: Finished
     8  24.000 Customer02: Waited 19.016
     9  35.543 Customer03: Here I am
    10  36.000 Customer02: Finished
    11  36.000 Customer03: Waited  0.457
    12  48.000 Customer03: Finished
    13  48.992 Customer04: Here I am
    14  48.992 Customer04: Waited  0.000
    15  60.992 Customer04: Finished

### A server with a random service time

This is a simple change to the model in that we retain the single service counter but make the customer service time a random variable. As is traditional in the study of simple queues we first assume an exponential service time and set the mean to `timeInBank`.

The service time random variable, `tib`, is generated in line 26 and used in line 27. The argument to be used in the call of `expovariate` is not the mean of the distribution, `timeInBank`, but is the rate `1.0/timeInBank`.

We have put together the exeriment data by defining a number of appropriate variables and giving them values. These are in lines 44 to 48.

     1""" bank08_OO: A counter with a random service time """
     2from SimPy.Simulation import Simulation, Process, Resource, hold, request, release
     3from random import expovariate, seed
     4
     5
     6## Model components ------------------------
     7
     8class Source(Process):
     9    """ Source generates customers randomly """
    10
    11    def generate(self, number, meanTBA):
    12        for i in range(number):
    13            c = Customer(name="Customer%02d" % (i), sim=self.sim)
    14            self.sim.activate(c, c.visit(b=self.sim.k))
    15            t = expovariate(1.0 / meanTBA)
    16            yield hold, self, t
    17
    18
    19class Customer(Process):
    20    """ Customer arrives, is served and leaves """
    21
    22    def visit(self, b):
    23        arrive = self.sim.now()
    24        print("%8.4f %s: Here I am     " % (self.sim.now(), self.name))
    25        yield request, self, b
    26        wait = self.sim.now() - arrive
    27        print("%8.4f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
    28        tib = expovariate(1.0 / timeInBank)
    29        yield hold, self, tib
    30        yield release, self, b
    31        print("%8.4f %s: Finished      " % (self.sim.now(), self.name))
    32
    33## Model -----------------------------------
    34
    35class BankModel(Simulation):
    36    def run(self, aseed):
    37        """ PEM """
    38        seed(aseed)
    39        self.k = Resource(name="Counter", unitName="Clerk", sim=self)
    40        s = Source('Source', sim=self)
    41        self.activate(s, s.generate(number=maxNumber, meanTBA=ARRint), at=0.0)
    42        self.simulate(until=maxTime)
    43
    44## Experiment data -------------------------
    45
    46maxNumber = 5
    47maxTime = 400.0    # minutes
    48timeInBank = 12.0  # mean, minutes
    49ARRint = 10.0      # mean, minutes
    50seedVal = 99999
    51
    52## Experiment ------------------------------
    53
    54mymodel = BankModel()
    55mymodel.run(aseed=seedVal)
    56

And the output:

     1  0.0000 Customer00: Here I am
     2  0.0000 Customer00: Waited  0.000
     3  1.2839 Customer01: Here I am
     4  4.4403 Customer00: Finished
     5  4.4403 Customer01: Waited  3.156
     6 20.5786 Customer01: Finished
     7 31.8430 Customer02: Here I am
     8 31.8430 Customer02: Waited  0.000
     9 34.5594 Customer02: Finished
    10 36.2308 Customer03: Here I am
    11 36.2308 Customer03: Waited  0.000
    12 41.4313 Customer04: Here I am
    13 67.1315 Customer03: Finished
    14 67.1315 Customer04: Waited 25.700
    15 87.9241 Customer04: Finished

This model with random arrivals and exponential service times is an example of an M/M/1 queue and could rather easily be solved analytically to calculate the steady-state mean waiting time and other operating characteristics. (But not so easily solved for its transient behavior.)

## Several Service Counters

When we introduce several counters we must decide on a queue discipline. Are customers going to make one queue or are they going to form separate queues in front of each counter? Then there are complications - will they be allowed to switch lines (jockey)? We first consider a single queue with several counters and later consider separate isolated queues. We will not look at jockeying.

### Several Counters but a Single Queue

Here we model a bank whose customers arrive randomly and are to be served at a group of counters, taking a random time for service, where we assume that waiting customers form a single first-in first-out queue.

The only difference between this model and the single-server model is in line 37. We have provided two counters by increasing the capacity of the `counter` resource to 2. This value is set in line 50 (`Nc = 2`). These units of the resource correspond to the two counters. Because both clerks cannot be called `Karen`, we have used a general name of `Clerk` as resource unit.

     1""" bank09_OO: Several Counters but a Single Queue """
     2from SimPy.Simulation import Simulation, Process, Resource, hold, request, release
     3from random import expovariate, seed
     4
     5## Model components ------------------------
     6
     7class Source(Process):
     8    """ Source generates customers randomly """
     9
    10    def generate(self, number, meanTBA):
    11        for i in range(number):
    12            c = Customer(name="Customer%02d" % (i), sim=self.sim)
    13            self.sim.activate(c, c.visit(b=self.sim.k))
    14            t = expovariate(1.0 / meanTBA)
    15            yield hold, self, t
    16
    17
    18class Customer(Process):
    19    """ Customer arrives, is served and leaves """
    20
    21    def visit(self, b):
    22        arrive = self.sim.now()
    23        print("%8.4f %s: Here I am     " % (self.sim.now(), self.name))
    24        yield request, self, b
    25        wait = self.sim.now() - arrive
    26        print("%8.4f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
    27        tib = expovariate(1.0 / timeInBank)
    28        yield hold, self, tib
    29        yield release, self, b
    30        print("%8.4f %s: Finished      " % (self.sim.now(), self.name))
    31
    32## Model -----------------------------------
    33
    34class BankModel(Simulation):
    35    def run(self, aseed):
    36        """ PEM """
    37        seed(aseed)
    38        self.k = Resource(capacity=Nc, name="Counter", unitName="Clerk",
    39                     sim=self)
    40        s = Source('Source', sim=self)
    41        self.activate(s, s.generate(number=maxNumber, meanTBA=ARRint), at=0.0)
    42        self.simulate(until=maxTime)
    43
    44## Experiment data -------------------------
    45
    46maxNumber = 5      # of customers
    47maxTime = 400.0    # minutes
    48timeInBank = 12.0  # mean, minutes
    49ARRint = 10.0      # mean, minutes
    50Nc = 2             # of clerks/counters
    51seedVal = 99999
    52
    53## Experiment ------------------------------
    54mymodel = BankModel()
    55mymodel.run(aseed=seedVal)

The waiting times in this model are much shorter than those for the single service counter. For example, the waiting time for `Customer02` has been reduced from `51.213` to `12.581` minutes. Again we have too few customers processed to draw general conclusions.

     1  0.0000 Customer00: Here I am
     2  0.0000 Customer00: Waited  0.000
     3  1.2839 Customer01: Here I am
     4  1.2839 Customer01: Waited  0.000
     5  4.4403 Customer00: Finished
     6 17.4222 Customer01: Finished
     7 31.8430 Customer02: Here I am
     8 31.8430 Customer02: Waited  0.000
     9 34.5594 Customer02: Finished
    10 36.2308 Customer03: Here I am
    11 36.2308 Customer03: Waited  0.000
    12 41.4313 Customer04: Here I am
    13 41.4313 Customer04: Waited  0.000
    14 62.2239 Customer04: Finished
    15 67.1315 Customer03: Finished

### Several Counters with individual queues

Each counter is now assumed to have its own queue. The programming is more complicated because the customer has to decide which queue to join. The obvious technique is to make each counter a separate resource and it is useful to make a list of resource objects (line 56).

In practice, a customer will join the shortest queue. So we define the Python function, `NoInSystem(R)` (lines 17-19) which returns the sum of the number waiting and the number being served for a particular counter, `R`. This function is used in line 28 to list the numbers at each counter. It is then easy to find which counter the arriving customer should join. We have also modified the trace printout, line 29 to display the state of the system when the customer arrives. We choose the shortest queue in lines 30-33 (the variable `choice`).

The rest of the program is the same as before.

     1""" bank10_OO: Several Counters with individual queues"""
     2from SimPy.Simulation import Simulation, Process, Resource, hold, request, release
     3from random import expovariate, seed
     4
     5## Model components ------------------------
     6
     7class Source(Process):
     8    """ Source generates customers randomly"""
     9
    10    def generate(self, number, interval):
    11        for i in range(number):
    12            c = Customer(name="Customer%02d" % (i,), sim=self.sim)
    13            self.sim.activate(c, c.visit(counters=self.sim.kk))
    14            t = expovariate(1.0 / interval)
    15            yield hold, self, t
    16
    17def NoInSystem(R):
    18    """ Total number of customers in the resource R"""
    19    return (len(R.waitQ) + len(R.activeQ))
    20
    21
    22class Customer(Process):
    23    """ Customer arrives, chooses the shortest queue
    24        is served and leaves
    25    """
    26
    27    def visit(self, counters):
    28        arrive = self.sim.now()
    29        Qlength = [NoInSystem(counters[i]) for i in range(Nc)]
    30        print("%7.4f %s: Here I am. %s" % (self.sim.now(), self.name, Qlength))
    31        for i in range(Nc):
    32            if Qlength[i] == 0 or Qlength[i] == min(Qlength):
    33                choice = i  # the chosen queue number
    34                break
    35
    36        yield request, self, counters[choice]
    37        wait = self.sim.now() - arrive
    38        print("%7.4f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
    39        tib = expovariate(1.0 / timeInBank)
    40        yield hold, self, tib
    41        yield release, self, counters[choice]
    42
    43        print("%7.4f %s: Finished" % (self.sim.now(), self.name))
    44
    45
    46## Model -----------------------------------
    47
    48class BankModel(Simulation):
    49    def run(self, aseed):
    50        """ PEM """
    51        seed(aseed)
    52        self.kk = [Resource(name="Clerk0", sim=self),
    53              Resource(name="Clerk1", sim=self)]
    54        s = Source('Source', sim=self)
    55        self.activate(s, s.generate(number=maxNumber, interval=ARRint), at=0.0)
    56        self.simulate(until=maxTime)
    57
    58## Experiment data -------------------------
    59
    60maxNumber = 5
    61maxTime = 400.0    # minutes
    62timeInBank = 12.0  # mean, minutes
    63ARRint = 10.0      # mean, minutes
    64Nc = 2             # number of counters
    65seedVal = 787878
    66
    67## Experiment ------------------------------
    68
    69mymodel = BankModel()
    70mymodel.run(aseed=seedVal)
    71

The results show how the customers choose the counter with the smallest number. Unlucky `Customer02` who joins the wrong queue has to wait until `Customer00` finishes at time `55.067`. There are, however, too few arrivals in these runs, limited as they are to five customers, to draw any general conclusions about the relative efficiencies of the two systems.

     1 0.0000 Customer00: Here I am. [0, 0]
     2 0.0000 Customer00: Waited  0.000
     3 9.7519 Customer00: Finished
     412.0829 Customer01: Here I am. [0, 0]
     512.0829 Customer01: Waited  0.000
     625.9167 Customer02: Here I am. [1, 0]
     725.9167 Customer02: Waited  0.000
     838.2349 Customer03: Here I am. [1, 1]
     940.4032 Customer04: Here I am. [2, 1]
    1043.0677 Customer02: Finished
    1143.0677 Customer04: Waited  2.664
    1244.0242 Customer01: Finished
    1344.0242 Customer03: Waited  5.789
    1460.1271 Customer03: Finished
    1570.2500 Customer04: Finished

## Monitors and Gathering Statistics

The traces of output that have been displayed so far are valuable for checking that the simulation is operating correctly but would become too much if we simulate a whole day. We do need to get results from our simulation to answer the original questions. What, then, is the best way to summarize the results?

One way is to analyze the traces elsewhere, piping the trace output, or a modified version of it, into a real statistical program such as R for statistical analysis, or into a file for later examination by a spreadsheet. We do not have space to examine this thoroughly here. Another way of presenting the results is to provide graphical output.

SimPy offers an easy way to gather a few simple statistics such as averages: the `Monitor` and `Tally` classes. The `Monitor` records the values of chosen variables as time series. (but see the comments in Final Remarks).

### The Bank with a Monitor

We now demonstrate a `Monitor` that records the average waiting times for our customers. We return to the system with random arrivals, random service times and a single queue and remove the old trace statements. In practice, we would make the printouts controlled by a variable, say, `TRACE` which is set in the experimental data (or read in as a program option - but that is a different story). This would aid in debugging and would not complicate the data analysis. We will run the simulations for many more arrivals.

In addition to the imports in the programs shown before, we now have to import the `Monitor` class (line 2).

A Monitor, `wM`, is created in line 37. We make the monitor an attribute of the `BankModel` by the assignment to `self.wM`. The monitor `observes` the waiting time mentioned in line 25. As the monitor is an attribute of the `BankModel` to which the customer belongs, `self.sim.wM` can refere to it. We run `maxNumber = 50` customers (in the call of `generate` in line 39) and have increased `maxTime` to `1000.0` minutes.

     1""" bank11: The bank with a Monitor"""
     2from SimPy.Simulation import Simulation, Process, Resource, Monitor, hold,\
     3                               request, release
     4from random import expovariate, seed
     5
     6## Model components ------------------------
     7
     8class Source(Process):
     9    """ Source generates customers randomly"""
    10
    11    def generate(self, number, interval):
    12        for i in range(number):
    13            c = Customer(name="Customer%02d" % (i), sim=self.sim)
    14            self.sim.activate(c, c.visit(b=self.sim.k))
    15            t = expovariate(1.0 / interval)
    16            yield hold, self, t
    17
    18
    19class Customer(Process):
    20    """ Customer arrives, is served and leaves """
    21
    22    def visit(self, b):
    23        arrive = self.sim.now()
    24        yield request, self, b
    25        wait = self.sim.now() - arrive
    26        self.sim.wM.observe(wait)
    27        tib = expovariate(1.0 / timeInBank)
    28        yield hold, self, tib
    29        yield release, self, b
    30
    31## Model -----------------------------------
    32
    33class BankModel(Simulation):
    34    def run(self, aseed):
    35        """ PEM """
    36        seed(aseed)
    37        self.k = Resource(capacity=Nc, name="Clerk", sim=self)
    38        self.wM = Monitor(sim=self)
    39        s = Source('Source', sim=self)
    40        self.activate(s, s.generate(number=maxNumber, interval=ARRint), at=0.0)
    41        self.simulate(until=maxTime)
    42
    43## Experiment data -------------------------
    44
    45maxNumber = 50
    46maxTime = 1000.0   # minutes
    47timeInBank = 12.0  # mean, minutes
    48ARRint = 10.0      # mean, minutes
    49Nc = 2             # number of counters
    50seedVal = 99999
    51
    52## Experiment   -----------------------------
    53
    54experi = BankModel()
    55experi.run(aseed=seedVal)
    56
    57## Result  ----------------------------------
    58
    59result = experi.wM.count(), experi.wM.mean()
    60print("Average wait for %3d completions was %5.3f minutes." % result)

In previous programs, we have generated the `BankModel` anonymously. Here, we do it differently: we assign the `BankModel` object to the variable `experi` (line 53). This way, we can reference its monitor attribute by `experi.wM` (line 58). The average waiting time for 50 customers in this 2-counter system is more reliable (i.e., less subject to random simulation effects) than the times we measured before but it is still not sufficiently reliable for real-world decisions. We should also replicate the runs using different random number seeds. The result of this run is:

    Average wait for  50 completions was 8.941 minutes.

### Multiple runs

To get a number of independent measurements we must replicate the runs using different random number seeds. Each replication must be independent of previous ones so the Monitor and Resources must be nely generated for each run.

`model()` is run for several different random-number seeds to get a set of replications (lines 55-58). The seeds are stored in a list `seedVals` (line 50). The `for` loop walks through this list and runs the model’s `run` method for each entry.

Note that the `bankModel` is only generated once (line 54). This is sufficient, as the `run` method freshly generates an empty event list, a new counter resource, a new monitor, and a new source. This way, all iterations are independent of each other.

     1""" bank12_OO: Multiple runs of the bank with a Monitor"""
     2from SimPy.Simulation import Simulation, Process, Resource, Monitor, hold,\
     3                               request, release
     4from random import expovariate, seed
     5
     6## Model components ------------------------
     7
     8class Source(Process):
     9    """ Source generates customers randomly"""
    10
    11    def generate(self, number, interval):
    12        for i in range(number):
    13            c = Customer(name="Customer%02d" % (i), sim=self.sim)
    14            self.sim.activate(c, c.visit(b=self.sim.k))
    15            t = expovariate(1.0 / interval)
    16            yield hold, self, t
    17
    18
    19class Customer(Process):
    20    """ Customer arrives, is served and leaves """
    21
    22    def visit(self, b):
    23        arrive = self.sim.now()
    24        yield request, self, b
    25        wait = self.sim.now() - arrive
    26        self.sim.wM.observe(wait)
    27        tib = expovariate(1.0 / timeInBank)
    28        yield hold, self, tib
    29        yield release, self, b
    30
    31## Model  ----------------------------------
    32
    33class BankModel(Simulation):
    34    def run(self, aseed):
    35        self.initialize()
    36        seed(aseed)
    37        self.k = Resource(capacity=Nc, name="Clerk", sim=self)
    38        self.wM = Monitor(sim=self)
    39        s = Source('Source', sim=self)
    40        self.activate(s, s.generate(number=maxNumber, interval=ARRint), at=0.0)
    41        self.simulate(until=maxTime)
    42        return (self.wM.count(), self.wM.mean())
    43
    44## Experiment data -------------------------
    45
    46maxNumber = 50
    47maxTime = 2000.0    # minutes
    48timeInBank = 12.0   # mean, minutes
    49ARRint = 10.0       # mean, minutes
    50Nc = 2              # number of counters
    51seedVals = [393939, 31555999, 777999555, 319999771]
    52
    53## Experiment/Result  ----------------------------------
    54
    55mymodel = BankModel()
    56for Sd in seedVals:
    57    mymodel.run(aseed=Sd)
    58    moni = mymodel.wM
    59    print("Average wait for %3d completions was %6.2f minutes." % (moni.count(), moni.mean()))

The results show some variation. Remember, though, that the system is still only operating for 50 customers so the system may not be in steady-state.

    1Average wait for  50 completions was   3.66 minutes.
    2Average wait for  50 completions was   2.62 minutes.
    3Average wait for  50 completions was   8.97 minutes.
    4Average wait for  50 completions was   5.34 minutes.

## Final Remarks

This introduction is too long and the examples are getting longer. There is much more to say about simulation with SimPy but no space. I finish with a list of topics for further study:

- GUI input. Graphical input of simulation parameters could be an advantage in some cases. SimPy allows this and programs using these facilities have been developed (see, for example, program `MM1.py` in the examples in the SimPy distribution)

- Graphical Output. Similarly, graphical output of results can also be of value, not least in debugging simulation programs and checking for steady-state conditions. SimPlot is useful here.

- Statistical Output. The `Monitor` class is useful in presenting results but more powerful methods of analysis are often needed. One solution is to output a trace and read that into a large-scale statistical system such as R.

- Priorities and Reneging in queues. SimPy allows processes to request units of resources under a priority queue discipline (preemptive or not). It also allows processes to renege from a queue.

- Other forms of Resource Facilities. SimPy has two other resource structures: `Levels` to hold bulk commodities, and `Stores` to contain an inventory of different object types.

- Advanced synchronization/scheduling commands. SimPy allows process synchronization by events and signals.

## Acknowledgements

I thank Klaus Muller, Bob Helmbold, Mukhlis Matti and other developers and users of SimPy for improving this document by sending their comments. I would be grateful for further suggestions or corrections. Please send them to: vignaux at users.sourceforge.net.

## References

- Python website: http://www.Python.org

- SimPy website: http://sourceforge.net/projects/simpy