Skip to content

Commit

Permalink
Update env checker goal env check and links (#1517)
Browse files Browse the repository at this point in the history
* Update env checker goal env check and links

* Update stable_baselines3/common/env_checker.py

Co-authored-by: coin15 <j.andregon15@gmail.com>

---------

Co-authored-by: coin15 <j.andregon15@gmail.com>
  • Loading branch information
araffin and andregonz committed May 24, 2023
1 parent 9c338f9 commit c8210dd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
7 changes: 7 additions & 0 deletions docs/guide/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Installation
============

.. warning::

Only Stable-Baselines3 2.x supports Gymnasium. As SB3 2.x is currently in beta,
you will need to specify ``pip install "stable_baselines3[extra]>=2.0.0a9"`` explicitly,
or install the master version (see below).


Prerequisites
-------------

Expand Down
3 changes: 2 additions & 1 deletion docs/misc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Changelog
==========

Release 2.0.0a9 (WIP)
Release 2.0.0a10 (WIP)
--------------------------

**Gymnasium support**
Expand Down Expand Up @@ -64,6 +64,7 @@ Others:
- Added render test for ``VecEnv``
- Update issue templates and env info saved with the model
- Changed ``seed()`` method return type from ``List`` to ``Sequence``
- Updated env checker doc and requirements for tuple spaces/goal envs

Documentation:
^^^^^^^^^^^^^^
Expand Down
33 changes: 16 additions & 17 deletions stable_baselines3/common/env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _check_unsupported_spaces(env: gym.Env, observation_space: spaces.Space, act
"The observation space is a Tuple,"
"this is currently not supported by Stable Baselines3. "
"However, you can convert it to a Dict observation space "
"(cf. https://github.com/openai/gym/blob/master/gym/spaces/dict.py). "
"(cf. https://gymnasium.farama.org/api/spaces/composite/#dict). "
"which is supported by SB3."
)

Expand Down Expand Up @@ -126,11 +126,11 @@ def _is_goal_env(env: gym.Env) -> bool:
def _check_goal_env_obs(obs: dict, observation_space: spaces.Dict, method_name: str) -> None:
"""
Check that an environment implementing the `compute_rewards()` method
(previously known as GoalEnv in gym) contains three elements,
(previously known as GoalEnv in gym) contains at least three elements,
namely `observation`, `desired_goal`, and `achieved_goal`.
"""
assert len(observation_space.spaces) == 3, (
"A goal conditioned env must contain 3 observation keys: `observation`, `desired_goal`, and `achieved_goal`."
assert len(observation_space.spaces) >= 3, (
"A goal conditioned env must contain at least 3 observation keys: `observation`, `desired_goal`, and `achieved_goal`. "
f"The current observation contains {len(observation_space.spaces)} keys: {list(observation_space.spaces.keys())}"
)

Expand Down Expand Up @@ -327,23 +327,22 @@ def _check_spaces(env: gym.Env) -> None:
"""
Check that the observation and action spaces are defined and inherit from spaces.Space. For
envs that follow the goal-conditioned standard (previously, the gym.GoalEnv interface) we check
the observation space is gym.spaces.Dict
the observation space is gymnasium.spaces.Dict
"""
# Helper to link to the code, because gym has no proper documentation
gym_spaces = " cf https://github.com/openai/gym/blob/master/gym/spaces/"
gym_spaces = "cf. https://gymnasium.farama.org/api/spaces/"

assert hasattr(env, "observation_space"), "You must specify an observation space (cf gym.spaces)" + gym_spaces
assert hasattr(env, "action_space"), "You must specify an action space (cf gym.spaces)" + gym_spaces
assert hasattr(env, "observation_space"), f"You must specify an observation space ({gym_spaces})"
assert hasattr(env, "action_space"), f"You must specify an action space ({gym_spaces})"

assert isinstance(env.observation_space, spaces.Space), (
"The observation space must inherit from gymnasium.spaces" + gym_spaces
)
assert isinstance(env.action_space, spaces.Space), "The action space must inherit from gymnasium.spaces" + gym_spaces
assert isinstance(
env.observation_space, spaces.Space
), f"The observation space must inherit from gymnasium.spaces ({gym_spaces})"
assert isinstance(env.action_space, spaces.Space), f"The action space must inherit from gymnasium.spaces ({gym_spaces})"

if _is_goal_env(env):
assert isinstance(
env.observation_space, spaces.Dict
), "Goal conditioned envs (previously gym.GoalEnv) require the observation space to be gym.spaces.Dict"
), "Goal conditioned envs (previously gym.GoalEnv) require the observation space to be gymnasium.spaces.Dict"


# Check render cannot be covered by CI
Expand Down Expand Up @@ -376,7 +375,7 @@ def check_env(env: gym.Env, warn: bool = True, skip_render_check: bool = True) -
"""
Check that an environment follows Gym API.
This is particularly useful when using a custom environment.
Please take a look at https://github.com/openai/gym/blob/master/gym/core.py
Please take a look at https://gymnasium.farama.org/api/env/
for more information about the API.
It also optionally check that the environment is compatible with Stable-Baselines.
Expand All @@ -389,7 +388,7 @@ def check_env(env: gym.Env, warn: bool = True, skip_render_check: bool = True) -
"""
assert isinstance(
env, gym.Env
), "Your environment must inherit from the gym.Env class cf https://github.com/openai/gym/blob/master/gym/core.py"
), "Your environment must inherit from the gymnasium.Env class cf. https://gymnasium.farama.org/api/env/"

# ============= Check the spaces (observation and action) ================
_check_spaces(env)
Expand Down Expand Up @@ -421,7 +420,7 @@ def check_env(env: gym.Env, warn: bool = True, skip_render_check: bool = True) -
):
warnings.warn(
"We recommend you to use a symmetric and normalized Box action space (range=[-1, 1]) "
"cf https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.html"
"cf. https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.html"
)

if isinstance(action_space, spaces.Box):
Expand Down
2 changes: 1 addition & 1 deletion stable_baselines3/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0a9
2.0.0a10

0 comments on commit c8210dd

Please sign in to comment.