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

env_checker to use exact data_equivilance #953

Merged
merged 6 commits into from
Mar 14, 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
42 changes: 36 additions & 6 deletions gymnasium/utils/env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def check_reset_seed_determinism(env: gym.Env):
assert data_equivalence(
Kallinteris-Andreas marked this conversation as resolved.
Show resolved Hide resolved
obs_1, obs_2
), "Using `env.reset(seed=123)` is non-deterministic as the observations are not equivalent."
if not data_equivalence(obs_1, obs_2, exact=True):
logger.warn(
"Using `env.reset(seed=123)` observations are not equal although similar."
)

assert (
env.unwrapped._np_random.bit_generator.state
== seed_123_rng.bit_generator.state
Expand Down Expand Up @@ -165,11 +170,11 @@ def check_reset_options(env: gym.Env):
def check_step_determinism(env: gym.Env, seed=123):
"""Check that the environment steps deterministically after reset.

Note: This check assumes that seeded `reset()` is derministic (it must have passed `check_reset_seed`) and that `step()` returns valid values (passed `env_step_passive_checker`).
Note: This check assumes that seeded `reset()` is deterministic (it must have passed `check_reset_seed`) and that `step()` returns valid values (passed `env_step_passive_checker`).
Note: A single step should be enough to assert that the state transition function is deterministic (at least for most environments).

Raises:
AssertionError: The environment cannot be step determistially after resetting with a random seed,
AssertionError: The environment cannot be step deterministically after resetting with a random seed,
or it truncates after 1 step.
"""
if env.spec is not None and env.spec.nondeterministic is True:
Expand All @@ -189,13 +194,38 @@ def check_step_determinism(env: gym.Env, seed=123):
env.unwrapped._np_random.bit_generator.state # pyright: ignore [reportOptionalMemberAccess]
== seeded_rng.bit_generator.state
), "The `.np_random` is not properly been updated after step."
assert data_equivalence(obs_0, obs_1), "step observation is not deterministic."
assert data_equivalence(rew_0, rew_1), "step reward is not deterministic."
assert data_equivalence(term_0, term_0), "step terminal is not deterministic."

assert data_equivalence(
obs_0, obs_1
), "Deterministic step observations are not equivalent for the same seed and action"
if not data_equivalence(obs_0, obs_1, exact=True):
logger.warn(
"Step observations are not equal although similar given the same seed and action"
)

assert data_equivalence(
rew_0, rew_1
), "Deterministic step rewards are not equivalent for the same seed and action"
if not data_equivalence(rew_0, rew_1, exact=True):
logger.warn(
"Step rewards are not equal although similar given the same seed and action"
)

assert data_equivalence(
term_0, term_0, exact=True
), "Deterministic step termination are not equivalent for the same seed and action"
assert (
trunc_0 is False and trunc_1 is False
), "Environment truncates after 1 step, something has gone very wrong."
assert data_equivalence(info_0, info_1), "step info is not deterministic."

assert data_equivalence(
info_0,
info_1,
), "Deterministic step info are not equivalent for the same seed and action"
if not data_equivalence(info_0, info_1, exact=True):
logger.warn(
"Step info are not equal although similar given the same seed and action"
)


def check_reset_return_info_deprecation(env: gym.Env):
Expand Down
6 changes: 3 additions & 3 deletions tests/utils/test_env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,17 @@ def test_check_reset_options():
[
AssertionError,
lambda self, action: (np.random.normal(), 0, False, False, {}),
"step observation is not deterministic.",
"Deterministic step observations are not equivalent for the same seed and action",
],
[
AssertionError,
lambda self, action: (0, np.random.normal(), False, False, {}),
"step reward is not deterministic.",
"Deterministic step rewards are not equivalent for the same seed and action",
],
[
AssertionError,
lambda self, action: (0, 0, False, False, {"value": np.random.normal()}),
"step info is not deterministic.",
"Deterministic step info are not equivalent for the same seed and action",
],
],
)
Expand Down
Loading