def test_ghost_edge(self): """Validate the functionality of the ghost edge feature.""" # =================================================================== # # Without a ghost edge # # =================================================================== # # create the network env, _, _ = highway_exp_setup( net_params=NetParams(additional_params={ "length": 1000, "lanes": 4, "speed_limit": 30, "num_edges": 1, "use_ghost_edge": False, "ghost_speed_limit": 25 }) ) env.reset() # check the network length self.assertEqual(env.k.network.length(), 1000) # check the edge list self.assertEqual(env.k.network.get_edge_list(), ["highway_0"]) # check the speed limits of the edges self.assertEqual(env.k.network.speed_limit("highway_0"), 30) # =================================================================== # # With a ghost edge # # =================================================================== # # create the network env, _, _ = highway_exp_setup( net_params=NetParams(additional_params={ "length": 1000, "lanes": 4, "speed_limit": 30, "num_edges": 1, "use_ghost_edge": True, "ghost_speed_limit": 25 }) ) env.reset() # check the network length self.assertEqual(env.k.network.length(), 1500.1) # check the edge list self.assertEqual(env.k.network.get_edge_list(), ["highway_0", "highway_end"]) # check the speed limits of the edges self.assertEqual(env.k.network.speed_limit("highway_0"), 30) self.assertEqual(env.k.network.speed_limit("highway_end"), 25)
def test_no_edge_behind(self): """ Tests that, when there are no edges behind, prev_edge() returns an empty list """ env, scenario = highway_exp_setup() prev_edge = scenario.prev_edge(env.scenario.get_edge_list()[0], 0) self.assertTrue(len(prev_edge) == 0)
def test_no_edge_ahead(self): """ Tests that, when there are no edges in front, next_edge() returns an empty list """ env, scenario = highway_exp_setup() next_edge = scenario.next_edge(env.scenario.get_edge_list()[0], 0) self.assertTrue(len(next_edge) == 0)
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, "boundary_cell_length": 300, } 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, "boundary_cell_length": 300, } 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, "boundary_cell_length": 300, } 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, "boundary_cell_length": 300, } 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)