【问题标题】:How to simulate arrivals of clients according to the schedule如何根据时间表模拟客户的到达
【发布时间】:2016-11-03 14:51:55
【问题描述】:

我想模拟客户在特定时间段的到达(不是根据统计分布生成的)。到达时间在我加载到 pandas 数据框df 的 csv 文件中定义:

df.head()

arrival_time  start_service  end_service  waiting_time  service_duration
09:00:20      09:01:00       09:06:00     0.40      5.00
09:01:00      09:02:20       09:04:00     1.20      1.40

这是我当前的代码,但我不知道如何强制实体(客户端)根据df 中定义的时间表到达,例如在09:00:20,然后在09:01:00,等等。我假设我还应该在Environment 中设置开始模拟时间,但我该怎么做呢? (我不需要实时模拟):

import random
import simpy
import pandas as pd

def source(env, df, counter):
    for i, row in df.iterrows():
        c = client(env, 'Client%02d' % i, counter, row, time_in_bank=row["service_duration"])
        env.process(c)   

def client(env, name, counter, row, time_in_bank):
    arrive = env.now # probably some changes to be done here
    print('%s arrived at %7.4f' % (name,arrive))

    with counter.request() as req:
        results = yield req

        wait = env.now - row["waiting_time"]

        print('%s waited %6.3f' % (name, wait))

        yield env.timeout(time_in_bank)
        print('%s exited the office at %7.4f' % (name, env.now))


df = pd.read_csv("arrivals.csv",sep=",",header=0)

env = simpy.Environment()

counter = simpy.Resource(env, capacity=1)
env.process(source(env, df.head(), counter))
env.run()

【问题讨论】:

    标签: python simpy


    【解决方案1】:

    你需要:

    • 将日期时间对象转换为时间戳并使用这些对象
    • 定义一个 initial_time 你传递给Environment()
    • 定义一个 SimPy 步骤有多长,例如1 step == 1 sec

    示例(以arrow 为特色):

    import arrow
    import simpy
    
    start = arrow.get('2016-11-05T00:00:00')
    env = simpy.Environment(initial_time=start.timestamp)
    
    def proc(env):
        print('Proc start at', env.now, arrow.get(env.now))
        yield env.timeout(10)  # 10 seconds
        print('Proc stop at ', env.now, arrow.get(env.now))
    
    p = env.process(proc(env))
    env.run(p)
    

    输出:

    Proc start at 1478304000 2016-11-05T00:00:00+00:00
    Proc stop at  1478304010 2016-11-05T00:00:10+00:00
    

    【讨论】:

    • 你有一些小例子吗?
    • 在我的回答中添加了一个例子
    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多