无法在 SIMPY 中触发两个事件(python 中的模拟)
Cannot trigger two events in SIMPY (simulation in python)
最近我了解了 SIMPY,我认为它可以为我的工作提供一些信息并提供决策背景非常有帮助。请注意,我对使用 python 进行编程还很陌生,我了解基础知识,但我正在边学边学。
问题:
我在一个集装箱码头工作,我们有三种类型的移动:
- 与容器相关的移动(大约每 120 秒生成一次移动。)
- 与仓库相关的移动(大约每 960 秒生成一次移动。)
- 与门相关的移动(大约每 800 秒生成一次移动。)
所有这些动作都在 'generated' 的同时进行,并具有所描述的时间间隔。
船只和仓库都有自己的运输工具。但是,上述所有步骤的目标都是相同的。容器堆栈按照 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 语句中,但我不知道如何正确处理这个问题。我一直在使用 Google(可能不知道找到答案的正确术语)但没有成功。
正如您在评论中指出的那样,问题在于您使用了 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
最近我了解了 SIMPY,我认为它可以为我的工作提供一些信息并提供决策背景非常有帮助。请注意,我对使用 python 进行编程还很陌生,我了解基础知识,但我正在边学边学。
问题: 我在一个集装箱码头工作,我们有三种类型的移动:
- 与容器相关的移动(大约每 120 秒生成一次移动。)
- 与仓库相关的移动(大约每 960 秒生成一次移动。)
- 与门相关的移动(大约每 800 秒生成一次移动。)
所有这些动作都在 'generated' 的同时进行,并具有所描述的时间间隔。 船只和仓库都有自己的运输工具。但是,上述所有步骤的目标都是相同的。容器堆栈按照 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 语句中,但我不知道如何正确处理这个问题。我一直在使用 Google(可能不知道找到答案的正确术语)但没有成功。
正如您在评论中指出的那样,问题在于您使用了 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