如何在 SimPy 4 中终止模拟

问题描述

有没有办法通过使用像 env.exit() 这样的命令来终止简单的模拟?我不明白如何将事件放入 env.run(until=event)。当我的某个 Simpy Stores 中没有任何对象时,我想终止模拟。我该怎么做?

解决方法

一切都是简单的事件,甚至环境本身。因此,您可以终止模拟标记为“根”事件成功。

# Save the event somewhere
end_event = env.event()

# Later,when you want to terminate the simulation,run
end_event.succeed()

为了检查 store 是否为空,只需检查其 items len 是否等于 0。

如果你把所有东西放在一起,你可以做类似的事情来解决你的问题:

store = simpy.FilterStore(env,capacity=10)
if len(store.items) == 0:
    end_event.succeed()