Change GymV22Compatibility to GymV21Compatibility (#282)

This commit is contained in:
Mark Towers
2023-01-26 09:46:02 +00:00
committed by GitHub
parent d46ff0e129
commit b4c02d4b4b
4 changed files with 25 additions and 9 deletions

View File

@@ -20,24 +20,36 @@ To perform conversion through a wrapper, the environment itself can be passed to
An example of this is atari 0.8.0 which does not have a gymnasium implementation. An example of this is atari 0.8.0 which does not have a gymnasium implementation.
```python ```python
import gymnasium as gym import gymnasium as gym
env = gym.make("GymV26Environment-v0", env_id="ALE/Pong-v5") env = gym.make("GymV26Environment-v0", env_id="ALE/Pong-v5")
``` ```
## Gym v0.22 Environment Compatibility ## Gym v0.21 Environment Compatibility
```{eval-rst} ```{eval-rst}
.. py:currentmodule:: gymnasium .. py:currentmodule:: gymnasium
A number of environments have not updated to the recent Gym changes, in particular since v0.21. A number of environments have not updated to the recent Gym changes, in particular since v0.21.
This update is significant for the introduction of ``termination`` and ``truncation`` signatures in favour of the previously used ``done``. This update is significant for the introduction of ``termination`` and ``truncation`` signatures in favour of the previously used ``done``.
To allow backward compatibility, Gym and Gymnasium v0.26+ include an ``apply_api_compatibility`` kwarg when calling :meth:`make` that automatically converts a v0.21 API compliant environment one that is compatible with v0.26+. To allow backward compatibility, Gym and Gymnasium v0.26+ include an ``apply_api_compatibility`` kwarg when calling :meth:`make` that automatically converts a v0.21 API compliant environment to one that is compatible with v0.26+.
``` ```
```python ```python
import gym import gym
env = gym.make("OldV21Env-v0", apply_api_compatibility=True) env = gym.make("OldV21Env-v0", apply_api_compatibility=True)
``` ```
Additionally, in Gymnasium we provide specialist environments for compatibility that for ``env_id`` will call ``gym.make``.
```python
import gymnasium
env = gymnasium.make("GymV21Environment-v0", env_id="CartPole-v1", make_kwargs={"length": 1}, render_mode="human")
# or
env = gymnasium.make("GymV21Environment-v0", env=OldV21Env())
```
## Step API Compatibility ## Step API Compatibility
```{eval-rst} ```{eval-rst}

View File

@@ -1,4 +1,6 @@
"""Registers the internal gym envs then loads the env plugins for module using the entry point.""" """Registers the internal gym envs then loads the env plugins for module using the entry point."""
from typing import Any
from gymnasium.envs.registration import ( from gymnasium.envs.registration import (
load_env_plugins, load_env_plugins,
make, make,
@@ -349,14 +351,14 @@ register(
# --- For shimmy compatibility # --- For shimmy compatibility
def _raise_shimmy_error(): def _raise_shimmy_error(*args: Any, **kwargs: Any):
raise ImportError( raise ImportError(
"To use the gym compatibility environments, run `pip install shimmy[gym]`" "To use the gym compatibility environments, run `pip install shimmy[gym]`"
) )
# When installed, shimmy will re-register these environments with the correct entry_point # When installed, shimmy will re-register these environments with the correct entry_point
register(id="GymV22Environment-v0", entry_point=_raise_shimmy_error) register(id="GymV21Environment-v0", entry_point=_raise_shimmy_error)
register(id="GymV26Environment-v0", entry_point=_raise_shimmy_error) register(id="GymV26Environment-v0", entry_point=_raise_shimmy_error)

View File

@@ -65,8 +65,8 @@ class EnvCompatibility(gym.Env):
render_mode (str): the render mode to use when rendering the environment, passed automatically to env.render render_mode (str): the render mode to use when rendering the environment, passed automatically to env.render
""" """
logger.warn( logger.warn(
"The `gymnasium.make(..., apply_api_compatibility=...)` parameter is deprecated and will be removed in v0.28. " "The `gymnasium.make(..., apply_api_compatibility=...)` parameter is deprecated and will be removed in v0.29. "
"Instead use `gym.make('GymV22Environment-v0', env_name=...)` or `from shimmy import GymV22CompatibilityV0`" "Instead use `gym.make('GymV21Environment-v0', env_name=...)` or `from shimmy import GymV21CompatibilityV0`"
) )
self.metadata = getattr(old_env, "metadata", {"render_modes": []}) self.metadata = getattr(old_env, "metadata", {"render_modes": []})
self.render_mode = render_mode self.render_mode = render_mode

View File

@@ -143,7 +143,7 @@ def test_make_compatibility_in_make():
def test_shimmy_gym_compatibility(): def test_shimmy_gym_compatibility():
assert gymnasium.spec("GymV22Environment-v0") is not None assert gymnasium.spec("GymV21Environment-v0") is not None
assert gymnasium.spec("GymV26Environment-v0") is not None assert gymnasium.spec("GymV26Environment-v0") is not None
if shimmy is None: if shimmy is None:
@@ -153,14 +153,14 @@ def test_shimmy_gym_compatibility():
"To use the gym compatibility environments, run `pip install shimmy[gym]`" "To use the gym compatibility environments, run `pip install shimmy[gym]`"
), ),
): ):
gymnasium.make("GymV22Environment-v0") gymnasium.make("GymV21Environment-v0", env_id="CartPole-v1")
with pytest.raises( with pytest.raises(
ImportError, ImportError,
match=re.escape( match=re.escape(
"To use the gym compatibility environments, run `pip install shimmy[gym]`" "To use the gym compatibility environments, run `pip install shimmy[gym]`"
), ),
): ):
gymnasium.make("GymV26Environment-v0") gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")
elif gym is None: elif gym is None:
with pytest.raises( with pytest.raises(
DependencyNotInstalled, DependencyNotInstalled,
@@ -168,6 +168,7 @@ def test_shimmy_gym_compatibility():
"No module named 'gym' (Hint: You need to install gym with `pip install gym` to use gym environments" "No module named 'gym' (Hint: You need to install gym with `pip install gym` to use gym environments"
), ),
): ):
# todo - update when shimmy is updated to v0.28
gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1") gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1")
with pytest.raises( with pytest.raises(
DependencyNotInstalled, DependencyNotInstalled,
@@ -177,5 +178,6 @@ def test_shimmy_gym_compatibility():
): ):
gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1") gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")
else: else:
# todo - update when shimmy is updated to v0.28
gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1") gymnasium.make("GymV22Environment-v0", env_id="CartPole-v1")
gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1") gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")