| Author: | G A Vignaux |
|---|---|
| Date: | 2004/02/19 |
The purpose of this example is show Monitor in use to estimate the average waiting time for customers in a bank.
The example is taken from the Bank tutorial, included in the SimPy documentation. It simulates a bank with customers who arrive at random to be served at one of two counters. A customer chooses the shortest queue to join and takes a random time to be served.
#!/usr/bin/env python
""" bank11: Simulate customers arriving
at random, using a Source, requesting service
from two counters each with their own queue
random servicetime.
Uses a Monitor object to record waiting times
"""
from __future__ import generators #(not needed in Python 2.3+)
from SimPy.Simulation import *
from random import Random
class Source(Process):
""" Source generates customers randomly"""
def __init__(self,seed=333):
Process.__init__(self)
self.SEED = seed
def generate(self,number,interval):
rv = Random(self.SEED)
for i in range(number):
c = Customer(name = "Customer%02d"%(i,))
activate(c,c.visit(timeInBank=12.0))
t = rv.expovariate(1.0/interval)
yield hold,self,t
def NoInSystem(R):
""" The number of customers in the resource R
in waitQ and active Q"""
return (len(R.waitQ)+len(R.activeQ))
class Customer(Process):
""" Customer arrives, is served and leaves """
def __init__(self,name):
Process.__init__(self)
self.name = name
def visit(self,timeInBank=0):
arrive=now()
Qlength = [NoInSystem(counter[i]) for i in range(Nc)]
for i in range(Nc):
if Qlength[i] ==0 or Qlength[i]==min(Qlength): join =i ; break
yield request,self,counter[join]
wait=now()-arrive
waitMonitor.observe(wait)
tib = counterRV.expovariate(1.0/timeInBank)
yield hold,self,tib
yield release,self,counter[join]
def model(counterseed=393939):
global Nc,counter,counterRV,waitMonitor
Nc = 2
counter = [Resource(name="Clerk0"),Resource(name="Clerk1")]
counterRV = Random(counterseed)
waitMonitor = Monitor()
initialize()
sourceseed = 99999
source = Source(seed = sourceseed)
activate(source,source.generate(50,10.0),0.0)
simulate(until=2000.0)
return (waitMonitor.count(),waitMonitor.mean())
result = model(393939)
print "Average wait for %4d was %6.2f"% result
The Customer class is a SimPy process with a PEM called visit. On arrival the time is recorded and the shortest queue chosen (variable join). The function NoInSystem is used to find the number at each counter. A request is made for that particular counter[join], and, when service starts, the waiting time, wait is calculated. This is Monitored. The actual time for service, tib is sampled (from a distribution with mean timeInBank) and the counter held for that time before release.
Customers are generated using the Source process which creates and activates a Customer at exponential (random) delays with mean interval.
The problem is set up in the function model. Doing this within a function is a little clumsy as it involves the use either of a number of arguments or, as I have done here, of global variables, like counter and waitMonitor, but it means that a number of independent runs can be made within the same script.
Here I did not use the full power of this technique. I ran a single run of 50 customers (in the call of generate) for a maximum time of 2000. The result is:
Average wait for 50 was 5.83