| 1 |
"""bank14_OO: *waituntil* the Bank door opens""" |
| 2 |
from SimPy.Simulation import Simulation, Process, Resource, hold, waituntil, request, release |
| 3 |
from random import expovariate, seed |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class Doorman(Process): |
| 8 |
""" Doorman opens the door""" |
| 9 |
def openthedoor(self): |
| 10 |
""" He will open the door when he arrives""" |
| 11 |
yield hold, self, expovariate(1.0 / 10.0) |
| 12 |
self.sim.door = 'Open' |
| 13 |
print("%7.4f Doorman: Ladies and "\ |
| 14 |
"Gentlemen! You may all enter." % (self.sim.now())) |
| 15 |
|
| 16 |
|
| 17 |
class Source(Process): |
| 18 |
""" Source generates customers randomly""" |
| 19 |
def generate(self, number, rate): |
| 20 |
for i in range(number): |
| 21 |
c = Customer(name="Customer%02d" % (i), sim=self.sim) |
| 22 |
self.sim.activate(c, c.visit(timeInBank=12.0)) |
| 23 |
yield hold, self, expovariate(rate) |
| 24 |
|
| 25 |
|
| 26 |
class Customer(Process): |
| 27 |
""" Customer arrives, is served and leaves """ |
| 28 |
def visit(self, timeInBank=10): |
| 29 |
arrive = self.sim.now() |
| 30 |
|
| 31 |
if self.sim.dooropen(): |
| 32 |
msg = ' and the door is open.' |
| 33 |
else: |
| 34 |
msg = ' but the door is shut.' |
| 35 |
print("%7.4f %s: Here I am%s" % (self.sim.now(), self.name, msg)) |
| 36 |
|
| 37 |
yield waituntil, self, self.sim.dooropen |
| 38 |
|
| 39 |
print("%7.4f %s: I can go in!" % (self.sim.now(), self.name)) |
| 40 |
wait = self.sim.now() - arrive |
| 41 |
print("%7.4f %s: Waited %6.3f" % (self.sim.now(), self.name, wait)) |
| 42 |
|
| 43 |
yield request, self, self.sim.counter |
| 44 |
tib = expovariate(1.0 / timeInBank) |
| 45 |
yield hold, self, tib |
| 46 |
yield release, self, self.sim.counter |
| 47 |
|
| 48 |
print("%7.4f %s: Finished " % (self.sim.now(), self.name)) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class BankModel(Simulation): |
| 53 |
def dooropen(self): |
| 54 |
return self.door == 'Open' |
| 55 |
|
| 56 |
def run(self, aseed): |
| 57 |
""" PEM """ |
| 58 |
seed(aseed) |
| 59 |
self.counter = Resource(capacity=1, name="Clerk", sim=self) |
| 60 |
self.door = 'Shut' |
| 61 |
doorman = Doorman(sim=self) |
| 62 |
self.activate(doorman, doorman.openthedoor()) |
| 63 |
source = Source(sim=self) |
| 64 |
self.activate(source, |
| 65 |
source.generate(number=5, rate=0.1), at=0.0) |
| 66 |
self.simulate(until=400.0) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
maxTime = 2000.0 |
| 71 |
seedVal = 393939 |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
mymodel = BankModel() |
| 76 |
mymodel.run(aseed=seedVal) |
| 77 |
|