Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the determinism of env.reset(seed=42); env.reset() #1086

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions gymnasium/utils/env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
seed_123_rng_1 = deepcopy(env.unwrapped._np_random)

obs_2, info = env.reset(seed=123)
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:
Expand Down
2 changes: 1 addition & 1 deletion tests/testing_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down
4 changes: 2 additions & 2 deletions tests/utils/test_env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
],
],
)
Expand Down
Loading