Skip to content

lit_sb3.vec_env.base_vec_env

source module lit_sb3.vec_env.base_vec_env

Classes

  • VecEnv — An abstract asynchronous, vectorized environment.

  • VecEnvWrapper — Vectorized environment base class.

  • CloudpickleWrapper — Uses cloudpickle to serialize contents (otherwise multiprocessing tries to use pickle).

Functions

  • tile_images — Tile N images into one big PxQ image.

source tile_images(images_nhwc: Sequence[np.ndarray]) → np.ndarray

Tile N images into one big PxQ image.

(P,Q) are chosen to be as close as possible, and if N is square, then P=Q.

Parameters

  • images_nhwc : Sequence[np.ndarray] — list or array of images, ndim=4 once turned into array. n = batch index, h = height, w = width, c = channel

Returns

  • np.ndarray — img_HWc, ndim=3

source class VecEnv(num_envs: int, observation_space: spaces.Space, action_space: spaces.Space)

Bases : ABC

An abstract asynchronous, vectorized environment.

Parameters

  • num_envs : int — Number of environments

  • observation_space : spaces.Space — Observation space

  • action_space : spaces.Space — Action space

Methods

  • reset — Reset all the environments and return an array of.

  • step_async — Tell all the environments to start taking a step.

  • step_wait — Wait for the step taken with step_async().

  • close — Clean up the environment's resources.

  • has_attr — Check if an attribute exists for a vectorized environment.

  • get_attr — Return attribute from vectorized environment.

  • set_attr — Set attribute inside vectorized environments.

  • env_method — Call instance methods of vectorized environments.

  • env_is_wrapped — Check if environments are wrapped with a given wrapper.

  • step — Step the environments with the given action.

  • get_images — Return RGB images from each environment when available.

  • render — Gym environment rendering.

  • seed — Sets the random seeds for all environments, based on a given seed.

  • set_options — Set environment options for all environments.

  • getattr_depth_check — Check if an attribute reference is being hidden in a recursive call to getattr.

source method VecEnv.reset() → VecEnvObs

Reset all the environments and return an array of.

observations, or a tuple of observation arrays.

If step_async is still doing work, that work will be cancelled and step_wait() should not be called until step_async() is invoked again.

Returns

  • VecEnvObs — observation

Raises

  • NotImplementedError

source method VecEnv.step_async(actions: np.ndarray) → None

Tell all the environments to start taking a step.

with the given actions. Call step_wait() to get the results of the step.

You should not call this if a step_async run is already pending.

Raises

  • NotImplementedError

source method VecEnv.step_wait() → VecEnvStepReturn

Wait for the step taken with step_async().

Returns

  • VecEnvStepReturn — observation, reward, done, information

Raises

  • NotImplementedError

source method VecEnv.close() → None

Clean up the environment's resources.

Raises

  • NotImplementedError

source method VecEnv.has_attr(attr_name: str) → bool

Check if an attribute exists for a vectorized environment.

Parameters

  • attr_name : str — The name of the attribute to check

Returns

  • bool — True if 'attr_name' exists in all environments

source method VecEnv.get_attr(attr_name: str, indices: VecEnvIndices = None) → list[Any]

Return attribute from vectorized environment.

Parameters

  • attr_name : str — The name of the attribute whose value to return

  • indices : VecEnvIndices — Indices of envs to get attribute from

Returns

  • list[Any] — List of values of 'attr_name' in all environments

Raises

  • NotImplementedError

source method VecEnv.set_attr(attr_name: str, value: Any, indices: VecEnvIndices = None) → None

Set attribute inside vectorized environments.

Parameters

  • attr_name : str — The name of attribute to assign new value

  • value : Any — Value to assign to attr_name

  • indices : VecEnvIndices — Indices of envs to assign value

Raises

  • NotImplementedError

source method VecEnv.env_method(method_name: str, *method_args, indices: VecEnvIndices = None, **method_kwargs) → list[Any]

Call instance methods of vectorized environments.

Parameters

  • method_name : str — The name of the environment method to invoke.

  • indices : VecEnvIndices — Indices of envs whose method to call

  • method_args — Any positional arguments to provide in the call

  • method_kwargs — Any keyword arguments to provide in the call

Returns

  • list[Any] — List of items returned by the environment's method call

Raises

  • NotImplementedError

source method VecEnv.env_is_wrapped(wrapper_class: type[gym.Wrapper], indices: VecEnvIndices = None) → list[bool]

Check if environments are wrapped with a given wrapper.

Parameters

  • method_name — The name of the environment method to invoke.

  • indices : VecEnvIndices — Indices of envs whose method to call

  • method_args — Any positional arguments to provide in the call

  • method_kwargs — Any keyword arguments to provide in the call

  • wrapper_class : type[gym.Wrapper] — The wrapper class parameter.

Returns

  • list[bool] — True if the env is wrapped, False otherwise, for each env queried.

Raises

  • NotImplementedError

source method VecEnv.step(actions: np.ndarray) → VecEnvStepReturn

Step the environments with the given action.

Parameters

  • actions : np.ndarray — the action

Returns

  • VecEnvStepReturn — observation, reward, done, information

source method VecEnv.get_images() → Sequence[np.ndarray | None]

Return RGB images from each environment when available.

Raises

  • NotImplementedError

source method VecEnv.render(mode: str | None = None) → np.ndarray | None

Gym environment rendering.

Parameters

  • mode : str | None — the rendering type

source method VecEnv.seed(seed: int | None = None) → Sequence[None | int]

Sets the random seeds for all environments, based on a given seed.

Each individual environment will still get its own seed, by incrementing the given seed. WARNING: since gym 0.26, those seeds will only be passed to the environment at the next reset.

Parameters

  • seed : int | None — The random seed. May be None for completely random seeding.

Returns

  • Sequence[None | int] — Returns a list containing the seeds for each individual env. Note that all list elements may be None, if the env does not return anything when being seeded.

source method VecEnv.set_options(options: list[dict] | dict | None = None) → None

Set environment options for all environments.

If a dict is passed instead of a list, the same options will be used for all environments. WARNING: Those options will only be passed to the environment at the next reset.

Parameters

  • options : list[dict] | dict | None — A dictionary of environment options to pass to each environment at the next reset.

source property VecEnv.unwrapped: VecEnv

source method VecEnv.getattr_depth_check(name: str, already_found: bool) → str | None

Check if an attribute reference is being hidden in a recursive call to getattr.

Parameters

  • name : str — name of attribute to check for

  • already_found : bool — whether this attribute has already been found in a wrapper

Returns

  • str | None — name of module whose attribute is being shadowed, if any.

source class VecEnvWrapper(venv: VecEnv, observation_space: spaces.Space | None = None, action_space: spaces.Space | None = None)

Bases : VecEnv

Vectorized environment base class.

Parameters

  • venv : VecEnv — the vectorized environment to wrap

  • observation_space : spaces.Space | None — the observation space (can be None to load from venv)

  • action_space : spaces.Space | None — the action space (can be None to load from venv)

Methods

source method VecEnvWrapper.step_async(actions: np.ndarray) → None

source method VecEnvWrapper.reset() → VecEnvObs

source method VecEnvWrapper.step_wait() → VecEnvStepReturn

source method VecEnvWrapper.seed(seed: int | None = None) → Sequence[None | int]

source method VecEnvWrapper.set_options(options: list[dict] | dict | None = None) → None

source method VecEnvWrapper.close() → None

source method VecEnvWrapper.render(mode: str | None = None) → np.ndarray | None

source method VecEnvWrapper.get_images() → Sequence[np.ndarray | None]

source method VecEnvWrapper.has_attr(attr_name: str) → bool

source method VecEnvWrapper.get_attr(attr_name: str, indices: VecEnvIndices = None) → list[Any]

source method VecEnvWrapper.set_attr(attr_name: str, value: Any, indices: VecEnvIndices = None) → None

source method VecEnvWrapper.env_method(method_name: str, *method_args, indices: VecEnvIndices = None, **method_kwargs) → list[Any]

source method VecEnvWrapper.env_is_wrapped(wrapper_class: type[gym.Wrapper], indices: VecEnvIndices = None) → list[bool]

source method VecEnvWrapper.getattr_recursive(name: str) → Any

Recursively check wrappers to find attribute.

Parameters

  • name : str — name of attribute to look for

Returns

  • Any — attribute

source method VecEnvWrapper.getattr_depth_check(name: str, already_found: bool) → str | None

See base class.

Returns

  • str | None — name of module whose attribute is being shadowed, if any.

source class CloudpickleWrapper(var: Any)

Uses cloudpickle to serialize contents (otherwise multiprocessing tries to use pickle).

Parameters

  • var : Any — the variable you wish to wrap for pickling with cloudpickle