MCPcopy
hub / github.com/DLR-RM/stable-baselines3 / check_env

Function check_env

stable_baselines3/common/env_checker.py:467–547  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

465
466
467def 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

Callers 15

test_envFunction · 0.90
test_envFunction · 0.90
test_check_env_oneofFunction · 0.90
test_envFunction · 0.90
test_envFunction · 0.90
test_envFunction · 0.90

Calls 9

check_for_nested_spacesFunction · 0.90
_check_spacesFunction · 0.85
_check_box_obsFunction · 0.85
_check_returned_valuesFunction · 0.85
_check_renderFunction · 0.85
_check_nanFunction · 0.85
warnMethod · 0.80
resetMethod · 0.45

Tested by 15

test_envFunction · 0.72
test_envFunction · 0.72
test_check_env_oneofFunction · 0.72
test_envFunction · 0.72
test_envFunction · 0.72
test_envFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…