2014-10-28 37 views
0

我使用的SimPy 2.3並且具有在以隨機速率的ATM和以隨機速率的客戶提供服務的另一過程生成的客戶的處理。當線路空了時,我希望ATM在做其他事情之前等待下一個客戶。收率保持直到其他線> 0

下面是一些代碼

class ATM(Process): 
    def Run(self): 
     while 1: 
      if self.atm_line.customers is 0: 
       yield hold, wait for self.atm_line.customers != 0 # this is the line I'm stuck on 
      yield hold, self, random.expovariate(.1) 
      self.atm_line.customers -= 1 

class ATM_Line(Process): 
    def __init__(self): 
     self.customers = 0 
     Process.__init__(self) 

    def Run(self): 
     while 1: 
      yield hold, self, random.expovariate(.1) 
      self.customers += 1 

initialize() 
a = ATM() 
a.atm_line = ATM_Line() 
activate(a, a.Run()) 
activate(a.atm_line, a.atm_line.Run()) 
simulate(until=10000) 

什麼是做到這一點的好辦法?

+1

您的僞代碼很難回答。 'line'從哪裏來?有什麼我們可以循環生成'線'(也許'hold'和'wait')? – abarnert 2014-10-28 01:04:39

+0

對不起,現在應該更清楚了。 Line只是ATM_Line的一個實例,不能迭代。 – Coat 2014-10-28 01:10:19

+2

您已經創建了一個名爲'a.line'的屬性,將'line'的一個實例更改爲'self.atm_line',並將另一個更改爲'line',但仍不完全清楚。另外,我不確定你的所有非Python代碼應該做什麼。 ATM流程是否真的應該讓循環每次循環更長,或者你的'++'不是和C中的一樣嗎?你能把這個寫成真正的Python,並且使用一致的變量名稱,而不是讓我們猜測它嗎? – abarnert 2014-10-28 01:13:20

回答

1

我能夠解決在使用收益waitevent和信號這個問題。工作代碼如下。

from SimPy.Simulation import * 
from random import Random, expovariate, uniform 

class ATM(Process): 
    def Run(self): 
     while 1: 
      if self.atm_line.customers is 0: 
       yield waitevent, self, self.atm_line.new_customer 
      yield hold, self, random.expovariate(.05) 
      self.atm_line.customers -= 1 

class ATM_Line(Process): 
    def __init__(self): 
     self.customers = 0 
     self.new_customer = SimEvent() 
     Process.__init__(self) 

    def Run(self): 
     while 1: 
      yield hold, self, random.expovariate(.07) 
      self.new_customer.signal() 
      self.customers += 1 

initialize() 
a = ATM() 
a.atm_line = ATM_Line() 
activate(a, a.Run()) 
activate(a.atm_line, a.atm_line.Run()) 
simulate(until=50) 
+0

如果這個答案解決了您的問題,並且您認爲這對其他有類似問題的人會有幫助,請記住接受您自己的答案。 – abarnert 2014-10-28 18:24:54

相關問題