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
|
from SimPy.Simulation import *
import random
"""carwash.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.
"""
##Data:
nrMachines=2
tInter=2 #minutes
washtime=3.5 #minutes
initialSeed=123456
simTime=100
#####################################################
## Model 1: Carwash is master, car is slave
#####################################################
class Carwash(Process):
"""Carwash machine; master"""
def __init__(self,name):
Process.__init__(self,name)
self.carBeingWashed=None
def lifecycle(self):
while True:
yield get,self,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):
Process.__init__(self,name)
self.doneSignal=SimEvent()
def lifecycle(self):
yield put,self,waitingCars,[self]
yield waitevent,self,self.doneSignal
whichWash=self.doneSignal.signalparam
print "%s: %s done by %s"%(now(),self.name,whichWash)
class CarGenerator(Process):
"""Car arrival generation"""
def generate(self):
i=0
while True:
yield hold,self,r.expovariate(1.0/tInter)
c=Car("car%s"%i)
activate(c,c.lifecycle())
i+=1
print "Model 1: carwash is master"
print "--------------------------"
initialize()
r=random.Random()
r.seed(initialSeed)
waiting=[]
for j in range(1,5):
c=Car("car%s"%-j)
activate(c,c.lifecycle())
waiting.append(c)
waitingCars=Store(capacity=40,initialBuffered=waiting)
cw=[]
for i in range(2):
c=Carwash("Carwash %s"%`i`)
cw.append(c)
activate(c,c.lifecycle())
cg=CarGenerator()
activate(cg,cg.generate())
simulate(until=simTime)
print "waiting cars: %s"%[x.name for x in waitingCars.theBuffer]
print "cars being washed: %s"%[y.carBeingWashed.name for y in cw]
#####################################################
## Model 2: Car is master, carwash is slave
#####################################################
class CarM(Process):
"""Car is master"""
def __init__(self,name):
Process.__init__(self,name)
def lifecycle(self):
yield get,self,washers,1
whichWash=self.got[0]
carsBeingWashed.append(self)
yield hold,self,washtime
print "%s: %s done by %s"%(now(),self.name,whichWash.name)
whichWash.doneSignal.signal()
carsBeingWashed.remove(self)
class CarwashS(Process):
def __init__(self,name):
Process.__init__(self,name)
self.doneSignal=SimEvent()
def lifecycle(self):
while True:
yield put,self,washers,[self]
yield waitevent,self,self.doneSignal
class CarGenerator1(Process):
def generate(self):
i=0
while True:
yield hold,self,r.expovariate(1.0/tInter)
c=CarM("car%s"%i)
activate(c,c.lifecycle())
i+=1
print "\nModel 2: car is master"
print "----------------------"
initialize()
r=random.Random()
r.seed(initialSeed)
washers=Store(capacity=nrMachines)
carsBeingWashed=[]
for j in range(1,5):
c=CarM("car%s"%-j)
activate(c,c.lifecycle())
for i in range(2):
cw=CarwashS("Carwash %s"%`i`)
activate(cw,cw.lifecycle())
cg=CarGenerator1()
activate(cg,cg.generate())
simulate(until=simTime)
print "waiting cars: %s"%[x.name for x in washers.getQ]
print "cars being washed: %s"%[x.name for x in carsBeingWashed]
|