Debian Codegraph @

docs/examples/Carwash.py

raw symbols SCIP
1 from SimPy.Simulation import *
2 import random
3 """carwash.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 ##Data:
18 nrMachines=2
19 tInter=2 #minutes
20 washtime=3.5 #minutes
21 initialSeed=123456
22 simTime=100
23
24 #####################################################
25 ## Model 1: Carwash is master, car is slave
26 #####################################################
27
28 class Carwash(Process):
29 """Carwash machine; master"""
30 def __init__(self,name):
31 Process.__init__(self,name)
32 self.carBeingWashed=None
33
34 def lifecycle(self):
35 while True:
36 yield get,self,waitingCars,1
37 self.carBeingWashed=self.got[0]
38 yield hold,self,washtime
39 self.carBeingWashed.doneSignal.signal(self.name)
40
41 class Car(Process):
42 """Car; slave"""
43 def __init__(self,name):
44 Process.__init__(self,name)
45 self.doneSignal=SimEvent()
46 def lifecycle(self):
47 yield put,self,waitingCars,[self]
48 yield waitevent,self,self.doneSignal
49 whichWash=self.doneSignal.signalparam
50 print "%s: %s done by %s"%(now(),self.name,whichWash)
51
52 class CarGenerator(Process):
53 """Car arrival generation"""
54 def generate(self):
55 i=0
56 while True:
57 yield hold,self,r.expovariate(1.0/tInter)
58 c=Car("car%s"%i)
59 activate(c,c.lifecycle())
60 i+=1
61
62 print "Model 1: carwash is master"
63 print "--------------------------"
64 initialize()
65 r=random.Random()
66 r.seed(initialSeed)
67 waiting=[]
68 for j in range(1,5):
69 c=Car("car%s"%-j)
70 activate(c,c.lifecycle())
71 waiting.append(c)
72 waitingCars=Store(capacity=40,initialBuffered=waiting)
73 cw=[]
74 for i in range(2):
75 c=Carwash("Carwash %s"%`i`)
76 cw.append(c)
77 activate(c,c.lifecycle())
78 cg=CarGenerator()
79 activate(cg,cg.generate())
80 simulate(until=simTime)
81 print "waiting cars: %s"%[x.name for x in waitingCars.theBuffer]
82 print "cars being washed: %s"%[y.carBeingWashed.name for y in cw]
83
84 #####################################################
85 ## Model 2: Car is master, carwash is slave
86 #####################################################
87
88 class CarM(Process):
89 """Car is master"""
90 def __init__(self,name):
91 Process.__init__(self,name)
92
93 def lifecycle(self):
94 yield get,self,washers,1
95 whichWash=self.got[0]
96 carsBeingWashed.append(self)
97 yield hold,self,washtime
98 print "%s: %s done by %s"%(now(),self.name,whichWash.name)
99 whichWash.doneSignal.signal()
100 carsBeingWashed.remove(self)
101
102 class CarwashS(Process):
103 def __init__(self,name):
104 Process.__init__(self,name)
105 self.doneSignal=SimEvent()
106 def lifecycle(self):
107 while True:
108 yield put,self,washers,[self]
109 yield waitevent,self,self.doneSignal
110
111 class CarGenerator1(Process):
112 def generate(self):
113 i=0
114 while True:
115 yield hold,self,r.expovariate(1.0/tInter)
116 c=CarM("car%s"%i)
117 activate(c,c.lifecycle())
118 i+=1
119
120 print "\nModel 2: car is master"
121 print "----------------------"
122 initialize()
123 r=random.Random()
124 r.seed(initialSeed)
125 washers=Store(capacity=nrMachines)
126 carsBeingWashed=[]
127 for j in range(1,5):
128 c=CarM("car%s"%-j)
129 activate(c,c.lifecycle())
130 for i in range(2):
131 cw=CarwashS("Carwash %s"%`i`)
132 activate(cw,cw.lifecycle())
133 cg=CarGenerator1()
134 activate(cg,cg.generate())
135 simulate(until=simTime)
136 print "waiting cars: %s"%[x.name for x in washers.getQ]
137 print "cars being washed: %s"%[x.name for x in carsBeingWashed]
138