【问题标题】:Linking Simpy simulation time to Python Calendar for week day specific actions将 Simpy 模拟时间链接到 Python 日历以执行工作日特定操作
【发布时间】:2015-11-12 19:52:51
【问题描述】:

我想使用 SimPy 构建一个生产网络的仿真模型,该模型包含以下关于时间的特征:

  • 工厂周一至周五工作(两班 8 小时)
  • 除周日外,一周中的所有日子都有重型卡车行驶
  • 轻型卡车全天行驶,包括周日

为此,我想构造一个BroadcastPipe as given in the docs 与超时相结合,以使对象在它们不工作的日子里等待(对于工厂来说,需要额外的逻辑来模拟班次)。这个 BroadcastPipe 只会计算天数(假设每天 24*60 分钟),然后说 “大家好,今天是星期一”。然后对象(工厂、轻型和重型卡车)将单独处理这些信息并采取相应的行动。

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

【问题讨论】:

    标签: python calendar weekday simpy


    【解决方案1】:

    我通常设置一个开始日期,并定义它等于模拟时间(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())
    

    【讨论】:

    • 链接已关闭 :)
    • 另外——使用 start.replace(seconds=env.now) 返回错误。原来正确的语法是 start.shift(seconds=env.now)。不管怎样,很棒的推荐,谢谢 Stefan!
    【解决方案2】:

    您可以使用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...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2022-01-22
      • 2016-12-18
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多