Debian Codegraph @

docs/examples/bank08renege_OO.py

raw symbols SCIP
1 """ bank08_OO
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 ## Model components ------------------------
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,),sim=self.sim)
18 self.sim.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 = self.sim.now()
27 print "%7.4f %s: Here I am "%(self.sim.now(),self.name)
28
29 yield (request,self,counter),(hold,self,Customer.patience())
30
31 if self.acquired(counter):
32 wait = self.sim.now()-arrive
33 print "%7.4f %s: Waited %6.3f"%(self.sim.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"%(self.sim.now(),self.name)
38 else:
39 wait = self.sim.now()-arrive
40 print "%7.4f %s: RENEGED after %6.3f"%(self.sim.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 ## Model ---------------------------------------------
48
49 class BankModel(Simulation):
50 def run(self):
51 self.initialize()
52 counter = Resource(name="Karen",sim=self)
53 pat = Customer.fpatience(minpatience=1,maxpatience=3)
54 Customer.patience=pat.next
55 source = Source(name='Source',sim=self)
56 self.activate(source,source.generate(NumCustomers,
57 interval=IntervalCustomers,
58 counter=counter))
59 self.simulate(until=maxTime)
60
61 ## Experiment data -------------------------
62
63 maxTime = 400.0
64 theseed = 12345
65 NumCustomers = 5
66 IntervalCustomers = 10.0
67 ## Experiment ------------------------------
68
69 seed(theseed)
70 print 'bank08_OO_renege'
71 BankModel().run()
72