如何根据时间表模拟客户到达

时间:2016-11-03 14:51:56

标签: python simpy

我想模拟特定时间段的客户到达时间(不是根据统计分布生成的)。到达时间在我加载到pandas dataframe 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()

1 个答案:

答案 0 :(得分:0)

你需要:

  • 将datetime对象转换为时间戳并使用这些
  • 定义传递给Environment()
  • initial_time
  • 定义一个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