I have been experimenting with the Bus breakdown example in the Simpy manual and am really struggling to understand why when I create multiple instances of Bus, the last instance seems to get "of sequence" after the first repair. I have modified the example code in the manual very slightly just below the initialize() statement to create two instances of Bus (Bus1 and Bus2). Here is my code:
from SimPy.Simulation import *
class Bus(Process):
def operate(self,repairduration,triplength): # PEM
tripleft = triplength
# "tripleft" is the driving time to finish trip
# if there are no further breakdowns
while tripleft > 0:
yield hold,self,tripleft # try to finish the trip
# if a breakdown intervenes
if self.interrupted():
print self.interruptCause.name, 'at %s' %now()
tripleft=self.interruptLeft
# update driving time to finish
# the trip if no more breakdowns
self.interruptReset() # end self-interrupted state
# update next breakdown time
reactivate(br,delay=repairduration)
# impose delay for repairs on self
yield hold,self,repairduration
print '%s repaired at %s' %(self.name, now())
else: # no breakdowns intervened, so bus finished trip
break
print 'Bus has arrived at %s' %now()
class Breakdown(Process):
def __init__(self,myBus):
Process.__init__(self,name='Breakdown '+myBus.name)
self.bus=myBus
def breakBus(self,interval): # Process Execution Method
while True:
yield hold,self,interval # driving time between breakdowns
if self.bus.terminated(): break
# signal "self.bus" to break itself down
self.interrupt(self.bus)
initialize()
for i in range(1,5):
b=Bus('Bus%s' %i) # create a Bus object "b" called "Bus"
activate(b,b.operate(repairduration=20,triplength=1000))
# create a Breakdown object "br" for bus "b", and
br=Breakdown(b)
# activate it with driving time between
# breakdowns equal to 300
activate(br,br.breakBus(300))
simulate(until=4000)
print 'SimPy: No more events at time %s' %now()
The above gives the following output:
Breakdown Bus1 at 300
Breakdown Bus2 at 300
Bus1 repaired at 320
Bus2 repaired at 320
Breakdown Bus1 at 600
Bus1 repaired at 620
Breakdown Bus2 at 620
Bus2 repaired at 640
Breakdown Bus1 at 900
Bus1 repaired at 920
Breakdown Bus2 at 920
Bus2 repaired at 940
Bus has arrived at 1060
Bus has arrived at 1060
SimPy: No more events at time 1240
Now, the question: at the t=600 point, why does Bus 1 get repaired before Bus 2 breaks down? I would have expected that both Buses would break down and get repaired in "lock step." Further, if I create four Buses, the first three fail and get repaired in "lock step" as shown below; however, Bus 4 gets off sequence by 20 after the first repair. I cannot figure out why this happens and would appreciate any insight that anyone might be able to provide. It always happens to the last instance.
Breakdown Bus1 at 300
Breakdown Bus2 at 300
Breakdown Bus3 at 300
Breakdown Bus4 at 300
Bus1 repaired at 320
Bus2 repaired at 320
Bus3 repaired at 320
Bus4 repaired at 320
Breakdown Bus1 at 600
Breakdown Bus2 at 600
Breakdown Bus3 at 600
Bus1 repaired at 620
Bus2 repaired at 620
Bus3 repaired at 620
Breakdown Bus4 at 620
Bus4 repaired at 640
Breakdown Bus1 at 900
Breakdown Bus2 at 900
Breakdown Bus3 at 900
Bus1 repaired at 920
Bus2 repaired at 920
Bus3 repaired at 920
Breakdown Bus4 at 920
Bus4 repaired at 940
Bus has arrived at 1060
Bus has arrived at 1060
Bus has arrived at 1060
Bus has arrived at 1060
SimPy: No more events at time 1240
Thanks, Seymour
This seems to work as expected. The breakdown process has to be initialized and activated within the Bus class.
This results in the four instances of Bus breaking at the same time and being repaired at the same time, as shown by the following output: