Beispiel #1
0
    def test_sorting(self):
        """
        Tests that the sorting method returns a list of ids sorted by the
        absolute_position variable when sorting is requested, and does
        nothing if it is not requested.
        """
        env_params = self.env_params
        env_params.additional_params['sort_vehicles'] = True
        self.network.initial_config.shuffle = True

        env = AccelEnv(
            sim_params=self.sim_params,
            network=self.network,
            env_params=env_params
        )

        env.reset()
        env.additional_command()

        sorted_ids = env.sorted_ids
        positions = [env.absolute_position[veh_id] for veh_id in 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 test_observation_action_space(self):
        """Tests the observation and action spaces upon initialization."""
        env = AccelEnv(sim_params=self.sim_params,
                       scenario=self.scenario,
                       env_params=self.env_params)

        # check the observation space
        self.assertTrue(
            test_space(env.observation_space,
                       expected_size=2 * env.scenario.vehicles.num_vehicles,
                       expected_min=0,
                       expected_max=1))

        # check the action space
        self.assertTrue(
            test_space(
                env.action_space,
                expected_size=env.scenario.vehicles.num_rl_vehicles,
                expected_min=-abs(
                    env.env_params.additional_params["max_decel"]),
                expected_max=env.env_params.additional_params["max_accel"]))

        env.terminate()
    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
        env_params = self.env_params
        env_params.additional_params['sort_vehicles'] = False
        self.scenario.initial_config.shuffle = True

        env = AccelEnv(sim_params=self.sim_params,
                       scenario=self.scenario,
                       env_params=env_params)

        env.reset()
        env.additional_command()

        sorted_ids = list(env.sorted_ids)
        ids = env.k.vehicle.get_ids()

        # ensure that the list of ids did not change
        self.assertListEqual(sorted_ids, ids)