|
23 | 23 | import numpy as np
|
24 | 24 |
|
25 | 25 | from gym.envs.__relocated__ import internal_env_relocation_map
|
26 |
| -from gym.utils.env_checker import check_env |
27 | 26 | from gym.wrappers import AutoResetWrapper, OrderEnforcing, TimeLimit
|
| 27 | +from gym.wrappers.env_checker import PassiveEnvChecker |
28 | 28 |
|
29 | 29 | if sys.version_info < (3, 10):
|
30 | 30 | import importlib_metadata as metadata # type: ignore
|
|
43 | 43 | )
|
44 | 44 |
|
45 | 45 |
|
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 | + """ |
47 | 55 | mod_name, attr_name = name.split(":")
|
48 | 56 | mod = importlib.import_module(mod_name)
|
49 | 57 | fn = getattr(mod, attr_name)
|
@@ -519,11 +527,6 @@ def make(
|
519 | 527 | ) -> Env:
|
520 | 528 | """Create an environment according to the given ID.
|
521 | 529 |
|
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 |
| -
|
527 | 530 | Args:
|
528 | 531 | id: Name of the environment. Optionally, a module to import can be included, eg. 'module:Env-v0'
|
529 | 532 | max_episode_steps: Maximum length of an episode (TimeLimit wrapper).
|
@@ -578,49 +581,44 @@ def make(
|
578 | 581 | _kwargs = spec_.kwargs.copy()
|
579 | 582 | _kwargs.update(kwargs)
|
580 | 583 |
|
581 |
| - # TODO: add a minimal env checker on initialization |
582 | 584 | if spec_.entry_point is None:
|
583 | 585 | raise error.Error(f"{spec_.id} registered but entry_point is not specified")
|
584 | 586 | elif callable(spec_.entry_point):
|
585 |
| - cls = spec_.entry_point |
| 587 | + env_creator = spec_.entry_point |
586 | 588 | else:
|
587 | 589 | # Assume it's a string
|
588 |
| - cls = load(spec_.entry_point) |
| 590 | + env_creator = load(spec_.entry_point) |
589 | 591 |
|
590 |
| - env = cls(**_kwargs) |
| 592 | + env = env_creator(**_kwargs) |
591 | 593 |
|
| 594 | + # Copies the environment creation specification and kwargs to add to the environment specification details |
592 | 595 | spec_ = copy.deepcopy(spec_)
|
593 | 596 | spec_.kwargs = _kwargs
|
594 |
| - |
595 | 597 | env.unwrapped.spec = spec_
|
596 | 598 |
|
| 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 |
597 | 604 | if spec_.order_enforce:
|
598 | 605 | env = OrderEnforcing(env)
|
599 | 606 |
|
| 607 | + # Add the time limit wrapper |
600 | 608 | if max_episode_steps is not None:
|
601 | 609 | env = TimeLimit(env, max_episode_steps)
|
602 | 610 | elif spec_.max_episode_steps is not None:
|
603 | 611 | env = TimeLimit(env, spec_.max_episode_steps)
|
604 | 612 |
|
| 613 | + # Add the autoreset wrapper |
605 | 614 | if autoreset:
|
606 | 615 | env = AutoResetWrapper(env)
|
607 | 616 |
|
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 |
| - |
617 | 617 | return env
|
618 | 618 |
|
619 | 619 |
|
620 | 620 | 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.""" |
624 | 622 | spec_ = registry.get(env_id)
|
625 | 623 | if spec_ is None:
|
626 | 624 | ns, name, version = parse_env_id(env_id)
|
|
0 commit comments