Debian Codegraph @

docs/examples/Carwash_OO.py

raw symbols SCIP
1 from SimPy.Simulation import *
2 import random
3 """Carwash_OO.py
4 Scenario:
5 A carwash installation has nrMachines washing machines which wash a car in washTime minutes.
6 Cars arrive with a negative exponential interarrival time with a mean of tInter
7 minutes.
8
9 Model the carwash operation as cooperation between two processes, the car being
10 washed and the machine doing the washing.
11
12 Build two implementations:
13 Model 1: the machine is master, the car slave;
14 Model 2: the car is master, the machine is slave.
15
16 """
17 ## Experiment data -------------------------
18 nrMachines = 2
19 tInter = 2 #minutes
20 washtime = 3.5 #minutes
21 initialSeed = 123456
22 simTime = 100 #minutes
23
24 ## Model 1 components ----------------------
25
26 class Carwash(Process):
27 """Carwash machine; master"""
28 def __init__(self,name,sim):
29 Process.__init__(self,name=name,sim=sim)
30 self.carBeingWashed=None
31
32 def lifecycle(self):
33 while True:
34 yield get,self,self.sim.waitingCars,1
35 self.carBeingWashed=self.got[0]
36 yield hold,self,washtime
37 self.carBeingWashed.doneSignal.signal(self.name)
38
39 class Car(Process):
40 """Car; slave"""
41 def __init__(self,name,sim):
42 Process.__init__(self,name=name,sim=sim)
43 self.doneSignal=SimEvent(sim=sim)
44 def lifecycle(self):
45 yield put,self,self.sim.waitingCars,[self]
46 yield waitevent,self,self.doneSignal
47 whichWash = self.doneSignal.signalparam
48 print "%s: %s done by %s"%(self.sim.now(),self.name,whichWash)
49
50 class CarGenerator(Process):
51 """Car arrival generation"""
52 def generate(self):
53 i=0
54 while True:
55 yield hold,self,self.sim.r.expovariate(1.0/tInter)
56 c = Car("car%s"%i,sim=self.sim)
57 self.sim.activate(c,c.lifecycle())
58 i+=1
59
60 ## Model 1: Carwash is master, car is slave
61
62 class CarWashModel1(Simulation):
63 def run(self):
64 print "Model 1: carwash is master"
65 print "--------------------------"
66 self.initialize()
67 self.r=random.Random()
68 self.r.seed(initialSeed)
69 waiting=[]
70 for j in range(1,5):
71 c = Car("car%s"%-j,sim=self)
72 self.activate(c,c.lifecycle())
73 waiting.append(c)
74 self.waitingCars=Store(capacity=40,initialBuffered=waiting,sim=self)
75 cw=[]
76 for i in range(nrMachines):
77 c = Carwash("Carwash %s"%`i`,sim=self)
78 cw.append(c)
79 self.activate(c,c.lifecycle())
80 cg = CarGenerator(sim=self)
81 self.activate(cg,cg.generate())
82 self.simulate(until=simTime)
83
84 print "waiting cars: %s"%[x.name for x in self.waitingCars.theBuffer]
85 print "cars being washed: %s"%[y.carBeingWashed.name for y in cw]
86
87 ## Experiment 1 ----------------------------
88 CarWashModel1().run()
89
90 ############################################
91
92 ## Model 2 components ----------------------
93
94 class CarM(Process):
95 """Car is master"""
96
97 def lifecycle(self):
98 yield get,self,self.sim.washers,1
99 whichWash = self.got[0]
100 self.sim.carsBeingWashed.append(self)
101 yield hold,self,washtime
102 print "%s: %s done by %s"%(self.sim.now(),self.name,whichWash.name)
103 whichWash.doneSignal.signal()
104 self.sim.carsBeingWashed.remove(self)
105
106 class CarwashS(Process):
107 def __init__(self,name,sim):
108 Process.__init__(self,name=name,sim=sim)
109 self.doneSignal = SimEvent(sim=sim)
110 def lifecycle(self):
111 while True:
112 yield put,self,self.sim.washers,[self]
113 yield waitevent,self,self.doneSignal
114
115 class CarGenerator1(Process):
116 def generate(self):
117 i=0
118 while True:
119 yield hold,self,self.sim.r.expovariate(1.0/tInter)
120 c = CarM("car%s"%i,sim=self.sim)
121 self.sim.activate(c,c.lifecycle())
122 i+=1
123
124 ## Model 2: Car is master, carwash is slave
125
126 class CarWashModel2(Simulation):
127 def run(self):
128 print "\nModel 2: car is master"
129 print "----------------------"
130 self.initialize()
131 self.r=random.Random()
132 self.r.seed(initialSeed)
133 self.washers=Store(capacity=nrMachines,sim=self)
134 self.carsBeingWashed=[]
135 for j in range(1,5):
136 c = CarM("car%s"%-j,sim=self)
137 self.activate(c,c.lifecycle())
138 for i in range(2):
139 cw = CarwashS("Carwash %s"%`i`,sim=self)
140 self.activate(cw,cw.lifecycle())
141 cg = CarGenerator1(sim=self)
142 self.activate(cg,cg.generate())
143 self.simulate(until=simTime)
144
145 print "waiting cars: %s"%[x.name for x in self.washers.getQ]
146 print "cars being washed: %s"%[x.name for x in self.carsBeingWashed]
147
148 ## Experiment 1 ----------------------------
149 CarWashModel2().run()
150