-
-
Notifications
You must be signed in to change notification settings - Fork 838
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
add check_environments_match
function for checking if two environments are identical
#576
Merged
pseudo-rnd-thoughts
merged 7 commits into
Farama-Foundation:main
from
Kallinteris-Andreas:patch-25
Jul 1, 2023
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
161c09f
add `check_environments_match`
Kallinteris-Andreas da02867
Update env_tests.py
Kallinteris-Andreas 07e4467
pre-commit
Kallinteris-Andreas feef3e9
Update env_tests.py\\
Kallinteris-Andreas b915517
Update env_tests.py
Kallinteris-Andreas 1675a6e
Merge branch 'Farama-Foundation:main' into patch-25
Kallinteris-Andreas f02374b
rename file
Kallinteris-Andreas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""A set of tests to help the desiner of gymansium environments verify that they work correctly.""" | ||
|
||
import gymnasium as gym | ||
from gymnasium.utils.env_checker import data_equivalence | ||
|
||
|
||
def check_environments_match( | ||
env_a: gym.Env, | ||
env_b: gym.Env, | ||
num_steps: int, | ||
seed: int = 0, | ||
skip_obs: bool = False, | ||
skip_rew: bool = False, | ||
skip_terminal: bool = False, | ||
skip_truncated: bool = False, | ||
info_comparison: str = "equivalence", | ||
): | ||
"""Checks if the environments `env_a` & `env_b` are identical. | ||
|
||
Args: | ||
env_a: First environment to check. | ||
env_b: Second environment to check. | ||
num_steps: number of timesteps to test for, setting to 0 tests only resetting. | ||
seed: used the seed the reset & actions. | ||
skip_obs: If `True` it does not check for equivalence of the observation. | ||
skip_rew: If `True` it does not check for equivalence of the observation. | ||
skip_terminal: If `True` it does not check for equivalence of the observation. | ||
skip_truncated: If `True` it does not check for equivalence of the observation. | ||
skip_info: If `True` it does not check for equivalence of the observation. | ||
info_comparison: If "equivalence" then checks if the `info`s are identical, | ||
if "superset" checks if `info_b` is a (non-strict) superset of `info_a` | ||
if "skip" no checks are made at the `info`. | ||
""" | ||
assert info_comparison in ["equivalence", "superset", "skip"] | ||
|
||
assert env_a.action_space == env_b.action_space | ||
assert skip_obs or env_b.observation_space == env_b.observation_space | ||
|
||
env_a.action_space.seed(seed) | ||
obs_a, info_a = env_a.reset(seed=seed) | ||
obs_b, info_b = env_b.reset(seed=seed) | ||
|
||
assert skip_obs or data_equivalence( | ||
obs_a, obs_b | ||
), "resetting observation is not equivalent" | ||
if info_comparison == "equivalence": | ||
assert data_equivalence(info_a, info_b), "resetting info is not equivalent" | ||
elif info_comparison == "superset": | ||
for key in info_a: | ||
assert data_equivalence( | ||
info_a[key], info_b[key] | ||
), "resetting info is not a superset" | ||
|
||
for _ in range(num_steps): | ||
action = env_a.action_space.sample() | ||
obs_a, rew_a, terminal_a, truncated_a, info_a = env_a.step(action) | ||
obs_b, rew_b, terminal_b, truncated_b, info_b = env_b.step(action) | ||
assert skip_obs or data_equivalence( | ||
obs_a, obs_b | ||
), "stepping observation is not equivalent" | ||
assert skip_rew or data_equivalence( | ||
rew_a, rew_b | ||
), "stepping reward is not equivalent" | ||
assert ( | ||
skip_terminal or terminal_a == terminal_b | ||
), "stepping terminal is not equivalent" | ||
assert ( | ||
skip_truncated or truncated_a == truncated_b | ||
), "stepping truncated is not equivalent" | ||
if info_comparison == "equivalence": | ||
assert data_equivalence(info_a, info_b), "stepping info is not equivalent" | ||
elif info_comparison == "superset": | ||
for key in info_a: | ||
assert data_equivalence( | ||
info_a[key], info_b[key] | ||
), "stepping info is not a superset" | ||
|
||
if terminal_a or truncated_a or terminal_b or truncated_b: | ||
obs_a, info_a = env_a.reset(seed=seed) | ||
obs_b, info_b = env_b.reset(seed=seed) | ||
assert skip_obs or data_equivalence( | ||
obs_a, obs_b | ||
), "resetting observation is not equivalent" | ||
if info_comparison == "equivalence": | ||
assert data_equivalence( | ||
info_a, info_b | ||
), "resetting info is not equivalent" | ||
elif info_comparison == "superset": | ||
for key in info_a: | ||
assert data_equivalence( | ||
info_a[key], info_b[key] | ||
), "resetting info is not a superset" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to
env_match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant the filename rather the function name, I think
check_environments_match
is a good name