2017-04-10 46 views
0

我正在使用Simpy來對機器修復工作進行建模。機器可以進入工廠進行修理,分配技術人員,然後進行修理直至完成。Simpy - 如何異步使用過濾器存儲

我正在使用Filter Store來跟蹤技術人員。

NUM_TECHNICIANS = 3    # number of technicians 
REPAIR_TIME = 5    # time to repair machine in hours 

def task_network(technicians): 

    # Request a technician  
    t = yield technicians.get() 
    print("Assigned to Technician %d at time %d" % (t.id, env.now)) 
    yield env.process(t.repair_machine(machine1)) 

    t = yield technicians.get(lambda t: t.fatigue < 30) 
    print("Assigned to Technician %d at time %d" % (t.id, env.now)) 
    yield env.process(t.repair_machine(machine2)) 

class Machine(object): 
    def __init__(self, env, id, type, isBroken): 
     self.env = env 
     self.id = id 
     self.type = type 
     self.isBroken = isBroken 

class Technician(object): 
    def __init__(self, env, id, skill_level, fatigue, shifts_remaining): 
     self.env = env 
     self.id = id 
     self.skill_level = skill_level 
     self.fatigue = fatigue 
     self.shifts_remaining = shifts_remaining 
     self.isAvailable = True 

    def repair_machine(self, machine): 
     if machine.type == "MN152": 
      self.fatigue += 10 
      self.shifts_remaining -= 0.25 
      self.isAvailable = False 
      print("Repairing...") 
      yield env.timeout(REPAIR_TIME) 
      print("Technician %d is done repairing at time %d" % (self.id, env.now)) 

env = simpy.Environment() 

# Filter Store allows us to have processes ask for objects as resources (the technicians) 
# and get them based off of some criteria (e.g. this radio is complex so requires a more experienced technician) 
# If no criteria is specified, Filter Store is FIFO 
technicians = simpy.FilterStore(env, capacity = NUM_TECHNICIANS) 

t0 = Technician(env, id=0, skill_level=74, fatigue=15, shifts_remaining=2) 
t1 = Technician(env, id=1, skill_level=45, fatigue=50, shifts_remaining=1) 
t2 = Technician(env, id=2, skill_level=56, fatigue=0, shifts_remaining=3) 

technicians.put(jt0) 
technicians.put(jt1) 
technicians.put(jt2) 

machine1 = Machine(env, id=0, type="MN150", isBroken=True) 
machine2 = Machine(env, id=1, type="MN152", isBroken=True) 

env.process(task_network(technicians)) 
env.run() 

上面的代碼運行,因爲它應該和打印

Assigned to Technician 0 at time 0 
Repairing... 
Technician 0 is done repairing at time 5 
Assigned to Technician 2 at time 5 
Repairing... 
Technician 2 is done repairing at time 10 

但是,我怎麼讓這樣的機器可以進來,進行分配,並修復和異步返回?目前(因爲我的yield語句),仿真會暫停,直到yield進程返回,這就是爲什麼在啓動另一臺機器修復作業之前它會一直等到一臺機器完成修復。在商店裏有三名技術人員,所以它應該允許所有三個人異步修理和返回機器。我如何實現這一目標?謝謝。

回答

2

可以產生多個進程,然後使用Environment.all_of等待他們:

procs = [env.process(my_task() for _ in range(3)) 

# do other stuff ... 

results = yield env.all_of(procs) 
+0

這幫助了我。謝謝! – noblerare