Exemplo n.º 1
0
def _check_obs(
    obs: Union[tuple, dict, np.ndarray, int],
    observation_space: spaces.Space,
    method_name: str,
) -> None:
    """
    Check that the observation returned by the environment
    correspond to the declared one.
    """
    if not isinstance(observation_space, spaces.Tuple):
        assert not isinstance(
            obs, tuple
        ), f"The observation returned by the `{method_name}()` method should be a single value, not a tuple"

    if isinstance(observation_space, spaces.Discrete):
        assert isinstance(
            obs, int
        ), f"The observation returned by `{method_name}()` method must be an int"
    elif _is_numpy_array_space(observation_space):
        assert isinstance(
            obs, np.ndarray
        ), f"The observation returned by `{method_name}()` method must be a numpy array"

    assert observation_space.contains(
        obs
    ), f"The observation returned by the `{method_name}()` method does not match the given observation space"
Exemplo n.º 2
0
def _check_obs(obs: Union[tuple, dict, np.ndarray, int],
               observation_space: spaces.Space, method_name: str) -> None:
    """
    Check that the observation returned by the environment
    correspond to the declared one.
    """
    if not isinstance(observation_space, spaces.Tuple):
        assert not isinstance(
            obs, tuple
        ), "The observation returned by the `{}()` method should be a single value, not a tuple".format(
            method_name)

    # The check for a GoalEnv is done by the base class
    if isinstance(observation_space, spaces.Discrete):
        assert isinstance(
            obs, int
        ), "The observation returned by `{}()` method must be an int".format(
            method_name)
    elif _enforce_array_obs(observation_space):
        assert isinstance(
            obs, np.ndarray
        ), "The observation returned by `{}()` method must be a numpy array".format(
            method_name)

    assert observation_space.contains(
        obs
    ), "The observation returned by the `{}()` method does not match the given observation space".format(
        method_name)
Exemplo n.º 3
0
def _check_obs(obs, observation_space: spaces.Space, method_name: str):
    """Check that the observation returned by the environment correspond to the declared one.

    Args:
        obs: The observation to check
        observation_space: The observation space of the observation
        method_name: The method name that generated the observation
    """
    pre = f"The observation returned by the `{method_name}()` method"

    assert observation_space.contains(
        obs
    ), f"{pre} is not contained with the observation space ({observation_space})"

    if isinstance(observation_space, spaces.Discrete):
        assert isinstance(
            obs, int
        ), f"The observation returned by `{method_name}()` method must be an int, actually {type(obs)}"
    elif isinstance(
        observation_space, (spaces.Box, spaces.MultiBinary, spaces.MultiDiscrete)
    ):
        assert isinstance(
            obs, np.ndarray
        ), f"The observation returned by `{method_name}()` method must be a numpy array, actually {type(obs)}"
    elif isinstance(observation_space, spaces.Tuple):
        assert isinstance(
            obs, tuple
        ), f"The observation returned by the `{method_name}()` method must be a tuple, actually {type(obs)}"
        for sub_obs, sub_space in zip(obs, observation_space.spaces):
            _check_obs(sub_obs, sub_space, method_name)
    elif isinstance(observation_space, spaces.Dict):
        assert isinstance(
            obs, dict
        ), f"The observation returned by the `{method_name}()` method must be a dict, actually {type(obs)}"
        for space_key in observation_space.keys():
            _check_obs(obs[space_key], observation_space[space_key], method_name)