diff --git a/shimmy/openai_gym_compatibility.py b/shimmy/openai_gym_compatibility.py index 0ffb225..dacd579 100644 --- a/shimmy/openai_gym_compatibility.py +++ b/shimmy/openai_gym_compatibility.py @@ -152,7 +152,7 @@ def close(self): """Close the environment.""" ... - def seed(self, seed: int | None = None): + def seed(self, seed: int | None = None) -> list[int] | None: """Set the seed for this env's random number generator(s).""" ... @@ -223,6 +223,9 @@ def reset( Returns: (observation, info) """ + # Initialise Gymnasium's RNG (check_env / seeding API expect `_np_random`). + super().reset(seed=seed) + if seed is not None: self.gym_env.seed(seed) diff --git a/tests/test_dm_control.py b/tests/test_dm_control.py index dff710e..e10a145 100644 --- a/tests/test_dm_control.py +++ b/tests/test_dm_control.py @@ -50,6 +50,10 @@ def test_dm_control_suite_envs(): ] ] CHECK_ENV_IGNORE_WARNINGS.append("`in1d` is deprecated. Use `np.isin` instead.") +CHECK_ENV_IGNORE_WARNINGS.append( + "Setting the shape on a NumPy array has been deprecated in NumPy 2.5.\n" + "As an alternative, you can create a new view using np.reshape (with copy=False if needed)." +) @pytest.mark.parametrize("env_id", DM_CONTROL_ENV_IDS) diff --git a/tests/test_gym.py b/tests/test_gym.py index d75160c..c369fb2 100644 --- a/tests/test_gym.py +++ b/tests/test_gym.py @@ -22,12 +22,22 @@ "A Box observation space maximum value is infinity. This is probably too high.", "For Box action spaces, we recommend using a symmetric and normalized space (range=[-1, 1] or [0, 1]). See https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.html for more information.", "The environment CartPole-v0 is out of date. You should consider upgrading to version `v1`.", + # Gym v21 warnings + "Official support for the `seed` function is dropped. Standard practice is to reset gymnasium environments " + "using `env.reset(seed=)`", + "Gym v21 environment do not accept options as a reset parameter, options={}", ] ] CHECK_ENV_IGNORE_WARNINGS.append( "`np.bool8` is a deprecated alias for `np.bool_`. (Deprecated NumPy 1.24)" ) +# Gym V26 introduced render_mode / the new step API; V21 uses the legacy API. +if openai_gym.__version__ >= "0.26": + GYM_COMPAT_ENV_ID = "GymV26Environment-v0" +else: + GYM_COMPAT_ENV_ID = "GymV21Environment-v0" + # We do not test Atari environment's here because we check all variants of Pong in test_envs.py (There are too many Atari environments) if openai_gym.__version__ >= "0.24.0": CLASSIC_CONTROL_ENVS = [ @@ -48,7 +58,7 @@ ) def test_gym_conversion_by_id(env_id): """Tests that the gym conversion works through specifying the env_id.""" - env = gymnasium.make("GymV26Environment-v0", env_id=env_id).unwrapped + env = gymnasium.make(GYM_COMPAT_ENV_ID, env_id=env_id).unwrapped with warnings.catch_warnings(record=True) as caught_warnings: check_env(env, skip_render_check=True) @@ -67,7 +77,7 @@ def test_gym_conversion_by_id(env_id): def test_gym_conversion_instantiated(env_id): """Tests that the gym conversion works with an instantiated gym environment.""" env = openai_gym.make(env_id) - env = gymnasium.make("GymV26Environment-v0", env=env).unwrapped + env = gymnasium.make(GYM_COMPAT_ENV_ID, env=env).unwrapped print("render-mode", env.render_mode) print("render-modes", env.metadata) @@ -88,8 +98,11 @@ class EnvWithData(openai_gym.Env): def __init__(self): """Initialises the environment with hidden data.""" - self.observation_space = openai_Box(low=0, high=1) - self.action_space = openai_Box(low=0, high=1) + # gym 0.21 requires an explicit shape when low/high are scalars. + self.observation_space = openai_Box(low=0, high=1, shape=()) + self.action_space = openai_Box(low=0, high=1, shape=()) + # Present so GymV26CompatibilityV0 can read it on gym<0.26 installs. + self.render_mode = None self.data = 123