| 1 |
__doc__=""" 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.Monitor import * |
| 8 |
from SimPy.Simulation import * |
| 9 |
|
| 10 |
from random import Random |
| 11 |
from SimPy.SimGUI import * |
| 12 |
|
| 13 |
class Source(Process): |
| 14 |
""" Source generates customers randomly""" |
| 15 |
def __init__(self,seed=333): |
| 16 |
Process.__init__(self) |
| 17 |
self.SEED = seed |
| 18 |
|
| 19 |
def generate(self,number,interval): |
| 20 |
rv = Random(self.SEED) |
| 21 |
for i in range(number): |
| 22 |
c = Customer(name = "Customer%02d"%(i,)) |
| 23 |
activate(c,c.visit(timeInBank=12.0)) |
| 24 |
t = rv.expovariate(1.0/interval) |
| 25 |
yield hold,self,t |
| 26 |
|
| 27 |
def NoInSystem(R): |
| 28 |
""" The number of customers in the resource R |
| 29 |
in waitQ and active Q""" |
| 30 |
return (len(R.waitQ)+len(R.activeQ)) |
| 31 |
|
| 32 |
class Customer(Process): |
| 33 |
""" Customer arrives, is served and leaves """ |
| 34 |
def __init__(self,name): |
| 35 |
Process.__init__(self) |
| 36 |
self.name = name |
| 37 |
|
| 38 |
def visit(self,timeInBank=0): |
| 39 |
arrive=now() |
| 40 |
Qlength = [NoInSystem(counter[i]) for i in range(Nc)] |
| 41 |
if gui.params.trace: |
| 42 |
gui.writeConsole("%7.4f %s: Here I am. Queues are: %s"%(now(),self.name,Qlength)) |
| 43 |
for i in range(Nc): |
| 44 |
if Qlength[i] ==0 or Qlength[i]==min(Qlength): join =i ; break |
| 45 |
yield request,self,counter[join] |
| 46 |
wait=now()-arrive |
| 47 |
waitMonitor.observe(wait,t=now()) |
| 48 |
if gui.params.trace: |
| 49 |
gui.writeConsole("%7.4f %s: Waited %6.3f"%(now(),self.name,wait)) |
| 50 |
tib = counterRV.expovariate(1.0/timeInBank) |
| 51 |
yield hold,self,tib |
| 52 |
yield release,self,counter[join] |
| 53 |
if gui.params.trace: |
| 54 |
gui.writeConsole("%7.4f %s: Finished "%(now(),self.name)) |
| 55 |
|
| 56 |
def model(counterseed=3939393): |
| 57 |
global Nc,counter,counterRV,waitMonitor |
| 58 |
Nc = 2 |
| 59 |
counter = [Resource(name="Clerk0"),Resource(name="Clerk1")] |
| 60 |
counterRV = Random(counterseed) |
| 61 |
gui.mon1=waitMonitor = Monitor("Customer waiting times") |
| 62 |
waitMonitor.tlab="arrival time" |
| 63 |
waitMonitor.ylab="waiting time" |
| 64 |
initialize() |
| 65 |
source = Source(seed = gui.params.sourceseed) |
| 66 |
activate(source,source.generate(gui.params.numberCustomers |
| 67 |
,gui.params.interval),0.0) |
| 68 |
result=simulate(until=gui.params.endtime) |
| 69 |
gui.writeStatusLine(text="Time at simulation end: %.1f -- Customers still waiting: %s" |
| 70 |
%(now(),len(counter[0].waitQ)+len(counter[1].waitQ))) |
| 71 |
|
| 72 |
def statistics(): |
| 73 |
if gui.noRunYet: |
| 74 |
showwarning(title='Model warning', |
| 75 |
message="Run simulation first -- no data available.") |
| 76 |
return |
| 77 |
gui.writeConsole(text="\nRun parameters: %s"%gui.params) |
| 78 |
gui.writeConsole(text="Average wait for %4d customers was %6.2f"% |
| 79 |
(waitMonitor.count(), waitMonitor.mean())) |
| 80 |
|
| 81 |
def run(): |
| 82 |
model(gui.params.counterseed) |
| 83 |
gui.noRunYet=False |
| 84 |
|
| 85 |
def showAuthors(): |
| 86 |
gui.showTextBox(text="Tony Vignaux\nKlaus Muller",title="Author information") |
| 87 |
|
| 88 |
class MyGUI(SimGUI): |
| 89 |
def __init__(self,win,**p): |
| 90 |
SimGUI.__init__(self,win,**p) |
| 91 |
self.help.add_command(label="Author(s)", |
| 92 |
command=showAuthors,underline=0) |
| 93 |
self.view.add_command(label="Statistics", |
| 94 |
command=statistics,underline=0) |
| 95 |
self.run.add_command(label="Run bank11 model", |
| 96 |
command=run,underline=0) |
| 97 |
|
| 98 |
self.params=Parameters( |
| 99 |
endtime=2000, |
| 100 |
sourceseed=1133, |
| 101 |
counterseed=3939393, |
| 102 |
numberCustomers=50, |
| 103 |
interval=10.0, |
| 104 |
trace=0) |
| 105 |
|
| 106 |
root=Tk() |
| 107 |
gui=MyGUI(root,title="SimPy GUI example",doc=__doc__,consoleHeight=40) |
| 108 |
gui.mainloop() |