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)
), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, sims_per_step=2, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": NUM_RL, }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params=additional_net_params, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), )
def reset(self): """Reset the environment with a new inflow rate. The diverse set of inflows are used to generate a policy that is more robust with respect to the inflow rate. The inflow rate is update by creating a new scenario similar to the previous one, but with a new Inflow object with a rate within the additional environment parameter "inflow_range", which is a list consisting of the smallest and largest allowable inflow rates. **WARNING**: The inflows assume there are vehicles of type "followerstopper" and "human" within the VehicleParams object. """ add_params = self.env_params.additional_params if add_params.get("reset_inflow"): inflow_range = add_params.get("inflow_range") flow_rate = np.random.uniform(min(inflow_range), max(inflow_range)) * self.scaling # We try this for 100 trials in case unexpected errors during # instantiation. for _ in range(100): try: # introduce new inflows within the pre-defined inflow range inflow = InFlows() inflow.add( veh_type="followerstopper", # FIXME: make generic edge="1", vehs_per_hour=flow_rate * .1, departLane="random", departSpeed=10) inflow.add(veh_type="human", edge="1", vehs_per_hour=flow_rate * .9, departLane="random", departSpeed=10) # all other network parameters should match the previous # environment (we only want to change the inflow) additional_net_params = { "scaling": self.scaling, "speed_limit": self.net_params.additional_params['speed_limit'] } net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="human", # FIXME: make generic car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # 1621,#0b100000101, ), num_vehicles=1 * self.scaling) vehicles.add( veh_id="followerstopper", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * self.scaling) # recreate the scenario object self.scenario = self.scenario.__class__( name=self.scenario.orig_name, vehicles=vehicles, net_params=net_params, initial_config=self.initial_config, traffic_lights=self.scenario.traffic_lights) observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation except Exception as e: print('error on reset ', e) # perform the generic reset function observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation
# environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, sims_per_step=2, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": NUM_RL, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( in_flows=inflow, no_internal_links=False, additional_params=additional_net_params, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), )
additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": NUM_RL, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( template={ "net": os.path.join(os.path.dirname(os.path.abspath(__file__)), "us_merge.net.xml"), "rou": os.path.join(os.path.dirname(os.path.abspath(__file__)), "us_merge.rou.xml") }, inflows=inflow, no_internal_links=False, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), )
def stabilizing_triangle(highway_inflow): # experiment number # - 0: 10% RL penetration, 5 max controllable vehicles # - 1: 25% RL penetration, 13 max controllable vehicles # - 2: 33% RL penetration, 17 max controllable vehicles EXP_NUM = 0 # time horizon of a single rollout HORIZON = 600 # number of rollouts per training iteration N_ROLLOUTS = 20 # number of parallel workers N_CPUS = 2 # inflow rate at the highway FLOW_RATE = highway_inflow # percent of autonomous vehicles RL_PENETRATION = [0.1, 0.25, 0.33][EXP_NUM] # num_rl term (see ADDITIONAL_ENV_PARAMs) NUM_RL = [5, 13, 17][EXP_NUM] # We consider a highway network with an upstream merging lane producing # shockwaves # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), lane_change_params=SumoLaneChangeParams( lane_change_mode="strategic", ), num_vehicles=0) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), 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_2", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, departLane="random", departSpeed=10) inflow.add(veh_type="rl", edge="inflow_highway_2", vehs_per_hour=RL_PENETRATION * FLOW_RATE, departLane="random", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge_2", vehs_per_hour=500, departLane="random", departSpeed=7.5) flow_params = dict( # name of the experiment exp_tag="stabilizing_triangle_merge", # name of the flow environment the experiment is running on env_name="AccelEnv", # name of the scenario class the experiment is running on scenario="TriangleMergeScenario", # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( render=False, sim_step=0.2, emission_path= '/Users/apple/Desktop/Berkeley/Repo/Flow/triange-data/'), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams(additional_params=ADDITIONAL_ENV_PARAMS), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, # our inflows no_internal_links=False, additional_params=additional_net_params), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(spacing="random", perturbation=1), )
def highway_ramps_example(render=None): """ Perform a simulation of vehicles on a highway section with ramps. Parameters ---------- render: bool, optional Specifies whether or not to use the GUI during the simulation. Returns ------- exp: flow.core.experiment.Experiment A non-RL experiment demonstrating the performance of human-driven vehicles on a highway section with on and off ramps. """ sim_params = SumoParams(render=True, emission_path="./data/", sim_step=0.2, restart_instance=True) if render is not None: sim_params.render = render vehicles = VehicleParams() vehicles.add( veh_id="human", car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", # for safer behavior at the merges tau=1.5 # larger distance between cars ), lane_change_params=SumoLaneChangeParams(lane_change_mode=1621)) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS, sims_per_step=5, warmup_steps=0) inflows = InFlows() inflows.add(veh_type="human", edge="highway_0", vehs_per_hour=HIGHWAY_INFLOW_RATE, depart_lane="free", depart_speed="max", name="highway_flow") for i in range(len(additional_net_params["on_ramps_pos"])): inflows.add(veh_type="human", edge="on_ramp_{}".format(i), vehs_per_hour=ON_RAMPS_INFLOW_RATE, depart_lane="first", depart_speed="max", name="on_ramp_flow") net_params = NetParams(inflows=inflows, additional_params=additional_net_params) initial_config = InitialConfig() # no vehicles initially network = HighwayRampsNetwork(name="highway-ramp", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = AccelEnv(env_params, sim_params, network) return Experiment(env)
sim_step=0.1, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": 20, "max_accel": 3, "max_decel": 3, "sort_vehicles": False }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( no_internal_links=False, additional_params=deepcopy(ADDITIONAL_NET_PARAMS), ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), )
def getOmgeving(HORIZON): sim_params = SumoParams(render=True, sim_step=1, restart_instance=True, emission_path='result') # temp inflow edge_inflow = 300 # params for grid env inner_length = 300 long_length = 500 short_lengh = 500 rows = 1 columns = 1 num_cars_left = 1 num_cars_right = 1 num_cars_top = 1 num_cars_bottom = 1 enterSpeed = 30 tot_cars = (num_cars_left + num_cars_right) * columns + ( num_cars_top + num_cars_bottom) * rows grid_array = { "short_length": short_lengh, "inner_length": inner_length, "long_length": long_length, "row_num": rows, "col_num": columns, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bottom } # vehicles # add the starting vehicles vehicles = VehicleParams() vehicles.add("human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( speed_mode="right_of_way", min_gap=2.5, max_speed=enterSpeed, decel=7.5, ), routing_controller=(GridRouter, {}), num_vehicles=tot_cars) # inflow # outer_edges of the network (zie traffic_light_grid.py file) outer_edges = [] outer_edges += ["left{}_{}".format(rows, i) for i in range(columns)] outer_edges += ["right0_{}".format(i) for i in range(rows)] outer_edges += ["bot{}_0".format(i) for i in range(columns)] outer_edges += ["top{}_{}".format(i, columns) for i in range(rows)] inflow = InFlows() for edge in outer_edges: if edge == "left1_0": prob = 0.10 elif edge == "right0_0": prob = 0.10 elif edge == "bot0_0": prob = 0.10 elif edge == "top0_1": prob = 0.10 inflow.add( veh_type="human", edge=edge, #vehs_per_hour=edge_inflow, probability=prob, depart_lane="free", depart_speed=enterSpeed) # Net Params additional_net_params = { "speed_limit": enterSpeed + 5, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1, #"traffic_lights": True} } net_params = NetParams(inflows=inflow, additional_params=additional_net_params) # Env Params # => switch_time is de minimum tijd dat een licht in een bepaalde state zit # => num_observed aantal vehicles dat geobservered wordt vanuit elke richting van het kruispunt # => target_velocity is de snelheid dat elk voertuig moet proberen te behalen wanneer deze zich op het kruispunt bevindt additional_env_params = { "switch_time": 3.0, "tl_type": "controlled", # kan ook actuated/controlled zijn "discrete": True, "num_observed": 2, "target_velocity": 50 } env_params = EnvParams(horizon=HORIZON, additional_params=additional_env_params) # Initial config initial_config = InitialConfig(spacing='custom', shuffle=True) # Flow Params flow_param = dict( # name of the experiment exp_tag="RL_traffic_lights_one_by_one", # name of the flow environment the experiment is running on env_name=TrafficLightGridPOEnv, # name of the network class the experiment uses network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # simulation-related parameters sim=sim_params, # environment related parameters (see flow.core.params.EnvParams) env=env_params, # network-related parameters (see flow.core.params.NetParams and # the network's documentation or ADDITIONAL_NET_PARAMS component) net=net_params, # vehicles to be placed in the network at the start of a rollout # (see flow.core.vehicles.Vehicles) veh=vehicles, # (optional) parameters affecting the positioning of vehicles upon # initialization/reset (see flow.core.params.InitialConfig) initial=initial_config) return flow_param
def test_reset_inflows(self): """Tests that the inflow change within the expected range when calling reset.""" # set a random seed for inflows to be the same every time np.random.seed(seed=123) sim_params = SumoParams(sim_step=0.5, restart_instance=True) vehicles = VehicleParams() vehicles.add(veh_id="human") vehicles.add(veh_id="followerstopper") # edge name, how many segments to observe/control, whether the segment # is controlled controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True), ("4", 2, True), ("5", 1, False)] num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)] env_params = EnvParams( additional_params={ "target_velocity": 40, "disable_tb": True, "disable_ramp_metering": True, "controlled_segments": controlled_segments, "symmetric": False, "observed_segments": num_observed_segments, "reset_inflow": True, # this must be set to True for the test "lane_change_duration": 5, "max_accel": 3, "max_decel": 3, "inflow_range": [1000, 2000] # this is what we're testing }) inflow = InFlows() inflow.add( veh_type="human", edge="1", vehs_per_hour=1500, # the initial inflow we're checking for departLane="random", departSpeed=10) net_params = NetParams(inflows=inflow, no_internal_links=False, additional_params={ "scaling": 1, "speed_limit": 23 }) scenario = BottleneckScenario(name="bay_bridge_toll", vehicles=vehicles, net_params=net_params) env = DesiredVelocityEnv(env_params, sim_params, scenario) # reset the environment and get a new inflow rate env.reset() expected_inflow = 1343.178 # just from checking the new inflow # check that the first inflow rate is approximately 1500 for _ in range(500): env.step(rl_actions=None) self.assertAlmostEqual( env.k.vehicle.get_inflow_rate(250) / expected_inflow, 1, 1) # reset the environment and get a new inflow rate env.reset() expected_inflow = 1729.050 # just from checking the new inflow # check that the new inflow rate is approximately as expected for _ in range(500): env.step(rl_actions=None) self.assertAlmostEqual( env.k.vehicle.get_inflow_rate(250) / expected_inflow, 1, 1)
def test_no_junctions_highway(self): additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 1, "use_ghost_edge": False, "ghost_speed_limit": 25, } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_0', 20), ('highway_0', 30), ('highway_0', 10)], "start_lanes": [1, 2, 0]} initial_config.additional_params = initial_pos env, _, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in central lane # test_1 should be leading car in lane 2 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [1000, 1000, 5.0] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [5.0, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [0.0, 0.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Next, test the case where all vehicles are on the same # edge and there's two vehicles in each lane # Cases to test # 1. For lane 0, should find a leader and follower for tested car # 2. For lane 1, both vehicles are behind the test car # 3. For lane 2, both vehicles are in front of the tested car # 4. For lane 3, one vehicle in front and one behind the tested car additional_net_params = { "length": 100, "lanes": 4, "speed_limit": 30, "resolution": 40, "num_edges": 1, "use_ghost_edge": False, "ghost_speed_limit": 25, } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=9, initial_speed=1.0) initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_0', 50), ('highway_0', 60), ('highway_0', 40), ('highway_0', 40), ('highway_0', 30), ('highway_0', 60), ('highway_0', 70), ('highway_0', 60), ('highway_0', 40), ], "start_lanes": [0, 0, 0, 1, 1, 2, 2, 3, 3]} initial_config.additional_params = initial_pos env, _, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", "test_5", "test_7"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [5.0, 1000, 5.0, 5.0] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "test_3", "", "test_8"] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [5.0, 5.0, 1000, 5.0] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [1.0, 0.0, 1.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 1.0, 0.0, 1.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Now test if all the vehicles are on different edges and # different lanes additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 3, "use_ghost_edge": False, "ghost_speed_limit": 25, } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)], "start_lanes": [1, 2, 0]} initial_config.additional_params = initial_pos env, _, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in central lane # test_1 should be leading car in lane 2 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [1000, 1000, 22.996667] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [20.096667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [0.0, 0.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Now test if all the vehicles are on different edges and same # lanes additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 3, "use_ghost_edge": False, "ghost_speed_limit": 25, } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)], "start_lanes": [0, 0, 0]} initial_config.additional_params = initial_pos env, _, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in lane 0 # test_1 should be leading car in lane 0 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", ""] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [22.996667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [20.096667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [1.0, 0.0, 0.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed)
"num_local_lights": 4, }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params={ "speed_limit": 11, # inherited from grid0 benchmark "grid_array": { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": N_ROWS, "col_num": N_COLUMNS, "cars_left": N_LEFT, "cars_right": N_RIGHT, "cars_top": N_TOP, "cars_bot": N_BOTTOM, }, "horizontal_lanes": 1, "vertical_lanes": 1, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization
def test_convert_to_csv(self): dir_path = os.path.dirname(os.path.realpath(__file__)) sim_params = SumoParams(emission_path="{}/".format(dir_path)) vehicles = VehicleParams() vehicles.add( veh_id="idm", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), num_vehicles=1) additional_env_params = { "target_velocity": 8, "max_accel": 1, "max_decel": 1, "sort_vehicles": False, } env_params = EnvParams( horizon=10, additional_params=additional_env_params) additional_net_params = { "length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) flow_params = dict( exp_tag="RingRoadTest", env_name=AccelEnv, network=RingNetwork, simulator='traci', sim=sim_params, env=env_params, net=net_params, veh=vehicles, initial=InitialConfig(lanes_distribution=1), tls=TrafficLightParams(), ) exp = Experiment(flow_params) exp.run(num_runs=1, convert_to_csv=True) time.sleep(1.0) # check that both the csv file exists and the xml file doesn't. self.assertFalse(os.path.isfile(dir_path + "/{}-0_emission.xml".format( exp.env.network.name))) self.assertTrue(os.path.isfile(dir_path + "/{}-0_emission.csv".format( exp.env.network.name))) # check that the keys within the emission file matches its expected # values with open(dir_path + "/{}-0_emission.csv".format( exp.env.network.name), "r") as f: reader = csv.reader(f) header = next(reader) self.assertListEqual(header, [ "time", "id", "x", "y", "speed", "headway", "leader_id", "target_accel_with_noise_with_failsafe", "target_accel_no_noise_no_failsafe", "target_accel_with_noise_no_failsafe", "target_accel_no_noise_with_failsafe", "realized_accel", "road_grade", "edge_id", "lane_number", "distance", "relative_position", "follower_id", "leader_rel_speed", ]) time.sleep(0.1) # delete the files os.remove(os.path.expanduser(dir_path + "/{}-0_emission.csv".format( exp.env.network.name)))
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(),
# environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ 'target_velocity': 20, 'max_accel': 3, 'max_decel': 3, 'sort_vehicles': False }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( additional_params=ADDITIONAL_NET_PARAMS.copy(), ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), ) def setup_exps(): """Return the relevant components of an RLlib experiment.
return np.random.choice(adjacent_lanes, size=1)[0] vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=22, lane_change_controller=(CustomLaneChangeController, { "time_between": 25 })) sim_params = SumoParams(sim_step=0.1, render=True) initial_config = InitialConfig(bunching=40) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) net_params = NetParams(additional_params=ADDITIONAL_NET_PARAMS) flow_params = dict( exp_tag="custom_network", env_name=AccelEnv, network=CustomRingNetwork, simulator='traci', sim=sim_params, net=net_params, env=env_params, veh=vehicles, initial=initial_config, ) flow_params['env'].horizon = 1500 exp = Experiment(flow_params=flow_params)
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" sim_params = SumoParams(sim_step=0.1, render=False, seed=0) vehicles = VehicleParams() 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": None, "max_accel": 1, "max_decel": 1 } env_params = EnvParams(horizon=HORIZON, additional_params=additional_env_params, warmup_steps=1500) additional_net_params = { "length": 230, "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, vehicles, net_params, initial_config=initial_config) env_name = "WaveAttenuationPOEnv" simulator = 'aimsun' pass_params = (env_name, sim_params, vehicles, env_params, net_params, initial_config, scenario, simulator) 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=(3, 3), ) 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(),
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", generator="MergeGenerator", sumo=SumoParams( restart_instance=True, sim_step=0.5, sumo_binary="sumo", ), 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( in_flows=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')) # TODO(ak): deal with this hack imported_flow_params["initial"].positions = None imported_flow_params["initial"].lanes = None # test that this inflows are correct self.assertTrue(imported_flow_params["net"].in_flows.__dict__ == flow_params["net"].in_flows.__dict__) imported_flow_params["net"].in_flows = None flow_params["net"].in_flows = 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"]) self.assertTrue( imported_flow_params["generator"] == flow_params["generator"]) 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
def triangle_scenario_example(highway_inflow, middle_length, emission_dir, render_=False): # create an empty vehicles object vehicles = VehicleParams() # add some vehicles to this object of type "human" vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), lane_change_params=SumoLaneChangeParams( lane_change_mode="strategic", ), num_vehicles=0) inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway_2", vehs_per_hour=highway_inflow, departSpeed=10, departLane="random") inflow.add(veh_type="human", edge="inflow_merge_2", vehs_per_hour=500, departSpeed=10, departLane="random") additional_net_params = { # length of the merge edge "merge_length": 100, # length of the highway leading to the merge "pre_merge_length": 200, # length of the highway past the merge "post_merge_length": 100, # number of lanes in the merge "merge_lanes": 2, # number of lanes in the highway "highway_lanes": 5, # max speed limit of the network "speed_limit": 30, } # we choose to make the main highway slightly longer additional_net_params["pre_merge_length"] = middle_length net_params = NetParams( inflows=inflow, # our inflows no_internal_links=False, additional_params=additional_net_params) sumo_params = SumoParams(render=render_, sim_step=0.2, emission_path=emission_dir) # '/Users/apple/Desktop/Berkeley/Repo/Flow/triange-data/' env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) initial_config = InitialConfig(spacing="random", perturbation=1) scenario = TriangleMergeScenario(name="custom-triangle-merge-example", vehicles=vehicles, net_params=net_params, initial_config=initial_config, inflow_edge_len=middle_length) env = AccelEnv(env_params, sumo_params, scenario) return Experiment(env)
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", generator="Figure8Generator", sumo=SumoParams( sim_step=0.1, sumo_binary="sumo", ), 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 # TODO(ak): deal with this hack flow_params["initial"].positions = \ env.env.scenario.initial_config.positions flow_params["initial"].lanes = env.env.scenario.initial_config.lanes # 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"]) self.assertEqual(env.env.scenario.generator_class.__name__, flow_params["generator"])
def get_flow_params(exp_num=1, horizon=6000, simulator="traci", multiagent=False): """Return the flow-specific parameters of the merge network. This scenario consists of a single-lane highway network with an on-ramp used to generate periodic perturbations to sustain congested behavior. In order to model the effect of p% CAV penetration on the network, every 100/pth vehicle is replaced with an automated vehicle whose actions are sampled from an RL policy. This benchmark is adapted from the following article: Kreidieh, Abdul Rahman, Cathy Wu, and Alexandre M. Bayen. "Dissipating stop-and-go waves in closed and open networks via deep reinforcement learning." 2018 21st International Conference on Intelligent Transportation Systems (ITSC). IEEE, 2018. Parameters ---------- exp_num : int experiment number * 0: 10% RL penetration, 5 max controllable vehicles * 1: 25% RL penetration, 13 max controllable vehicles * 2: 33% RL penetration, 17 max controllable vehicles horizon : int time horizon of a single rollout simulator : str the simulator used, one of {'traci', 'aimsun'} multiagent : bool whether the automated vehicles are via a single-agent policy or a shared multi-agent policy with the actions of individual vehicles assigned by a separate policy call Returns ------- dict flow-related parameters, consisting of the following keys: * exp_tag: name of the experiment * env_name: environment class of the flow environment the experiment is running on. (note: must be in an importable module.) * network: network class the experiment uses. * simulator: simulator that is used by the experiment (e.g. aimsun) * sim: simulation-related parameters (see flow.core.params.SimParams) * env: environment related parameters (see flow.core.params.EnvParams) * net: network-related parameters (see flow.core.params.NetParams and the network's documentation or ADDITIONAL_NET_PARAMS component) * veh: vehicles to be placed in the network at the start of a rollout (see flow.core.params.VehicleParams) * initial (optional): parameters affecting the positioning of vehicles upon initialization/reset (see flow.core.params.InitialConfig) * tls (optional): traffic lights to be introduced to specific nodes (see flow.core.params.TrafficLightParams) Raises ------ AssertionError if the `exp_num` parameter is a value other than 0, 1, or 2 """ assert exp_num in [0, 1, 2], "exp_num must be 0, 1, or 2" # inflow rate at the highway flow_rate = 2000 # percent of autonomous vehicles rl_penetration = [0.1, 0.25, 0.33][exp_num] # num_rl term (see ADDITIONAL_ENV_PARAMs) num_rl = [5, 13, 17][exp_num] # We consider a highway network with an upstream merging lane producing # shockwaves 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 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), 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, depart_lane="free", depart_speed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=rl_penetration * flow_rate, depart_lane="free", depart_speed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, depart_lane="free", depart_speed=7.5) return dict( # name of the experiment exp_tag="merge", # name of the flow environment the experiment is running on env_name=MultiAgentMergePOEnv if multiagent else MergePOEnv, # name of the network class the experiment is running on network=MergeNetwork, # simulator that is used by the experiment simulator=simulator, # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=0.2, render=False, restart_instance=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=horizon, sims_per_step=5, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": num_rl, }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params=additional_net_params, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon init/reset # (see flow.core.params.InitialConfig) initial=InitialConfig(), )
def reset(self): add_params = self.env_params.additional_params if add_params.get("reset_inflow"): inflow_range = add_params.get("inflow_range") flow_rate = np.random.uniform(min(inflow_range), max(inflow_range)) * self.scaling for _ in range(100): try: inflow = InFlows() inflow.add(veh_type="followerstopper", edge="1", vehs_per_hour=flow_rate * .1, departLane="random", departSpeed=10) inflow.add(veh_type="human", edge="1", vehs_per_hour=flow_rate * .9, departLane="random", departSpeed=10) additional_net_params = { "scaling": self.scaling, "speed_limit": self.scenario.net_params. additional_params['speed_limit'] } net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="human", car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # 1621,#0b100000101, ), num_vehicles=1 * self.scaling) vehicles.add( veh_id="followerstopper", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * self.scaling) self.scenario = self.scenario.__class__( name=self.scenario.orig_name, vehicles=vehicles, net_params=net_params, initial_config=self.scenario.initial_config, traffic_lights=self.scenario.traffic_lights) observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation except Exception as e: print('error on reset ', e) # perform the generic reset function observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation
sumo=SumoParams( sim_step=0.1, sumo_binary="sumo", ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": 20, "max_accel": 3, "max_decel": 3, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( no_internal_links=False, additional_params=ADDITIONAL_NET_PARAMS, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), )
def test_convert_to_csv(self): dir_path = os.path.dirname(os.path.realpath(__file__)) sim_params = SumoParams(emission_path="{}/".format(dir_path)) vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), num_vehicles=1) additional_env_params = { "target_velocity": 8, "max_accel": 1, "max_decel": 1, "sort_vehicles": False, } env_params = EnvParams(horizon=10, additional_params=additional_env_params) additional_net_params = { "length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) flow_params = dict( exp_tag="RingRoadTest", env_name=AccelEnv, network=RingNetwork, simulator='traci', sim=sim_params, env=env_params, net=net_params, veh=vehicles, initial=InitialConfig(lanes_distribution=1), tls=TrafficLightParams(), ) exp = Experiment(flow_params) exp.run(num_runs=1, convert_to_csv=True) time.sleep(1.0) # check that both the csv file exists and the xml file doesn't. self.assertFalse( os.path.isfile(dir_path + "/{}-emission.xml".format(exp.env.network.name))) self.assertTrue( os.path.isfile(dir_path + "/{}-emission.csv".format(exp.env.network.name))) time.sleep(0.1) # delete the files os.remove( os.path.expanduser( dir_path + "/{}-emission.csv".format(exp.env.network.name)))
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 config['gamma'] = 0.999 # discount rate config['model'].update({'fcnet_hiddens': [32, 32]}) config['use_gae'] = True config['lambda'] = 0.97
"491266613", #after window "422314897#1", "489256509", "456874110", ], #"max_inflow":FLOW_RATE + 3*MERGE_RATE, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, #no_internal_links=False, additional_params=additional_net_params, template={ "net": scenario_road_data["net"], # see above "rou": scenario_road_data["rou"], # see above }), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig( # Distributing only at the beginning of routes scenario_road_data["edges_distribution"]), )
"tl_type": "actuated" }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, no_internal_links=False, additional_params={ "speed_limit": V_ENTER + 5, "grid_array": { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": N_ROWS, "col_num": N_COLUMNS, "cars_left": N_LEFT, "cars_right": N_RIGHT, "cars_top": N_TOP, "cars_bot": N_BOTTOM, }, "horizontal_lanes": 1, "vertical_lanes": 1, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/
def get_flow_params(config): """Return Flow experiment parameters, given an experiment result folder. Parameters ---------- config : dict < dict > or str May be one of two things: * If it is a dict, then it is the stored RLlib configuration dict. * If it is a string, then it is the path to a flow_params json file. Returns ------- dict flow-related parameters, consisting of the following keys: * exp_tag: name of the experiment * env_name: name of the flow environment the experiment is running on * network: name of the network class the experiment uses * simulator: simulator that is used by the experiment (e.g. aimsun) * sim: simulation-related parameters (see flow.core.params.SimParams) * env: environment related parameters (see flow.core.params.EnvParams) * net: network-related parameters (see flow.core.params.NetParams and the network's documentation or ADDITIONAL_NET_PARAMS component) * veh: vehicles to be placed in the network at the start of a rollout (see flow.core.params.VehicleParams) * initial: parameters affecting the positioning of vehicles upon initialization/reset (see flow.core.params.InitialConfig) * tls: traffic lights to be introduced to specific nodes (see flow.core.params.TrafficLightParams) """ # collect all data from the json file if type(config) == dict: flow_params = json.loads(config['env_config']['flow_params']) else: flow_params = json.load(open(config, 'r')) # reinitialize the vehicles class from stored data veh = VehicleParams() 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]) # TODO: make ambiguous car_following_params = SumoCarFollowingParams() car_following_params.__dict__ = veh_params["car_following_params"] # TODO: make ambiguous lane_change_params = SumoLaneChangeParams() lane_change_params.__dict__ = veh_params["lane_change_params"] del veh_params["car_following_params"], \ veh_params["lane_change_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, car_following_params=car_following_params, lane_change_params=lane_change_params, **veh_params) # convert all parameters from dict to their object form sim = SumoParams() # TODO: add check for simulation type sim.__dict__ = flow_params["sim"].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 = TrafficLightParams() if "tls" in flow_params: tls.__dict__ = flow_params["tls"].copy() flow_params["sim"] = sim flow_params["env"] = env flow_params["initial"] = initial flow_params["net"] = net flow_params["veh"] = veh flow_params["tls"] = tls return flow_params
def make_flow_params(n_rows, n_columns, edge_inflow): """ Generate the flow params for the experiment. Parameters ---------- n_rows : int number of rows in the traffic light grid n_columns : int number of columns in the traffic light grid edge_inflow : float Returns ------- dict flow_params object """ # we place a sufficient number of vehicles to ensure they confirm with the # total number specified above. We also use a "right_of_way" speed mode to # support traffic light compliance vehicles = VehicleParams() num_vehicles = (N_LEFT + N_RIGHT) * n_columns + (N_BOTTOM + N_TOP) * n_rows vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=num_vehicles) # inflows of vehicles are place on all outer edges (listed here) outer_edges = [] outer_edges += ["left{}_{}".format(n_rows, i) for i in range(n_columns)] outer_edges += ["right0_{}".format(i) for i in range(n_rows)] outer_edges += ["bot{}_0".format(i) for i in range(n_rows)] outer_edges += ["top{}_{}".format(i, n_columns) for i in range(n_rows)] # equal inflows for each edge (as dictate by the EDGE_INFLOW constant) inflow = InFlows() for edge in outer_edges: inflow.add(veh_type="human", edge=edge, vehs_per_hour=edge_inflow, departLane="free", departSpeed=V_ENTER) flow_params = dict( # name of the experiment exp_tag="grid_0_{}x{}_i{}_multiagent".format(n_rows, n_columns, edge_inflow), # name of the flow environment the experiment is running on env_name='MultiTrafficLightGridPOEnv', # name of the network class the experiment is running on network="TrafficLightGridNetwork", # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( restart_instance=True, sim_step=1, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": 50, "switch_time": 3, "num_observed": 2, "discrete": False, "tl_type": "actuated", "num_local_edges": 4, "num_local_lights": 4, }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params={ "speed_limit": V_ENTER + 5, # inherited from grid0 benchmark "grid_array": { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": n_rows, "col_num": n_columns, "cars_left": N_LEFT, "cars_right": N_RIGHT, "cars_top": N_TOP, "cars_bot": N_BOTTOM, }, "horizontal_lanes": 1, "vertical_lanes": 1, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization # or reset (see flow.core.params.InitialConfig) initial=InitialConfig( spacing='custom', shuffle=True, ), ) return flow_params
env=EnvParams( horizon=HORIZON, warmup_steps=750, clip_actions=False, additional_params={ "max_accel": 1, "max_decel": 1, "ring_length": [220, 270], }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(additional_params={ "length": 260, "lanes": 1, "speed_limit": 30, "resolution": 40, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(), ) def run_model(num_cpus=1, rollout_size=50, num_steps=50): """Run the model for num_steps if provided. The total rollout length is rollout_size."""