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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
from SimPy.Simulation import *
import random
"""Carwash_OO.py
Scenario:
A carwash installation has nrMachines washing machines which wash a car in washTime minutes.
Cars arrive with a negative exponential interarrival time with a mean of tInter
minutes.
Model the carwash operation as cooperation between two processes, the car being
washed and the machine doing the washing.
Build two implementations:
Model 1: the machine is master, the car slave;
Model 2: the car is master, the machine is slave.
"""
## Experiment data -------------------------
nrMachines = 2
tInter = 2 #minutes
washtime = 3.5 #minutes
initialSeed = 123456
simTime = 100 #minutes
## Model 1 components ----------------------
class Carwash(Process):
"""Carwash machine; master"""
def __init__(self,name,sim):
Process.__init__(self,name=name,sim=sim)
self.carBeingWashed=None
def lifecycle(self):
while True:
yield get,self,self.sim.waitingCars,1
self.carBeingWashed=self.got[0]
yield hold,self,washtime
self.carBeingWashed.doneSignal.signal(self.name)
class Car(Process):
"""Car; slave"""
def __init__(self,name,sim):
Process.__init__(self,name=name,sim=sim)
self.doneSignal=SimEvent(sim=sim)
def lifecycle(self):
yield put,self,self.sim.waitingCars,[self]
yield waitevent,self,self.doneSignal
whichWash = self.doneSignal.signalparam
print "%s: %s done by %s"%(self.sim.now(),self.name,whichWash)
class CarGenerator(Process):
"""Car arrival generation"""
def generate(self):
i=0
while True:
yield hold,self,self.sim.r.expovariate(1.0/tInter)
c = Car("car%s"%i,sim=self.sim)
self.sim.activate(c,c.lifecycle())
i+=1
## Model 1: Carwash is master, car is slave
class CarWashModel1(Simulation):
def run(self):
print "Model 1: carwash is master"
print "--------------------------"
self.initialize()
self.r=random.Random()
self.r.seed(initialSeed)
waiting=[]
for j in range(1,5):
c = Car("car%s"%-j,sim=self)
self.activate(c,c.lifecycle())
waiting.append(c)
self.waitingCars=Store(capacity=40,initialBuffered=waiting,sim=self)
cw=[]
for i in range(nrMachines):
c = Carwash("Carwash %s"%`i`,sim=self)
cw.append(c)
self.activate(c,c.lifecycle())
cg = CarGenerator(sim=self)
self.activate(cg,cg.generate())
self.simulate(until=simTime)
print "waiting cars: %s"%[x.name for x in self.waitingCars.theBuffer]
print "cars being washed: %s"%[y.carBeingWashed.name for y in cw]
## Experiment 1 ----------------------------
CarWashModel1().run()
############################################
## Model 2 components ----------------------
class CarM(Process):
"""Car is master"""
def lifecycle(self):
yield get,self,self.sim.washers,1
whichWash = self.got[0]
self.sim.carsBeingWashed.append(self)
yield hold,self,washtime
print "%s: %s done by %s"%(self.sim.now(),self.name,whichWash.name)
whichWash.doneSignal.signal()
self.sim.carsBeingWashed.remove(self)
class CarwashS(Process):
def __init__(self,name,sim):
Process.__init__(self,name=name,sim=sim)
self.doneSignal = SimEvent(sim=sim)
def lifecycle(self):
while True:
yield put,self,self.sim.washers,[self]
yield waitevent,self,self.doneSignal
class CarGenerator1(Process):
def generate(self):
i=0
while True:
yield hold,self,self.sim.r.expovariate(1.0/tInter)
c = CarM("car%s"%i,sim=self.sim)
self.sim.activate(c,c.lifecycle())
i+=1
## Model 2: Car is master, carwash is slave
class CarWashModel2(Simulation):
def run(self):
print "\nModel 2: car is master"
print "----------------------"
self.initialize()
self.r=random.Random()
self.r.seed(initialSeed)
self.washers=Store(capacity=nrMachines,sim=self)
self.carsBeingWashed=[]
for j in range(1,5):
c = CarM("car%s"%-j,sim=self)
self.activate(c,c.lifecycle())
for i in range(2):
cw = CarwashS("Carwash %s"%`i`,sim=self)
self.activate(cw,cw.lifecycle())
cg = CarGenerator1(sim=self)
self.activate(cg,cg.generate())
self.simulate(until=simTime)
print "waiting cars: %s"%[x.name for x in self.washers.getQ]
print "cars being washed: %s"%[x.name for x in self.carsBeingWashed]
## Experiment 1 ----------------------------
CarWashModel2().run()
|