From f38559a476c30de3767eb856037eba8e3ebf5225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Gallou=C3=A9dec?= <45557362+qgallouedec@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:31:26 +0200 Subject: [PATCH 1/3] further check reset determinism --- gymnasium/utils/env_checker.py | 38 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/gymnasium/utils/env_checker.py b/gymnasium/utils/env_checker.py index def0c0705..cf7deaa85 100644 --- a/gymnasium/utils/env_checker.py +++ b/gymnasium/utils/env_checker.py @@ -91,33 +91,51 @@ def check_reset_seed_determinism(env: gym.Env): assert ( env.unwrapped._np_random is not None ), "Expects the random number generator to have been generated given a seed was passed to reset. Most likely the environment reset function does not call `super().reset(seed=seed)`." - seed_123_rng = deepcopy(env.unwrapped._np_random) - - obs_2, info = env.reset(seed=123) + seed_123_rng_1 = deepcopy(env.unwrapped._np_random) + + obs_2, info = env.reset() assert ( obs_2 in env.observation_space + ), "The observation returned by `env.reset()` is not within the observation space." + + obs_3, info = env.reset(seed=123) + assert ( + obs_3 in env.observation_space ), "The observation returned by `env.reset(seed=123)` is not within the observation space." + seed_123_rng_3 = deepcopy(env.unwrapped._np_random) + + obs_4, info = env.reset() + assert ( + obs_4 in env.observation_space + ), "The observation returned by `env.reset()` is not within the observation space." + if env.spec is not None and env.spec.nondeterministic is False: assert data_equivalence( - obs_1, obs_2 + obs_1, obs_3 ), "Using `env.reset(seed=123)` is non-deterministic as the observations are not equivalent." - if not data_equivalence(obs_1, obs_2, exact=True): + assert data_equivalence( + obs_2, obs_4 + ), "Using `env.reset(seed=123)` then `env.reset()` is non-deterministic as the observations are not equivalent." + if not data_equivalence(obs_1, obs_3, exact=True): logger.warn( "Using `env.reset(seed=123)` observations are not equal although similar." ) + if not data_equivalence(obs_2, obs_4, exact=True): + logger.warn( + "Using `env.reset(seed=123)` then `env.reset()` observations are not equal although similar." + ) assert ( - env.unwrapped._np_random.bit_generator.state - == seed_123_rng.bit_generator.state + seed_123_rng_1.bit_generator.state == seed_123_rng_3.bit_generator.state ), "Most likely the environment reset function does not call `super().reset(seed=seed)` as the random generates are not same when the same seeds are passed to `env.reset`." - obs_3, info = env.reset(seed=456) + obs_5, info = env.reset(seed=456) assert ( - obs_3 in env.observation_space + obs_5 in env.observation_space ), "The observation returned by `env.reset(seed=456)` is not within the observation space." assert ( env.unwrapped._np_random.bit_generator.state - != seed_123_rng.bit_generator.state + != seed_123_rng_1.bit_generator.state ), "Most likely the environment reset function does not call `super().reset(seed=seed)` as the random number generators are not different when different seeds are passed to `env.reset`." except TypeError as e: From e5d6e5b14647bc0c6cd3dfa1014eee158bdbb7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Gallou=C3=A9dec?= <45557362+qgallouedec@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:41:06 +0200 Subject: [PATCH 2/3] format --- gymnasium/utils/env_checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gymnasium/utils/env_checker.py b/gymnasium/utils/env_checker.py index cf7deaa85..c97855644 100644 --- a/gymnasium/utils/env_checker.py +++ b/gymnasium/utils/env_checker.py @@ -92,7 +92,7 @@ def check_reset_seed_determinism(env: gym.Env): env.unwrapped._np_random is not None ), "Expects the random number generator to have been generated given a seed was passed to reset. Most likely the environment reset function does not call `super().reset(seed=seed)`." seed_123_rng_1 = deepcopy(env.unwrapped._np_random) - + obs_2, info = env.reset() assert ( obs_2 in env.observation_space From 681a9630ff7ad189a998d203cb4f27f9d0b3c5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Gallou=C3=A9dec?= Date: Fri, 14 Jun 2024 19:00:58 +0000 Subject: [PATCH 3/3] fix seed in tests --- tests/testing_env.py | 2 +- tests/utils/test_env_checker.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testing_env.py b/tests/testing_env.py index 7d5dff617..dafbf15a6 100644 --- a/tests/testing_env.py +++ b/tests/testing_env.py @@ -20,7 +20,7 @@ def basic_reset_func( ) -> tuple[ObsType, dict]: """A basic reset function that will pass the environment check using random actions from the observation space.""" super(GenericTestEnv, self).reset(seed=seed) - self.observation_space.seed(seed) + self.observation_space.seed(self.np_random_seed) return self.observation_space.sample(), {"options": options} diff --git a/tests/utils/test_env_checker.py b/tests/utils/test_env_checker.py index b951d5457..daf8fca60 100644 --- a/tests/utils/test_env_checker.py +++ b/tests/utils/test_env_checker.py @@ -70,7 +70,7 @@ def _super_reset_fixed(self, seed=None, options=None): return self.observation_space.sample(), {} -def _reset_default_seed(self: GenericTestEnv, seed="Error", options=None): +def _reset_default_seed(self: GenericTestEnv, seed=23, options=None): super(GenericTestEnv, self).reset(seed=seed) self.observation_space._np_random = ( # pyright: ignore [reportPrivateUsage] self.np_random @@ -104,7 +104,7 @@ def _reset_default_seed(self: GenericTestEnv, seed="Error", options=None): [ UserWarning, _reset_default_seed, - "The default seed argument in reset should be `None`, otherwise the environment will by default always be deterministic. Actual default: Error", + "The default seed argument in reset should be `None`, otherwise the environment will by default always be deterministic. Actual default: 23", ], ], )