| 1 |
""" bank11.py: Simulate customers arriving |
| 2 |
at random, using a Source, requesting service |
| 3 |
from two counters each with their own queue |
| 4 |
random servicetime. |
| 5 |
Uses a Monitor object to record waiting times |
| 6 |
""" |
| 7 |
from SimPy.Simulation import * |
| 8 |
from SimPy.SimPlot import * |
| 9 |
from random import Random |
| 10 |
|
| 11 |
class Bank11(SimPlot): |
| 12 |
def __init__(self,**p): |
| 13 |
SimPlot.__init__(self,**p) |
| 14 |
|
| 15 |
def NoInSystem(self,R): |
| 16 |
""" The number of customers in the resource R |
| 17 |
in waitQ and active Q""" |
| 18 |
return (len(R.waitQ)+len(R.activeQ)) |
| 19 |
|
| 20 |
def model(self): |
| 21 |
self.counterRV = Random(self.params["counterseed"]) |
| 22 |
self.sourceseed = self.params["sourceseed"] |
| 23 |
nrRuns=self.params["nrRuns"] |
| 24 |
self.lastLeave=0 |
| 25 |
self.noRunYet=True |
| 26 |
for runNr in range(nrRuns): |
| 27 |
self.noRunYet=False |
| 28 |
self.Nc = 2 |
| 29 |
self.counter = [Resource(name="Clerk0",monitored=False), |
| 30 |
Resource(name="Clerk1",monitored=False)] |
| 31 |
self.waitMonitor = Monitor(name='Waiting Times') |
| 32 |
self.waitMonitor.xlab='Time' |
| 33 |
self.waitMonitor.ylab='Waiting time' |
| 34 |
self.serviceMonitor = Monitor(name='Service Times') |
| 35 |
self.serviceMonitor.xlab='Time' |
| 36 |
self.serviceMonitor.ylab='wait+service' |
| 37 |
initialize() |
| 38 |
source = Source(self,seed = self.sourceseed*1000) |
| 39 |
activate(source,source.generate(self.params["numberCustomers"], |
| 40 |
self.params["interval"]),0.0) |
| 41 |
simulate(until=self.params['endtime']) |
| 42 |
self.lastLeave+=now() |
| 43 |
print "%s run(s) completed"%(nrRuns) |
| 44 |
print("Parameters:\n%s"%self.params) |
| 45 |
|
| 46 |
class Source(Process): |
| 47 |
""" Source generates customers randomly""" |
| 48 |
def __init__(self,modInst,seed=333): |
| 49 |
Process.__init__(self) |
| 50 |
self.modInst=modInst |
| 51 |
self.SEED = seed |
| 52 |
|
| 53 |
def generate(self,number,interval): |
| 54 |
rv = Random(self.SEED) |
| 55 |
for i in range(number): |
| 56 |
c = Customer(self.modInst,name = "Customer%02d"%(i,)) |
| 57 |
activate(c,c.visit(timeInBank=12.0)) |
| 58 |
t = rv.expovariate(1.0/interval) |
| 59 |
yield hold,self,t |
| 60 |
|
| 61 |
class Customer(Process): |
| 62 |
""" Customer arrives, is served and leaves """ |
| 63 |
def __init__(self,modInst,**p): |
| 64 |
Process.__init__(self,**p) |
| 65 |
self.modInst=modInst |
| 66 |
|
| 67 |
def visit(self,timeInBank=0): |
| 68 |
arrive=now() |
| 69 |
Qlength = [self.modInst.NoInSystem(self.modInst.counter[i])\ |
| 70 |
for i in range(self.modInst.Nc)] |
| 71 |
for i in range(self.modInst.Nc): |
| 72 |
if Qlength[i] ==0 or Qlength[i]==min(Qlength): join =i ; break |
| 73 |
yield request,self,self.modInst.counter[join] |
| 74 |
wait=now()-arrive |
| 75 |
self.modInst.waitMonitor.observe(wait,t=now()) |
| 76 |
|
| 77 |
tib = self.modInst.counterRV.expovariate(1.0/timeInBank) |
| 78 |
yield hold,self,tib |
| 79 |
yield release,self,self.modInst.counter[join] |
| 80 |
self.modInst.serviceMonitor.observe(now()-arrive,t=now()) |
| 81 |
|
| 82 |
root=Tk() |
| 83 |
plt=Bank11() |
| 84 |
plt.params={"endtime":2000, |
| 85 |
"sourceseed":1133, |
| 86 |
"counterseed":3939393, |
| 87 |
"numberCustomers":50, |
| 88 |
"interval":10.0, |
| 89 |
"trace":0, |
| 90 |
"nrRuns":1} |
| 91 |
plt.model() |
| 92 |
plt.plotLine(plt.waitMonitor,color='blue',width=2) |
| 93 |
plt.plotLine(plt.serviceMonitor,color='red',width=2) |
| 94 |
root.mainloop() |