The Bank Tutorial (OO API) Part 2: More examples of SimPy Simulation — SimPy 2.3.1 documentation

### Navigation

- index
- next |
- previous |
- SimPy 2.3.1 documentation »
- SimPy Tutorials »

# The Bank Tutorial (OO API) Part 2: More examples of SimPy Simulation

Authors: | G A Vignaux, K G Muller
Date: | 2010 April
SimPy release: | 2.3.1
Python-Version: | 2.6 and later

## OO API Bank Tutorial version

This manual is a rework of the Bank Tutorial Part 2. Its goal is to show how the simple tutorial models can be written in the advanced OO API.

Note

To contrast the OO API with the procedural SimPy API, the reader should read both “Bank Tutorial Part 2” documents side by side.

## Introduction

The first Bank tutorial, The Bank, developed and explained a series of simulation models of a simple bank using SimPy. In various models, customers arrived randomly, queued up to be served at one or several counters, modelled using the Resource class, and, in one case, could choose the shortest among several queues. It demonstrated the use of the Monitor class to record delays and showed how a `model()` mainline for the simulation was convenient to execute replications of simulation runs.

In this extension to The Bank, I provide more examples of SimPy facilities for which there was no room and for some that were developed since it was written. These facilities are generally more complicated than those introduced before. They include queueing with priority, possibly with preemption, reneging, plotting, interrupting, waiting until a condition occurs (`waituntil`) and waiting for events to occur.

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.

The programs are available without line numbers and ready to go, in directory `bankprograms`. Some have trace statements for demonstration purposes, others produce graphical output to the screen. Let me encourage you to run them and modify them for yourself.

SimPy itself can be obtained from: http://simpy.sourceforge.net/. It is compatible with Python version 2.3 onwards. The examples in this documentation run with SimPy version 1.5 and later.

This tutorial should be read with the SimPy Manual and CheatsheetOO at your side for reference.

## Priority Customers

In many situations there is a system of priority service. Those customers with high priority are served first, those with low priority must wait. In some cases, preemptive priority will even allow a high-priority customer to interrupt the service of one with a lower priority.

SimPy implements priority requests with an extra numerical priority argument in the `yield request` command, higher values meaning higher priority. For this to operate, the requested Resource must have been defined with `qType=PriorityQ`. This require importing the `PriorityQ` class from `SimPy.Simulation`.

### Priority Customers without preemption

In the first example, we modify the program with random arrivals, one counter, and a fixed service time (like `bank07.py` in The Bank tutorial) to process a high priority customer. Warning: the `seedVal` value has been changed to `98989` to make the story more exciting.

The modifications are to the definition of the `counter` where we change the `qType` and to the `yield request` command in the `visit` PEM of the customer. We also need to provide each customer with a priority. Since the default is `priority=0` this is easy for most of them.

To observe the priority in action, while all other customers have the default priority of 0, in lines 43 to 44 we create and activate one special customer, `Guido`, with priority 100 who arrives at time `23.0` (line 44). This is to ensure that he arrives after `Customer03`.

The `visit` customer method has a new parameter, `P=0` (line 20) which allows us to set the customer priority.

In lines 39 to 40 the `BankModel` ‘s resource attribute `k` named `Counter` is defined with `qType=PriorityQ` so that we can request it with priority (line 25) using the statement `yield request,self,self.sim.k,P`

In line 23 we print out the number of customers waiting when each customer arrives.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    """ bank20_OO: One counter with a priority customer """
    from SimPy.Simulation import Simulation, Process, Resource, PriorityQ, hold, request, release
    from random import expovariate, seed

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly """

        def generate(self, number, interval):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0, P=0))
                t = expovariate(1.0 / interval)
                yield hold, self, t

    class Customer(Process):
        """ Customer arrives, is served and  leaves """

        def visit(self, timeInBank=0, P=0):
            arrive = self.sim.now()       # arrival time
            Nwaiting = len(self.sim.k.waitQ)
            print("%8.3f %s: Queue is %d on arrival" % (self.sim.now(), self.name, Nwaiting))

            yield request, self, self.sim.k, P
            wait = self.sim.now() - arrive  # waiting time
            print("%8.3f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
            yield hold, self, timeInBank
            yield release, self, self.sim.k

            print("%8.3f %s: Completed" % (self.sim.now(), self.name))

    ## Model ------------------------------
    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.k = Resource(name="Counter", unitName="Karen",
                 qType=PriorityQ, sim=self)
            s = Source('Source', sim=self)
            self.activate(s, s.generate(number=5, interval=10.0), at=0.0)
            guido = Customer(name="Guido     ", sim=self)
            self.activate(guido, guido.visit(timeInBank=12.0, P=100), at=23.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    maxTime = 400.0  # minutes
    seedVal = 98989

    ## Experiment ---------------------------

    mymodel = BankModel()
    mymodel.run(aseed=seedVal)

The resulting output is as follows. The number of customers in the queue just as each arrives is displayed in the trace. That count does not include any customer in service.

       0.000 Customer00: Queue is 0 on arrival
       0.000 Customer00: Waited  0.000
      12.000 Customer00: Completed
      16.359 Customer01: Queue is 0 on arrival
      16.359 Customer01: Waited  0.000
      23.000 Guido     : Queue is 0 on arrival
      28.359 Customer01: Completed
      28.359 Guido     : Waited  5.359
      29.991 Customer02: Queue is 0 on arrival
      35.776 Customer03: Queue is 1 on arrival
      36.650 Customer04: Queue is 2 on arrival
      40.359 Guido     : Completed
      40.359 Customer02: Waited 10.368
      52.359 Customer02: Completed
      52.359 Customer03: Waited 16.583
      64.359 Customer03: Completed
      64.359 Customer04: Waited 27.709
      76.359 Customer04: Completed

Reading carefully one can see that when `Guido` arrives `Customer00` has been served and left at `12.000`), `Customer01` is in service and two (customers 02 and 03) are queueing. `Guido` has priority over those waiting and is served before them at `24.000`. When `Guido` leaves at `36.000`, `Customer02` starts service.

### Priority Customers with preemption

Now we allow `Guido` to have preemptive priority. He will displace any customer in service when he arrives. That customer will resume when `Guido` finishes (unless higher priority customers intervene). It requires only a change to one line of the program, adding the argument, `preemptable=True` to the `Resource` statement in line 40.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    """ bank23_OO: One counter with a priority customer with preemption """
    from SimPy.Simulation import Simulation, Process, Resource, PriorityQ, hold, request, release
    from random import expovariate, seed

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly """

        def generate(self, number, interval):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0, P=0))
                t = expovariate(1.0 / interval)
                yield hold, self, t

    class Customer(Process):
        """ Customer arrives, is served and  leaves """

        def visit(self, timeInBank=0, P=0):
            arrive = self.sim.now()       # arrival time
            Nwaiting = len(self.sim.k.waitQ)
            print("%8.3f %s: Queue is %d on arrival" % (self.sim.now(), self.name, Nwaiting))

            yield request, self, self.sim.k, P
            wait = self.sim.now() - arrive  # waiting time
            print("%8.3f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
            yield hold, self, timeInBank
            yield release, self, self.sim.k

            print("%8.3f %s: Completed" % (self.sim.now(), self.name))

    ## Model -----------------------------------
    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.k = Resource(name="Counter", unitName="Karen",
                              qType=PriorityQ, preemptable=True, sim=self)
            s = Source('Source', sim=self)
            self.activate(s, s.generate(number=5, interval=10.0), at=0.0)
            guido = Customer(name="Guido     ", sim=self)
            self.activate(guido, guido.visit(timeInBank=12.0, P=100), at=23.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    maxTime = 400.0  # minutes
    seedVal = 98989

    ## Experiment -------- ---------------------

    mymodel = BankModel()
    mymodel.run(aseed=seedVal)

Though `Guido` arrives at the same time, `23.000`, he no longer has to wait and immediately goes into service, displacing the incumbent, `Customer01`. That customer had already completed `23.000-12.000 = 11.000` minutes of his service. When `Guido` finishes at `35.000`, `Customer01` resumes service and takes `36.000-35.000 = 1.000` minutes to finish. His total service time is the same as before (`12.000` minutes).

       0.000 Customer00: Queue is 0 on arrival
       0.000 Customer00: Waited  0.000
      12.000 Customer00: Completed
      16.359 Customer01: Queue is 0 on arrival
      16.359 Customer01: Waited  0.000
      23.000 Guido     : Queue is 0 on arrival
      23.000 Guido     : Waited  0.000
      29.991 Customer02: Queue is 1 on arrival
      35.000 Guido     : Completed
      35.776 Customer03: Queue is 1 on arrival
      36.650 Customer04: Queue is 2 on arrival
      40.359 Customer01: Completed
      40.359 Customer02: Waited 10.368
      52.359 Customer02: Completed
      52.359 Customer03: Waited 16.583
      64.359 Customer03: Completed
      64.359 Customer04: Waited 27.709
      76.359 Customer04: Completed

## Balking and Reneging Customers

Balking occurs when a customer refuses to join a queue if it is too long. Reneging (or, better, abandonment) occurs if an impatient customer gives up while still waiting and before being served.

### Balking Customers

Another term for a system with balking customers is one where “blocked customers” are “cleared”, termed by engineers a BCC system. This is very convenient analytically in queueing theory and formulae developed using this assumption are used extensively for planning communication systems. The easiest case is when no queueing is allowed.

As an example let us investigate a BCC system with a single server but the waiting space is limited. We will estimate the rate of balking when the maximum number in the queue is set to 1. On arrival into the system the customer must first check to see if there is room. We will need the number of customers in the system or waiting. We could keep a count, incrementing when a customer joins the queue or, since we have a Resource, use the length of the Resource’s `waitQ`. Choosing the latter we test (on line 23). If there is not enough room, we balk, incrementing a class variable `Customer.numBalking` at line 32 to get the total number balking during the run.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    """ bank24_OO. BCC system with several counters """
    from SimPy.Simulation import Simulation, Process, Resource, hold, request, release
    from random import expovariate, seed

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly """

        def generate(self, number, meanTBA):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit())
                t = expovariate(1.0 / meanTBA)
                yield hold, self, t

    class Customer(Process):
        """ Customer arrives,  is served and leaves """

        def visit(self):
            arrive = self.sim.now()
            print("%8.4f %s: Here I am " % (self.sim.now(), self.name))
            if len(self.sim.k.waitQ) < maxInQueue:     # the test
                yield request, self, self.sim.k
                wait = self.sim.now() - arrive
                print("%8.4f %s: Wait %6.3f" % (self.sim.now(), self.name, wait))
                tib = expovariate(1.0 / timeInBank)
                yield hold, self, tib
                yield release, self, self.sim.k
                print("%8.4f %s: Finished  " % (self.sim.now(), self.name))
            else:
                Customer.numBalking += 1
                print("%8.4f %s: BALKING   " % (self.sim.now(), self.name))

    ## Model
    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            Customer.numBalking = 0
            self.k = Resource(capacity=numServers,
                 name="Counter", unitName="Clerk", sim=self)
            s = Source('Source', sim=self)
            self.activate(s, s.generate(number=maxNumber, meanTBA=ARRint), at=0.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------------

    timeInBank = 12.0  # mean, minutes
    ARRint = 10.0      # mean interarrival time, minutes
    numServers = 1     # servers
    maxInSystem = 2    # customers
    maxInQueue = maxInSystem - numServers

    maxNumber = 8
    maxTime = 4000.0  # minutes
    theseed = 12345

    ## Experiment --------------------------------------

    mymodel = BankModel()
    mymodel.run(aseed=theseed)
    ## Results -----------------------------------------

    nb = float(Customer.numBalking)
    print("balking rate is %8.4f per minute" % (nb / mymodel.now()))

The resulting output for a run of this program showing balking occurring is given below:

      0.0000 Customer00: Here I am
      0.0000 Customer00: Wait  0.000
      0.1227 Customer00: Finished
      5.3892 Customer01: Here I am
      5.3892 Customer01: Wait  0.000
      9.6460 Customer01: Finished
     22.8307 Customer02: Here I am
     22.8307 Customer02: Wait  0.000
     25.4137 Customer02: Finished
     27.4258 Customer03: Here I am
     27.4258 Customer03: Wait  0.000
     29.5422 Customer03: Finished
     35.7731 Customer04: Here I am
     35.7731 Customer04: Wait  0.000
     37.1001 Customer05: Here I am
     42.5805 Customer04: Finished
     42.5805 Customer05: Wait  5.480
     44.8795 Customer05: Finished
     45.3572 Customer06: Here I am
     45.3572 Customer06: Wait  0.000
     50.6175 Customer06: Finished
     53.4141 Customer07: Here I am
     53.4141 Customer07: Wait  0.000
     54.5629 Customer07: Finished
    balking rate is   0.0000 per minute

When `Customer02` arrives, numbers 00 is already in service and 01 is waiting. There is no room so 02 balks. By the vagaries of exponential random numbers, 00 takes a very long time to serve (55.0607 minutes) so the first one to find room is number 07 at 73.0765.

### Reneging (or abandoning) Customers

Often in practice an impatient customer will leave the queue before being served. SimPy can model this reneging behaviour using a compound yield statement. In such a statement there are two yield clauses. An example is:

    yield (request,self,counter),(hold,self,maxWaitTime)

The first tuple of this statement is the usual `yield request`, asking for a unit of `counter` Resource. The process will either get the unit immediately or be queued by the Resource. The second tuple is a reneging clause which has the same syntax as a `yield hold`. The requesting process will renege if the wait exceeds `maxWaitTime`.

There is a complication, though. The requesting PEM must discover what actually happened. Did the process get the resource or did it renege? This involves a mandatory test of `self.acquired(`resource`)`. In our example, this test is in line 26.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    """ bank21_OO: One counter with impatient customers """
    from SimPy.Simulation import Simulation, Process, Resource, hold, request, release
    from random import expovariate, seed

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly """

        def generate(self, number, interval):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=15.0))
                t = expovariate(1.0 / interval)
                yield hold, self, t

    class Customer(Process):
        """ Customer arrives, is served and  leaves """

        def visit(self, timeInBank=0):
            arrive = self.sim.now()       # arrival time
            print("%8.3f %s: Here I am     " % (self.sim.now(), self.name))

            yield (request, self, self.sim.counter), (hold, self, maxWaitTime)
            wait = self.sim.now() - arrive  # waiting time
            if self.acquired(self.sim.counter):
                print("%8.3f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))
                yield hold, self, timeInBank
                yield release, self, self.sim.counter
                print("%8.3f %s: Completed" % (self.sim.now(), self.name))
            else:
                print("%8.3f %s: Waited %6.3f. I am off" % (self.sim.now(), self.name, wait))

    ## Model  ----------------------------------
    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.counter = Resource(name="Karen", sim=self)
            source = Source('Source', sim=self)
            self.activate(source,
                 source.generate(number=5, interval=10.0), at=0.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    maxTime = 400.0     # minutes
    maxWaitTime = 12.0  # minutes. maximum time to wait
    seedVal = 98989

    ## Experiment  ----------------------------------

    mymodel = BankModel()
    mymodel.run(aseed=seedVal)

       0.000 Customer00: Here I am
       0.000 Customer00: Waited  0.000
      15.000 Customer00: Completed
      16.359 Customer01: Here I am
      16.359 Customer01: Waited  0.000
      29.991 Customer02: Here I am
      31.359 Customer01: Completed
      31.359 Customer02: Waited  1.368
      35.776 Customer03: Here I am
      36.650 Customer04: Here I am
      46.359 Customer02: Completed
      46.359 Customer03: Waited 10.583
      48.650 Customer04: Waited 12.000. I am off
      61.359 Customer03: Completed

`Customer01` arrives after 00 but has only 12 minutes patience. After that time in the queue (at time 14.166) he abandons the queue to leave 02 to take his place. 03 also abandons. 04 finds an empty system and takes the server without having to wait.

## Processes

In some simulations it is valuable for one SimPy Process to interrupt another. This can only be done when the victim is “active”; that is when it has an event scheduled for it. It must be executing a `yield hold` statement.

A process waiting for a resource (after a `yield request` statement) is passive and cannot be interrupted by another. Instead the `yield waituntil` and `yield waitevent` facilities have been introduced to allow processes to wait for conditions set by other processes.

### Interrupting a Process.

`Klaus` goes into the bank to talk to the manager. For clarity we ignore the counters and other customers. During his conversation his cellphone rings. When he finishes the call he continues the conversation.

In this example, `call` is an object of the `Call` Process class whose only purpose is to make the cellphone ring after a delay, `timeOfCall`, an argument to its `ring` PEM (line 26).

`klaus`, a `Customer`, is interrupted by the call (line 29). He is in the middle of a `yield hold` (line 12). When he exits from that command it is as if he went into a trance when talking to the bank manager. He suddenly wakes up and must check (line 13) to see whether has finished his conversation (if there was no call) or has been interrupted.

If `self.interrupted()` is `False` he was not interrupted and leaves the bank (line 21) normally. If it is `True`, he was interrupted by the call, remembers how much conversation he has left (line 14), resets the interrupt (line 15) and then deals with the call. When he finishes (line 19) he can resume the conversation, with, now we assume, a thoroughly irritated bank manager v(line 20).

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    """ bank22_OO: An interruption by a phone call """
    from SimPy.Simulation  import Simulation, Process, hold

    ## Model components ------------------------

    class Customer(Process):
        """ Customer arrives, looks around and leaves """

        def visit(self, timeInBank, onphone):
            print("%7.4f %s: Here I am" % (self.sim.now(), self.name))
            yield hold, self, timeInBank
            if self.interrupted():
                timeleft = self.interruptLeft
                self.interruptReset()
                print("%7.4f %s: Excuse me" % (self.sim.now(), self.name))
                print("%7.4f %s: Hello! I'll call back" % (self.sim.now(), self.name))
                yield hold, self, onphone
                print("%7.4f %s: Sorry, where were we?" % (self.sim.now(), self.name))
                yield hold, self, timeleft
            print("%7.4f %s: I must leave" % (self.sim.now(), self.name))

    class Call(Process):
        """ Cellphone call arrives and interrupts """

        def ring(self, klaus, timeOfCall):
            yield hold, self, timeOfCall
            print("%7.4f Ringgg!" % (self.sim.now()))
            self.interrupt(klaus)

    ## Model -----------------------------------

    class BankModel(Simulation):
        def run(self):
            """ PEM """
            klaus = Customer(name="Klaus", sim=self)
            self.activate(klaus, klaus.visit(timeInBank, onphone))
            call = Call(sim=self)
            self.activate(call, call.ring(klaus, timeOfCall))
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    timeInBank = 20.0
    timeOfCall = 9.0
    onphone = 3.0
    maxTime = 100.0

    ## Experiment  -----------------------------
    mymodel = BankModel()
    mymodel.run()

     0.0000 Klaus: Here I am
     9.0000 Ringgg!
     9.0000 Klaus: Excuse me
     9.0000 Klaus: Hello! I'll call back
    12.0000 Klaus: Sorry, where were we?
    23.0000 Klaus: I must leave

As this has no random numbers the results are reasonably clear: the interrupting call occurs at 9.0. It takes `klaus` 3 minutes to listen to the message and he resumes the conversation with the bank manager at 12.0. His total time of conversation is 9.0 + 11.0 = 20.0 minutes as it would have been if the interrupt had not occurred.

### `waituntil` the Bank door opens

Customers arrive at random, some of them getting to the bank before the door is opened by a doorman. They wait for the door to be opened and then rush in and queue to be served. The door is modeled by an attribute `door` of `BankModel`.

This model uses the `waituntil` yield command. In the program listing the door is initially closed (line 58) and a method to test if it is open is defined at line 54.

The `Doorman` class is defined starting at line 7 and the single `doorman` is created and activated at at lines 59 and 60. The doorman waits for an average 10 minutes (line 11) and then opens the door.

The `Customer` class is defined at 24 and a new customer prints out `Here I am` on arrival. If the door is still closed, he adds `but the door is shut` and settles down to wait (line 35), using the `yield waituntil` command. When the door is opened by the doorman the `dooropen` state is changed and the customer (and all others waiting for the door) proceed. A customer arriving when the door is open will not be delayed.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    """bank14_OO: *waituntil* the Bank door opens"""
    from SimPy.Simulation import Simulation, Process, Resource, hold, waituntil, request, release
    from random import expovariate, seed

    ## Model components ------------------------

    class Doorman(Process):
        """ Doorman opens the door"""
        def openthedoor(self):
            """ He will open the door when he arrives"""
            yield hold, self, expovariate(1.0 / 10.0)
            self.sim.door = 'Open'
            print("%7.4f Doorman: Ladies and "\
                  "Gentlemen! You may all enter." % (self.sim.now()))

    class Source(Process):
        """ Source generates customers randomly"""
        def generate(self, number, rate):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0))
                yield hold, self, expovariate(rate)

    class Customer(Process):
        """ Customer arrives, is served and leaves """
        def visit(self, timeInBank=10):
            arrive = self.sim.now()

            if self.sim.dooropen():
                msg = ' and the door is open.'
            else:
                msg = ' but the door is shut.'
            print("%7.4f %s: Here I am%s" % (self.sim.now(), self.name, msg))

            yield waituntil, self, self.sim.dooropen

            print("%7.4f %s: I can  go in!" % (self.sim.now(), self.name))
            wait = self.sim.now() - arrive
            print("%7.4f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))

            yield request, self, self.sim.counter
            tib = expovariate(1.0 / timeInBank)
            yield hold, self, tib
            yield release, self, self.sim.counter

            print("%7.4f %s: Finished    " % (self.sim.now(), self.name))

    ## Model  ----------------------------------

    class BankModel(Simulation):
        def dooropen(self):
            return self.door == 'Open'

        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.counter = Resource(capacity=1, name="Clerk", sim=self)
            self.door = 'Shut'
            doorman = Doorman(sim=self)
            self.activate(doorman, doorman.openthedoor())
            source = Source(sim=self)
            self.activate(source,
                 source.generate(number=5, rate=0.1), at=0.0)
            self.simulate(until=400.0)

    ## Experiment data -------------------------

    maxTime = 2000.0   # minutes
    seedVal = 393939

    ## Experiment  ----------------------------------

    mymodel = BankModel()
    mymodel.run(aseed=seedVal)

An output run for this programs shows how the first three customers have to wait until the door is opened.

     0.0000 Customer00: Here I am but the door is shut.
     1.1489 Doorman: Ladies and Gentlemen! You may all enter.
     1.1489 Customer00: I can  go in!
     1.1489 Customer00: Waited  1.149
     6.5691 Customer00: Finished
     8.3438 Customer01: Here I am and the door is open.
     8.3438 Customer01: I can  go in!
     8.3438 Customer01: Waited  0.000
    15.5704 Customer02: Here I am and the door is open.
    15.5704 Customer02: I can  go in!
    15.5704 Customer02: Waited  0.000
    21.2664 Customer03: Here I am and the door is open.
    21.2664 Customer03: I can  go in!
    21.2664 Customer03: Waited  0.000
    21.9473 Customer04: Here I am and the door is open.
    21.9473 Customer04: I can  go in!
    21.9473 Customer04: Waited  0.000
    27.6401 Customer01: Finished
    56.5248 Customer02: Finished
    57.3640 Customer03: Finished
    77.3587 Customer04: Finished

### Wait for the doorman to give a signal: `waitevent`

Customers arrive at random, some of them getting to the bank before the door is open. This is controlled by an automatic machine called the doorman which opens the door only at intervals of 30 minutes (it is a very secure bank). The customers wait for the door to be opened and all those waiting enter and proceed to the counter. The door is closed behind them.

This model uses the `yield waitevent` command which requires a `SimEvent` attribute for `BankModel` to be defined (line 56). The `Doorman` class is defined at line 7 and the `doorman` is created and activated at at labels 56 and 57. The doorman waits for a fixed time (label 12) and then tells the customers that the door is open. This is achieved on line 13 by signalling the `dooropen` event.

The `Customer` class is defined at 24 and in its PEM, when a customer arrives, he prints out `Here I am`. If the door is still closed, he adds “but the door is shut` and settles down to wait for the door to be opened using the `yield waitevent` command (line 34). When the door is opened by the doorman (that is, he sends the `dooropen.signal()` the customer and any others waiting may proceed.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    """ bank13_OO: Wait for the doorman to give a signal: *waitevent*"""
    from SimPy.Simulation import Simulation, Process, Resource, SimEvent, hold, request, release, waitevent
    from random import *

    ## Model components ------------------------

    class Doorman(Process):
        """ Doorman opens the door"""
        def openthedoor(self):
            """ He will opens the door at fixed intervals"""
            for i in range(5):
                yield hold, self,  30.0
                self.sim.dooropen.signal()
                print("%7.4f You may enter" % (self.sim.now()))

    class Source(Process):
        """ Source generates customers randomly"""
        def generate(self, number, rate):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0))
                yield hold, self, expovariate(rate)

    class Customer(Process):
        """ Customer arrives, is served and leaves """
        def visit(self, timeInBank=10):
            arrive = self.sim.now()

            if self.sim.dooropen.occurred:
                msg = '.'
            else:
                msg = ' but the door is shut.'
            print("%7.4f %s: Here I am%s" % (self.sim.now(), self.name, msg))
            yield waitevent, self, self.sim.dooropen

            print("%7.4f %s: The door is open!" % (self.sim.now(), self.name))

            wait = self.sim.now() - arrive
            print("%7.4f %s: Waited %6.3f" % (self.sim.now(), self.name, wait))

            yield request, self, self.sim.counter
            tib = expovariate(1.0 / timeInBank)
            yield hold, self, tib
            yield release, self, self.sim.counter

            print("%7.4f %s: Finished    " % (self.sim.now(), self.name))

    ## Model  ----------------------------------

    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.dooropen = SimEvent("Door Open", sim=self)
            self.counter = Resource(1, name="Clerk", sim=self)
            doorman = Doorman(sim=self)
            self.activate(doorman, doorman.openthedoor())
            source = Source(sim=self)
            self.activate(source,
                 source.generate(number=5, rate=0.1), at=0.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    maxTime = 400.0  # minutes
    seedVal = 393939

    ## Experiment  ----------------------------------

    mymodel = BankModel()
    mymodel.run(aseed=seedVal)

An output run for this programs shows how the first three customers have to wait until the door is opened.

     0.0000 Customer00: Here I am but the door is shut.
     1.1489 Customer01: Here I am but the door is shut.
     9.4928 Customer02: Here I am but the door is shut.
    14.0096 Customer03: Here I am but the door is shut.
    21.2361 Customer04: Here I am but the door is shut.
    30.0000 You may enter
    30.0000 Customer04: The door is open!
    30.0000 Customer04: Waited  8.764
    30.0000 Customer03: The door is open!
    30.0000 Customer03: Waited 15.990
    30.0000 Customer02: The door is open!
    30.0000 Customer02: Waited 20.507
    30.0000 Customer01: The door is open!
    30.0000 Customer01: Waited 28.851
    30.0000 Customer00: The door is open!
    30.0000 Customer00: Waited 30.000
    36.8352 Customer04: Finished
    37.6524 Customer03: Finished
    52.4654 Customer02: Finished
    60.0000 You may enter
    81.3502 Customer01: Finished
    82.1893 Customer00: Finished
    90.0000 You may enter
    120.0000 You may enter
    150.0000 You may enter

## Monitors

Monitors (and Tallys) are used to track and record values in a simulation. They store a list of [time,value] pairs, one pair being added whenever the `observe` method is called. A particularly useful characteristic is that they continue to exist after the simulation has been completed. Thus further analysis of the results can be carried out.

Monitors have a set of simple statistical methods such as `mean` and `var` to calculate the average and variance of the observed values – useful in estimating the mean delay, for example.

They also have the `timeAverage` method that calculates the time-weighted average of the recorded values. It determines the total area under the time~value graph and divides by the total time. This is useful for estimating the average number of customers in the bank, for example. There is an important caveat in using this method. To estimate the correct time average you must certainly `observe` the value (say the number of customers in the system) whenever it changes (as well as at any other time you wish) but, and this is important, observing the new value. The old value was recorded earlier. In practice this means that if we wish to observe a changing value, `n`, using the Monitor, `Mon`, we must keep to the the following pattern:

    n = n+1
    Mon.observe(n,self.sim.now())

Thus you make the change (not only increases) and then observe the new value. Of course the simulation time `now()` has not changed between the two statements.

### Plotting a Histogram of Monitor results

A Monitor can construct a histogram from its data using the `histogram` method. In this model we monitor the time in the system for the customers. This is calculated for each customer in line 29, using the arrival time saved in line 19. We create the Monitor attribute of `BankModel`, `Mon`, at line 39 and the times are `observed` at line 30.

The histogram is constructed from the Monitor, after the simulation has finished, at line 58. The SimPy SimPlot package allows simple plotting of results from simulations. Here we use the SimPlot `plotHistogram` method. The plotting routines appear in lines 60-64. The `plotHistogram` call is in line 61.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    """bank17_OO: Plotting a Histogram of Monitor results"""
    from SimPy.Simulation  import Simulation, Process, Resource, Monitor, hold, request, release
    from SimPy.SimPlot import *
    from random import  expovariate, seed

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly"""
        def generate(self, number, rate):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0))
                yield hold, self, expovariate(rate)

    class Customer(Process):
        """ Customer arrives, is served and leaves """
        def visit(self, timeInBank):
            arrive = self.sim.now()
            #print("%8.4f %s: Arrived     "%(now(), self.name))

            yield request, self, self.sim.counter
            #print("%8.4f %s: Got counter "%(now(), self.name))
            tib = expovariate(1.0 / timeInBank)
            yield hold, self, tib
            yield release, self, self.sim.counter

            #print("%8.4f %s: Finished    " % (now(), self.name))
            t = self.sim.now() - arrive
            self.sim.Mon.observe(t)

    ## Model  ----------------------------------

    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.counter = Resource(1, name="Clerk", sim=self)
            self.Mon = Monitor('Time in the Bank', sim=self)
            source = Source(sim=self)
            self.activate(source,
                 source.generate(number=20, rate=0.1), at=0.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    maxTime = 400.0   # minutes

    N = 0
    seedVal = 393939

    ## Experiment  -----------------------------

    modl = BankModel()
    modl.run(aseed=seedVal)

    ## Output ----------------------------------
    Histo = modl.Mon.histogram(low=0.0, high=200.0, nbins=20)

    plt = SimPlot()
    plt.plotHistogram(Histo, xlab='Time (min)',
                      title="Time in the Bank",
                      color="red", width=2)
    plt.mainloop()

### Monitoring a Resource

Now consider observing the number of customers waiting or executing in a Resource. Because of the need to `observe` the value after the change but at the same simulation instant, it is impossible to use the length of the Resource’s `waitQ` directly with a Monitor defined outside the Resource. Instead Resources can be set up with built-in Monitors.

Here is an example using a Monitored Resource. We intend to observe the average number waiting and active in the `counter` resource. `counter` is defined at line 35 as a `BankModel` attribute and we have set `monitored=True`. This establishes two Monitors: `waitMon`, to record changes in the numbers waiting and `actMon` to record changes in the numbers active in the `counter`. We need make no further change to the operation of the program as monitoring is then automatic. No `observe` calls are necessary.

After completion of the `run` method, we calculate the `timeAverage` of both `waitMon` and `actMon` (lines 53-54). These can then be printed at the end of the program (line 55).

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    """bank15_OO: Monitoring a Resource"""
    from SimPy.Simulation  import Simulation, Process, Resource, Monitor, hold, request, release
    from random import *

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly"""
        def generate(self, number, rate):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0, counter=self.sim.counter))
                yield hold, self, expovariate(rate)

    class Customer(Process):
        """ Customer arrives, is served and leaves """
        def visit(self, timeInBank, counter):
            arrive = self.sim.now()
            print("%8.4f %s: Arrived     " % (self.sim.now(), self.name))

            yield request, self, counter
            print("%8.4f %s: Got counter " % (self.sim.now(), self.name))
            tib = expovariate(1.0 / timeInBank)
            yield hold, self, tib
            yield release, self, counter

            print("%8.4f %s: Finished    " % (self.sim.now(), self.name))

    ## Model  ----------------------------------

    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.counter = Resource(capacity=1, name="Clerk", monitored=True, sim=self)
            source = Source(sim=self)
            self.activate(source,
                     source.generate(number=5, rate=0.1), at=0.0)
            self.simulate(until=maxTime)

            return

    ## Experiment data -------------------------

    maxTime = 400.0    # minutes
    seedVal = 393939

    ## Experiment  ----------------------------------

    modl = BankModel()
    modl.run(aseed=seedVal)

    nrwaiting = modl.counter.waitMon.timeAverage()
    nractive = modl.counter.actMon.timeAverage()
    print('Average waiting = %6.4f\nAverage active  = %6.4f\n' % (nrwaiting, nractive))

### Plotting from Resource Monitors

Like all Monitors, `waitMon` and `actMon` in a monitored Resource contain information that enables us to graph the output. Alternative plotting packages can be used; here we use the simple `SimPy.SimPlot` package just to graph the number of customers waiting for the counter. The program is a simple modification of the one that uses a monitored Resource.

The SimPlot package is imported at line 3. No major changes are made to the main part of the program except that I commented out the print statements. The changes occur in the `run` method from lines 38 to 39. The simulation now generates and processes 20 customers (line 39). The Monitors of the `counter` Resource attribute still exist when the simulation has terminated.

The additional plotting actions take place in lines 54 to 57. Line 55-56 construct a step plot and graphs the number in the waiting queue as a function of time. `waitMon` is primarily a list of [time,value] pairs which the `plotStep` method of the SimPlot object, `plt` uses without change. On running the program the graph is plotted; the user has to terminate the plotting `mainloop` on the screen.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    """bank16_OO: Plotting from  Resource Monitors"""
    from SimPy.Simulation import Simulation, Process, Resource, hold, request, release
    from SimPy.SimPlot import *
    from random import expovariate, seed

    ## Model components ------------------------

    class Source(Process):
        """ Source generates customers randomly"""
        def generate(self, number, rate):
            for i in range(number):
                c = Customer(name="Customer%02d" % (i), sim=self.sim)
                self.sim.activate(c, c.visit(timeInBank=12.0))
                yield hold, self, expovariate(rate)

    class Customer(Process):
        """ Customer arrives,  is served and leaves """
        def visit(self, timeInBank):
            arrive = self.sim.now()
            #print("%8.4f %s: Arrived     " % (now(), self.name))

            yield request, self, self.sim.counter
            #print("%8.4f %s: Got counter " % (now(), self.name))
            tib = expovariate(1.0 / timeInBank)
            yield hold, self, tib
            yield release, self, self.sim.counter

            #print("%8.4f %s: Finished    " % (now(), self.name))

    ## Model -----------------------------------

    class BankModel(Simulation):
        def run(self, aseed):
            """ PEM """
            seed(aseed)
            self.counter = Resource(1, name="Clerk", monitored=True, sim=self)
            source = Source(sim=self)
            self.activate(source,
                 source.generate(number=20, rate=0.1), at=0.0)
            self.simulate(until=maxTime)

    ## Experiment data -------------------------

    maxTime = 400.0   # minutes
    seedVal = 393939

    ## Experiment -----------------------------------

    mymodel = BankModel()
    mymodel.run(aseed=seedVal)

    ## Output ---------------------------------------

    plt = SimPlot()
    plt.plotStep(mymodel.counter.waitMon,
            color="red", width=2)
    plt.mainloop()

## Acknowledgements

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

## References

- Python website: http://www.Python.org
- SimPy homepage: http://simpy.sourceforge.net/
- The Bank:

### Navigation

- index
- next |
- previous |
- SimPy 2.3.1 documentation »
- SimPy Tutorials »