Skip to content

Commit 134de4a

Browse files
Updates the environment checker (#2864)
* Updated testing requirements based off extra["testing"] * Updated setup to check the version is valid, added testing and all dependency groups and collects the requirements from requirements.txt to keep everything standardized. * Updated requirements.txt based on the current minimum gym requirements.txt to work * Updated requirements.txt based on the current minimum gym requirements.txt to work * Updated test_requirements.txt based on the current gym full testing requirements * Pre-commit updates * Add integer check for the `n` parameter * The type of self.spaces is an Iterable which is absorbed by the tuple. * Simplifies the environment checker to two files, env_checker.py and passive_env_checker.py with a new wrapper env_checker.py * Adds the passive environment checker on `gym.make` * Ignore the `check_env` warn parameter * Ignore the `check_env` warn parameter * Use the `data_equivalence` function * Revert rewrite setup.py changes * Remove smart formatting for 3.6 support * Fixed `check_action_space` and `check_observation_space` * Added disable_env_checker to vector.make such that env_checker would only run on the first environment created. * Removing check that different seeds would produce different initialising states * Use the unwrapped environment np_random * Fixed vector environment creator
1 parent 9fa7ede commit 134de4a

File tree

9 files changed

+598
-477
lines changed

9 files changed

+598
-477
lines changed

gym/envs/registration.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import numpy as np
2424

2525
from gym.envs.__relocated__ import internal_env_relocation_map
26-
from gym.utils.env_checker import check_env
2726
from gym.wrappers import AutoResetWrapper, OrderEnforcing, TimeLimit
27+
from gym.wrappers.env_checker import PassiveEnvChecker
2828

2929
if sys.version_info < (3, 10):
3030
import importlib_metadata as metadata # type: ignore
@@ -43,7 +43,15 @@
4343
)
4444

4545

46-
def load(name: str) -> type:
46+
def load(name: str) -> callable:
47+
"""Loads an environment with name and returns an environment creation function
48+
49+
Args:
50+
name: The environment name
51+
52+
Returns:
53+
Calls the environment constructor
54+
"""
4755
mod_name, attr_name = name.split(":")
4856
mod = importlib.import_module(mod_name)
4957
fn = getattr(mod, attr_name)
@@ -519,11 +527,6 @@ def make(
519527
) -> Env:
520528
"""Create an environment according to the given ID.
521529
522-
Warnings:
523-
In v0.24, `gym.utils.env_checker.env_checker` is run for every initialised environment.
524-
This calls the :meth:`Env.reset`, :meth:`Env.step` and :meth:`Env.render` functions to valid
525-
if they follow the gym API. To disable this feature, set parameter `disable_env_checker=True`.
526-
527530
Args:
528531
id: Name of the environment. Optionally, a module to import can be included, eg. 'module:Env-v0'
529532
max_episode_steps: Maximum length of an episode (TimeLimit wrapper).
@@ -578,49 +581,44 @@ def make(
578581
_kwargs = spec_.kwargs.copy()
579582
_kwargs.update(kwargs)
580583

581-
# TODO: add a minimal env checker on initialization
582584
if spec_.entry_point is None:
583585
raise error.Error(f"{spec_.id} registered but entry_point is not specified")
584586
elif callable(spec_.entry_point):
585-
cls = spec_.entry_point
587+
env_creator = spec_.entry_point
586588
else:
587589
# Assume it's a string
588-
cls = load(spec_.entry_point)
590+
env_creator = load(spec_.entry_point)
589591

590-
env = cls(**_kwargs)
592+
env = env_creator(**_kwargs)
591593

594+
# Copies the environment creation specification and kwargs to add to the environment specification details
592595
spec_ = copy.deepcopy(spec_)
593596
spec_.kwargs = _kwargs
594-
595597
env.unwrapped.spec = spec_
596598

599+
# Run the environment checker as the lowest level wrapper
600+
if disable_env_checker is False:
601+
env = PassiveEnvChecker(env)
602+
603+
# Add the order enforcing wrapper
597604
if spec_.order_enforce:
598605
env = OrderEnforcing(env)
599606

607+
# Add the time limit wrapper
600608
if max_episode_steps is not None:
601609
env = TimeLimit(env, max_episode_steps)
602610
elif spec_.max_episode_steps is not None:
603611
env = TimeLimit(env, spec_.max_episode_steps)
604612

613+
# Add the autoreset wrapper
605614
if autoreset:
606615
env = AutoResetWrapper(env)
607616

608-
if not disable_env_checker:
609-
try:
610-
check_env(env)
611-
except Exception as e:
612-
logger.warn(
613-
f"Env check failed with the following message: {e}\n"
614-
f"You can set `disable_env_checker=True` to disable this check."
615-
)
616-
617617
return env
618618

619619

620620
def spec(env_id: str) -> EnvSpec:
621-
"""
622-
Retrieve the spec for the given environment from the global registry.
623-
"""
621+
"""Retrieve the spec for the given environment from the global registry."""
624622
spec_ = registry.get(env_id)
625623
if spec_ is None:
626624
ns, name, version = parse_env_id(env_id)

gym/spaces/discrete.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(
3333
seed: Optionally, you can use this argument to seed the RNG that is used to sample from the ``Dict`` space.
3434
start (int): The smallest element of this space.
3535
"""
36+
assert isinstance(n, (int, np.integer))
3637
assert n > 0, "n (counts) have to be positive"
3738
assert isinstance(start, (int, np.integer))
3839
self.n = int(n)

gym/spaces/tuple.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def __init__(
3333
spaces (Iterable[Space]): The spaces that are involved in the cartesian product.
3434
seed: Optionally, you can use this argument to seed the RNGs of the ``spaces`` to ensure reproducible sampling.
3535
"""
36-
spaces = tuple(spaces)
37-
self.spaces = spaces
38-
for space in spaces:
36+
self.spaces = tuple(spaces)
37+
for space in self.spaces:
3938
assert isinstance(
4039
space, Space
4140
), "Elements of the tuple must be instances of gym.Space"

0 commit comments

Comments
 (0)