0

最近我了解了 SIMPY,我认为它可以为我的工作提供一些信息并提供决策背景非常有帮助。请注意,我对使用 python 编程很陌生,我知道基础知识,但我正在学习。

问题: 我正在一个集装箱码头工作,我们有三种移动方式:

  • 从相关船只移动(大约每 120 秒产生一次移动。)
  • 与仓库相关的移动(大约每 960 秒产生一次移动。)
  • 与门相关的移动(大约每 800 秒产生一次移动。)

在此处输入图像描述

所有这些动作都以所描述的间隔同时“生成”。船只和仓库都有自己的运输工具。但是,上述所有步骤的目的地都是相同的。容器堆栈采用 FIFO 原则(先进先出),一次可以处理四个移动。一个动作大约需要 270 秒。

我想要实现的是通过更改资源量(船舶和仓库的运输次数以及闸门移动)来查看对集装箱堆内业务的影响。

我得到了适用于其中一个流程(VESSEL)的基本模拟,我现在正在尝试添加仓库流程,但是我被卡住了。当我使用下面的代码时,只会生成来自容器的移动:

def move_generator(env):

    while True:
        yield env.timeout(120)
        print("Vessel move generated at: %.1f" % (env.now))

    while True:
        yield env.timeout(960)
        print("Warehouse move generated at: %.1f" % (env.now)) 


import simpy
env = simpy.Environment()
env.process(move_generator(env))
    
env.run(until=3600)

上述代码的输出是:

Vessel move generated at: 120.0
Vessel move generated at: 240.0
Vessel move generated at: 360.0
Vessel move generated at: 480.0
Vessel move generated at: 600.0
Vessel move generated at: 720.0
Vessel move generated at: 840.0
Vessel move generated at: 960.0
Vessel move generated at: 1080.0
Vessel move generated at: 1200.0
Vessel move generated at: 1320.0
Vessel move generated at: 1440.0
Vessel move generated at: 1560.0
Vessel move generated at: 1680.0
Vessel move generated at: 1800.0
Vessel move generated at: 1920.0
Vessel move generated at: 2040.0
Vessel move generated at: 2160.0
Vessel move generated at: 2280.0
Vessel move generated at: 2400.0
Vessel move generated at: 2520.0
Vessel move generated at: 2640.0
Vessel move generated at: 2760.0
Vessel move generated at: 2880.0
Vessel move generated at: 3000.0
Vessel move generated at: 3120.0
Vessel move generated at: 3240.0
Vessel move generated at: 3360.0
Vessel move generated at: 3480.0

然而,所需的输出将是:

Vessel move generated at: 120.0
Vessel move generated at: 240.0
Vessel move generated at: 360.0
Vessel move generated at: 480.0
Vessel move generated at: 600.0
Vessel move generated at: 720.0
Vessel move generated at: 840.0
Vessel move generated at: 960.0
Warehouse move generated at: 960.0
Vessel move generated at: 1080.0
Vessel move generated at: 1200.0
Vessel move generated at: 1320.0
Vessel move generated at: 1440.0
Vessel move generated at: 1560.0
Vessel move generated at: 1680.0
Vessel move generated at: 1800.0
Vessel move generated at: 1920.0
Warehouse move generated at: 1920.0
Vessel move generated at: 2040.0
Vessel move generated at: 2160.0
Vessel move generated at: 2280.0
Vessel move generated at: 2400.0
Vessel move generated at: 2520.0
Vessel move generated at: 2640.0
Vessel move generated at: 2760.0
Vessel move generated at: 2880.0
Warehouse move generated at: 2880.0
Vessel move generated at: 3000.0
Vessel move generated at: 3120.0
Vessel move generated at: 3240.0
Vessel move generated at: 3360.0
Vessel move generated at: 3480.0

我认为问题出在两个 while True 语句中,但我无法弄清楚如何正确处理这个问题。我一直在使用谷歌(可能不知道找到答案的正确术语)但没有成功。

4

1 回答 1

2

正如您在评论中指出的那样,问题在于您对 python 的使用,而不是 simpy。

while True:
  <do anything>

# already unreachable
while True:
  # also unreachable

对于您的示例,请尝试查看https://simpy.readthedocs.io/en/latest/simpy_intro/shared_resources.html

for i in range(4):
    env.process(car(env, 'Car %d' % i, bcs, i*2, 5))
env.run()

您可以看到process之前可以多次调用它run,从而实现您想要的“并行”工作。

import simpy


def vessel(env):
    while True:
        yield env.timeout(120)
        print("Vessel move generated at: %.1f" % (env.now))


def warehouse(env):
    while True:
        yield env.timeout(960)
        print("Warehouse move generated at: %.1f" % (env.now)) 


def main():
    env = simpy.Environment()
    env.process(vessel(env))
    env.process(warehouse(env))
    env.run(until=3600)

if __name__ == '__main__':
    main()

这给

Vessel move generated at: 120.0
Vessel move generated at: 240.0
Vessel move generated at: 360.0
Vessel move generated at: 480.0
Vessel move generated at: 600.0
Vessel move generated at: 720.0
Vessel move generated at: 840.0
Warehouse move generated at: 960.0
Vessel move generated at: 960.0
Vessel move generated at: 1080.0
Vessel move generated at: 1200.0
Vessel move generated at: 1320.0
Vessel move generated at: 1440.0
Vessel move generated at: 1560.0
Vessel move generated at: 1680.0
Vessel move generated at: 1800.0
Warehouse move generated at: 1920.0
Vessel move generated at: 1920.0
Vessel move generated at: 2040.0
Vessel move generated at: 2160.0
Vessel move generated at: 2280.0
Vessel move generated at: 2400.0
Vessel move generated at: 2520.0
Vessel move generated at: 2640.0
Vessel move generated at: 2760.0
Warehouse move generated at: 2880.0
Vessel move generated at: 2880.0
Vessel move generated at: 3000.0
Vessel move generated at: 3120.0
Vessel move generated at: 3240.0
Vessel move generated at: 3360.0
Vessel move generated at: 3480.0
于 2021-03-15T18:41:02.243 回答