2012-05-16 21 views
0

我一直在試驗Simpy 手冊中的總線分解示例,並且我非常努力地理解爲什麼當我創建多個總線實例時,最後一個實例似乎得到了「第一次修復後「 序列」。我修改了 手冊中的示例代碼,稍微在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個GET巴士前修好 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

這似乎按預期方式工作。分類過程必須在總線類中初始化並激活。

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() 

這導致斷裂的同時,並在同一時間被修復,如由下面的輸出總線的四個實例:

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