2014-07-21 28 views
1

我想模擬一個情況,我們有5臺機器出現在1→3→1的情況。即中間的3個並行操作以減少它們的有效時間。SimPy資源3其中每個都有不同的特徵

我可以很容易地模擬這種通過創建一個SimPy資源有三個這樣的值:

simpy.Resource(env, capacity=3) 

但是在我的處境每三個資源的方式略有不同,有時工作我希望能夠使用他們中的任何一個(當我操作時)或預訂特定的一個(當我想清潔)。基本上這三臺機器以不同的速度慢慢地弄髒,並且運行速度較慢,我希望能夠模擬這些並且在太髒的時候也能夠進行清潔。

我已經嘗試了幾種模擬方法,但每次都遇到問題和問題。

第一個是當它預訂資源時,它還預訂了3臺機器(A,B,C)全局標誌之一和一個標誌本身,告訴它它正在使用哪臺機器。這有效,但它不乾淨,並且很難理解隨處可見的巨大if語句正在發生什麼。

第二個是它用三個獨立的資源,然後嘗試等待,並要求3的一臺機器的東西,如:

reqA = A.res.request() 
reqB = B.res.request() 
reqC = C.res.request() 

unitnumber = yield reqA | reqB | reqC 
yield env.process(batch_op(env, name, machineA, machineB, machineC, unitnumber)) 

但是,這並不工作,我不能工作了最好的方法來看待選擇之一。

什麼是模擬這種情況的最佳方法。爲了完整這裏是尋找什麼即時通訊爲:

  1. 請求任何3個機
  2. 請求特定的機器
  3. 的在每臺計算機跟蹤它的歷史
  4. 讓每個機器的特點是不同的。即在犯規得更快,但工程快最初
  5. 檢測,並安排一個乾淨的基礎上,性能或指標

這是我到目前爲止在我最新的嘗試每個模式作爲單獨的資源

版本
class Machine(object): 

    def __init__(self, env, cycletime, cleantime, k1foul, k2foul): 
     self.env = env 
     self.res = simpy.Resource(env, 1) 

     self.cycletime = cycletime 
     self.cleantime = cleantime 
     self.k1foul = k1foul 
     self.k2foul = k2foul 
     self.batchessinceclean = 0 

    def operate(self): 
     self.cycletime = self.cycletime + self.k2foul * np.log(self.k1foul * self.batchessinceclean + 1) 
     self.batchessinceclean += 1 
     yield self.env.timeout(self.cycletime) 

    def clean(self): 
     print('%s begin cleaning at %s' % (self.env.now)) 
     self.batchessinceclean = 0 
     yield env.timeout(self.cleantime) 
     print('%s finished cleaning at %s' % (self.env.now)) 

回答

4

你應該嘗試(過濾器)商店:

import simpy 


def user(machine): 
    m = yield machine.get() 
    print(m) 
    yield machine.put(m) 

    m = yield machine.get(lambda m: m['id'] == 1) 
    print(m) 
    yield machine.put(m) 

    m = yield machine.get(lambda m: m['health'] > 98) 
    print(m) 
    yield machine.put(m) 


env = simpy.Environment() 
machine = simpy.FilterStore(env, 3) 
machine.put({'id': 0, 'health': 100}) 
machine.put({'id': 1, 'health': 95}) 
machine.put({'id': 2, 'health': 97.2}) 

env.process(user(machine)) 

env.run() 
+0

再次感謝這個完美的作品! SimPy一直是一個偉大的工具,真正幫助我的建模。 – Alarr

相關問題