Check that an environment follows Gym API. This is particularly useful when using a custom environment. Please take a look at https://gymnasium.farama.org/api/env/ for more information about the API. It also optionally check that the environment is compatible with Stable-Baseli
(env: gym.Env, warn: bool = True, skip_render_check: bool = True)
| 465 | |
| 466 | |
| 467 | def check_env(env: gym.Env, warn: bool = True, skip_render_check: bool = True) -> None: |
| 468 | """ |
| 469 | Check that an environment follows Gym API. |
| 470 | This is particularly useful when using a custom environment. |
| 471 | Please take a look at https://gymnasium.farama.org/api/env/ |
| 472 | for more information about the API. |
| 473 | |
| 474 | It also optionally check that the environment is compatible with Stable-Baselines. |
| 475 | |
| 476 | :param env: The Gym environment that will be checked |
| 477 | :param warn: Whether to output additional warnings |
| 478 | mainly related to the interaction with Stable Baselines |
| 479 | :param skip_render_check: Whether to skip the checks for the render method. |
| 480 | True by default (useful for the CI) |
| 481 | """ |
| 482 | assert isinstance( |
| 483 | env, gym.Env |
| 484 | ), "Your environment must inherit from the gymnasium.Env class cf. https://gymnasium.farama.org/api/env/" |
| 485 | |
| 486 | # ============= Check the spaces (observation and action) ================ |
| 487 | _check_spaces(env) |
| 488 | |
| 489 | # Define aliases for convenience |
| 490 | observation_space = env.observation_space |
| 491 | action_space = env.action_space |
| 492 | |
| 493 | try: |
| 494 | env.reset(seed=0) |
| 495 | except TypeError as e: |
| 496 | raise TypeError("The reset() method must accept a `seed` parameter") from e |
| 497 | |
| 498 | # Warn the user if needed. |
| 499 | # A warning means that the environment may run but not work properly with Stable Baselines algorithms |
| 500 | should_skip = False |
| 501 | if warn: |
| 502 | should_skip = _check_unsupported_spaces(env, observation_space, action_space) |
| 503 | |
| 504 | obs_spaces = observation_space.spaces if isinstance(observation_space, spaces.Dict) else {"": observation_space} |
| 505 | for key, space in obs_spaces.items(): |
| 506 | if isinstance(space, spaces.Box): |
| 507 | _check_box_obs(space, key) |
| 508 | |
| 509 | # Check for the action space, it may lead to hard-to-debug issues |
| 510 | if isinstance(action_space, spaces.Box) and ( |
| 511 | np.any(np.abs(action_space.low) != np.abs(action_space.high)) |
| 512 | or np.any(action_space.low != -1) |
| 513 | or np.any(action_space.high != 1) |
| 514 | ): |
| 515 | warnings.warn( |
| 516 | "We recommend you to use a symmetric and normalized Box action space (range=[-1, 1]) " |
| 517 | "cf. https://stable-baselines3.readthedocs.io/en/master/guide/rl_tips.html" |
| 518 | ) |
| 519 | |
| 520 | if isinstance(action_space, spaces.Box): |
| 521 | assert np.all( |
| 522 | np.isfinite(np.array([action_space.low, action_space.high])) |
| 523 | ), "Continuous action space must have a finite lower and upper bound" |
| 524 |
searching dependent graphs…