Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New inflow #911

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
27 changes: 25 additions & 2 deletions flow/controllers/base_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ class BaseController:
Should be either "instantaneous" or "safe_velocity"
noise : double
variance of the gaussian from which to sample a noisy acceleration
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
"""

def __init__(self,
veh_id,
car_following_params,
delay=0,
fail_safe=None,
noise=0):
noise=0,
ignore_noise=None):
"""Instantiate the base class for acceleration behavior."""
self.veh_id = veh_id
self.ignore_noise = ignore_noise or []

# magnitude of gaussian noise
self.accel_noise = noise
Expand Down Expand Up @@ -107,7 +115,22 @@ def get_action(self, env):

# add noise to the accelerations, if requested
if self.accel_noise > 0:
accel += np.random.normal(0, self.accel_noise)
if self.ignore_noise is None:
# Add noise to the vehicle for all positions in this case.
accel += np.random.normal(0, self.accel_noise)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realized that this should probably be seeded with SUMO seed as well.

else:
pos = env.k.vehicle.get_x_by_id(self.veh_id)

# Check whether to apply the acceleration. If you are within
# one of the ignore_pos positions, noise is not applied to the
# accelerations.
apply_noise = True
for (min_pos, max_pos) in self.ignore_noise:
if min_pos <= pos < max_pos:
apply_noise = False

if apply_noise:
accel += np.random.normal(0, self.accel_noise)

# run the failsafes, if requested
if self.fail_safe == 'instantaneous':
Expand Down
86 changes: 73 additions & 13 deletions flow/controllers/car_following_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class CFMController(BaseController):
time delay (default: 0.0)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -56,6 +62,7 @@ def __init__(self,
v_des=8,
time_delay=0.0,
noise=0,
ignore_noise=None,
fail_safe=None):
"""Instantiate a CFM controller."""
BaseController.__init__(
Expand All @@ -64,9 +71,10 @@ def __init__(self,
car_following_params,
delay=time_delay,
fail_safe=fail_safe,
noise=noise)
noise=noise,
ignore_noise=ignore_noise
)

self.veh_id = veh_id
self.k_d = k_d
self.k_v = k_v
self.k_c = k_c
Expand Down Expand Up @@ -117,6 +125,12 @@ class BCMController(BaseController):
time delay (default: 0.5)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -132,6 +146,7 @@ def __init__(self,
v_des=8,
time_delay=0.0,
noise=0,
ignore_noise=None,
fail_safe=None):
"""Instantiate a Bilateral car-following model controller."""
BaseController.__init__(
Expand All @@ -140,9 +155,10 @@ def __init__(self,
car_following_params,
delay=time_delay,
fail_safe=fail_safe,
noise=noise)
noise=noise,
ignore_noise=ignore_noise
)

self.veh_id = veh_id
self.k_d = k_d
self.k_v = k_v
self.k_c = k_c
Expand Down Expand Up @@ -197,6 +213,12 @@ class LACController(BaseController):
time delay (default: 0.5)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -212,6 +234,7 @@ def __init__(self,
a=0,
time_delay=0.0,
noise=0,
ignore_noise=None,
fail_safe=None):
"""Instantiate a Linear Adaptive Cruise controller."""
BaseController.__init__(
Expand All @@ -220,9 +243,10 @@ def __init__(self,
car_following_params,
delay=time_delay,
fail_safe=fail_safe,
noise=noise)
noise=noise,
ignore_noise=ignore_noise
)

self.veh_id = veh_id
self.k_1 = k_1
self.k_2 = k_2
self.h = h
Expand Down Expand Up @@ -274,6 +298,12 @@ class OVMController(BaseController):
time delay (default: 0.5)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -289,6 +319,7 @@ def __init__(self,
v_max=30,
time_delay=0,
noise=0,
ignore_noise=None,
fail_safe=None):
"""Instantiate an Optimal Vehicle Model controller."""
BaseController.__init__(
Expand All @@ -297,8 +328,10 @@ def __init__(self,
car_following_params,
delay=time_delay,
fail_safe=fail_safe,
noise=noise)
self.veh_id = veh_id
noise=noise,
ignore_noise=ignore_noise
)

self.v_max = v_max
self.alpha = alpha
self.beta = beta
Expand Down Expand Up @@ -351,6 +384,12 @@ class LinearOVM(BaseController):
time delay (default: 0.5)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -364,6 +403,7 @@ def __init__(self,
h_st=5,
time_delay=0.0,
noise=0,
ignore_noise=None,
fail_safe=None):
"""Instantiate a Linear OVM controller."""
BaseController.__init__(
Expand All @@ -372,8 +412,10 @@ def __init__(self,
car_following_params,
delay=time_delay,
fail_safe=fail_safe,
noise=noise)
self.veh_id = veh_id
noise=noise,
ignore_noise=ignore_noise
)

# 4.8*1.85 for case I, 3.8*1.85 for case II, per Nakayama
self.v_max = v_max
# TAU in Traffic Flow Dynamics textbook
Expand Down Expand Up @@ -429,6 +471,12 @@ class IDMController(BaseController):
linear jam distance, in m (default: 2)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -444,6 +492,7 @@ def __init__(self,
s0=2,
time_delay=0.0,
noise=0,
ignore_noise=None,
fail_safe=None,
car_following_params=None):
"""Instantiate an IDM controller."""
Expand All @@ -453,7 +502,10 @@ def __init__(self,
car_following_params,
delay=time_delay,
fail_safe=fail_safe,
noise=noise)
noise=noise,
ignore_noise=ignore_noise
)

self.v0 = v0
self.T = T
self.a = a
Expand Down Expand Up @@ -530,6 +582,12 @@ class GippsController(BaseController):
reaction time in s (default: 1)
noise : float
std dev of normal perturbation to the acceleration (default: 0)
ignore_noise : list of (float, float)
a list of (min_pos, max_pos) positions where noise should not be
applied to the accelerations. For example, if you would not like to
apply acceleration noise within the positions (0, 100) and (200, 300),
then this term is written as: [(0, 100), (200, 300)]. If set to None,
noise is applied to the accelerations everywhere.
fail_safe : str
type of flow-imposed failsafe the vehicle should posses, defaults
to no failsafe (None)
Expand All @@ -546,6 +604,7 @@ def __init__(self,
tau=1,
delay=0,
noise=0,
ignore_noise=None,
fail_safe=None):
"""Instantiate a Gipps' controller."""
BaseController.__init__(
Expand All @@ -554,8 +613,9 @@ def __init__(self,
car_following_params,
delay=delay,
fail_safe=fail_safe,
noise=noise
)
noise=noise,
ignore_noise=ignore_noise
)

self.v_desired = v0
self.acc = acc
Expand Down
7 changes: 6 additions & 1 deletion flow/core/kernel/network/traci.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ def generate_cfg(self, net_params, traffic_lights, routes):
# do not want to affect the original values
sumo_inflow = deepcopy(inflow)

# The vehsPerHour feature has been moved to the VehicleParams
# class.
if "vehsPerHour" in sumo_inflow.keys():
continue

# convert any non-string element in the inflow dict to a string
for key in sumo_inflow:
if not isinstance(sumo_inflow[key], str):
Expand All @@ -780,7 +785,7 @@ def generate_cfg(self, net_params, traffic_lights, routes):
sumo_inflow['name'] += str(i)
sumo_inflow['route'] = 'route{}_{}'.format(edge, i)

for key in ['vehsPerHour', 'probability', 'period']:
for key in ['probability', 'period']:
if key in sumo_inflow:
sumo_inflow[key] = str(float(inflow[key]) * ft)

Expand Down
3 changes: 2 additions & 1 deletion flow/core/kernel/simulation/traci.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def start_simulation(self, network, sim_params):
sumo_binary, "-c", network.cfg,
"--remote-port", str(sim_params.port),
"--num-clients", str(sim_params.num_clients),
"--step-length", str(sim_params.sim_step)
"--step-length", str(sim_params.sim_step),
"--max-depart-delay", "0",
]

# use a ballistic integration step (if request)
Expand Down
7 changes: 5 additions & 2 deletions flow/core/kernel/vehicle/aimsun.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ def __init__(self,
# FIXME lots of these used in simulation/aimsun.py, used when
# we want to store the values in an emission file (necessary?)

def initialize(self, vehicles):
def initialize(self, vehicles, net_params):
"""Initialize vehicle state information.

This is responsible for collecting vehicle type information from the
VehicleParams object and placing them within the Vehicles kernel.
VehicleParams object and inflow information from the NetParams object
and placing them within the Vehicles kernel.

Parameters
----------
vehicles : flow.core.params.VehicleParams
initial vehicle parameter information, including the types of
individual vehicles and their initial speeds
net_params : flow.core.params.NetParams
network-specific parameters
"""
self.type_parameters = vehicles.type_parameters
self.num_vehicles = 0
Expand Down
4 changes: 1 addition & 3 deletions flow/core/kernel/vehicle/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class KernelVehicle(object):
vehicle kernel of separate simulators.
"""

def __init__(self,
master_kernel,
sim_params):
def __init__(self, master_kernel, sim_params):
"""Instantiate the Flow vehicle kernel.

Parameters
Expand Down
Loading