def test_output_shape_as_expected_with_gfootball_env_reset(self):
        obs = self._gfootball_env.reset()
        # Act
        raw_obs = RawObs.convert_observation(obs)

        # Assert
        self.assertEqual(RawObs().shape, raw_obs.shape)
    def test_with_obs_indexed_out_of_list(self):
        # Arrange
        ro = RawObs().set_obs(self._raw_obs_fixture.data[0])

        # Act
        raw_obs = ro.process()

        # Assert
        self.assertEqual(ro.shape, raw_obs.shape)
    def test_output_shape_as_expected_with_kaggle_env_reset(self):
        # Arrange
        obs = self._kaggle_env.reset()

        # Act
        raw_obs = RawObs.convert_observation(obs)

        # Assert
        self.assertEqual(RawObs().shape, raw_obs.shape)
    def test_process_returns_none_if_nothing_to_do(self):
        # Arrange
        ro = RawObs(using=[]).set_obs(self._raw_obs_fixture.data[0])

        # Act
        raw_obs = ro.process()

        # Assert
        self.assertEqual((1, 0), ro.shape)
        self.assertIsNone(raw_obs)
    def test_process_defaults_shape_as_expected_with_gfootball_env(self):
        # Arrange
        raw_obs = self._gfootball_env.reset()

        # Act
        processed_obs = RawObs(
            using=RawObs.standard_keys).set_obs(raw_obs).process()

        self.assertEqual(
            RawObs(using=RawObs.standard_keys).shape, processed_obs.shape)
Exemplo n.º 6
0
    def process_obs(obs: Union[Dict[str, Any], List[Any]],
                    using: List[str] = None) -> np.ndarray:
        """Generate array with simple obs and raw obs."""

        if isinstance(obs, dict):
            obs = obs['players_raw']

        simple_obs = Simple115StateWrapper.convert_observation(
            obs, fixed_positions=False).reshape(-1)
        raw_obs = RawObs(using=using).set_obs(obs[0]).process()

        return np.concatenate([simple_obs, raw_obs.reshape(-1)
                               ]) if raw_obs is not None else simple_obs
    def test_process_all_shape_as_expected_with_gfootball_env(self):
        # Arrange
        raw_obs = self._gfootball_env.reset()

        # Act
        processed_obs = self._sut.set_obs(raw_obs).process()

        self.assertEqual(RawObs().shape, processed_obs.shape)
Exemplo n.º 8
0
    def test_shapes_as_expected_without_env_on_reset(self):
        # Arrange
        raw_obs = self._env.reset()

        # Act
        processed_obs = self._sut.process_obs(raw_obs)

        # Assert
        self.assertIsInstance(processed_obs, np.ndarray)
        self.assertEqual((RawObs().shape[1] + 115, ), processed_obs.shape)
Exemplo n.º 9
0
    def test_shapes_as_expected_with_kaggle_env(self):
        # Arrange
        wrapped_env = self._sut(gym.make("GFootball-kaggle_11_vs_11-v0"))

        # Act
        obs = wrapped_env.reset()

        # Assert
        self.assertIsInstance(obs, np.ndarray)
        self.assertEqual(wrapped_env.observation_space.shape, obs.shape)
        self.assertEqual((RawObs().shape[1] + 115, ), obs.shape)
Exemplo n.º 10
0
    def test_shapes_as_expected_with_env_on_reset(self):
        # Arrange
        wrapped_env = self._sut(self._env)

        # Act
        obs = wrapped_env.reset()

        # Assert
        self.assertIsInstance(obs, np.ndarray)
        self.assertEqual(wrapped_env.observation_space.shape, obs.shape)
        self.assertEqual((RawObs().shape[1] + 115, ), obs.shape)
Exemplo n.º 11
0
    def test_shapes_as_expected_without_env_on_step(self):
        # Arrange
        _ = self._env.reset()
        obs, reward, done, info = self._env.step(0)

        # Act
        obs = self._sut.process_obs(obs)

        # Assert
        self.assertIsInstance(obs, np.ndarray)
        self.assertEqual((RawObs().shape[1] + 115, ), obs.shape)
Exemplo n.º 12
0
    def process_obs(obs: Union[Dict[str, Any], List[Any]]) -> np.ndarray:

        if isinstance(obs, dict):
            # From kaggle env
            obs_for_s115 = obs['players_raw']
            obs_for_raw = obs['players_raw']

        elif isinstance(obs, list):
            # From gfootball Env
            obs_for_s115 = obs
            obs_for_raw = obs[0]

        else:
            raise ValueError("Something unexpected about obs")

        simple_obs = Simple115StateWrapper.convert_observation(
            obs_for_s115, fixed_positions=False).reshape(-1)
        raw_obs = RawObs.convert_observation(obs_for_raw)

        return np.concatenate([simple_obs, raw_obs.squeeze()])
Exemplo n.º 13
0
    def __init__(self,
                 env: gym.Env = None,
                 raw_using: List[str] = None,
                 raw_dump_path: str = None) -> None:
        """
        :param env: A gym env, or None.
        :param raw_using: List of keys to use in raw observations.
        """
        if env is not None:
            super().__init__(env)

        self.raw_dump_path = raw_dump_path
        self.raw_obs = RawObs(using=raw_using)

        self.simple_obs_shape = 115
        self.raw_obs_shape = self.raw_obs.shape[1]

        self.observation_space = gym.spaces.Box(low=-np.inf,
                                                high=np.inf,
                                                shape=(self.simple_obs_shape +
                                                       self.raw_obs_shape, ),
                                                dtype=np.float32)
 def setUp(self):
     self._raw_obs_fixture = RawObsFixture()
     self.sut = RawObs(using=RawObs.standard_keys)
     self.sut.set_obs(self._raw_obs_fixture.data)
class TestRawObs(unittest.TestCase):
    """
    Note that this test tests with environments, rather than the fixture. The fixture matches the output passed to
    agent when evaluated with env.make() and env.run() using kaggle_environments.make. This differs slightly to the raw
    observations returned by the envs (gym.make or gfootball.make), which contains arrays in place of lists for
    some fields.

    The processing in Raw obs must work with both, so can't do things like [] + [] as if one of those is an array,
    something different happens....
    """
    @classmethod
    def setUpClass(cls) -> None:
        register_all()

    def setUp(self):
        self._kaggle_env = gym.make("GFootball-kaggle_11_vs_11-v0")
        self._gfootball_env = self._env = FootballEnv(config=Config())
        self._sut = RawObs()

    def test_process_active_output_shapes_as_expected_with_kaggle_env(self):
        # Arrange
        ro = self._sut.set_obs(self._kaggle_env.reset())

        # Act
        active = ro.process_key('active')

        # Assert
        self.assertEqual((1, RawObs.active_n), active.shape)

    def test_process_active_output_shapes_as_expected_with_gf_env(self):
        # Arrange
        ro = self._sut.set_obs(self._gfootball_env.reset())

        # Act
        active = ro.process_key('active')

        # Assert
        self.assertEqual((1, RawObs.active_n), active.shape)

    def test_process_ball_output_shapes_as_expected_with_kaggle_env(self):
        # Arrange
        ro = self._sut.set_obs(self._kaggle_env.reset())

        # Act
        active = ro.process_key('ball')

        # Assert
        self.assertEqual((1, RawObs.ball_n), active.shape)

    def test_process_ball_info_output_shapes_as_expected_with_gf_env(self):
        # Arrange
        ro = self._sut.set_obs(self._gfootball_env.reset())

        # Act
        active = ro.process_key('ball')

        # Assert
        self.assertEqual((1, RawObs.ball_n), active.shape)

    def test_process_tired_factor_output_shapes_as_expected_with_kaggle_env(
            self):
        # Arrange
        ro = self._sut.set_obs(self._kaggle_env.reset())

        # Act
        active = ro.process_key('left_team_tired_factor')

        # Assert
        self.assertEqual((1, RawObs.left_team_tired_factor_n), active.shape)

    def test_process_tired_factor_output_shapes_as_expected_with_gf_env(self):
        # Arrange
        ro = self._sut.set_obs(self._gfootball_env.reset())

        # Act
        active = ro.process_key('right_team_tired_factor')

        # Assert
        self.assertEqual((1, RawObs.right_team_tired_factor_n), active.shape)

    def test_output_shape_as_expected_with_kaggle_env_reset(self):
        # Arrange
        obs = self._kaggle_env.reset()

        # Act
        raw_obs = RawObs.convert_observation(obs)

        # Assert
        self.assertEqual(RawObs().shape, raw_obs.shape)

    def test_output_shape_as_expected_with_gfootball_env_reset(self):
        obs = self._gfootball_env.reset()
        # Act
        raw_obs = RawObs.convert_observation(obs)

        # Assert
        self.assertEqual(RawObs().shape, raw_obs.shape)

    def test_process_all_shape_as_expected_with_kaggle_env(self):
        # Arrange
        raw_obs = self._kaggle_env.reset()

        # Act
        processed_obs = self._sut.set_obs(raw_obs).process()

        self.assertEqual(RawObs().shape, processed_obs.shape)

    def test_process_defaults_shape_as_expected_with_kaggle_env(self):
        # Arrange
        raw_obs = self._kaggle_env.reset()

        # Act
        processed_obs = RawObs(
            using=RawObs.standard_keys).set_obs(raw_obs).process()

        self.assertEqual(
            RawObs(using=RawObs.standard_keys).shape, processed_obs.shape)

    def test_process_all_shape_as_expected_with_gfootball_env(self):
        # Arrange
        raw_obs = self._gfootball_env.reset()

        # Act
        processed_obs = self._sut.set_obs(raw_obs).process()

        self.assertEqual(RawObs().shape, processed_obs.shape)

    def test_process_defaults_shape_as_expected_with_gfootball_env(self):
        # Arrange
        raw_obs = self._gfootball_env.reset()

        # Act
        processed_obs = RawObs(
            using=RawObs.standard_keys).set_obs(raw_obs).process()

        self.assertEqual(
            RawObs(using=RawObs.standard_keys).shape, processed_obs.shape)
 def setUp(self):
     self._kaggle_env = gym.make("GFootball-kaggle_11_vs_11-v0")
     self._gfootball_env = self._env = FootballEnv(config=Config())
     self._sut = RawObs()
class TestRawObs(unittest.TestCase):
    def setUp(self):
        self._raw_obs_fixture = RawObsFixture()
        self.sut = RawObs(using=RawObs.standard_keys)
        self.sut.set_obs(self._raw_obs_fixture.data)

    def test_process_list_field(self):
        # Act
        obs = self.sut.process_key('ball')

        # Assert
        self.assertEqual(2, len(obs.shape))
        self.assertEqual(1, obs.shape[0])

    def test_process_non_list_field(self):
        # Act
        obs = self.sut.process_key('active')

        # Assert
        self.assertEqual(2, len(obs.shape))
        self.assertEqual(1, obs.shape[0])

    def test_process_non_flat_field(self):
        # Act
        obs = self.sut.process_key('left_team')

        # Assert
        self.assertEqual(2, len(obs.shape))
        self.assertEqual(1, obs.shape[0])

    def test_process(self):
        # Act
        raw_obs = self.sut.process()

        # Assert
        self.assertEqual(self.sut.shape, raw_obs.shape)

    def test__add_distance_to_ball(self):
        # Act
        distance_to_ball = self.sut._add_distance_to_ball()

        self.assertEqual((1, self.sut.distance_to_ball_n),
                         distance_to_ball.shape)

    def test_with_obs_indexed_out_of_list(self):
        # Arrange
        ro = RawObs().set_obs(self._raw_obs_fixture.data[0])

        # Act
        raw_obs = ro.process()

        # Assert
        self.assertEqual(ro.shape, raw_obs.shape)

    def test_all_keys(self):
        for key in self.sut.standard_keys:
            obs = self.sut.process_key(key)
            self.assertEqual(getattr(self.sut, f"{key}_n"), obs.shape[1])

    def test_process_returns_none_if_nothing_to_do(self):
        # Arrange
        ro = RawObs(using=[]).set_obs(self._raw_obs_fixture.data[0])

        # Act
        raw_obs = ro.process()

        # Assert
        self.assertEqual((1, 0), ro.shape)
        self.assertIsNone(raw_obs)