python:在simpy中暂停模拟

时间:2016-05-17 02:41:57

标签: python queue simulation simpy

我希望有人可以指出我正确的方向,因为我在Simpy建立一个队列模型。 Simpy网站上的文档和示例非常棒,我想知道我是否可以构建他们提供的示例。

基本上,我正在模拟一个职员必须审查文书工作的过程。文书工作以随机的服务规则以随机的时间间隔(很像MM1队列)到达和退出他的队列(即被服务)。

我发现的棘手部分是我想对这个过程进行建模,以便职员在重返工作岗位之前必须睡一段时间。例如,如果模拟运行了#4; 4周和#34;那么职员应该在此期间每天睡12小时。我似乎无法得到最后的警告。请参阅下面的代码,谢谢!

import random

import simpy

import matplotlib.pyplot as plt


RANDOM_SEED = 123456 #Random seed
INTERVAL_RECORDS = 30.0  #review records roughly every xx hours?
T_INTER = 28 #generate records roughly every xx hours?
WEEKS = 4
SIM_TIME = WEEKS*7*24*60 #simulation time (hours?)

class record_review:

    def __init__(self, env):
        self.env = env
        self.shift_exchange = env.event()


def record(env, interval, counter, data, t_inter):
        """Source generates records randomly"""
        for i in range(1):
            """start off with x=i records in queue"""
            c = review(env, 'Record%02d' % i, counter, time_in_queue=30.0)
            env.process(c)
            t = random.expovariate(1.0 / interval) #set up random generation     rate 
            yield env.timeout(t)

        while True:
            """continually generate records throughout simulation"""
            yield env.timeout(random.randint(t_inter-2, t_inter+2))   #generate record between +/- 2 of interal
            i += 1
            c = review(env, 'Record%02d' % i, counter, time_in_queue=30.0)
            env.process(c)   
            t = random.expovariate(1.0 / interval) #random generation rate
            yield env.timeout(t)


def review(env, name, counter, time_in_queue):
        """Record arrives, is reviewed and exits."""
        arrive = env.now
        print('%7.4f %s: Record has entered queue' % (arrive, name))

        with counter.request() as req:
            yield req 
            wait = env.now - arrive #total wait time / record
            tib = random.expovariate(1.0 / time_in_queue) #time in  queue/review rate
            yield env.timeout(tib)
            data.append(wait) #monitor
            print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
            print('%7.4f %s: Finished' % (env.now, name))

def shift_exchange(self, env):

           while True:
               for i in range(SIM_TIME):
                   yield env.timeout(60)
                   self.shift_exchange = env.event()


# Setup and start the simulation
print('Batch Record Review Simulation')
random.seed(RANDOM_SEED)
env = simpy.Environment()
data = []

# Start processes and run
    counter = simpy.Resource(env, capacity=1)
    env.process(record(env, INTERVAL_RECORDS, counter, data, T_INTER))
    env.run(until=SIM_TIME)

0 个答案:

没有答案