【问题标题】:Simpy - How to use Filter Store asynchronouslySimpy - 如何异步使用过滤器存储
【发布时间】:2017-04-10 14:52:41
【问题描述】:

我正在使用 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 进程返回,这就是为什么它等到一台机器完成修复后再开始另一台机器修复工作。商店里有 3 名技术人员,因此应该允许所有 3 人异步维修和退回机器。我如何实现这一目标?谢谢。

【问题讨论】:

    标签: python simpy


    【解决方案1】:

    您可以生成多个进程,然后使用Environment.all_of 等待它们:

    procs = [env.process(my_task() for _ in range(3))
    
    # do other stuff ...
    
    results = yield env.all_of(procs)
    

    【讨论】:

    • 这对我有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 2019-01-22
    • 1970-01-01
    • 2020-08-28
    • 2016-04-13
    • 2016-07-05
    相关资源
    最近更新 更多