根据时间表在Simpy中生成客户

时间:2019-04-02 06:54:56

标签: python simulation simpy

我正在尝试使用Simpy v3模拟特定时间(不是根据统计分布生成)的客户到达。到达时间在我加载到pandas数据框中的csv文件中定义。

在先前版本的Simpy(v2.2)中,激活函数“ at”有一个参数,该参数将在特定实例处激活客户 activate(c,c.visit(timeInBank),at=5.0)。 在Simpy版本3中如何做到这一点?

数据帧“ df”传递每个客户的到达时间

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 

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)

simpy.util中有一个start_delayed函数可能适合您的需求。

import random
import simpy
from simpy.util import start_delayed
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"])
        # Some code to format timestamp to seconds
        delay = timestamp_to_seconds(row["arrival_time"])
        start_delayed(env, c, delay)   

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()