将 Simpy 模拟时间链接到 Python 工作日特定操作的日历

Linking Simpy simulation time to Python Calendar for week day specific actions

我想使用 SimPy 构建生产网络的仿真模型,包括以下与时间有关的特征:

为此,我想构建一个 BroadcastPipe as given in the docs 并结合超时,使对象在不工作的日子里等待(对于工厂,需要额外的逻辑来模拟轮班)。这个 BroadcastPipe 只会计算天数(假设每天 24*60 分钟)然后说 "It's Monday, everybody"。然后,对象(工厂、轻型和重型卡车)将单独处理此信息并采取相应行动。

现在,我想知道是否有一种优雅的方法可以 link 模拟时间到常规 Python Calender 对象,以便轻松访问星期几。这对于银行假期和不同的开始日期等清晰度和增强功能很有用。你有什么建议如何做到这一点? (或关于如何更好地建模的一般建议?)。提前致谢!

您可以使用 datetime module 并创建一个 day_of_week 对象,但您仍然需要计算运行时间:

import datetime
# yyyy = four digit year integer
# mm = 1- or 2-digit month integer
# dd = 1- or 2-digit day integer

day_of_week = datetime.datetime(yyyy, mm, dd).strftime('%a')

if day_of_week == 'Mon':
    # Do Monday tasks...
elif day_of_week == 'Tue':
    # Tuesday...

我一般设置一个开始日期,定义为等于仿真时间(Environment.now)0。由于SimPy的仿真时间没有固有单位,所以我也定义为秒。使用 arrow,我可以轻松地从当前模拟时间计算出实际日期和时间:

import arrow
import simpy

start = arrow.get('2015-01-01T00:00:00')
env = simpy.Environment()

# do some simulation ...

current_date = start.replace(seconds=env.now)
print('Curret weekday:', current_date.weekday())