def setUp(self): # add a traffic light to the top node traffic_lights = TrafficLights() # Phase durations in seconds self.green = 4 self.yellow = 1 self.red = 4 phases = [{ "duration": repr(self.green), "state": "G" }, { "duration": repr(self.yellow), "state": "y" }, { "duration": repr(self.red), "state": "r" }] traffic_lights.add("top", phases=phases) # create a ring road with two lanes additional_net_params = { "length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, traffic_lights=traffic_lights)
def setUp(self): # add a traffic light to the top node traffic_lights = TrafficLights() traffic_lights.add("top") # create a ring road with two lanes additional_net_params = {"length": 230, "lanes": 2, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, traffic_lights=traffic_lights)
def test_multi_lane(self): # add a traffic light to the top node traffic_lights = TrafficLights() traffic_lights.add("top") # create a ring road with two lanes additional_net_params = {"length": 230, "lanes": 2, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, traffic_lights=traffic_lights) self.env.reset() self.env.step([]) state = self.env.traffic_lights.get_state("top") self.assertEqual(state, "GG")
def setUp(self): tl_logic = TrafficLights(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGGrrrGGGrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrGGGrrrGGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrryyyrrryyy" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1, offset=1) tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1) tl_logic.add("center3", tls_type="actuated", phases=phases, programID=1, maxGap=3.0, detectorGap=0.8, showDetectors=True, file="testindividuallights.xml", freq=100) env, scenario = grid_mxn_exp_setup(row_num=1, col_num=4, tl_logic=tl_logic) self.exp = SumoExperiment(env, scenario)
} # 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-gui", print_warnings=False, restart_instance=False, )
"duration": "6", "state": "yyyrrryyyrrr" }, { "duration": "50", # how long the light is green east-west "state": "rrrGGGrrrGGG" }, { "duration": "6", "state": "rrryyyrrryyy" } ] for i in range(N_ROWS * N_COLUMNS): tl_logic.add("center" + str(i), tls_type="static", phases=phases, programID=1) net_params = NetParams( in_flows=inflow, no_internal_links=False, additional_params={ "speed_limit": V_ENTER + 5, "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,
def bottleneck_example(flow_rate, horizon, sumo_binary=None): if sumo_binary is None: sumo_binary = "sumo" sumo_params = SumoParams(sim_step=0.5, sumo_binary=sumo_binary, overtake_right=False, restart_instance=True) vehicles = Vehicles() vehicles.add(veh_id="human", speed_mode=25, lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_mode=1621, num_vehicles=1) additional_env_params = { "target_velocity": 40, "max_accel": 1, "max_decel": 1, "lane_change_duration": 5, "add_rl_if_exit": False, "disable_tb": DISABLE_TB, "disable_ramp_metering": DISABLE_RAMP_METER } 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 = 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) initial_config = InitialConfig(spacing="random", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"]) scenario = BBTollScenario(name="bay_bridge_toll", generator_class=BBTollGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = BottleneckEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
def grid1_baseline(num_runs, render=True): """Run script for the grid1 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render: bool, optional specifies whether to use sumo's gui during execution Returns ------- SumoExperiment class needed to run simulations """ # 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 = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(SumoCarFollowingController, {}), sumo_car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, ), routing_controller=(GridRouter, {}), num_vehicles=(N_LEFT + N_RIGHT) * N_COLUMNS + (N_BOTTOM + N_TOP) * N_ROWS, speed_mode="right_of_way") # 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="max") # define the traffic light logic tl_logic = TrafficLights(baseline=False) phases = [{ "duration": "31", "minDur": "5", "maxDur": "45", "state": "GGGrrrGGGrrr" }, { "duration": "2", "minDur": "2", "maxDur": "2", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "5", "maxDur": "45", "state": "rrrGGGrrrGGG" }, { "duration": "2", "minDur": "2", "maxDur": "2", "state": "rrryyyrrryyy" }] for i in range(N_ROWS * N_COLUMNS): tl_logic.add("center" + str(i), tls_type="actuated", phases=phases, programID=1) net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params={ "speed_limit": V_ENTER + 5, "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, }, ) sumo_params = SumoParams( restart_instance=False, sim_step=1, render=render, ) env_params = EnvParams( evaluate=True, # Set to True to evaluate traffic metrics horizon=HORIZON, additional_params={ "target_velocity": 50, "switch_time": 2, "num_observed": 2, "discrete": False, "tl_type": "actuated" }, ) initial_config = InitialConfig(shuffle=True) scenario = SimpleGridScenario(name="grid", generator_class=SimpleGridGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = PO_TrafficLightGridEnv(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) results = exp.run(num_runs, HORIZON) total_delay = np.mean(results["returns"]) return total_delay
def grid_example(render=None): """ Perform a simulation of vehicles on a grid. 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 and balanced traffic lights on a grid. """ inner_length = 300 long_length = 500 short_length = 300 n = 2 m = 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) * m \ + (num_cars_top + num_cars_bot) * n grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n, "col_num": m, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sumo_params = SumoParams(sim_step=0.1, render=True) if render is not None: sumo_params.render = render vehicles = Vehicles() vehicles.add(veh_id="human", routing_controller=(GridRouter, {}), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLights(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGGrrrGGGrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrGGGrrrGGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrryyyrrryyy" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1) additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig() scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
for edge in outer_edges: inflow.add(veh_type="human", edge=edge, vehs_per_hour=EDGE_INFLOW, departLane="free", departSpeed="max") # define the traffic light logic tl_logic = TrafficLights(baseline=False) phases = [{"duration": "31", "minDur": "8", "maxDur": "45", "state": "GGGrrrGGGrrr"}, {"duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyrrryyyrrr"}, {"duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrGGGrrrGGG"}, {"duration": "6", "minDur": "3", "maxDur": "6", "state": "rrryyyrrryyy"}] for i in range(N_ROWS*N_COLUMNS): tl_logic.add("center"+str(i), tls_type="actuated", phases=phases, programID=1) net_params = NetParams( in_flows=inflow, no_internal_links=False, additional_params={ "speed_limit": V_ENTER + 5, "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,
def grid0_baseline(num_runs, render=True): """Run script for the grid0 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render : bool, optional specifies whether to use sumo's gui during execution Returns ------- SumoExperiment class needed to run simulations """ exp_tag = flow_params['exp_tag'] sumo_params = flow_params['sumo'] vehicles = flow_params['veh'] env_params = flow_params['env'] net_params = flow_params['net'] initial_config = flow_params.get('initial', InitialConfig()) # define the traffic light logic tl_logic = TrafficLights(baseline=False) phases = [{'duration': '31', 'minDur': '5', 'maxDur': '45', 'state': 'GGGrrrGGGrrr'}, {'duration': '2', 'minDur': '2', 'maxDur': '2', 'state': 'yyyrrryyyrrr'}, {'duration': '31', 'minDur': '5', 'maxDur': '45', 'state': 'rrrGGGrrrGGG'}, {'duration': '2', 'minDur': '2', 'maxDur': '2', 'state': 'rrryyyrrryyy'}] for i in range(N_ROWS * N_COLUMNS): tl_logic.add('center'+str(i), tls_type='actuated', phases=phases, programID=1) # modify the rendering to match what is requested sumo_params.render = render # set the evaluation flag to True env_params.evaluate = True # import the scenario class module = __import__('flow.scenarios', fromlist=[flow_params['scenario']]) scenario_class = getattr(module, flow_params['scenario']) # create the scenario object scenario = scenario_class( name=exp_tag, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic ) # import the environment class module = __import__('flow.envs', fromlist=[flow_params['env_name']]) env_class = getattr(module, flow_params['env_name']) # create the environment object env = env_class(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) results = exp.run(num_runs, env_params.horizon) total_delay = np.mean(results['returns']) return total_delay
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 grid_example(sumo_binary=None): inner_length = 300 long_length = 500 short_length = 300 n = 2 m = 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) * m \ + (num_cars_top + num_cars_bot) * n grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n, "col_num": m, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo-gui") if sumo_binary is not None: sumo_params.sumo_binary = sumo_binary vehicles = Vehicles() vehicles.add(veh_id="human", routing_controller=(GridRouter, {}), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLights(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGGrrrGGGrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrGGGrrrGGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrryyyrrryyy" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1) additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig() scenario = SimpleGridScenario(name="grid-intersection", generator_class=SimpleGridGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
def bottleneck_example(flow_rate, horizon, render=None): """ Perform a simulation of vehicles on a bottleneck. Parameters ---------- flow_rate : float total inflow rate of vehicles into the bottlneck horizon : int time horizon 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 bottleneck. """ if render is None: render = False sumo_params = SumoParams(sim_step=0.5, render=render, overtake_right=False, restart_instance=True) vehicles = Vehicles() vehicles.add(veh_id="human", speed_mode=25, lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_mode=1621, num_vehicles=1) additional_env_params = { "target_velocity": 40, "max_accel": 1, "max_decel": 1, "lane_change_duration": 5, "add_rl_if_exit": False, "disable_tb": DISABLE_TB, "disable_ramp_metering": DISABLE_RAMP_METER } 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 = 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(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", generator_class=BottleneckGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = BottleneckEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)