def test_it_works(self): warmup_step = 5 # some value # start an environment with a number of simulations per step greater # than one env_params = EnvParams(warmup_steps=warmup_step, additional_params=ADDITIONAL_ENV_PARAMS) env, scenario = ring_road_exp_setup(env_params=env_params) # time before running a reset t1 = 0 # perform a reset env.reset() # time after a reset t2 = env.time_counter # ensure that the difference in time is equal to sims_per_step self.assertEqual(t2 - t1, warmup_step)
def test_desired_velocity(self): """Test the desired_velocity method.""" vehicles = VehicleParams() vehicles.add("test", num_vehicles=10) env_params = EnvParams( additional_params={ "target_velocity": np.sqrt(10), "max_accel": 1, "max_decel": 1, "sort_vehicles": False }) env, _ = ring_road_exp_setup(vehicles=vehicles, env_params=env_params) # check that the fail attribute leads to a zero return self.assertEqual(desired_velocity(env, fail=True), 0) # check the average speed upon reset self.assertEqual(desired_velocity(env, fail=False), 0) # check the average speed upon reset with a subset of edges self.assertEqual( desired_velocity(env, edge_list=["bottom"], fail=False), 0) # change the speed of one vehicle env.k.vehicle.test_set_speed("test_0", np.sqrt(10)) # check the new average speed self.assertAlmostEqual(desired_velocity(env, fail=False), 1 - np.sqrt(90) / 10) # check the new average speed for a subset of edges self.assertAlmostEqual( desired_velocity(env, edge_list=["bottom"], fail=False), 1 - np.sqrt(20) / np.sqrt(30)) # change the speed of one of the vehicles outside the edge list env.k.vehicle.test_set_speed("test_8", 10) # check that the new average speed is the same as before self.assertAlmostEqual( desired_velocity(env, edge_list=["bottom"], fail=False), 1 - np.sqrt(20) / np.sqrt(30))
def test_edge_length_edges(self): """ Tests the edge_length() method when called on edges """ additional_net_params = { "length": 1000, "lanes": 2, "speed_limit": 60, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) # create the environment and network classes for a ring road env, _ = ring_road_exp_setup(net_params=net_params) self.assertEqual(env.k.network.edge_length("top"), 250) # test for errors as well self.assertAlmostEqual(env.k.network.edge_length("wrong_name"), -1001)
def test_num_lanes_edges(self): """ Tests the num_lanes() method when called on edges """ additional_net_params = { "length": 230, "lanes": 2, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) # create the environment and scenario classes for a figure eight env, scenario = ring_road_exp_setup(net_params=net_params) self.assertEqual(env.k.scenario.num_lanes("top"), 2) # test for errors as well self.assertAlmostEqual(env.k.scenario.num_lanes("wrong_name"), -1001)
def test_it_works(self): sims_per_step = 5 # some value # start an environment with a number of simulations per step greater # than one env_params = EnvParams(sims_per_step=sims_per_step, additional_params=ADDITIONAL_ENV_PARAMS) env, _, _ = ring_road_exp_setup(env_params=env_params) env.reset() # time before running a step t1 = env.time_counter # perform a step env.step(rl_actions=[]) # time after a step t2 = env.time_counter # ensure that the difference in time is equal to sims_per_step self.assertEqual(t2 - t1, sims_per_step)
def test_no_junctions(self): """ Tests the above mentioned methods in the absence of junctions. """ # setup a network with no junctions and several vehicles # also, setup with a deterministic starting position to ensure that the # headways/lane leaders are what is expected additional_net_params = {"length": 230, "lanes": 3, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) vehicles = Vehicles() vehicles.add(veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=21) initial_config = InitialConfig(lanes_distribution=float("inf")) env, scenario = ring_road_exp_setup(net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # check the lane leaders method is outputting the right values actual_lane_leaders = env.vehicles.get_lane_leaders("test_0") expected_lane_leaders = ["test_3", "test_1", "test_2"] self.assertCountEqual(actual_lane_leaders, expected_lane_leaders) # check the lane headways is outputting the right values actual_lane_head = env.vehicles.get_lane_headways("test_0") expected_lane_head = [27.85714285714286, -5, -5] self.assertCountEqual(actual_lane_head, expected_lane_head) # check the lane followers method is outputting the right values actual_lane_followers = env.vehicles.get_lane_followers("test_0") expected_lane_followers = ["test_18", "test_19", "test_20"] self.assertCountEqual(actual_lane_followers, expected_lane_followers) # check the lane tailways is outputting the right values actual_lane_tail = env.vehicles.get_lane_tailways("test_0") expected_lane_tail = [27.85714285714286] * 3 np.testing.assert_array_almost_equal(actual_lane_tail, expected_lane_tail)
def test_all(self): vehicles = VehicleParams() vehicles.add("human", num_vehicles=10) # add an RL vehicle to ensure that its color will be distinct vehicles.add("rl", acceleration_controller=(RLController, {}), num_vehicles=1) env, scenario = ring_road_exp_setup(vehicles=vehicles) # set one vehicle as observed env.vehicles.set_observed("human_0") # update the colors of all vehicles env.update_vehicle_colors() env.k.simulation.simulation_step() # check that, when rendering is off, the colors don't change (this # avoids unnecessary API calls) for veh_id in env.vehicles.get_ids(): self.assertEqual(env.traci_connection.vehicle.getColor(veh_id), YELLOW) # a little hack to ensure the colors change env.sim_params.render = True # set one vehicle as observed env.vehicles.set_observed("human_0") # update the colors of all vehicles env.update_vehicle_colors() env.k.simulation.simulation_step() # check the colors of all vehicles for veh_id in env.vehicles.get_ids(): if veh_id == "human_0": self.assertEqual(env.traci_connection.vehicle.getColor(veh_id), CYAN) elif veh_id == "rl_0": self.assertEqual(env.traci_connection.vehicle.getColor(veh_id), RED) else: self.assertEqual(env.traci_connection.vehicle.getColor(veh_id), WHITE)
def setUp(self): # add a few vehicles to the network using the requested model # also make sure that the input params are what is expected contr_params = {"v0": 30, "b": 1.5, "delta": 4, "s0": 2, "s1": 0, "noise": 0} vehicles = Vehicles() vehicles.add( veh_id="test", acceleration_controller=(IDMController, contr_params), routing_controller=(ContinuousRouter, {}), sumo_car_following_params=SumoCarFollowingParams( tau=1, accel=1, decel=5), num_vehicles=5 ) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(vehicles=vehicles)
def setUp_failsafe(self, vehicles): additional_env_params = {"target_velocity": 8, "max_accel": 3, "max_decel": 3} env_params = EnvParams(additional_params=additional_env_params) additional_net_params = {"length": 100, "lanes": 1, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) initial_config = InitialConfig(bunching=10) # create the environment and scenario classes for a ring road env, scenario = ring_road_exp_setup(vehicles=vehicles, env_params=env_params, net_params=net_params, initial_config=initial_config) # instantiate an experiment class self.exp = SumoExperiment(env, scenario)
def setUp(self): # add a few vehicles to the network using the requested model # also make sure that the input params are what is expected contr_params = \ {"time_delay": 0, "k_d": 1, "k_v": 1, "k_c": 1, "d_des": 1, "v_des": 8, "noise": 0} vehicles = Vehicles() vehicles.add( veh_id="test", acceleration_controller=(BCMController, contr_params), routing_controller=(ContinuousRouter, {}), sumo_car_following_params=SumoCarFollowingParams( accel=15, decel=5), num_vehicles=5 ) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(vehicles=vehicles)
def test_add_vehicles_rl(self): """ Ensure that added rl vehicles are placed in the current vehicle IDs, and that the number of vehicles is correct. """ vehicles = VehicleParams() vehicles.add( "test_rl", num_vehicles=10, acceleration_controller=(RLController, {})) env, _ = ring_road_exp_setup(vehicles=vehicles) self.assertEqual(env.k.vehicle.num_vehicles, 10) self.assertEqual(len(env.k.vehicle.get_ids()), 10) self.assertEqual(len(env.k.vehicle.get_rl_ids()), 10) self.assertEqual(len(env.k.vehicle.get_human_ids()), 0) self.assertEqual(len(env.k.vehicle.get_controlled_ids()), 0) self.assertEqual(len(env.k.vehicle.get_controlled_lc_ids()), 0)
def setUp_gen_start_pos(self, initial_config=InitialConfig()): # ensures that the random starting position method is being used initial_config.spacing = "random" # create a multi-lane ring road network additional_net_params = {"length": 230, "lanes": 4, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) # place 5 vehicles in the network (we need at least more than 1) vehicles = Vehicles() vehicles.add(veh_id="test", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=5) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, initial_config=initial_config, vehicles=vehicles)
def setUp(self): # turn on vehicle arrangement shuffle env_params = EnvParams( additional_params=ADDITIONAL_ENV_PARAMS) # place 5 vehicles in the network (we need at least more than 1) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=5) initial_config = InitialConfig(x0=5, shuffle=True) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup( env_params=env_params, initial_config=initial_config, vehicles=vehicles)
def test_no_sorting(self): # setup a environment with the "sort_vehicles" attribute set to False, # and shuffling so that the vehicles are not sorted by their ids additional_env_params = ADDITIONAL_ENV_PARAMS env_params = EnvParams(additional_params=additional_env_params, sort_vehicles=True) initial_config = InitialConfig(shuffle=True) vehicles = Vehicles() vehicles.add(veh_id="test", num_vehicles=5) self.env, scenario = ring_road_exp_setup(env_params=env_params, initial_config=initial_config, vehicles=vehicles) self.env.reset() sorted_ids = list(self.env.sorted_ids) ids = self.env.vehicles.get_ids() # ensure that the list of ids did not change self.assertListEqual(sorted_ids, ids)
def test_convert_to_csv(self): dir_path = os.path.dirname(os.path.realpath(__file__)) sim_params = SumoParams(emission_path="{}/".format(dir_path)) env, network = ring_road_exp_setup(sim_params=sim_params) exp = Experiment(env) exp.run(num_runs=1, num_steps=10, convert_to_csv=True) time.sleep(0.1) # check that both the csv file exists and the xml file doesn't. self.assertFalse(os.path.isfile(dir_path + "/{}-emission.xml".format( network.name))) self.assertTrue(os.path.isfile(dir_path + "/{}-emission.csv".format( network.name))) time.sleep(0.1) # delete the files os.remove(os.path.expanduser(dir_path + "/{}-emission.csv".format( network.name)))
def test_multi_lane(self): # add a traffic light to the top node traffic_lights = TrafficLights() traffic_lights.add("top") # create a ring road with two lanes additional_net_params = {"length": 230, "lanes": 2, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, traffic_lights=traffic_lights) self.env.reset() self.env.step([]) state = self.env.traffic_lights.get_state("top") self.assertEqual(state, "GG")
def test_sorting(self): # setup a environment with the "sort_vehicles" attribute set to True additional_env_params = ADDITIONAL_ENV_PARAMS env_params = EnvParams(additional_params=additional_env_params, sort_vehicles=True) initial_config = InitialConfig(shuffle=True) vehicles = Vehicles() vehicles.add(veh_id="test", num_vehicles=5) self.env, scenario = ring_road_exp_setup(env_params=env_params, initial_config=initial_config, vehicles=vehicles) self.env.reset() sorted_ids = self.env.sorted_ids positions = self.env.vehicles.get_absolute_position(sorted_ids) # ensure vehicles ids are in sorted order by positions self.assertTrue( all(positions[i] <= positions[i + 1] for i in range(len(positions) - 1)))
def setUp(self): # add a few vehicles to the network using the requested model # also make sure that the input params are what is expected contr_params = { "k_1": 0.3, "k_2": 0.4, "h": 1, "tau": 0.1, "noise": 0 } vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(LACController, contr_params), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( accel=15, decel=5), num_vehicles=5) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(vehicles=vehicles)
def setUp_gen_start_pos(self, initial_config=InitialConfig()): """ Replace with any scenario you would like to test gen_even_start_pos on. In ordering for all the tests to be meaningful, the scenario must contain MORE THAN TWO LANES. """ # create a multi-lane ring road network additional_net_params = {"length": 230, "lanes": 4, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) # place 5 vehicles in the network (we need at least more than 1) vehicles = Vehicles() vehicles.add(veh_id="test", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=15) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, initial_config=initial_config, vehicles=vehicles)
def test_all(self): vehicles = VehicleParams() vehicles.add("human", num_vehicles=10) # add an RL vehicle to ensure that its color will be distinct vehicles.add("rl", acceleration_controller=(RLController, {}), num_vehicles=1) _, network, _ = ring_road_exp_setup(vehicles=vehicles) env = TestEnv(EnvParams(), SumoParams(), network) env.reset() # set one vehicle as observed env.k.vehicle.set_observed("human_0") # update the colors of all vehicles env.step(rl_actions=None) # check that, when rendering is off, the colors don't change (this # avoids unnecessary API calls) for veh_id in env.k.vehicle.get_ids(): self.assertEqual(env.k.vehicle.get_color(veh_id), YELLOW) # a little hack to ensure the colors change env.sim_params.render = True # set one vehicle as observed env.k.vehicle.set_observed("human_0") # update the colors of all vehicles env.step(rl_actions=None) # check the colors of all vehicles for veh_id in env.k.vehicle.get_ids(): if veh_id in ["human_0"]: self.assertEqual(env.k.vehicle.get_color(veh_id), CYAN) elif veh_id == "rl_0": self.assertEqual(env.k.vehicle.get_color(veh_id), RED) else: self.assertEqual(env.k.vehicle.get_color(veh_id), WHITE)
def setUp(self): # add a traffic light to the top node traffic_lights = TrafficLights() # Phase durations in seconds self.green = 4 self.yellow = 1 self.red = 4 phases = [{"duration": repr(self.green), "state": "G"}, {"duration": repr(self.yellow), "state": "y"}, {"duration": repr(self.red), "state": "r"}] traffic_lights.add("top", phases=phases) # create a ring road with two lanes additional_net_params = {"length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40} net_params = NetParams(additional_params=additional_net_params) # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(net_params=net_params, traffic_lights=traffic_lights)
def test_rl_actions(self): def rl_actions(*_): return [1] # actions are always an acceleration of 1 for one veh # create an environment using AccelEnv with 1 RL vehicle vehicles = VehicleParams() vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), num_vehicles=1) env, scenario = ring_road_exp_setup(vehicles=vehicles) exp = Experiment(env=env) exp.run(1, 10, rl_actions=rl_actions) # check that the acceleration of the RL vehicle was that specified by # the rl_actions method self.assertAlmostEqual(exp.env.vehicles.get_speed("rl_0"), 1, places=1)
def test_convert_to_csv(self): dir_path = os.path.dirname(os.path.realpath(__file__)) sumo_params = SumoParams(emission_path="{}/".format(dir_path)) env, scenario = ring_road_exp_setup(sumo_params=sumo_params) exp = SumoExperiment(env, scenario) exp.run(num_runs=1, num_steps=10, convert_to_csv=True) time.sleep(0.1) # check that both the emission xml and csv files exist self.assertTrue(os.path.isfile(dir_path + "/{}-emission.xml".format( scenario.name))) self.assertTrue(os.path.isfile(dir_path + "/{}-emission.csv".format( scenario.name))) time.sleep(0.1) # delete the files os.remove(os.path.expanduser(dir_path + "/{}-emission.xml".format( scenario.name))) os.remove(os.path.expanduser(dir_path + "/{}-emission.csv".format( scenario.name)))
def setUp(self): # add a few vehicles to the network using the requested model # also make sure that the input params are what is expected contr_params = { "alpha": .5, "beta": 20, "h_st": 2, "h_go": 10, "v_max": 32, "want_max_accel": False, } vehicles = VehicleParams() vehicles.add(veh_id="test", acceleration_controller=(BandoFTLController, contr_params), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(accel=15, decel=5), num_vehicles=5) # create the environment and network classes for a ring road self.env, _, _ = ring_road_exp_setup(vehicles=vehicles)
def test_penalize_near_standstill(self): """Test the penalize_near_standstill method.""" vehicles = Vehicles() vehicles.add("test", num_vehicles=10) env_params = EnvParams(additional_params={ "target_velocity": 10, "max_accel": 1, "max_decel": 1 }) env, scenario = ring_road_exp_setup(vehicles=vehicles, env_params=env_params) # check the penalty is acknowledging all vehicles self.assertEqual(penalize_near_standstill(env, gain=1), -10) self.assertEqual(penalize_near_standstill(env, gain=2), -20) # change the speed of one vehicle env.vehicles.test_set_speed("test_0", 1) # check the penalty with good and bad thresholds self.assertEqual(penalize_near_standstill(env, thresh=2), -10) self.assertEqual(penalize_near_standstill(env, thresh=0.5), -9)
def setUp(self): # add a few vehicles to the network using the requested model # also make sure that the input params are what is expected contr_params = { "v0": 30, "acc": 1.5, "b": -1, "b_l": -1, "s0": 2, "tau": 1, "delay": 0, "noise": 0, } vehicles = VehicleParams() vehicles.add(veh_id="test", acceleration_controller=(GippsController, contr_params), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(accel=15, decel=5), num_vehicles=5) # create the environment and network classes for a ring road self.env, _, _ = ring_road_exp_setup(vehicles=vehicles)
def setUp(self): # create the environment and scenario classes for a ring road env, scenario = ring_road_exp_setup() # instantiate an experiment class self.exp = SumoExperiment(env, scenario)
def setUp(self): # create the environment and network classes for a ring road env, _ = ring_road_exp_setup() # instantiate an experiment class self.exp = Experiment(env)
def setUp(self): # create the environment and network classes for a figure eight vehicles = VehicleParams() vehicles.add(veh_id="test", num_vehicles=20) self.env, _, _ = ring_road_exp_setup(vehicles=vehicles)
def setUp(self): # set sumo_params to default sumo_params = SumoParams() # create the environment and scenario classes for a ring road self.env, scenario = ring_road_exp_setup(sumo_params=sumo_params)