如何在 Simpy 中设置初始队列长度?
How to set initial queue length in Simpy?
我刚开始进行事件模拟,我遇到了一些队列长度问题。问题是如何在以下代码中设置初始队列?例如,有没有办法设置 initial_queue = 200 以便在模拟开始时队列中已经有 200 辆汽车?
import simpy
num_of_machines = 1
env = simpy.Environment()
bcs = simpy.Resource(env, capacity=num_of_machines)
def process_client(env):
with bcs.request() as req:
i = 0
while True:
i += 1
yield req
print('Car %s starting to charge at %s' % (i, env.now))
yield env.timeout(90)
print('Car %s ending charge at %s' % (i, env.now))
print('Queue size: %s' % len(bcs.queue))
env.process(process_client(env))
env.process(process_client(env))
env.run(until=300)
哪个returns:
Car 1 starting to charge at 0
Car 1 ending charge at 90
Queue size: 0
Car 2 starting to charge at 90
Car 2 ending charge at 180
Queue size: 0
Car 3 starting to charge at 180
Car 3 ending charge at 270
Queue size: 0
Car 4 starting to charge at 270
所以你走在正确的轨道上,但是你的 while 循环永远不会结束,所以 env.process(process_client(env))
永远不会被执行,但无论如何都会是一个不需要的递归调用。
我把你的代码分成两部分。第一部分仅模拟一辆试图充电的汽车。第二个创建第一个进程的 200 个实例。请注意,我们是在模型启动之前创建这些流程。在 env.run(900)
行执行之前,模型不会启动。当模型启动时,所有 200 个进程将尝试获取资源,其中 199 个将进入队列。
这是代码
"""
Simple example of starting a resource with a queue of 200 requests
programmer: Michael R. Gibbs
"""
import simpy
def charge_process(env, res_pool, car_id):
"""
Models a car waiting in a queue for a charger
and charging
"""
with res_pool.request() as req:
yield req
print('Car %s starting to charge at %s' % (car_id, env.now))
yield env.timeout(90)
print('Car %s ending charge at %s' % (car_id, env.now))
print('Queue size: %s' % len(res_pool.queue))
def load_queue(env, res_pool):
"""
create 200 charging processes.
each one will queue up for a charing station
"""
for i in range(1,201):
env.process(charge_process(env, res_pool, i))
# boot
env = simpy.Environment()
res_pool = simpy.Resource(env, capacity=1)
load_queue(env, res_pool)
env.run(900)
我刚开始进行事件模拟,我遇到了一些队列长度问题。问题是如何在以下代码中设置初始队列?例如,有没有办法设置 initial_queue = 200 以便在模拟开始时队列中已经有 200 辆汽车?
import simpy
num_of_machines = 1
env = simpy.Environment()
bcs = simpy.Resource(env, capacity=num_of_machines)
def process_client(env):
with bcs.request() as req:
i = 0
while True:
i += 1
yield req
print('Car %s starting to charge at %s' % (i, env.now))
yield env.timeout(90)
print('Car %s ending charge at %s' % (i, env.now))
print('Queue size: %s' % len(bcs.queue))
env.process(process_client(env))
env.process(process_client(env))
env.run(until=300)
哪个returns:
Car 1 starting to charge at 0
Car 1 ending charge at 90
Queue size: 0
Car 2 starting to charge at 90
Car 2 ending charge at 180
Queue size: 0
Car 3 starting to charge at 180
Car 3 ending charge at 270
Queue size: 0
Car 4 starting to charge at 270
所以你走在正确的轨道上,但是你的 while 循环永远不会结束,所以 env.process(process_client(env))
永远不会被执行,但无论如何都会是一个不需要的递归调用。
我把你的代码分成两部分。第一部分仅模拟一辆试图充电的汽车。第二个创建第一个进程的 200 个实例。请注意,我们是在模型启动之前创建这些流程。在 env.run(900)
行执行之前,模型不会启动。当模型启动时,所有 200 个进程将尝试获取资源,其中 199 个将进入队列。
这是代码
"""
Simple example of starting a resource with a queue of 200 requests
programmer: Michael R. Gibbs
"""
import simpy
def charge_process(env, res_pool, car_id):
"""
Models a car waiting in a queue for a charger
and charging
"""
with res_pool.request() as req:
yield req
print('Car %s starting to charge at %s' % (car_id, env.now))
yield env.timeout(90)
print('Car %s ending charge at %s' % (car_id, env.now))
print('Queue size: %s' % len(res_pool.queue))
def load_queue(env, res_pool):
"""
create 200 charging processes.
each one will queue up for a charing station
"""
for i in range(1,201):
env.process(charge_process(env, res_pool, i))
# boot
env = simpy.Environment()
res_pool = simpy.Resource(env, capacity=1)
load_queue(env, res_pool)
env.run(900)