在Simpy中,我如何同步两个进程以启动第三个进程?

问题描述 投票:0回答:1

我试图模拟一个有多台机器的生产过程。机器1的体积容量为1升,机器2的体积容量为2升。机器2的体积容量为2升。我们要生产2升的化学品X,所以我们在机器1中放入1升的原材料。完成后,将这1L移到机器2上。同时,在机器1上再运行一轮,再投入第二批1L。然后将其加入到已经装了一半的Machine2中。然后启动Machine2,最终生产两升。如果Machine1需要3个小时,Machine2需要12个小时,那么这个过程就需要3+3+12=18个小时。

现在我的方法如下。我将Machine1创建为容量为1的资源,将Machine2创建为容量为2的资源。我希望在两个批次都通过Machine1后启动Machine2。我的尝试代码如下。

import simpy
from collections import namedtuple

Chemical = namedtuple("Chemical", "name")

X_batch1 = Chemical("X_batch1")
X_batch2 = Chemical("X_batch2")


def do_process_1(env, machine1, Chemical):
    print(f"Chemical {Chemical.name} requested Machine1 at {env.now}")
    with machine1.request() as req:
        yield req
        print(f"Chemical {Chemical.name} got into Machine1 at {env.now}")
        yield env.timeout(3)
        print(f"Chemical {Chemical.name} got out of Machine1 at {env.now}")
        # yield env.timeout(2)


def do_process_2(env, machine1, machine2, Chemicals):

    # <some condition to check if process 1 is done for both Chemicals>

    # make each batch for machine2, one after the other
    print(f"{Chemicals[0]} asks for Machine2 at {env.now}.")
    print(f"{Chemicals[1]} asks for Machine2 at {env.now}.")
    with machine2.request() as req1, machine2.request() as req2:
        yield req1 & req2
        print(f"{Chemicals[0]} gets machine2 at {env.now}.")
        print(f"{Chemicals[1]} gets Machine2 at {env.now}.")
        yield env.timeout(12)
    print(f"{Chemicals[0]} got out of machine2 at {env.now}.")
    print(f"{Chemicals[1]} got out of machine2 at {env.now}.")


env = simpy.Environment()
machine1 = simpy.Resource(env, 1)
machine2 = simpy.Resource(env, 2)

env.process(do_process_1(env, machine1, X_batch1))
env.process(do_process_1(env, machine1, X_batch2))
env.process(do_process_2(env, machine2, machine1, (X_batch1, X_batch2)))
env.run(until=60)

我在上面有两个路障

  1. 机器2应该在两台机器都通过机器1之后才会被请求。我不知道如何添加这个条件,在哪里添加。
  2. Machine2必须被请求两次(按不同批次),之后应该继续开始工作。当只有一个请求时,它不应该开始工作。

如果有任何帮助,我将非常感激


python simulation yield simpy event-simulation
1个回答
0
投票

我用下面的代码解决了自己的问题。简而言之,我将第一台机器的过程保存在一个变量中,并将该变量传递给第二台机器。然后,我在第二台机器的过程中使用一个带有该过程的产量。

import simpy
from collections import namedtuple

Chemical = namedtuple("Chemical", "name")

X_batch1 = Chemical("X_batch1")
X_batch2 = Chemical("X_batch2")


def do_process_1(env, machine1, Chemical):
    print(f"Chemical {Chemical.name} requested Machine1 at {env.now}")
    with machine1.request() as req:
        yield req
        print(f"Chemical {Chemical.name} got into Machine1 at {env.now}")
        yield env.timeout(3)
        print(f"Chemical {Chemical.name} got out of Machine1 at {env.now}")
        # yield env.timeout(2)


def do_process_2(env, machine2, Chemicals, procs):

    yield env.all_of(procs)

    # make each batch for machine2, one after the other
    print(f"{Chemicals[0]} asks for Machine2 at {env.now}.")
    with machine2.request() as req1, machine2.request() as req2:
        yield req1 & req2
        print(f"{Chemicals[0]} gets machine2 at {env.now}.")
        yield env.timeout(12)
    print(f"{Chemicals[0]} got out of machine2 at {env.now}.")


env = simpy.Environment()
machine1 = simpy.Resource(env, 1)
machine2 = simpy.Resource(env, 2)

proc1 = env.process(do_process_1(env, machine1, X_batch1))
proc2 = env.process(do_process_1(env, machine1, X_batch2))
env.process(do_process_2(env, machine2, (X_batch1), (proc1, proc2)))
env.process(do_process_2(env, machine2, (X_batch2), (proc1, proc2)))
env.run(until=60)

© www.soinside.com 2019 - 2024. All rights reserved.