I'm trying to get a random generator of vehicles from the simulation procedure from SimPy. The code I'm using I extracted it and adapted from http://phillipmfeldman.org/Python/discrete_event_simulation/traffic_sim.py. The problem is that I'd like to have vehicles arriving at not less than specific headway for example (4 seconds). The code below presents vehicles arriving at random but sometimes the headway is too small (for example Vehicle#2 arrived at 2.03 and Vehicle#3 arrives at 2.45, this difference is less than 1 second). I'd like to know if there is a way to set a specific threshold for the simulation, thanks...
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)
Aucun commentaire:
Enregistrer un commentaire