我一直在试验Simpy中的总线故障示例 手册,我真的很难理解为什么我创建 多个Bus实例,最后一个实例似乎得到了“ 序列“在第一次修复后。我修改了示例代码 手册非常略低于initialize()语句下面的 创建两个总线实例(Bus1和Bus2)。这是我的代码:
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()
以上给出了以下输出:
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
现在,问题是:在t = 600点,为什么总线1会被修复 在2号巴士发生故障之前?我原以为两辆公共汽车都会 在“锁定步骤”中分解并修复。 此外,如果我创建四个总线,前三个失败并得到修复 在“锁步”中如下图所示;然而,4号巴士下车顺序为20 第一次修复后。我无法弄清楚为什么会发生这种情况 欣赏任何人都可以提供的任何见解。它永远 发生在最后一个例子。
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
谢谢, 西摩
答案 0 :(得分:0)
这似乎按预期工作。必须在总线类中初始化和激活故障过程。
from SimPy.Simulation import *
class Bus(Process):
def __init__(self,name):
Process.__init__(self,name)
self.name = name
self.br=Breakdown(self)
activate(self.br,self.br.breakBus(300))
def operate(self,repairduration,triplength): # PEM
tripleft = triplength
while tripleft > 0:
yield hold,self,tripleft # try to finish the trip
if self.interrupted():
print self.interruptCause.name, 'at %s' %now()
tripleft=self.interruptLeft
self.interruptReset() # end self-interrupted state
# update next breakdown time
reactivate(self.br,delay=repairduration)
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)
activate(b,b.operate(repairduration=20,triplength=1000))
simulate(until=4000)
print 'SimPy: No more events at time %s' %now()
这导致四个Bus同时断开并同时修复,如下面的输出所示:
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
Breakdown Bus4 at 600
Bus1 repaired at 620
Bus2 repaired at 620
Bus3 repaired at 620
Bus4 repaired at 620
Breakdown Bus1 at 900
Breakdown Bus2 at 900
Breakdown Bus3 at 900
Breakdown Bus4 at 900
Bus1 repaired at 920
Bus2 repaired at 920
Bus3 repaired at 920
Bus4 repaired at 920
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 1200