evaluate=True, # Set to True to evaluate traffic metrics warmup_steps=40, sims_per_step=1, horizon=HORIZON, additional_params=additional_env_params, ) initial_config = InitialConfig( spacing="uniform", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"], ) scenario = BottleneckScenario(name="bay_bridge_toll", generator_class=BottleneckGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = DesiredVelocityEnv(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) num_runs = 2 results = exp.run(num_runs, HORIZON) avg_outflow = np.mean([outflow[-1] for outflow in results["per_step_returns"]]) print('The average outflow over 500 seconds ' 'across {} runs is {}'.format(num_runs, avg_outflow))
def bottleneck_example(flow_rate, horizon, restart_instance=False, render=None): """ Perform a simulation of vehicles on a bottleneck. Parameters ---------- flow_rate : float total inflow rate of vehicles into the bottleneck horizon : int time horizon restart_instance: bool, optional whether to restart the instance upon reset 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 a bottleneck. """ if render is None: render = False sim_params = SumoParams(sim_step=0.5, render=render, overtake_right=False, restart_instance=restart_instance) vehicles = VehicleParams() vehicles.add(veh_id="human", lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(speed_mode=31, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=00, ), num_vehicles=1) num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)] controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True), ("4", 2, True), ("5", 1, False)] additional_env_params = { "target_velocity": 40, "disable_tb": True, "disable_ramp_metering": True, "symmetric": True, "observed_segments": num_observed_segments, "controlled_segments": controlled_segments, "reset_inflow": False, "lane_change_duration": 5, "max_accel": 2, "max_decel": 2, "inflow_range": [1000, 2000] } env_params = EnvParams(horizon=horizon, additional_params=additional_env_params) inflow = InFlows() inflow.add(veh_type="human", edge="1", vehsPerHour=flow_rate, departLane="random", departSpeed=10) traffic_lights = TrafficLightParams() if not DISABLE_TB: traffic_lights.add(node_id="2") if not DISABLE_RAMP_METER: traffic_lights.add(node_id="3") additional_net_params = {"scaling": SCALING, "speed_limit": 23} net_params = NetParams(inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="random", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"]) scenario = BottleneckScenario(name="tcy_base", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = DesiredVelocityEnv(env_params, sim_params, scenario) return BottleneckDensityExperiment(env)
def bottleneck0_baseline(num_runs, sumo_binary="sumo-gui"): """Run script for the bottleneck0 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over sumo_binary: str, optional specifies whether to use sumo's gui during execution Returns ------- SumoExperiment class needed to run simulations """ vehicles = Vehicles() vehicles.add(veh_id="human", speed_mode=9, routing_controller=(ContinuousRouter, {}), lane_change_mode=0, num_vehicles=1 * SCALING) 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)] additional_env_params = { "target_velocity": 40, "disable_tb": True, "disable_ramp_metering": True, "controlled_segments": controlled_segments, "symmetric": False, "observed_segments": num_observed_segments, "reset_inflow": False, "lane_change_duration": 5, "max_accel": 3, "max_decel": 3, "inflow_range": [1000, 2000] } # flow rate flow_rate = 1900 * SCALING # percentage of flow coming out of each lane inflow = InFlows() inflow.add(veh_type="human", edge="1", vehs_per_hour=flow_rate, departLane="random", departSpeed=10) traffic_lights = TrafficLights() if not DISABLE_TB: traffic_lights.add(node_id="2") if not DISABLE_RAMP_METER: traffic_lights.add(node_id="3") additional_net_params = {"scaling": SCALING} net_params = NetParams(in_flows=inflow, no_internal_links=False, additional_params=additional_net_params) sumo_params = SumoParams( sim_step=0.5, sumo_binary=sumo_binary, print_warnings=False, restart_instance=False, ) env_params = EnvParams( evaluate=True, # Set to True to evaluate traffic metrics warmup_steps=40, sims_per_step=1, horizon=HORIZON, additional_params=additional_env_params, ) initial_config = InitialConfig( spacing="uniform", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"], ) scenario = BottleneckScenario(name="bay_bridge_toll", generator_class=BottleneckGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = DesiredVelocityEnv(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) results = exp.run(num_runs, HORIZON) avg_outflow = np.mean([outflow[-1] for outflow in results["per_step_returns"]]) return avg_outflow
def bottleneck_example(flow_rate, horizon, restart_instance=False, render=False, scaling=1, disable_ramp_meter=True, disable_tb=True, lc_on=False, n_crit=8.0, q_max=None, q_min=None, feedback_coef=1, q_init=2300, penetration_rate=0.4): """ Perform a simulation of vehicles on a bottleneck. Parameters ---------- flow_rate : float total inflow rate of vehicles into the bottleneck horizon : int time horizon restart_instance: bool, optional whether to restart the instance upon reset render: bool, optional specifies whether to use the gui during execution scaling: int, optional This sets the number of lanes so that they go from 4 * scaling -> 2 * scaling -> 1 * scaling disable_tb: bool, optional whether the toll booth should be active disable_ramp_meter: bool, optional specifies if ALINEA should be active. For more details, look at the BottleneckEnv documentation lc_on: bool, optional if true, the vehicles have LC mode 1621 which is all safe lane changes allowed. Otherwise, it is 0 for no lane changing. n_crit: float, optional number of vehicles in the bottleneck we feedback around. Look at BottleneckEnv for details q_max: float, optional maximum permissible ALINEA flow. Look at BottleneckEnv for details q_min: float, optional minimum permissible ALINEA flow. Look at BottleneckEnv for details feedback_coeff: float, optional gain coefficient for ALINEA. Look at BottleneckEnv for details Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles on a bottleneck. """ if render is None: render = False sim_params = SumoParams( sim_step=0.5, render=render, restart_instance=restart_instance) vehicles = VehicleParams() if lc_on: lc_mode = 1621 else: lc_mode = 0 controlled_segments = [("1", 1, True), ("2", 8, True), ("3", 3, True), ("4", 1, True), ("5", 3, True)] # 12 controllable segments num_observed_segments = [('1', 1), ('2', 3), ('3', 3), ('4', 3), ('5', 1)] v_regions = [23, 23, 23, 10, 5, 5, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23] # set default q_max, q_min values if not q_max: if disable_ramp_meter: q_max = 14401 else: q_max = 3000 if not q_min: if disable_ramp_meter: q_min = 200 else: q_min = 900 additional_env_params = { "target_velocity": 40, "max_accel": 3, "max_decel": 3, "lane_change_duration": 5, "add_rl_if_exit": False, "disable_tb": disable_tb, "disable_ramp_metering": disable_ramp_meter, "n_crit": n_crit, "q_max": 14401, "q_min": 200, "q_init": q_init, "feedback_coeff": feedback_coef, "controlled_segments": controlled_segments, "inflow_range": [flow_rate, flow_rate], "reset_inflow": False, "symmetric": True, "observed_segments": num_observed_segments, "congest_penalty": False, "lc_mode": lc_mode, 'start_inflow': flow_rate, 'life_penalty': 0.00, 'keep_past_actions': False, "num_sample_seconds": 0.5, "speed_reward": False, 'fair_reward': False, # This doesn't do anything, remove 'exit_history_seconds': 10, # This doesn't do anything, remove } if penetration_rate != 0.0: vehicles.add( veh_id="AV", lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), # acceleration_controller=(CFMController, {"v_des": 10, "d_des": 30, "k_d": 30, "k_v": 15}), # acceleration_controller=(HandTunedVelocityController, {"v_regions": v_regions}), # acceleration_controller=(DecentralizedALINEAController, {"stop_edge": "2", "stop_pos": 310, "additional_env_params": additional_env_params}), acceleration_controller=(FakeDecentralizedALINEAController, {"stop_edge": "2", "stop_pos": 310, "additional_env_params": additional_env_params}), car_following_params=SumoCarFollowingParams( speed_mode=31, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=lc_mode ), num_vehicles=1) vehicles.add( veh_id="human", lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=31, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=lc_mode ), num_vehicles=1) env_params = EnvParams( horizon=horizon, additional_params=additional_env_params) inflow = InFlows() if penetration_rate != 0.0: av_veh_per_hour = flow_rate * (penetration_rate) human_veh_per_hour = flow_rate * (1 - penetration_rate) inflow.add( veh_type="AV", edge="1", vehsPerHour=av_veh_per_hour, departLane="random", departSpeed=23) else: human_veh_per_hour = flow_rate inflow.add( veh_type="human", edge="1", vehsPerHour=human_veh_per_hour, departLane="random", departSpeed=23) traffic_lights = TrafficLightParams() if not disable_tb: traffic_lights.add(node_id="2") if not disable_ramp_meter: traffic_lights.add(node_id="3") additional_net_params = {"scaling": scaling, "speed_limit": 60} net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig( spacing="random", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"]) scenario = BottleneckScenario( name="bay_bridge_toll", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = DesiredVelocityEnv(env_params, sim_params, scenario) return BottleneckDensityExperiment(env, int(flow_rate))