File: bank08renege_OO.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (72 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
""" bank08_OO

 A counter with a random service time
 and customers who renege. Based on the program bank08.py
 from TheBank tutorial. (KGM)
"""
from SimPy.Simulation  import *
from random import expovariate, seed, uniform

## Model components ------------------------

class Source(Process):
    """ Source generates customers randomly"""

    def generate(self,number,interval,counter):       
        for i in range(number):
            c = Customer(name = "Customer%02d"%(i,),sim=self.sim)
            self.sim.activate(c,c.visit(counter,timeInBank=12.0))
            t = expovariate(1.0/interval)
            yield hold,self,t

class Customer(Process):
    """ Customer arrives, is served and leaves """
        
    def visit(self,counter,timeInBank=0):       
        arrive = self.sim.now()
        print "%7.4f %s: Here I am     "%(self.sim.now(),self.name)

        yield (request,self,counter),(hold,self,Customer.patience())

        if self.acquired(counter):
            wait = self.sim.now()-arrive
            print "%7.4f %s: Waited %6.3f"%(self.sim.now(),self.name,wait)
            tib = expovariate(1.0/timeInBank)            
            yield hold,self,tib                          
            yield release,self,counter
            print "%7.4f %s: Finished"%(self.sim.now(),self.name)
        else:
            wait = self.sim.now()-arrive
            print "%7.4f %s: RENEGED after %6.3f"%(self.sim.now(),self.name,wait)            
        
    def fpatience(minpatience=0,maxpatience=10000000000):
        while True:
            yield uniform(minpatience,maxpatience)
    fpatience=staticmethod(fpatience)

## Model ---------------------------------------------

class BankModel(Simulation):
    def run(self):    
        self.initialize()
        counter = Resource(name="Karen",sim=self) 
        pat = Customer.fpatience(minpatience=1,maxpatience=3)
        Customer.patience=pat.next
        source = Source(name='Source',sim=self)
        self.activate(source,source.generate(NumCustomers,
                                    interval=IntervalCustomers,
                                    counter=counter))
        self.simulate(until=maxTime)

## Experiment data -------------------------

maxTime = 400.0
theseed = 12345
NumCustomers = 5
IntervalCustomers = 10.0
## Experiment ------------------------------

seed(theseed)
print 'bank08_OO_renege'
BankModel().run()