Exemple #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_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)