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)
# percent of autonomous vehicles RL_PENETRATION = 0.333 # num_rl term (see ADDITIONAL_ENV_PARAMs) NUM_RL = 17 # We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = deepcopy(ADDITIONAL_NET_PARAMS) additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 500 # RL vehicles constitute 5% of the total number of vehicles vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(SumoCarFollowingController, {}), 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",
def test_encoder_and_get_flow_params(self): """Tests both FlowParamsEncoder and get_flow_params. FlowParamsEncoder is used to serialize the data from a flow_params dict for replay by the visualizer later. Then, the get_flow_params method is used to try and read the parameters from the config file, and is checked to match expected results. """ # use a flow_params dict derived from flow/benchmarks/merge0.py vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), speed_mode="no_collide", num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), speed_mode="no_collide", num_vehicles=0) inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=1800, departLane="free", departSpeed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=200, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) flow_params = dict( exp_tag="merge_0", env_name="WaveAttenuationMergePOEnv", scenario="MergeScenario", sumo=SumoParams( restart_instance=True, sim_step=0.5, render=False, ), env=EnvParams( horizon=750, sims_per_step=2, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": 5, }, ), net=NetParams( inflows=inflow, no_internal_links=False, additional_params={ "merge_length": 100, "pre_merge_length": 500, "post_merge_length": 100, "merge_lanes": 1, "highway_lanes": 1, "speed_limit": 30, }, ), veh=vehicles, initial=InitialConfig(), tls=TrafficLights(), ) # create an config dict with space for the flow_params dict config = {"env_config": {}} # save the flow params for replay flow_json = json.dumps(flow_params, cls=FlowParamsEncoder, sort_keys=True, indent=4) config['env_config']['flow_params'] = flow_json # dump the config so we can fetch it json_out_file = 'params.json' with open(os.path.expanduser(json_out_file), 'w+') as outfile: json.dump(config, outfile, cls=FlowParamsEncoder, sort_keys=True, indent=4) # fetch values using utility function `get_flow_params` imported_flow_params = get_flow_params(config) # delete the created file os.remove(os.path.expanduser('params.json')) # test that this inflows are correct self.assertTrue(imported_flow_params["net"].inflows.__dict__ == flow_params["net"].inflows.__dict__) imported_flow_params["net"].inflows = None flow_params["net"].inflows = None # make sure the rest of the imported flow_params match the originals self.assertTrue(imported_flow_params["env"].__dict__ == flow_params["env"].__dict__) self.assertTrue(imported_flow_params["initial"].__dict__ == flow_params["initial"].__dict__) self.assertTrue(imported_flow_params["tls"].__dict__ == flow_params["tls"].__dict__) self.assertTrue(imported_flow_params["sumo"].__dict__ == flow_params["sumo"].__dict__) self.assertTrue(imported_flow_params["net"].__dict__ == flow_params["net"].__dict__) self.assertTrue( imported_flow_params["exp_tag"] == flow_params["exp_tag"]) self.assertTrue( imported_flow_params["env_name"] == flow_params["env_name"]) self.assertTrue( imported_flow_params["scenario"] == flow_params["scenario"]) def search_dicts(obj1, obj2): """Searches through dictionaries as well as lists of dictionaries recursively to determine if any two components are mismatched.""" for key in obj1.keys(): # if an next element is a list, either compare the two lists, # or if the lists contain dictionaries themselves, look at each # dictionary component recursively to check for mismatches if isinstance(obj1[key], list): if len(obj1[key]) > 0: if isinstance(obj1[key][0], dict): for i in range(len(obj1[key])): if not search_dicts(obj1[key][i], obj2[key][i]): return False elif obj1[key] != obj2[key]: return False # if the next element is a dict, run through it recursively to # determine if the separate elements of the dict match if isinstance(obj1[key], (dict, collections.OrderedDict)): if not search_dicts(obj1[key], obj2[key]): return False # if it is neither a list or a dictionary, compare to determine # if the two elements match elif obj1[key] != obj2[key]: # if the two elements that are being compared are objects, # make sure that they are the same type if not isinstance(obj1[key], type(obj2[key])): return False return True # make sure that the Vehicles class that was imported matches the # original one if not search_dicts(imported_flow_params["veh"].__dict__, flow_params["veh"].__dict__): raise AssertionError
# time horizon of a single rollout HORIZON = 1000 SCALING = 2 NUM_LANES = 4 * SCALING # number of lanes in the widest highway DISABLE_TB = True DISABLE_RAMP_METER = True AV_FRAC = .10 vehicles = Vehicles() vehicles.add( veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), sumo_car_following_params=SumoCarFollowingParams( speed_mode=9, ), sumo_lc_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * SCALING) vehicles.add( veh_id="human", routing_controller=(ContinuousRouter, {}), sumo_car_following_params=SumoCarFollowingParams( speed_mode=9, ), sumo_lc_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * SCALING)
DISABLE_TB = True DISABLE_RAMP_METER = True AV_FRAC = .1 PARALLEL_ROLLOUTS = 32 i = 0 sumo_params = SumoParams(sim_step=0.5, sumo_binary="sumo", restart_instance=True) vehicles = Vehicles() vehicles.add( veh_id="human", speed_mode=9, lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_mode=0, # 1621,#0b100000101, num_vehicles=1 * SCALING) vehicles.add(veh_id="followerstopper", acceleration_controller=(RLController, { "fail_safe": "instantaneous" }), lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), speed_mode=9, lane_change_mode=0, num_vehicles=1 * SCALING) horizon = 1000 # edge name, how many segments to observe/control, whether the segment is
from flow.controllers import IDMController, ContinuousRouter, RLController from flow.scenarios.figure_eight import ADDITIONAL_NET_PARAMS # time horizon of a single rollout HORIZON = 1500 # number of rollouts per training iteration N_ROLLOUTS = 20 # number of parallel workers N_CPUS = 2 # We place one autonomous vehicle and 13 human-driven vehicles in the network vehicles = Vehicles() vehicles.add( veh_id='human', acceleration_controller=(IDMController, { 'noise': 0.2 }), routing_controller=(ContinuousRouter, {}), speed_mode='no_collide', num_vehicles=13) vehicles.add( veh_id='rl', acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), 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
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(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 = 0 num_cars_right = 0 num_cars_top = 0 num_cars_bot = 0 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) """""" inflow = InFlows() inflow.add(veh_type="human", edge="bot1_0", probability=1, departLane="free", departSpeed=20) """ inflow.add( veh_type="human", edge="bot0_0", probability=0.25, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="top1_3", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="top0_3", probability=0.25, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="left2_0", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="left2_1", probability=0.25, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="left2_2", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="right0_0", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="right0_1", probability=0.25, departLane="free", departSpeed=20) """ inflow.add(veh_type="human", edge="right0_2", probability=1, departLane="free", departSpeed=20) additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(inflows=inflow, 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)
def figure_eight_baseline(num_runs, render=True): """Run script for all figure eight baselines. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over flow_params : dict the flow meta-parameters describing the structure of a benchmark. Must be one of the figure eight flow_params 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'] env_params = flow_params['env'] net_params = flow_params['net'] initial_config = flow_params.get('initial', InitialConfig()) traffic_lights = flow_params.get('tls', TrafficLights()) # modify the rendering to match what is requested sumo_params.render = render # set the evaluation flag to True env_params.evaluate = True # we want no autonomous vehicles in the simulation vehicles = Vehicles() vehicles.add(veh_id='human', acceleration_controller=(IDMController, { 'noise': 0.2 }), routing_controller=(ContinuousRouter, {}), speed_mode='no_collide', num_vehicles=14) # 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=traffic_lights) # 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) avg_speed = np.mean(results['mean_returns']) return avg_speed
def setup_bottlenecks(sumo_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, traffic_lights=None, inflow=None, scaling=1): if sumo_params is None: # set default sumo_params configuration sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo") if vehicles is None: vehicles = Vehicles() vehicles.add(veh_id="human", speed_mode=25, lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_mode=1621, num_vehicles=1 * scaling) if env_params is None: additional_env_params = { "target_velocity": 40, "max_accel": 1, "max_decel": 1, "lane_change_duration": 5, "add_rl_if_exit": False, "disable_tb": True, "disable_ramp_metering": True } env_params = EnvParams(additional_params=additional_env_params) if inflow is None: inflow = InFlows() inflow.add(veh_type="human", edge="1", vehsPerHour=1000, departLane="random", departSpeed=10) if traffic_lights is None: traffic_lights = TrafficLights() if net_params is None: additional_net_params = {"scaling": scaling} net_params = NetParams(in_flows=inflow, no_internal_links=False, additional_params=additional_net_params) if initial_config is None: 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) # create the environment env = AccelEnv(env_params=env_params, sumo_params=sumo_params, scenario=scenario) return env, scenario
def bay_bridge_example(render=None, use_inflows=False, use_traffic_lights=False): """ Perform a simulation of vehicles on the Oakland-San Francisco Bay Bridge. Parameters ---------- render: bool, optional specifies whether to use sumo's gui during execution use_inflows: bool, optional whether to activate inflows from the peripheries of the network use_traffic_lights: bool, optional whether to activate the traffic lights in the scenario Returns ------- exp: flow.core.SumoExperiment type A non-rl experiment demonstrating the performance of human-driven vehicles simulated by sumo on the Bay Bridge. """ sumo_params = SumoParams(sim_step=0.6, overtake_right=True) if render is not None: sumo_params.render = render sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2) sumo_lc_params = SumoLaneChangeParams( lc_assertive=20, lc_pushy=0.8, lc_speed_gain=4.0, model="LC2013", # lcKeepRight=0.8 ) 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=1400) additional_env_params = {} env_params = EnvParams(additional_params=additional_env_params) traffic_lights = TrafficLights() inflow = InFlows() if use_inflows: # south inflow.add(veh_type="human", edge="183343422", vehsPerHour=528, departLane="0", departSpeed=20) inflow.add(veh_type="human", edge="183343422", vehsPerHour=864, departLane="1", departSpeed=20) inflow.add(veh_type="human", edge="183343422", vehsPerHour=600, departLane="2", departSpeed=20) inflow.add(veh_type="human", edge="393649534", probability=0.1, departLane="0", departSpeed=20) # no data for this # west inflow.add(veh_type="human", edge="11189946", vehsPerHour=1752, departLane="0", departSpeed=20) inflow.add(veh_type="human", edge="11189946", vehsPerHour=2136, departLane="1", departSpeed=20) inflow.add(veh_type="human", edge="11189946", vehsPerHour=576, departLane="2", departSpeed=20) # north inflow.add(veh_type="human", edge="28413687#0", vehsPerHour=2880, departLane="0", departSpeed=20) inflow.add(veh_type="human", edge="28413687#0", vehsPerHour=2328, departLane="1", departSpeed=20) inflow.add(veh_type="human", edge="28413687#0", vehsPerHour=3060, departLane="2", departSpeed=20) inflow.add(veh_type="human", edge="11198593", probability=0.1, departLane="0", departSpeed=20) # no data for this inflow.add(veh_type="human", edge="11197889", probability=0.1, departLane="0", departSpeed=20) # no data for this # midway through bridge inflow.add(veh_type="human", edge="35536683", probability=0.1, departLane="0", departSpeed=20) # no data for this net_params = NetParams(inflows=inflow, no_internal_links=False) net_params.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", min_gap=15) scenario = BayBridgeScenario(name="bay_bridge", vehicles=vehicles, traffic_lights=traffic_lights, net_params=net_params, initial_config=initial_config) env = BayBridgeEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
def variable_lanes_exp_setup(sumo_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, traffic_lights=None): """ Creates an environment and scenario pair for a ring road network with different number of lanes in each edge. Used for test purposes. Parameters ---------- sumo_params: SumoParams type 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: EnvParams type environment-specific parameters, defaults to a environment with no failsafes, where other parameters do not matter for non-rl runs net_params: NetParams type network-specific configuration parameters, defaults to a figure eight with a 30 m radius and "no_internal_links" set to False initial_config: InitialConfig type specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network traffic_lights: TrafficLights type traffic light signals, defaults to no traffic lights in the network """ logging.basicConfig(level=logging.WARNING) if sumo_params is None: # set default sumo_params configuration sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo") if vehicles is None: # set default vehicles configuration vehicles = Vehicles() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), 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, "num_steps": 500 } 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 = TrafficLights() # create the scenario scenario = LoopScenario(name="VariableLaneRingRoadTest", generator_class=VariableLanesGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # create the environment env = AccelEnv(env_params=env_params, sumo_params=sumo_params, scenario=scenario) return env, scenario
def grid_mxn_exp_setup(row_num=1, col_num=1, sumo_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, tl_logic=None): """ Creates an environment and scenario pair for grid 1x1 test experiments. sumo-related configuration parameters, defaults to a time step of 1s and no sumo-imposed failsafe on human or rl vehicles Parameters ---------- row_num: int, optional number of horizontal rows of edges in the grid network col_num: int, optional number of vertical columns of edges in the grid network sumo_params: SumoParams type 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: EnvParams type environment-specific parameters, defaults to a environment with failsafes, where other parameters do not matter for non-rl runs net_params: NetParams type network-specific configuration parameters, defaults to a 1x1 grid which traffic lights on and "no_internal_links" set to False initial_config: InitialConfig type specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network tl_logic: TrafficLights type specifies logic of any traffic lights added to the system """ logging.basicConfig(level=logging.WARNING) if tl_logic is None: tl_logic = TrafficLights(baseline=False) if sumo_params is None: # set default sumo_params configuration sumo_params = SumoParams(sim_step=1, sumo_binary="sumo") if vehicles is None: total_vehicles = 20 vehicles = Vehicles() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), sumo_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, "num_steps": 100, "switch_time": 3.0 } 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 grid_array = { "short_length": 100, "inner_length": 300, "long_length": 3000, "row_num": row_num, "col_num": col_num, "cars_left": int(total_vehicles / 4), "cars_right": int(total_vehicles / 4), "cars_top": int(total_vehicles / 4), "cars_bot": int(total_vehicles / 4) } 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(no_internal_links=False, additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig(spacing="uniform", additional_params={"enter_speed": 30}) # create the scenario scenario = SimpleGridScenario(name="Grid1x1Test", generator_class=SimpleGridGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) # create the environment env = GreenWaveTestEnv(env_params=env_params, sumo_params=sumo_params, scenario=scenario) return env, scenario
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, lateral_resolution=0.2, ) if render is not None: sumo_params.render = render vehicles = Vehicles() vehicles.add( veh_id="1", acceleration_controller=( IDMController, { # "noise": 0.2 }), routing_controller=(WeaveRouter, {}), speed_mode="all_checks", lane_change_mode="strategic", num_vehicles=5) vehicles.add( veh_id="2", acceleration_controller=( IDMController, { # "noise": 0.2 }), routing_controller=(WeaveRouter, {}), speed_mode="all_checks", lane_change_mode="strategic", num_vehicles=5) vehicles.add( veh_id="3", acceleration_controller=( IDMController, { # "noise": 0.2 }), routing_controller=(WeaveRouter, {}), speed_mode="all_checks", lane_change_mode="strategic", num_vehicles=5) vehicles.add( veh_id="4", acceleration_controller=( IDMController, { # "noise": 0.2 }), routing_controller=(WeaveRouter, {}), speed_mode="all_checks", lane_change_mode="strategic", num_vehicles=5) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS, sims_per_step=5, warmup_steps=0) inflow = InFlows() inflow.add( veh_type="1", edge="inflow_highway", vehs_per_hour=FLOW_RATE * 0.6, # TODO: change departLane="free", departSpeed=10) inflow.add( veh_type="2", edge="inflow_highway", vehs_per_hour=FLOW_RATE * 0.4, # TODO: change departLane="free", departSpeed=10) inflow.add( veh_type="3", edge="inflow_merge", vehs_per_hour=FLOW_RATE * 0.3, # TODO: change departLane="free", departSpeed=7.5) inflow.add( veh_type="4", edge="inflow_merge", vehs_per_hour=FLOW_RATE * 0.1, # TODO: change departLane="free", departSpeed=7.5) additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["merge_lanes"] = 1 additional_net_params["diverge_lanes"] = 1 additional_net_params["highway_lanes"] = 2 additional_net_params["pre_merge_length"] = 300 additional_net_params["post_merge_length"] = 200 additional_net_params["post_diverge_length"] = 400 additional_net_params["merge_length"] = 100 additional_net_params["diverge_length"] = 200 net_params = NetParams(inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", perturbation=5.0) scenario = EntryExitScenario(name="merge-baseline", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
# length of final edge in route LONG_LENGTH = 100 # length of edges that vehicles start on SHORT_LENGTH = 300 # number of vehicles originating in the left, right, top, and bottom edges N_LEFT, N_RIGHT, N_TOP, N_BOTTOM = 1, 1, 1, 1 # 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, speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=(N_LEFT + N_RIGHT) * N_COLUMNS + (N_BOTTOM + N_TOP) * N_ROWS) # 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:
def run_task(*_): sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo", seed=0) vehicles = Vehicles() vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=21) additional_env_params = { "target_velocity": 8, "ring_length": [220, 270], "max_accel": 1, "max_decel": 1 } env_params = EnvParams(horizon=HORIZON, additional_params=additional_env_params, warmup_steps=750) additional_net_params = { "length": 260, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", bunching=50) print("XXX name", exp_tag) scenario = LoopScenario(exp_tag, CircleGenerator, vehicles, net_params, initial_config=initial_config) env_name = "WaveAttenuationPOEnv" 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) horizon = env.horizon env = normalize(env) policy = GaussianGRUPolicy( env_spec=env.spec, hidden_sizes=(5, ), ) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=3600 * 72 * 2, max_path_length=horizon, n_itr=5, # whole_paths=True, discount=0.999, # step_size=v["step_size"], ) algo.train(),
def run_task(*_): v_enter = 10 inner_length = 300 long_length = 100 short_length = 300 n = 3 m = 3 num_cars_left = 1 num_cars_right = 1 num_cars_top = 1 num_cars_bot = 1 tot_cars = (num_cars_left + num_cars_right) * m \ + (num_cars_bot + num_cars_top) * 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=1, sumo_binary="sumo-gui") vehicles = Vehicles() vehicles.add(veh_id="idm", acceleration_controller=(SumoCarFollowingController, {}), sumo_car_following_params=SumoCarFollowingParams( min_gap=2.5, tau=1.1, max_speed=v_enter), routing_controller=(GridRouter, {}), num_vehicles=tot_cars, speed_mode="all_checks") tl_logic = TrafficLights(baseline=False) additional_env_params = { "target_velocity": 50, "num_steps": 500, "switch_time": 3.0 } env_params = EnvParams(additional_params=additional_env_params) additional_net_params = { "speed_limit": 35, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1 } initial_config, net_params = get_flow_params(10, 300, n, m, additional_net_params) scenario = SimpleGridScenario(name="grid-intersection", generator_class=SimpleGridGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env_name = "PO_TrafficLightGridEnv" 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) horizon = env.horizon env = normalize(env) policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32)) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=40000, max_path_length=horizon, # whole_paths=True, n_itr=800, discount=0.999, # step_size=0.01, ) algo.train()
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 = Vehicles() 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 = {} initial_pos["start_positions"] = [('highway_0', 20), ('highway_0', 30), ('highway_0', 10)] initial_pos["start_lanes"] = [1, 2, 0] initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sumo_params=SumoParams( sim_step=0.1, sumo_binary="sumo"), 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.vehicles.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.vehicles.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.vehicles.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.vehicles.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.vehicles.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.vehicles.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 = Vehicles() 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 = {} 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), ] initial_pos["start_lanes"] = [0, 0, 0, 1, 1, 2, 2, 3, 3] initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sumo_params=SumoParams( sim_step=0.1, sumo_binary="sumo"), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() actual_lane_leaders = env.vehicles.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.vehicles.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.vehicles.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.vehicles.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.vehicles.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.vehicles.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 = Vehicles() 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 = {} initial_pos["start_positions"] = [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)] initial_pos["start_lanes"] = [1, 2, 0] initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sumo_params=SumoParams( sim_step=0.1, sumo_binary="sumo", render=True), 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.vehicles.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.vehicles.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.vehicles.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.vehicles.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.vehicles.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.vehicles.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 = Vehicles() 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 = {} initial_pos["start_positions"] = [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)] initial_pos["start_lanes"] = [0, 0, 0] initial_config.additional_params = initial_pos env, scenario = highway_exp_setup(sumo_params=SumoParams( sim_step=0.1, sumo_binary="sumo"), 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.vehicles.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", ""] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.vehicles.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.vehicles.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.vehicles.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.vehicles.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.vehicles.get_lane_followers_speed("test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed)
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 """ # 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", 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 setUp(self): # create the environment and scenario classes for a figure eight vehicles = Vehicles() vehicles.add(veh_id="test", num_vehicles=20) self.env, scenario = ring_road_exp_setup(vehicles=vehicles)
# number of parallel workers PARALLEL_ROLLOUTS = 2 # number of rollouts per training iteration N_ROLLOUTS = PARALLEL_ROLLOUTS * 4 SCALING = 1 NUM_LANES = 4 * SCALING # number of lanes in the widest highway DISABLE_TB = True DISABLE_RAMP_METER = True AV_FRAC = 0.10 vehicles = Vehicles() vehicles.add( veh_id="human", speed_mode="all_checks", lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_mode=0, num_vehicles=1 * SCALING) vehicles.add( veh_id="followerstopper", acceleration_controller=(RLController, {}), lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), speed_mode=9, 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)]
from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline from rllab.policies.gaussian_mlp_policy import GaussianMLPPolicy SCALING = 1 DISABLE_TB = True DISABLE_RAMP_METER = True FLOW_RATE = 1500 * SCALING # inflow rate sumo_params = SumoParams(sim_step=0.5, sumo_binary="sumo") vehicles = Vehicles() vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), speed_mode=0b11111, lane_change_mode=1621, num_vehicles=4 * SCALING, sumo_lc_params=SumoLaneChangeParams()) vehicles.add(veh_id="human", speed_mode=0b11111, lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_mode=512, num_vehicles=15 * SCALING) vehicles.add(veh_id="rl2", acceleration_controller=(RLController, {}), lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), speed_mode=0b11111, lane_change_mode=1621,
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.experiment.Experiment 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 }), sumo_car_following_params=SumoCarFollowingParams( 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 Experiment(env)
from flow.scenarios.bottleneck.gen import BottleneckGenerator import numpy as np # time horizon of a single rollout HORIZON = 1000 SCALING = 2 NUM_LANES = 4 * SCALING # number of lanes in the widest highway DISABLE_TB = True DISABLE_RAMP_METER = True AV_FRAC = .10 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,
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" sumo_params = SumoParams(sim_step=0.1, render=True) vehicles = Vehicles() vehicles.add( veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), speed_mode="no_collide", num_vehicles=1) vehicles.add( veh_id="idm", acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), speed_mode="no_collide", num_vehicles=13) additional_env_params = { "target_velocity": 20, "max_accel": 3, "max_decel": 3 } env_params = EnvParams( horizon=HORIZON, additional_params=additional_env_params) additional_net_params = { "radius_ring": 30, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams( no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform") print("XXX name", exp_tag) scenario = Figure8Scenario( exp_tag, vehicles, net_params, initial_config=initial_config) env_name = "AccelEnv" 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) horizon = env.horizon env = normalize(env) policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(16, 16)) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=15000, max_path_length=horizon, n_itr=500, # whole_paths=True, discount=0.999, # step_size=v["step_size"], ) algo.train(),
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams from flow.core.vehicles import Vehicles from flow.controllers import IDMController, ContinuousRouter, RLController from flow.scenarios.figure8.figure8_scenario import ADDITIONAL_NET_PARAMS # time horizon of a single rollout HORIZON = 1500 # We place 8 autonomous vehicle and 8 human-driven vehicles in the network vehicles = Vehicles() for i in range(7): vehicles.add(veh_id="human{}".format(i), acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), speed_mode="no_collide", num_vehicles=1) vehicles.add(veh_id="rl{}".format(i), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), speed_mode="no_collide", num_vehicles=1) flow_params = dict( # name of the experiment exp_tag="figure_eight_1", # name of the flow environment the experiment is running on env_name="AccelEnv",
# number of parallel workers N_CPUS = 2 RING_RADIUS = 100 NUM_MERGE_HUMANS = 9 NUM_MERGE_RL = 1 # note that the vehicles are added sequentially by the scenario, # so place the merging vehicles after the vehicles in the ring vehicles = Vehicles() # Inner ring vehicles vehicles.add(veh_id='human', acceleration_controller=(IDMController, { 'noise': 0.2 }), lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=6, sumo_car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5), sumo_lc_params=SumoLaneChangeParams()) # A single learning agent in the inner ring vehicles.add(veh_id='rl', acceleration_controller=(RLController, {}), lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), speed_mode='no_collide', num_vehicles=1, sumo_car_following_params=SumoCarFollowingParams(minGap=0.01, tau=0.5), sumo_lc_params=SumoLaneChangeParams()) # Outer ring vehicles
m = 3 num_cars_left = 30 num_cars_right = 30 num_cars_top = 30 num_cars_bot = 30 rl_veh = 0 tot_cars = (num_cars_left + num_cars_right) * m + (num_cars_bot + num_cars_top) * 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, 'rl_veh': rl_veh} additional_env_params = ADDITIONAL_ENV_PARAMS#{ 'target_velocity': 50, 'switch_time': 3.0, 'num_observed': 2, 'discrete': False, 'tl_type': 'controlled' } additional_net_params = { 'speed_limit': 35, 'grid_array': grid_array, 'horizontal_lanes': 1, 'vertical_lanes': 1} vehicles = Vehicles() vehicles.add( veh_id='idm', acceleration_controller=(SumoCarFollowingController, {}), sumo_car_following_params=SumoCarFollowingParams( minGap=2.5, max_speed=v_enter, ), routing_controller=(GridRouter, {}), num_vehicles=tot_cars, speed_mode='all_checks') additional_init_params = {'enter_speed': v_enter} initial_config = InitialConfig(additional_params=additional_init_params) net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) flow_params = dict( exp_tag='green_wave', env_name='FerociousTrafficLightGridEnv', scenario='SimpleGridScenario', sumo=SumoParams( sim_step=1, render=True, ), env=EnvParams( horizon=HORIZON, additional_params=additional_env_params,), net=net_params, veh=vehicles, initial=initial_config,) def setup_exps(): alg_run = 'PPO' agent_cls = get_agent_class(alg_run) config = agent_cls._default_config.copy() config['num_workers'] = N_CPUS config['train_batch_size'] = HORIZON * N_ROLLOUTS
def test_make_create_env(self): """Tests that the make_create_env methods generates an environment with the expected flow parameters.""" # use a flow_params dict derived from flow/benchmarks/figureeight0.py vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), speed_mode="no_collide", num_vehicles=13) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), speed_mode="no_collide", num_vehicles=1) flow_params = dict( exp_tag="figure_eight_0", env_name="AccelEnv", scenario="Figure8Scenario", sumo=SumoParams( sim_step=0.1, render=False, ), env=EnvParams( horizon=1500, additional_params={ "target_velocity": 20, "max_accel": 3, "max_decel": 3, }, ), net=NetParams( no_internal_links=False, additional_params={ "radius_ring": 30, "lanes": 1, "speed_limit": 30, "resolution": 40, }, ), veh=vehicles, initial=InitialConfig(), tls=TrafficLights(), ) # some random version number for testing v = 23434 # call make_create_env create_env, env_name = make_create_env(params=flow_params, version=v) # check that the name is correct self.assertEqual(env_name, '{}-v{}'.format(flow_params["env_name"], v)) # create the gym environment env = create_env() # Note that we expect the port number in sumo_params to change, and # that this feature is in fact needed to avoid race conditions flow_params["sumo"].port = env.env.sumo_params.port # check that each of the parameter match self.assertEqual(env.env.env_params.__dict__, flow_params["env"].__dict__) self.assertEqual(env.env.sumo_params.__dict__, flow_params["sumo"].__dict__) self.assertEqual(env.env.traffic_lights.__dict__, flow_params["tls"].__dict__) self.assertEqual(env.env.scenario.net_params.__dict__, flow_params["net"].__dict__) self.assertEqual(env.env.scenario.net_params.__dict__, flow_params["net"].__dict__) self.assertEqual(env.env.scenario.initial_config.__dict__, flow_params["initial"].__dict__) self.assertEqual(env.env.__class__.__name__, flow_params["env_name"]) self.assertEqual(env.env.scenario.__class__.__name__, flow_params["scenario"])
def get_flow_params(config): """Return 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.inflows = InFlows() if flow_params["net"]["inflows"]: net.inflows.__dict__ = flow_params["net"]["inflows"].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