| 1 |
""" bank08 |
| 2 |
|
| 3 |
A counter with a random service time |
| 4 |
and customers who renege. Based on the program bank08.py |
| 5 |
from TheBank tutorial. (KGM) |
| 6 |
""" |
| 7 |
from SimPy.Simulation import * |
| 8 |
from random import expovariate, seed, uniform |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class Source(Process): |
| 13 |
""" Source generates customers randomly""" |
| 14 |
|
| 15 |
def generate(self,number,interval,counter): |
| 16 |
for i in range(number): |
| 17 |
c = Customer(name = "Customer%02d"%(i,)) |
| 18 |
activate(c,c.visit(counter,timeInBank=12.0)) |
| 19 |
t = expovariate(1.0/interval) |
| 20 |
yield hold,self,t |
| 21 |
|
| 22 |
class Customer(Process): |
| 23 |
""" Customer arrives, is served and leaves """ |
| 24 |
|
| 25 |
def visit(self,counter,timeInBank=0): |
| 26 |
arrive=now() |
| 27 |
print "%7.4f %s: Here I am "%(now(),self.name) |
| 28 |
|
| 29 |
yield (request,self,counter),(hold,self,Customer.patience()) |
| 30 |
|
| 31 |
if self.acquired(counter): |
| 32 |
wait=now()-arrive |
| 33 |
print "%7.4f %s: Waited %6.3f"%(now(),self.name,wait) |
| 34 |
tib = expovariate(1.0/timeInBank) |
| 35 |
yield hold,self,tib |
| 36 |
yield release,self,counter |
| 37 |
print "%7.4f %s: Finished"%(now(),self.name) |
| 38 |
else: |
| 39 |
wait=now()-arrive |
| 40 |
print "%7.4f %s: RENEGED after %6.3f"%(now(),self.name,wait) |
| 41 |
|
| 42 |
def fpatience(minpatience=0,maxpatience=10000000000): |
| 43 |
while True: |
| 44 |
yield uniform(minpatience,maxpatience) |
| 45 |
fpatience=staticmethod(fpatience) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
def model(): |
| 50 |
counter = Resource(name="Karen") |
| 51 |
pat=Customer.fpatience(minpatience=1,maxpatience=3) |
| 52 |
Customer.patience=pat.next |
| 53 |
initialize() |
| 54 |
source = Source('Source') |
| 55 |
activate(source,source.generate(NumCustomers, |
| 56 |
interval=IntervalCustomers, |
| 57 |
counter=counter)) |
| 58 |
simulate(until=maxTime) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
maxTime = 400.0 |
| 63 |
theseed = 12345 |
| 64 |
NumCustomers = 5 |
| 65 |
IntervalCustomers = 10.0 |
| 66 |
|
| 67 |
|
| 68 |
seed(theseed) |
| 69 |
print 'bank08renege' |
| 70 |
model() |
| 71 |
|