如何使用 SimPy 获得设置最小阈值的随机时间?
How do I get random times setting a minimum threshold with SimPy?
我正在尝试从 SimPy 的模拟程序中获取车辆的随机生成器。我正在使用的代码是我从 http://phillipmfeldman.org/Python/discrete_event_simulation/traffic_sim.py 中提取并改编的。问题是我想让车辆以不低于特定车头时距(例如 4 秒)的速度到达。下面的代码显示车辆随机到达,但有时车头时距太小(例如 Vehicle#2 到达 2.03,Vehicle#3 到达 2.45,这个差异小于 1 秒)。我想知道是否有办法为模拟设置一个特定的阈值,谢谢...
from collections import deque # double-ended queue
from numpy import random
import simpy
from simpy.util import start_delayed
class Struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
random.seed([1, 2, 3])
# Total number of seconds to be simulated:
end_time= 3600.0
# Cars cars arrive at the traffic light according to a Poisson process with an
# average rate of (0.00028-0.5) per second:
demand_per_hour = 1800 #veh/h
sh = 4 # sat headway 4 seconds,
arrival_rate= demand_per_hour/(3600*sh)
t_interarrival_mean= 1.0 / arrival_rate
queue= deque()
arrival_count = departure_count= 0
def arrival():
global arrival_count, env, queue
while True:
arrival_count+= 1
print("Vehicle #%d arrived at time "
"%.3f." % (arrival_count, env.now))
# Schedule next arrival:
yield env.timeout( random.exponential(t_interarrival_mean))
print("\nSimulation of Cars Arriving at Intersection Controlled by a Traffic "
"Light\n\n")
env= simpy.Environment()
t_first_arrival= random.exponential(t_interarrival_mean)
start_delayed(env, arrival(), delay=t_first_arrival)
env.run(until=end_time)
有几种方法可以做到这一点。
- 截断您的指数到达间隔时间分布:
t=max(threshold_value, random.exponential(t_interarrival_mean))
- 改变您的指数 IAT 分布(也称为 2 参数指数分布):
t = threshold_value + random.exponential(t_interarrival_mean)
- 使用不同的分布。你确定指数是最好的吗?三角分布怎么样?
我正在尝试从 SimPy 的模拟程序中获取车辆的随机生成器。我正在使用的代码是我从 http://phillipmfeldman.org/Python/discrete_event_simulation/traffic_sim.py 中提取并改编的。问题是我想让车辆以不低于特定车头时距(例如 4 秒)的速度到达。下面的代码显示车辆随机到达,但有时车头时距太小(例如 Vehicle#2 到达 2.03,Vehicle#3 到达 2.45,这个差异小于 1 秒)。我想知道是否有办法为模拟设置一个特定的阈值,谢谢...
from collections import deque # double-ended queue
from numpy import random
import simpy
from simpy.util import start_delayed
class Struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
random.seed([1, 2, 3])
# Total number of seconds to be simulated:
end_time= 3600.0
# Cars cars arrive at the traffic light according to a Poisson process with an
# average rate of (0.00028-0.5) per second:
demand_per_hour = 1800 #veh/h
sh = 4 # sat headway 4 seconds,
arrival_rate= demand_per_hour/(3600*sh)
t_interarrival_mean= 1.0 / arrival_rate
queue= deque()
arrival_count = departure_count= 0
def arrival():
global arrival_count, env, queue
while True:
arrival_count+= 1
print("Vehicle #%d arrived at time "
"%.3f." % (arrival_count, env.now))
# Schedule next arrival:
yield env.timeout( random.exponential(t_interarrival_mean))
print("\nSimulation of Cars Arriving at Intersection Controlled by a Traffic "
"Light\n\n")
env= simpy.Environment()
t_first_arrival= random.exponential(t_interarrival_mean)
start_delayed(env, arrival(), delay=t_first_arrival)
env.run(until=end_time)
有几种方法可以做到这一点。
- 截断您的指数到达间隔时间分布:
t=max(threshold_value, random.exponential(t_interarrival_mean))
- 改变您的指数 IAT 分布(也称为 2 参数指数分布):
t = threshold_value + random.exponential(t_interarrival_mean)
- 使用不同的分布。你确定指数是最好的吗?三角分布怎么样?