'resolution': 40 }) from flow.core.params import InitialConfig initial_config = InitialConfig(spacing="uniform", perturbation=1) from flow.core.params import TrafficLightParams traffic_lights = TrafficLightParams() from flow.envs.ring.accel import AccelEnv from flow.core.params import SumoParams sim_params = SumoParams(sim_step=0.1, render=True, emission_path='data') from flow.envs.ring.accel import ADDITIONAL_ENV_PARAMS print(ADDITIONAL_ENV_PARAMS) from flow.core.params import EnvParams env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) from flow.core.experiment import Experiment flow_params = dict( exp_tag='ring_example', env_name=AccelEnv, network=RingNetwork,
def traffic_light_grid_mxn_exp_setup(row_num=1, col_num=1, sim_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, tl_logic=None): """ Create an environment and network pair for traffic light grid 1x1 test experiments. Parameters ---------- row_num: int, optional number of horizontal rows of edges in the traffic light grid network col_num: int, optional number of vertical columns of edges in the traffic light grid network sim_params : flow.core.params.SumoParams sumo-related configuration parameters, defaults to a time step of 1s and no sumo-imposed failsafe on human or rl vehicles vehicles : Vehicles type vehicles to be placed in the network, default is 5 vehicles per edge for a total of 20 vehicles with an IDM acceleration controller and GridRouter routing controller. env_params : flow.core.params.EnvParams environment-specific parameters, defaults to a environment with failsafes, where other parameters do not matter for non-rl runs net_params : flow.core.params.NetParams network-specific configuration parameters, defaults to a 1x1 traffic light grid with traffic lights on initial_config : flow.core.params.InitialConfig specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network tl_logic: flow.core.params.TrafficLightParams specifies logic of any traffic lights added to the system """ logging.basicConfig(level=logging.WARNING) if tl_logic is None: tl_logic = TrafficLightParams(baseline=False) if sim_params is None: # set default sim_params configuration sim_params = SumoParams(sim_step=1, render=False) if vehicles is None: vehicles_per_edge = 5 num_edges = 2 * (row_num + col_num) total_vehicles = num_edges * vehicles_per_edge vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams(min_gap=2.5, tau=1.1, max_speed=30), routing_controller=(GridRouter, {}), num_vehicles=total_vehicles) if env_params is None: # set default env_params configuration additional_env_params = { "target_velocity": 50, "switch_time": 3.0, "tl_type": "controlled", "discrete": False } env_params = EnvParams(additional_params=additional_env_params, horizon=100) if net_params is None: # set default net_params configuration total_vehicles = vehicles.num_vehicles num_entries = 2 * row_num + 2 * col_num assert total_vehicles % num_entries == 0, "{} total vehicles should " \ "be divisible by {" \ "}".format(total_vehicles, num_entries) grid_array = { "short_length": 100, "inner_length": 300, "long_length": 3000, "row_num": row_num, "col_num": col_num, "cars_left": int(total_vehicles / num_entries), "cars_right": int(total_vehicles / num_entries), "cars_top": int(total_vehicles / num_entries), "cars_bot": int(total_vehicles / num_entries) } additional_net_params = { "length": 200, "lanes": 2, "speed_limit": 35, "resolution": 40, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig(spacing="custom", additional_params={"enter_speed": 30}) flow_params = dict( # name of the experiment exp_tag="Grid1x1Test", # name of the flow environment the experiment is running on env_name=TrafficLightGridTestEnv, # name of the network class the experiment is running on network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=sim_params, # environment related parameters (see flow.core.params.EnvParams) env=env_params, # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=net_params, # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=initial_config, # traffic lights to be introduced to specific nodes (see # flow.core.params.TrafficLightParams) tls=tl_logic) # create the network network = TrafficLightGridNetwork(name="Grid1x1Test", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) # create the environment env = TrafficLightGridTestEnv(env_params=env_params, sim_params=sim_params, network=network) # reset the environment env.reset() return env, network, flow_params
# name of the experiment exp_tag="merge_4_Sim_Number100_Initial_Angel405_PunishDelay_RL10", # name of the flow environment the experiment is running on env_name=MergePOEnvPunishDelay, # name of the network class the experiment is running on network=MergeNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( restart_instance=True, sim_step=0.5, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, sims_per_step=2, warmup_steps=0, additional_params={ "max_accel": 9, "max_decel": 9, "target_velocity": 30, "num_rl": NUM_RL, "max_num_vehicles": VEHICLE_NUMBER, "main_rl": MAIN_RL,
"3merge_i696_ALLHUMAN_horizon1500_wamrup0_SM9_inflow2000_merge200_depart10_noheadway", # name of the flow environment the experiment is running on #env_name=MergePOEnv, env_name=MergePOEnv, # name of the scenario class the experiment is running on network=Network, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( no_step_log=False, # this disables log writing? sim_step=0.5, # Daniel updated from osm.sumocfg lateral_resolution=0.25, # determines lateral discretization of lanes render= False, #True, # False for training, True for debugging restart_instance=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, sims_per_step=2, #5, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 30, "num_rl": NUM_RL, # used by WaveAttenuationMergePOEnv e.g. to fix action dimension
def para_produce_rl(HORIZON=3000, NUM_AUTOMATED=4): # time horizon of a single rollout HORIZON = 3000 # number of rollouts per training iteration N_ROLLOUTS = 20 # number of parallel workers N_CPUS = 2 # number of automated vehicles. Must be less than or equal to 22. NUM_AUTOMATED = NUM_AUTOMATED # We evenly distribute the automated vehicles in the network. num_human = 22 - NUM_AUTOMATED humans_remaining = num_human vehicles = VehicleParams() for i in range(NUM_AUTOMATED): # Add one automated vehicle. vehicles.add(veh_id="rl_{}".format(i), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) # Add a fraction of the remaining human vehicles. vehicles_to_add = round(humans_remaining / (NUM_AUTOMATED - i)) humans_remaining -= vehicles_to_add vehicles.add(veh_id="human_{}".format(i), acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams(min_gap=0), routing_controller=(ContinuousRouter, {}), num_vehicles=vehicles_to_add) flow_params = dict( # name of the experiment exp_tag="multiagent_ring", # name of the flow environment the experiment is running on env_name=MultiAgentWaveAttenuationPOEnv, # name of the network class the experiment is running on network=RingNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams(sim_step=0.1, render=False, restart_instance=False), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, warmup_steps=750, clip_actions=False, additional_params={ "max_accel": 1, "max_decel": 1, "ring_length": [220, 270], }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(additional_params={ "length": 260, "lanes": 1, "speed_limit": 30, "resolution": 40, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig()) flow_params['env'].horizon = HORIZON return flow_params
speed_mode='no_collide', num_vehicles=1) flow_params = dict( # name of the experiment exp_tag='figure_eight_intersection_control', # name of the flow environment the experiment is running on env_name='AccelEnv', # name of the scenario class the experiment is running on scenario='Figure8Scenario', # sumo-related parameters (see flow.core.params.SumoParams) sumo=SumoParams( sim_step=0.1, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ 'target_velocity': 20, 'max_accel': 3, 'max_decel': 3, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(
def merge_example(render=None): """ Perform a simulation of vehicles on a merge. Parameters ---------- render: bool, optional specifies whether to use sumo's gui during execution Returns ------- exp: flow.core.SumoExperiment type A non-rl experiment demonstrating the performance of human-driven vehicles on a merge. """ sumo_params = SumoParams(render=True, emission_path="./data/", sim_step=0.2, restart_instance=False) if render is not None: sumo_params.render = render vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), speed_mode="no_collide", num_vehicles=5) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS, sims_per_step=5, warmup_steps=0) inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=FLOW_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 500 net_params = NetParams(inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", perturbation=5.0) scenario = MergeScenario(name="merge-baseline", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
def grid_example(render=None): """ Perform a simulation of vehicles on a grid. Parameters ---------- render: bool, optional specifies whether to use the gui during execution Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles and balanced traffic lights on a grid. """ v_enter = 10 inner_length = 300 long_length = 500 short_length = 300 n_rows = 2 n_columns = 3 num_cars_left = 20 num_cars_right = 20 num_cars_top = 20 num_cars_bot = 20 tot_cars = (num_cars_left + num_cars_right) * n_columns \ + (num_cars_top + num_cars_bot) * n_rows grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n_rows, "col_num": n_columns, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sim_params = SumoParams(sim_step=0.1, render=True) if render is not None: sim_params.render = render vehicles = VehicleParams() vehicles.add( veh_id="human", routing_controller=(GridRouter, {}), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GrGrGrGrGrGr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yryryryryryr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rGrGrGrGrGrG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "ryryryryryry" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated") additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } if USE_INFLOWS: initial_config, net_params = get_flow_params( col_num=n_columns, row_num=n_rows, additional_net_params=additional_net_params) else: initial_config, net_params = get_non_flow_params( enter_speed=v_enter, add_net_params=additional_net_params) scenario = SimpleGridScenario( name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sim_params, scenario) return Experiment(env)
initial_config, net_params = \ get_non_flow_params(V_ENTER, additional_net_params) flow_params = dict( # name of the experiment exp_tag='green_wave', # name of the flow environment the experiment is running on env_name='PO_TrafficLightGridEnv', # name of the scenario class the experiment is running on scenario='SimpleGridScenario', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=1, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params=additional_env_params, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=net_params, # vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles,
edge='8649816#9', vehs_per_hour=200, departSpeed=10, departLane="random", departPos="last") initial_config = InitialConfig(spacing="random", perturbation=1) net_params = NetParams(inflows=inflow, osm_path='Kenmore.osm', no_internal_links=False) # create the remainding parameters env_params = EnvParams() env_params.additional_params = osm_traffic_env.ADDITIONAL_ENV_PARAMS sim_params = SumoParams(render=True, restart_instance=True) # initial_config = InitialConfig() vehicles = VehicleParams() vehicles.add('human', num_vehicles=100, acceleration_controller=(IDMController, {}), routing_controller=(OSMRouter, {})) # create the scenario scenario = OSMScenario(name='Kenmore', net_params=net_params, initial_config=initial_config, vehicles=vehicles) # create the environment env = OSMTrafficTestEnvironment(env_params=env_params,
def get_flow_params(config): """Returns Flow experiment parameters, given an experiment result folder Parameters ---------- config : dict stored RLlib configuration dict Returns ------- dict Dict of flow parameters, like net_params, env_params, vehicle characteristics, etc """ # collect all data from the json file flow_params = json.loads(config['env_config']['flow_params']) # reinitialize the vehicles class from stored data veh = Vehicles() for veh_params in flow_params["veh"]: module = __import__( "flow.controllers", fromlist=[veh_params['acceleration_controller'][0]]) acc_class = getattr(module, veh_params['acceleration_controller'][0]) lc_class = getattr(module, veh_params['lane_change_controller'][0]) acc_controller = (acc_class, veh_params['acceleration_controller'][1]) lc_controller = (lc_class, veh_params['lane_change_controller'][1]) rt_controller = None if veh_params['routing_controller'] is not None: rt_class = getattr(module, veh_params['routing_controller'][0]) rt_controller = (rt_class, veh_params['routing_controller'][1]) sumo_cf_params = SumoCarFollowingParams() sumo_cf_params.__dict__ = veh_params["sumo_car_following_params"] sumo_lc_params = SumoLaneChangeParams() sumo_lc_params.__dict__ = veh_params["sumo_lc_params"] del veh_params["sumo_car_following_params"], \ veh_params["sumo_lc_params"], \ veh_params["acceleration_controller"], \ veh_params["lane_change_controller"], \ veh_params["routing_controller"] veh.add(acceleration_controller=acc_controller, lane_change_controller=lc_controller, routing_controller=rt_controller, sumo_car_following_params=sumo_cf_params, sumo_lc_params=sumo_lc_params, **veh_params) # convert all parameters from dict to their object form sumo = SumoParams() sumo.__dict__ = flow_params["sumo"].copy() net = NetParams() net.__dict__ = flow_params["net"].copy() net.in_flows = InFlows() if flow_params["net"]["in_flows"]: net.in_flows.__dict__ = flow_params["net"]["in_flows"].copy() env = EnvParams() env.__dict__ = flow_params["env"].copy() initial = InitialConfig() if "initial" in flow_params: initial.__dict__ = flow_params["initial"].copy() tls = TrafficLights() if "tls" in flow_params: tls.__dict__ = flow_params["tls"].copy() flow_params["sumo"] = sumo flow_params["env"] = env flow_params["initial"] = initial flow_params["net"] = net flow_params["veh"] = veh flow_params["tls"] = tls return flow_params
def bay_bridge_bottleneck_example(render=None, use_traffic_lights=False): """Perform a simulation of the toll portion of the Bay Bridge. This consists of the toll booth and sections of the road leading up to it. Parameters ---------- render : bool, optional specifies whether to use sumo's gui during execution use_traffic_lights: bool, optional whether to activate the traffic lights in the scenario Note ---- Unlike the bay_bridge_example, inflows are always activated here. """ sumo_params = SumoParams(sim_step=0.4, overtake_right=True) if render is not None: sumo_params.render = render sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2) sumo_lc_params = SumoLaneChangeParams( model="LC2013", lcCooperative=0.2, lcSpeedGain=15) vehicles = Vehicles() vehicles.add( veh_id="human", acceleration_controller=(SumoCarFollowingController, {}), routing_controller=(BayBridgeRouter, {}), speed_mode="all_checks", lane_change_mode="no_lat_collide", sumo_car_following_params=sumo_car_following_params, sumo_lc_params=sumo_lc_params, num_vehicles=50) additional_env_params = {} env_params = EnvParams(additional_params=additional_env_params) inflow = InFlows() inflow.add( veh_type="human", edge="393649534", probability=0.2, departLane="random", departSpeed=10) inflow.add( veh_type="human", edge="4757680", probability=0.2, departLane="random", departSpeed=10) inflow.add( veh_type="human", edge="32661316", probability=0.2, departLane="random", departSpeed=10) inflow.add( veh_type="human", edge="90077193#0", vehs_per_hour=2000, departLane="random", departSpeed=10) net_params = NetParams( inflows=inflow, no_internal_links=False, netfile=NETFILE) # download the netfile from AWS if use_traffic_lights: my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \ "bay_bridge_TL_all_green.net.xml" else: my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \ "bay_bridge_junction_fix.net.xml" my_file = urllib.request.urlopen(my_url) data_to_write = my_file.read() with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), NETFILE), "wb+") as f: f.write(data_to_write) initial_config = InitialConfig( spacing="uniform", # "random", min_gap=15) scenario = BayBridgeTollScenario( name="bay_bridge_toll", generator_class=BayBridgeTollGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = BayBridgeEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
def minicity_example(render=None, save_render=None, sight_radius=None, pxpm=None, show_radius=None): """ Perform a simulation of vehicles on modified minicity of University of Delaware. Parameters ---------- render: bool, optional specifies whether to use the gui during execution Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles on the minicity scenario. """ sim_params = SumoParams(sim_step=0.25) # update sim_params values if provided as inputs sim_params.render = render or sim_params.render sim_params.save_render = save_render or sim_params.save_render sim_params.sight_radius = sight_radius or sim_params.sight_radius sim_params.pxpm = pxpm or sim_params.pxpm sim_params.show_radius = show_radius or sim_params.show_radius vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), routing_controller=(MinicityRouter, {}), car_following_params=SumoCarFollowingParams(speed_mode=1, ), lane_change_params=SumoLaneChangeParams( lane_change_mode="no_lat_collide", ), initial_speed=0, num_vehicles=90) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(MinicityRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), initial_speed=0, num_vehicles=10) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) additional_net_params = ADDITIONAL_NET_PARAMS.copy() net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="random", min_gap=5) scenario = MiniCityScenario(name="minicity", vehicles=vehicles, initial_config=initial_config, net_params=net_params) env = AccelEnv(env_params, sim_params, scenario) return Experiment(env)
def minicity_example(render=None, save_render=None, sight_radius=None, pxpm=None, show_radius=None): """ Perform a simulation of vehicles on modified minicity of University of Delaware. Parameters ---------- render: bool, optional specifies whether to use sumo's gui during execution Returns ------- exp: flow.core.SumoExperiment type A non-rl experiment demonstrating the performance of human-driven vehicles on the minicity scenario. """ sumo_params = SumoParams(sim_step=0.25) if render is not None: sumo_params.render = render if save_render is not None: sumo_params.save_render = save_render if sight_radius is not None: sumo_params.sight_radius = sight_radius if pxpm is not None: sumo_params.pxpm = pxpm if show_radius is not None: sumo_params.show_radius = show_radius vehicles = Vehicles() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), routing_controller=(MinicityRouter, {}), speed_mode=1, lane_change_mode="no_lat_collide", initial_speed=0, num_vehicles=90) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(MinicityRouter, {}), speed_mode="no_collide", initial_speed=0, num_vehicles=10) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) additional_net_params = ADDITIONAL_NET_PARAMS.copy() net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="random", min_gap=5) scenario = MiniCityScenario(name="minicity", vehicles=vehicles, initial_config=initial_config, net_params=net_params) env = AccelEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
flow_params = dict( # name of the experiment exp_tag='Lanechange', # name of the flow environment the experiment is running on env_name=LaneChangeAccelEnv, # name of the network class the experiment is running on network=LanechangeNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( render=True, lateral_resolution=1.0, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=2000, additional_params=ADDITIONAL_ENV_PARAMS.copy(), ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params=ADDITIONAL_NET_PARAMS.copy(), ),
# name of the experiment exp_tag="bottleneck_0", # name of the flow environment the experiment is running on env_name="DesiredVelocityEnv", # name of the scenario class the experiment is running on scenario="BottleneckScenario", # name of the generator used to create/modify network configuration files generator="BottleneckGenerator", # sumo-related parameters (see flow.core.params.SumoParams) sumo=SumoParams( sim_step=0.5, sumo_binary="sumo", print_warnings=False, restart_instance=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( warmup_steps=40, sims_per_step=1, horizon=HORIZON, additional_params=additional_env_params, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( in_flows=inflow,
}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(speed_mode='aggressive', ), num_vehicles=50) vehicles.add(veh_id="rl", lane_change_controller=(SimLaneChangeController, {}), lane_change_params=SumoLaneChangeParams( lane_change_mode="aggressive", ), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode='obey_safe_speed', ), num_vehicles=1) from flow.core.params import SumoParams sim_params = SumoParams(sim_step=0.1, render=False) # EnvParams specifies environment and experiment-specific parameters that either affect the training process or the dynamics of various components within the network. For the environment WaveAttenuationPOEnv, these parameters are used to dictate bounds on the accelerations of the autonomous vehicles, as well as the range of ring lengths (and accordingly network densities) the agent is trained on. #Finally, it is important to specify here the horizon of the experiment, which is the duration of one episode (during which the RL-agent acquire data). from flow.core.params import EnvParams env_params = EnvParams( # length of one rollout horizon=HORIZON, warmup_steps=750, clip_actions=False, additional_params={ # maximum acceleration of autonomous vehicles "max_accel": 1,
def make_flow_params(n_rows, n_columns, edge_inflow): """ Generate the flow params for the experiment. :param n_rows: :param n_columns: :param edge_inflow: :return: """ # we place a sufficient number of vehicles to ensure they confirm with the # total number specified above. We also use a "right_of_way" speed mode to # support traffic light compliance vehicles = VehicleParams() num_vehicles = (N_LEFT + N_RIGHT) * n_columns + (N_BOTTOM + N_TOP) * n_rows vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=num_vehicles) # inflows of vehicles are place on all outer edges (listed here) outer_edges = [] outer_edges += ["left{}_{}".format(n_rows, i) for i in range(n_columns)] outer_edges += ["right0_{}".format(i) for i in range(n_rows)] outer_edges += ["bot{}_0".format(i) for i in range(n_rows)] outer_edges += ["top{}_{}".format(i, n_columns) for i in range(n_rows)] # equal inflows for each edge (as dictate by the EDGE_INFLOW constant) inflow = InFlows() for edge in outer_edges: inflow.add(veh_type="human", edge=edge, vehs_per_hour=edge_inflow, departLane="free", departSpeed=V_ENTER) flow_params = dict( # name of the experiment exp_tag="grid_0_{}x{}_i{}_multiagent".format(n_rows, n_columns, edge_inflow), # name of the flow environment the experiment is running on env_name='MultiTrafficLightGridPOEnv', # name of the scenario class the experiment is running on scenario="SimpleGridScenario", # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( restart_instance=True, sim_step=1, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": 50, "switch_time": 3, "num_observed": 2, "discrete": False, "tl_type": "actuated", "num_local_edges": 4, "num_local_lights": 4, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, no_internal_links=False, additional_params={ "speed_limit": V_ENTER + 5, # inherited from grid0 benchmark "grid_array": { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": n_rows, "col_num": n_columns, "cars_left": N_LEFT, "cars_right": N_RIGHT, "cars_top": N_TOP, "cars_bot": N_BOTTOM, }, "horizontal_lanes": 1, "vertical_lanes": 1, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization # or reset (see flow.core.params.InitialConfig) initial=InitialConfig( spacing='custom', shuffle=True, ), ) return flow_params
def test_reset_inflows(self): """Tests that the inflow change within the expected range when calling reset.""" # set a random seed for inflows to be the same every time np.random.seed(seed=123) sim_params = SumoParams(sim_step=0.5, restart_instance=True) vehicles = VehicleParams() vehicles.add(veh_id="human") vehicles.add(veh_id="followerstopper") # edge name, how many segments to observe/control, whether the segment # is controlled controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True), ("4", 2, True), ("5", 1, False)] num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)] env_params = EnvParams( additional_params={ "target_velocity": 40, "disable_tb": True, "disable_ramp_metering": True, "controlled_segments": controlled_segments, "symmetric": False, "observed_segments": num_observed_segments, "reset_inflow": True, # this must be set to True for the test "lane_change_duration": 5, "max_accel": 3, "max_decel": 3, "inflow_range": [1000, 2000] # this is what we're testing } ) inflow = InFlows() inflow.add(veh_type="human", edge="1", vehs_per_hour=1500, # the initial inflow we're checking for departLane="random", departSpeed=10) net_params = NetParams( inflows=inflow, additional_params={"scaling": 1, "speed_limit": 23}) scenario = BottleneckScenario( name="bay_bridge_toll", vehicles=vehicles, net_params=net_params) env = DesiredVelocityEnv(env_params, sim_params, scenario) # reset the environment and get a new inflow rate env.reset() expected_inflow = 1353.6 # just from checking the new inflow # check that the first inflow rate is approximately what the seeded # value expects it to be for _ in range(500): env.step(rl_actions=None) self.assertAlmostEqual( env.k.vehicle.get_inflow_rate(250)/expected_inflow, 1, 1)
from rllab.envs.gym_env import GymEnv from rllab.envs.normalized_env import normalize from rllab.misc.instrument import run_experiment_lite from rllab.algos.ppo import PPO from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline from rllab.policies.gaussian_gru_policy import GaussianGRUPolicy SCALING = 1 NUM_LANES = 4 * SCALING # number of lanes in the widest highway DISABLE_TB = True DISABLE_RAMP_METER = True AV_FRAC = .1 N_CPUS = 32 i = 0 sim_params = SumoParams( sim_step=0.5, render=False, restart_instance=True) vehicles = VehicleParams() vehicles.add( veh_id="human", lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # 1621,#0b100000101, ), num_vehicles=1 * SCALING)
flow_params = dict( # name of the experiment exp_tag="ring_human", # name of the flow environment the experiment is running on env_name=WaveAttenuationPOEnv, # name of the network class the experiment is running on network=RingNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=0.1, render=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, warmup_steps=750, clip_actions=False, additional_params={ "max_accel": 1, "max_decel": 1, "ring_length": [220, 270], }, ), # network-related parameters (see flow.core.params.NetParams and the
# name of the experiment exp_tag="1x1_DECENTRALIZED_Thesis", # name of the flow environment the experiment is running on env_name=DeCentralizedGridEnv, # name of the network class the experiment is running on network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( restart_instance=True, sim_step=1, render=False, emission_path='~/flow/data', ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=200, additional_params={ "target_velocity": 11, "switch_time": 4, "yellow_phase_duration": 4, "num_observed": 2, "discrete": False, "tl_type": "actuated", "num_local_edges": 4, "num_local_lights": 4,
def para_produce_rl(HORIZON=3000): # Create default environment parameters env_params = EnvParams() # Vehicle definition vehicles = VehicleParams() num_vehicles = 1 vehicles.add( veh_id="human", routing_controller=(GridRouter, {}), lane_change_controller=(SimLaneChangeController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, decel=7.5, # avoid collisions at emergency stops ), lane_change_params=SumoLaneChangeParams( lane_change_mode=1621, ), num_vehicles=num_vehicles) # whether to allow turns at intersections ALLOW_TURNS = False # initialize traffic lights, used when you want define your own traffic lights tl_logic = TrafficLightParams(baseline=False) # To see static traffic lights in action, the `TrafficLightParams` object should be instantiated with `baseline=False` # when use off_ramp_grid.net.xml file, you should use a phase state example as "GGGgrrrrGGGgrrrr" # when use off_ramp_grid_turn.net.xml file, you should use a phase state example as "GGGggrrrrrGGGggrrrrr" if ALLOW_TURNS: phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", # for actuated traffic lights, you can add these optional values below # "maxGap": int, describes the maximum time gap between successive vehicle sthat will cause the current phase to be prolonged # "detectorGap": int, determines the time distance between the (automatically generated) detector and the stop line in seconds # "showDetectors": bool, toggles whether or not detectors are shown in sumo-gui "state": "GGGggrrrrrGGGggrrrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyyyrrrrryyyyyrrrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrrrGGGggrrrrrGGGgg" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrrrryyyyyrrrrryyyyy" }] tl_logic.add("center0", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center1", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center2", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center3", phases=phases, programID=1, detectorGap=1,tls_type="actuated") else: phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", # for actuated traffic lights, you can add these optional values below # "maxGap": int, describes the maximum time gap between successive vehicle sthat will cause the current phase to be prolonged # "detectorGap": int, determines the time distance between the (automatically generated) detector and the stop line in seconds # "showDetectors": bool, toggles whether or not detectors are shown in sumo-gui "state": "GGGgrrrrGGGgrrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyyrrrryyyyrrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrrGGGgrrrrGGGg" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrrryyyyrrrryyyy" }] # THIS IS A BUG THAT I DON'T KNOW WHY IT HAPPENS!!!!!! phase0 = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGrrGGrrGGrrGGrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyrryyrryyrryyrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrGGrrGGrrGGrrGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rryyrryyrryyrryy" }] tl_logic.add("center0", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center1", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center2", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center3", phases=phases, programID=1, detectorGap=1,tls_type="actuated") flow_params = dict( exp_tag='offramp_multiagent_inflow_1.0_speed_20', env_name=MultiTrafficLightGridPOEnv, network=offRampGrid, simulator='traci', sim=SumoParams( sim_step=0.1, render=False, #emission_path='./data', restart_instance=True, ), env=EnvParams( horizon=3000, additional_params=ADDITIONAL_ENV_PARAMS.copy(), ), net=net_params, veh=vehicles, initial=initial_config, # used when you define your own traffic lights #tls=tl_logic, ) #flow_params['env'].horizon = HORIZON return flow_params
# name of the experiment (for multiple policies!) exp_tag="multimultiagent-traffic-light", # name of the flow environment the experiment is running on env_name=MultiTrafficLightGridPOEnv, # name of the network class the experiment is running on network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( restart_instance=True, sim_step=SIM_STEP, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": V_TARGET, "switch_time": SWITCH_TIME, "num_observed": CARS_OBSERVED, "discrete": DISCRETE, "tl_type": "actuated", "num_local_edges": LOCAL_EDGES, "num_local_lights": LOCAL_LIGHTS, },
acceleration_controller=(RLController, {}), lane_change_controller=(StaticLaneChanger, {}), # routing_controller=(HighwayRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), lane_change_params=SumoLaneChangeParams(lane_change_mode=256, model="SL2015", lc_impatience="0.1", lc_time_to_impatience="1.0"), num_vehicles=0) from flow.core.params import SumoParams sim_params = SumoParams( sim_step=0.2, render=False, lateral_resolution=1.0, restart_instance=True, ) import os inflow = InFlows() inflow.add( veh_type="human", edge="WC", # depart_lane="best", depart_lane=1, arrivalLane=0, probability=0.1, depart_speed="random", )
def run_task(_): """Implement the run_task method needed to run experiments with rllab.""" sumo_params = SumoParams(sumo_binary="sumo-gui", sim_step=0.2, restart_instance=True) # RL vehicles constitute 5% of the total number of vehicles vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), speed_mode="no_collide", num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), speed_mode="no_collide", num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles # entering from the highway portion as well inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=RL_PENETRATION * FLOW_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) additional_env_params = { "target_velocity": 25, "num_rl": NUM_RL, "max_accel": 1.5, "max_decel": 1.5 } env_params = EnvParams(horizon=HORIZON, sims_per_step=5, warmup_steps=0, additional_params=additional_env_params) additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 500 net_params = NetParams(in_flows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", lanes_distribution=float("inf")) scenario = MergeScenario(name="merge-rl", generator_class=MergeGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config) env_name = "WaveAttenuationMergePOEnv" pass_params = (env_name, sumo_params, vehicles, env_params, net_params, initial_config, scenario) env = GymEnv(env_name, record_video=False, register_params=pass_params) env = normalize(env) policy = GaussianMLPPolicy( env_spec=env.spec, hidden_sizes=(32, 32, 32), ) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=HORIZON * N_ROLLOUTS, max_path_length=HORIZON, n_itr=1000, # whole_paths=True, discount=0.999, ) algo.train(),
def variable_lanes_exp_setup(sim_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, traffic_lights=None): """ Create an environment and network variable-lane ring road. Each edge in this network can have a different number of lanes. Used for test purposes. Parameters ---------- sim_params : flow.core.params.SumoParams sumo-related configuration parameters, defaults to a time step of 0.1s and no sumo-imposed failsafe on human or rl vehicles vehicles : Vehicles type vehicles to be placed in the network, default is one vehicles with an IDM acceleration controller and ContinuousRouter routing controller. env_params : flow.core.params.EnvParams environment-specific parameters, defaults to a environment with no failsafes, where other parameters do not matter for non-rl runs net_params : flow.core.params.NetParams network-specific configuration parameters, defaults to a figure eight with a 30 m radius initial_config : flow.core.params.InitialConfig specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network traffic_lights: flow.core.params.TrafficLightParams traffic light signals, defaults to no traffic lights in the network """ logging.basicConfig(level=logging.WARNING) if sim_params is None: # set default sim_params configuration sim_params = SumoParams(sim_step=0.1, render=False) if vehicles is None: # set default vehicles configuration vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), routing_controller=(ContinuousRouter, {}), num_vehicles=1) if env_params is None: # set default env_params configuration additional_env_params = { "target_velocity": 8, "max_accel": 1, "max_decel": 1, "sort_vehicles": False } env_params = EnvParams(additional_params=additional_env_params) if net_params is None: # set default net_params configuration additional_net_params = { "length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig() if traffic_lights is None: # set default to no traffic lights traffic_lights = TrafficLightParams() flow_params = dict( # name of the experiment exp_tag="VariableLaneRingRoadTest", # name of the flow environment the experiment is running on env_name=AccelEnv, # name of the network class the experiment is running on network=VariableLanesNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=sim_params, # environment related parameters (see flow.core.params.EnvParams) env=env_params, # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=net_params, # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=initial_config, # traffic lights to be introduced to specific nodes (see # flow.core.params.TrafficLightParams) tls=traffic_lights, ) # create the network network = VariableLanesNetwork(name="VariableLaneRingRoadTest", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # create the environment env = AccelEnv(env_params=env_params, sim_params=sim_params, network=network) # reset the environment env.reset() return env, network, flow_params
def get_flow_params(fixed_boundary, stopping_penalty, acceleration_penalty, evaluate=False, multiagent=False, imitation=False): """Return the flow-specific parameters of the single lane highway network. Parameters ---------- fixed_boundary : bool specifies whether the boundary conditions update in between resets stopping_penalty : bool whether to include a stopping penalty acceleration_penalty : bool whether to include a regularizing penalty for accelerations by the AVs evaluate : bool whether to compute the evaluation reward multiagent : bool whether the automated vehicles are via a single-agent policy or a shared multi-agent policy with the actions of individual vehicles assigned by a separate policy call imitation : bool whether to use the imitation environment Returns ------- dict flow-related parameters, consisting of the following keys: * exp_tag: name of the experiment * env_name: environment class of the flow environment the experiment is running on. (note: must be in an importable module.) * network: network class the experiment uses. * simulator: simulator that is used by the experiment (e.g. aimsun) * sim: simulation-related parameters (see flow.core.params.SimParams) * env: environment related parameters (see flow.core.params.EnvParams) * net: network-related parameters (see flow.core.params.NetParams and the network's documentation or ADDITIONAL_NET_PARAMS component) * veh: vehicles to be placed in the network at the start of a rollout (see flow.core.params.VehicleParams) * initial (optional): parameters affecting the positioning of vehicles upon initialization/reset (see flow.core.params.InitialConfig) * tls (optional): traffic lights to be introduced to specific nodes (see flow.core.params.TrafficLightParams) """ # steps to run before the agent is allowed to take control (set to lower # value during testing) warmup_steps = 50 if os.environ.get("TEST_FLAG") else 500 additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params.update({ # length of the highway "length": 2500, # number of lanes "lanes": 1, # speed limit for all edges "speed_limit": 30, # number of edges to divide the highway into "num_edges": 2, # whether to include a ghost edge of length 500m. This edge is provided # a different speed limit. "use_ghost_edge": True, # speed limit for the ghost edge "ghost_speed_limit": END_SPEED, # length of the cell imposing a boundary "boundary_cell_length": 300, }) vehicles = VehicleParams() inflows = InFlows() # human vehicles vehicles.add( "human", num_vehicles=0, acceleration_controller=(IDMController, { "a": 1.3, "b": 2.0, "noise": 0.3 if INCLUDE_NOISE else 0.0 }), car_following_params=SumoCarFollowingParams(min_gap=0.5), lane_change_params=SumoLaneChangeParams( model="SL2015", lc_sublane=2.0, ), ) inflows.add(veh_type="human", edge="highway_0", vehs_per_hour=int(TRAFFIC_FLOW * (1 - PENETRATION_RATE)), depart_lane="free", depart_speed=TRAFFIC_SPEED, name="idm_highway_inflow") # automated vehicles vehicles.add( "rl", num_vehicles=0, acceleration_controller=(RLController, {}), ) inflows.add(veh_type="rl", edge="highway_0", vehs_per_hour=int(TRAFFIC_FLOW * PENETRATION_RATE), depart_lane="free", depart_speed=TRAFFIC_SPEED, name="rl_highway_inflow") # SET UP THE FLOW PARAMETERS if multiagent: if imitation: env_name = None # to be added later else: env_name = AVOpenMultiAgentEnv else: if imitation: env_name = AVOpenImitationEnv else: env_name = AVOpenEnv return dict( # name of the experiment exp_tag="highway", # name of the flow environment the experiment is running on env_name=env_name, # name of the network class the experiment is running on network=HighwayNetwork, # simulator that is used by the experiment simulator="traci", # environment related parameters (see flow.core.params.EnvParams) env=EnvParams(evaluate=evaluate, horizon=HORIZON, warmup_steps=warmup_steps, sims_per_step=3, additional_params={ "max_accel": 0.5, "max_decel": 0.5, "target_velocity": 10, "stopping_penalty": stopping_penalty, "acceleration_penalty": acceleration_penalty, "inflows": None if fixed_boundary else INFLOWS, "rl_penetration": PENETRATION_RATE, "num_rl": 10, "control_range": [500, 2300], "expert_model": (IDMController, { "a": 1.3, "b": 2.0, }), }), # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=0.4, render=False, restart_instance=True, use_ballistic=True, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(inflows=inflows, additional_params=additional_net_params), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon init/reset # (see flow.core.params.InitialConfig) initial=InitialConfig(), )
# name of the experiment exp_tag="bottleneck_1", # name of the flow environment the experiment is running on env_name="DesiredVelocityEnv", # name of the scenario class the experiment is running on scenario="BottleneckScenario", # name of the generator used to create/modify network configuration files generator="BottleneckGenerator", # sumo-related parameters (see flow.core.params.SumoParams) sumo=SumoParams( sim_step=0.5, render=False, print_warnings=False, restart_instance=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( warmup_steps=40, sims_per_step=1, horizon=HORIZON, additional_params=additional_env_params, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow,
def test_no_junctions_highway(self): additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 1 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add(veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = { "start_positions": [('highway_0', 20), ('highway_0', 30), ('highway_0', 10)], "start_lanes": [1, 2, 0] } initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in central lane # test_1 should be leading car in lane 2 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [1000, 1000, 5.0] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [5.0, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [0.0, 0.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Next, test the case where all vehicles are on the same # edge and there's two vehicles in each lane # Cases to test # 1. For lane 0, should find a leader and follower for tested car # 2. For lane 1, both vehicles are behind the test car # 3. For lane 2, both vehicles are in front of the tested car # 4. For lane 3, one vehicle in front and one behind the tested car additional_net_params = { "length": 100, "lanes": 4, "speed_limit": 30, "resolution": 40, "num_edges": 1 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add(veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=9, initial_speed=1.0) initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = { "start_positions": [ ('highway_0', 50), ('highway_0', 60), ('highway_0', 40), ('highway_0', 40), ('highway_0', 30), ('highway_0', 60), ('highway_0', 70), ('highway_0', 60), ('highway_0', 40), ], "start_lanes": [0, 0, 0, 1, 1, 2, 2, 3, 3] } initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", "test_5", "test_7"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [5.0, 1000, 5.0, 5.0] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "test_3", "", "test_8"] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [5.0, 5.0, 1000, 5.0] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [1.0, 0.0, 1.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 1.0, 0.0, 1.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Now test if all the vehicles are on different edges and # different lanes additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 3 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add(veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = { "start_positions": [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)], "start_lanes": [1, 2, 0] } initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in central lane # test_1 should be leading car in lane 2 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [1000, 1000, 19.996667] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [19.996667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [0.0, 0.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Now test if all the vehicles are on different edges and same # lanes additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 3 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add(veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = { "start_positions": [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)], "start_lanes": [0, 0, 0] } initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in lane 0 # test_1 should be leading car in lane 0 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", ""] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [19.996667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [19.996667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [1.0, 0.0, 0.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed)