rllib/utils/pre_checks/env.py
def check_gym_environments(env: gym.Env) -> None:
    """Checking for common errors in gym environments.

    Args:
        env: Environment to be checked.

    Warning:
        If env has no attribute spec with a sub attribute,
            max_episode_steps.

    Raises:
        AttributeError: If env has no observation space.
        AttributeError: If env has no action space.
        ValueError: Observation space must be a gym.spaces.Space.
        ValueError: Action space must be a gym.spaces.Space.
        ValueError: Observation sampled from observation space must be
            contained in the observation space.
        ValueError: Action sampled from action space must be
            contained in the observation space.
        ValueError: If env cannot be resetted.
        ValueError: If an observation collected from a call to env.reset().
            is not contained in the observation_space.
        ValueError: If env cannot be stepped via a call to env.step().
        ValueError: If the observation collected from env.step() is not
            contained in the observation_space.
        AssertionError: If env.step() returns a reward that is not an
            int or float.
        AssertionError: IF env.step() returns a done that is not a bool.
        AssertionError: If env.step() returns an env_info that is not a dict.
    """

    # check that env has observation and action spaces
    if not hasattr(env, "observation_space"):
        raise AttributeError("Env must have observation_space.")
    if not hasattr(env, "action_space"):
        raise AttributeError("Env must have action_space.")

    # check that observation and action spaces are gym.spaces
    if not isinstance(env.observation_space, gym.spaces.Space):
        raise ValueError("Observation space must be a gym.space")
    if not isinstance(env.action_space, gym.spaces.Space):
        raise ValueError("Action space must be a gym.space")

    # raise a warning if there isn't a max_episode_steps attribute
    if not hasattr(env, "spec") or not hasattr(env.spec, "max_episode_steps"):
        logger.warning(
            "Your env doesn't have a .spec.max_episode_steps "
            "attribute. This is fine if you have set 'horizon' "
            "in your config dictionary, or `soft_horizon`. "
            "However, if you haven't, 'horizon' will default "
            "to infinity, and your environment will not be "
            "reset."
        )
    # check if sampled actions and observations are contained within their
    # respective action and observation spaces.

    def get_type(var):
        return var.dtype if hasattr(var, "dtype") else type(var)

    sampled_action = env.action_space.sample()
    sampled_observation = env.observation_space.sample()
    # check if observation generated from stepping the environment is
    # contained within the observation space
    reset_obs = env.reset()
    if not env.observation_space.contains(reset_obs):
        reset_obs_type = get_type(reset_obs)
        space_type = env.observation_space.dtype
        error = (
            f"The observation collected from env.reset() was not  "
            f"contained within your env's observation space. Its possible "
            f"that There was a type mismatch, or that one of the "
            f"sub-observations  was out of bounds: \n\n reset_obs: "
            f"{reset_obs}\n\n env.observation_space: "
            f"{env.observation_space}\n\n reset_obs's dtype: "
            f"{reset_obs_type}\n\n env.observation_space's dtype: "
            f"{space_type}"
        )
        temp_sampled_reset_obs = convert_element_to_space_type(
            reset_obs, sampled_observation
        )
        if not env.observation_space.contains(temp_sampled_reset_obs):
            raise ValueError(error)
    # check if env.step can run, and generates observations rewards, done
    # signals and infos that are within their respective spaces and are of
    # the correct dtypes
    next_obs, reward, done, info = env.step(sampled_action)
    if not env.observation_space.contains(next_obs):
        next_obs_type = get_type(next_obs)
        space_type = env.observation_space.dtype
        error = (
            f"The observation collected from env.step(sampled_action) was "
            f"not contained within your env's observation space. Its "
            f"possible that There was a type mismatch, or that one of the "
            f"sub-observations was out of bounds:\n\n next_obs: {next_obs}"
            f"\n\n env.observation_space: {env.observation_space}"
            f"\n\n next_obs's dtype: {next_obs_type}"
            f"\n\n env.observation_space's dtype: {space_type}"
        )
        temp_sampled_next_obs = convert_element_to_space_type(
            next_obs, sampled_observation
        )
        if not env.observation_space.contains(temp_sampled_next_obs):
            raise ValueError(error)
    _check_done(done)
    _check_reward(reward)
    _check_info(info)
Checking for common errors in gym environments.

    Args:
        env: Environment to be checked.

    Warning:
        If env has no attribute spec with a sub attribute,
            max_episode_steps.

    Raises:
        AttributeError: If env has no observation space.
        AttributeError: If env has no action space.
        ValueError: Observation space must be a gym.spaces.Space.
        ValueError: Action space must be a gym.spaces.Space.
        ValueError: Observation sampled from observation space must be
            contained in the observation space.
        ValueError: Action sampled from action space must be
            contained in the observation space.
        ValueError: If env cannot be resetted.
        ValueError: If an observation collected from a call to env.reset().
            is not contained in the observation_space.
        ValueError: If env cannot be stepped via a call to env.step().
        ValueError: If the observation collected from env.step() is not
            contained in the observation_space.
        AssertionError: If env.step() returns a reward that is not an
            int or float.
        AssertionError: IF env.step() returns a done that is not a bool.
        AssertionError: If env.step() returns an env_info that is not a dict.
    
Checking for common errors in gym environments.
Checking for common errors in gym environments .
def check_gym_environments(env: gym.Env) -> None:
    if not hasattr(env, "observation_space"):
        raise AttributeError("Env must have observation_space.")
    if not hasattr(env, "action_space"):
        raise AttributeError("Env must have action_space.")
    if not isinstance(env.observation_space, gym.spaces.Space):
        raise ValueError("Observation space must be a gym.space")
    if not isinstance(env.action_space, gym.spaces.Space):
        raise ValueError("Action space must be a gym.space")
    if not hasattr(env, "spec") or not hasattr(env.spec, "max_episode_steps"):
        logger.warning(
            "Your env doesn't have a .spec.max_episode_steps "
            "attribute. This is fine if you have set 'horizon' "
            "in your config dictionary, or `soft_horizon`. "
            "However, if you haven't, 'horizon' will default "
            "to infinity, and your environment will not be "
            "reset."
        )
    def get_type(var):
        return var.dtype if hasattr(var, "dtype") else type(var)
    sampled_action = env.action_space.sample()
    sampled_observation = env.observation_space.sample()
    reset_obs = env.reset()
    if not env.observation_space.contains(reset_obs):
        reset_obs_type = get_type(reset_obs)
        space_type = env.observation_space.dtype
        error = (
            f"The observation collected from env.reset() was not  "
            f"contained within your env's observation space. Its possible "
            f"that There was a type mismatch, or that one of the "
            f"sub-observations  was out of bounds: \n\n reset_obs: "
            f"{reset_obs}\n\n env.observation_space: "
            f"{env.observation_space}\n\n reset_obs's dtype: "
            f"{reset_obs_type}\n\n env.observation_space's dtype: "
            f"{space_type}"
        )
        temp_sampled_reset_obs = convert_element_to_space_type(
            reset_obs, sampled_observation
        )
        if not env.observation_space.contains(temp_sampled_reset_obs):
            raise ValueError(error)
    next_obs, reward, done, info = env.step(sampled_action)
    if not env.observation_space.contains(next_obs):
        next_obs_type = get_type(next_obs)
        space_type = env.observation_space.dtype
        error = (
            f"The observation collected from env.step(sampled_action) was "
            f"not contained within your env's observation space. Its "
            f"possible that There was a type mismatch, or that one of the "
            f"sub-observations was out of bounds:\n\n next_obs: {next_obs}"
            f"\n\n env.observation_space: {env.observation_space}"
            f"\n\n next_obs's dtype: {next_obs_type}"
            f"\n\n env.observation_space's dtype: {space_type}"
        )
        temp_sampled_next_obs = convert_element_to_space_type(
            next_obs, sampled_observation
        )
        if not env.observation_space.contains(temp_sampled_next_obs):
            raise ValueError(error)
    _check_done(done)
    _check_reward(reward)
    _check_info(info)
def check_gym_environments ( env : gym . Env ) -> None : if not hasattr ( env , "observation_space" ) : raise AttributeError ( "Env must have observation_space." ) if not hasattr ( env , "action_space" ) : raise AttributeError ( "Env must have action_space." ) if not isinstance ( env . observation_space , gym . spaces . Space ) : raise ValueError ( "Observation space must be a gym.space" ) if not isinstance ( env . action_space , gym . spaces . Space ) : raise ValueError ( "Action space must be a gym.space" ) if not hasattr ( env , "spec" ) or not hasattr ( env . spec , "max_episode_steps" ) : logger . warning ( "Your env doesn't have a .spec.max_episode_steps " "attribute. This is fine if you have set 'horizon' " "in your config dictionary, or `soft_horizon`. " "However, if you haven't, 'horizon' will default " "to infinity, and your environment will not be " "reset." ) def get_type ( var ) : return var . dtype if hasattr ( var , "dtype" ) else type ( var ) sampled_action = env . action_space . sample ( ) sampled_observation = env . observation_space . sample ( ) reset_obs = env . reset ( ) if not env . observation_space . contains ( reset_obs ) : reset_obs_type = get_type ( reset_obs ) space_type = env . observation_space . dtype error = ( f"The observation collected from env.reset() was not  " f"contained within your env's observation space. Its possible " f"that There was a type mismatch, or that one of the " f"sub-observations  was out of bounds: \n\n reset_obs: " f"{reset_obs}\n\n env.observation_space: " f"{env.observation_space}\n\n reset_obs's dtype: " f"{reset_obs_type}\n\n env.observation_space's dtype: " f"{space_type}" ) temp_sampled_reset_obs = convert_element_to_space_type ( reset_obs , sampled_observation ) if not env . observation_space . contains ( temp_sampled_reset_obs ) : raise ValueError ( error ) next_obs , reward , done , info = env . step ( sampled_action ) if not env . observation_space . contains ( next_obs ) : next_obs_type = get_type ( next_obs ) space_type = env . observation_space . dtype error = ( f"The observation collected from env.step(sampled_action) was " f"not contained within your env's observation space. Its " f"possible that There was a type mismatch, or that one of the " f"sub-observations was out of bounds:\n\n next_obs: {next_obs}" f"\n\n env.observation_space: {env.observation_space}" f"\n\n next_obs's dtype: {next_obs_type}" f"\n\n env.observation_space's dtype: {space_type}" ) temp_sampled_next_obs = convert_element_to_space_type ( next_obs , sampled_observation ) if not env . observation_space . contains ( temp_sampled_next_obs ) : raise ValueError ( error ) _check_done ( done ) _check_reward ( reward ) _check_info ( info )
Checking for common errors in gym environments.
Checking for common errors in gym environments .
"""Checking for common errors in gym environments.

    Args:
        env: Environment to be checked.

    Warning:
        If env has no attribute spec with a sub attribute,
            max_episode_steps.

    Raises:
        AttributeError: If env has no observation space.
        AttributeError: If env has no action space.
        ValueError: Observation space must be a gym.spaces.Space.
        ValueError: Action space must be a gym.spaces.Space.
        ValueError: Observation sampled from observation space must be
            contained in the observation space.
        ValueError: Action sampled from action space must be
            contained in the observation space.
        ValueError: If env cannot be resetted.
        ValueError: If an observation collected from a call to env.reset().
            is not contained in the observation_space.
        ValueError: If env cannot be stepped via a call to env.step().
        ValueError: If the observation collected from env.step() is not
            contained in the observation_space.
        AssertionError: If env.step() returns a reward that is not an
            int or float.
        AssertionError: IF env.step() returns a done that is not a bool.
        AssertionError: If env.step() returns an env_info that is not a dict.
    """ # check that env has observation and action spaces # check that observation and action spaces are gym.spaces # raise a warning if there isn't a max_episode_steps attribute # check if sampled actions and observations are contained within their # respective action and observation spaces. # check if observation generated from stepping the environment is # contained within the observation space # check if env.step can run, and generates observations rewards, done # signals and infos that are within their respective spaces and are of # the correct dtypes
If env has no observation space.
If env has no observation space .
AttributeError
If env has no action space.
If env has no action space .
AttributeError
Observation space must be a gym.spaces.Space.
Observation space must be a gym . spaces . Space .
ValueError
Action space must be a gym.spaces.Space.
Action space must be a gym . spaces . Space .
ValueError
Observation sampled from observation space must be
contained in the observation space.
Observation sampled from observation space must be contained in the observation space .
ValueError
Action sampled from action space must be
contained in the observation space.
Action sampled from action space must be contained in the observation space .
ValueError
If env cannot be resetted.
If env cannot be resetted .
ValueError
If an observation collected from a call to env.reset().
is not contained in the observation_space.
If an observation collected from a call to env . reset () . is not contained in the observation_space .
ValueError
If env cannot be stepped via a call to env.step().
If env cannot be stepped via a call to env . step () .
ValueError
If the observation collected from env.step() is not
contained in the observation_space.
If the observation collected from env . step () is not contained in the observation_space .
ValueError
If env.step() returns a reward that is not an
int or float.
If env . step () returns a reward that is not an int or float .
AssertionError
IF env.step() returns a done that is not a bool.
IF env . step () returns a done that is not a bool .
AssertionError
If env.step() returns an env_info that is not a dict.
If env . step () returns an env_info that is not a dict .
AssertionError
env
gym.Env
Environment to be checked.
Environment to be checked .