Simpy - 如何异步使用过滤器存储

稀有

我正在使用 Simpy 来模拟机器维修工作。机器可以进入工厂进行维修,指派一名技术人员,然后进行维修,直到完成。

我正在使用过滤器商店来跟踪技术人员。

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 名技术人员异步维修和归还机器。我如何实现这一目标?谢谢你。

斯蒂芬·舍夫克

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

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

# do other stuff ...

results = yield env.all_of(procs)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章