lit_sb3.utils.env
source module lit_sb3.utils.env
Environment creation and wrapping utilities.
Functions
-
maybe_make_env — If env is a string, make the environment; otherwise, return env.
-
unwrap_wrapper — Retrieve a
VecEnvWrapperobject by recursively searching. -
is_wrapped — Check if a given environment has been wrapped with a given wrapper.
-
make_vec_env — Create a wrapped, monitored
VecEnv. -
make_atari_env — Create a wrapped, monitored VecEnv for Atari.
-
create_test_env — Create a vectorised environment for evaluating a trained agent.
source maybe_make_env(env: GymEnv | str, verbose: int) → GymEnv
If env is a string, make the environment; otherwise, return env.
Parameters
-
env : GymEnv | str — The environment to learn from.
-
verbose : int — Verbosity level: 0 for no output, 1 for indicating if environment is created :return A Gym (vector) environment.
source unwrap_wrapper(env: gym.Env, wrapper_class: type[gym.Wrapper]) → gym.Wrapper | None
Retrieve a VecEnvWrapper object by recursively searching.
Parameters
-
env : gym.Env — Environment to unwrap
-
wrapper_class : type[gym.Wrapper] — Wrapper to look for
Returns
-
gym.Wrapper | None — Environment unwrapped till
wrapper_classif it has been wrapped with it
source is_wrapped(env: gym.Env, wrapper_class: type[gym.Wrapper]) → bool
Check if a given environment has been wrapped with a given wrapper.
Parameters
-
env : gym.Env — Environment to check
-
wrapper_class : type[gym.Wrapper] — Wrapper class to look for
Returns
-
bool — True if environment has been wrapped with
wrapper_class.
source make_vec_env(env_id: str | Callable[..., gym.Env], n_envs: int = 1, seed: int | None = None, start_index: int = 0, monitor_dir: str | Path | None = None, wrapper_class: Callable[[gym.Env], gym.Env] | None = None, env_kwargs: dict[str, Any] | None = None, vec_env_cls: type[DummyVecEnv | SubprocVecEnv] | None = None, vec_env_kwargs: dict[str, Any] | None = None, monitor_kwargs: dict[str, Any] | None = None, wrapper_kwargs: dict[str, Any] | None = None) → VecEnv
Create a wrapped, monitored VecEnv.
By default it uses a DummyVecEnv which is usually faster
than a SubprocVecEnv.
Parameters
-
env_id : str | Callable[..., gym.Env] — either the env ID, the env class or a callable returning an env
-
n_envs : int — the number of environments you wish to have in parallel
-
seed : int | None — the initial seed for the random number generator
-
start_index : int — start rank index
-
monitor_dir : str | Path | None — Path to a folder where the monitor files will be saved. If None, no file will be written, however, the env will still be wrapped in a Monitor wrapper to provide additional information about training.
-
wrapper_class : Callable[[gym.Env], gym.Env] | None — Additional wrapper to use on the environment. This can also be a function with single argument that wraps the environment in many things. Note: the wrapper specified by this parameter will be applied after the
Monitorwrapper. if some cases (e.g. with TimeLimit wrapper) this can lead to undesired behavior. See here for more details: https://github.com/DLR-RM/stable-baselines3/issues/894 -
env_kwargs : dict[str, Any] | None — Optional keyword argument to pass to the env constructor
-
vec_env_cls : type[DummyVecEnv | SubprocVecEnv] | None — A custom
VecEnvclass constructor. Default: None. -
vec_env_kwargs : dict[str, Any] | None — Keyword arguments to pass to the
VecEnvclass constructor. -
monitor_kwargs : dict[str, Any] | None — Keyword arguments to pass to the
Monitorclass constructor. -
wrapper_kwargs : dict[str, Any] | None — Keyword arguments to pass to the
Wrapperclass constructor.
Returns
-
VecEnv — The wrapped environment
source make_atari_env(env_id: str | Callable[..., gym.Env], n_envs: int = 1, seed: int | None = None, start_index: int = 0, monitor_dir: str | Path | None = None, wrapper_kwargs: dict[str, Any] | None = None, env_kwargs: dict[str, Any] | None = None, vec_env_cls: type[DummyVecEnv] | type[SubprocVecEnv] | None = None, vec_env_kwargs: dict[str, Any] | None = None, monitor_kwargs: dict[str, Any] | None = None) → VecEnv
Create a wrapped, monitored VecEnv for Atari.
It is a wrapper around make_vec_env that includes common preprocessing for Atari games.
.. note::
By default, the AtariWrapper uses terminal_on_life_loss=True, which causes
env.reset() to perform a no-op step instead of truly resetting when the environment
terminates due to a loss of life (but not game over). To ensure reset() always
resets the env, pass wrapper_kwargs=dict(terminal_on_life_loss=False).
Parameters
-
env_id : str | Callable[..., gym.Env] — either the env ID, the env class or a callable returning an env
-
n_envs : int — the number of environments you wish to have in parallel
-
seed : int | None — the initial seed for the random number generator
-
start_index : int — start rank index
-
monitor_dir : str | Path | None — Path to a folder where the monitor files will be saved. If None, no file will be written, however, the env will still be wrapped in a Monitor wrapper to provide additional information about training.
-
wrapper_kwargs : dict[str, Any] | None — Optional keyword argument to pass to the
AtariWrapper -
env_kwargs : dict[str, Any] | None — Optional keyword argument to pass to the env constructor
-
vec_env_cls : type[DummyVecEnv] | type[SubprocVecEnv] | None — A custom
VecEnvclass constructor. Default: None. -
vec_env_kwargs : dict[str, Any] | None — Keyword arguments to pass to the
VecEnvclass constructor. -
monitor_kwargs : dict[str, Any] | None — Keyword arguments to pass to the
Monitorclass constructor.
Returns
-
VecEnv — The wrapped environment
source create_test_env(env_id: str, n_envs: int = 1, stats_path: str | Path | None = None, seed: int = 0, log_dir: str | Path | None = None, should_render: bool = True, hyperparams: dict[str, Any] | None = None, env_kwargs: dict[str, Any] | None = None, vec_env_cls: type[VecEnv] | None = None, vec_env_kwargs: dict[str, Any] | None = None) → VecEnv
Create a vectorised environment for evaluating a trained agent.
Parameters
-
env_id : str — Gymnasium environment ID.
-
n_envs : int — Number of parallel environments.
-
stats_path : str | Path | None — Folder containing saved
VecNormalizestats. -
seed : int — Random seed.
-
log_dir : str | Path | None — Optional directory for episode reward logging.
-
should_render : bool — Add
render_mode="human"whenTrue. -
hyperparams : dict[str, Any] | None — Extra hyperparams (e.g.
frame_stack,normalize). -
env_kwargs : dict[str, Any] | None — Keyword arguments passed to the env constructor.
-
vec_env_cls : type[VecEnv] | None — VecEnv class to use (defaults to
DummyVecEnv). -
vec_env_kwargs : dict[str, Any] | None — Keyword arguments for the VecEnv constructor.
Returns
-
Wrapped — class:
~lit_sb3.vec_env.VecEnv.
Raises
-
ValueError