Skip to content

unienv_interface.env_base.env

Classes:

Name Description
Env

Stateful environment interface used throughout UniEnv.

Attributes:

Name Type Description
ContextType
ObsType
ActType
RenderFrame

ContextType module-attribute

ContextType = TypeVar('ContextType')

ObsType module-attribute

ObsType = TypeVar('ObsType')

ActType module-attribute

ActType = TypeVar('ActType')

RenderFrame module-attribute

RenderFrame = TypeVar('RenderFrame')

Env

Bases: ABC, Generic[BArrayType, ContextType, ObsType, ActType, RenderFrame, BDeviceType, BDtypeType, BRNGType]

Stateful environment interface used throughout UniEnv.

An Env exposes typed action, observation, and optional context spaces plus the standard reset/step/render lifecycle. Concrete implementations may be single-instance or batched depending on batch_size.

Batched-environment space invariant

The observation_space, action_space, and context_space (when present) always describe the data that the environment actually returns and accepts – including any batch dimension.

  • batch_size is None → the environment is unbatched. The spaces describe single-instance values (no leading batch axis).
  • batch_size == N (including N == 1) → the environment is batched with a leading batch dimension of size N. The spaces already include that leading batch dimension. Callers, wrappers, and downstream code must not apply an additional batch_space (or equivalent) on top of these spaces – doing so would double-batch and produce incorrect shapes.

This invariant matters whenever code builds source/target spaces from the env (e.g. replay-buffer builders, data transformations, or wrappers that unbatch per-slot data): always use the env-side spaces as-is and only unbatch/slice the data, never re-batch the space.

Methods:

Name Description
step

Advance the environment by one control step.

reset

Resets the environment to its initial state and returns the initial context and observation.

render

Render the current environment state using the configured render mode.

close
sample_space

Sample from space using and updating self.rng.

sample_action

Sample one action from action_space.

sample_observation

Sample one observation from observation_space.

sample_context

Sample one context value if context_space is defined.

update_observation_post_reset

Merge masked reset observations back into a full batched observation.

update_context_post_reset

Merge masked reset contexts back into a full batched context.

has_wrapper_attr

Checks if the attribute name exists in the environment.

get_wrapper_attr

Gets the attribute name from the environment.

set_wrapper_attr

Sets the attribute name on the environment with value.

Attributes:

Name Type Description
metadata dict[str, Any]
render_mode Optional[str]
render_fps Optional[int]
backend ComputeBackend[BArrayType, BDeviceType, BDtypeType, BRNGType]
device Optional[BDeviceType]
batch_size Optional[int]
action_space Space[ActType, BDeviceType, BDtypeType, BRNGType]
observation_space Space[ObsType, BDeviceType, BDtypeType, BRNGType]
context_space Optional[Space[ContextType, BDeviceType, BDtypeType, BRNGType]]
rng BRNGType
unwrapped Env
prev_wrapper_layer Optional[Env]

metadata class-attribute instance-attribute

metadata: dict[str, Any] = {'render_modes': []}

render_mode class-attribute instance-attribute

render_mode: Optional[str] = None

render_fps class-attribute instance-attribute

render_fps: Optional[int] = None

backend instance-attribute

backend: ComputeBackend[BArrayType, BDeviceType, BDtypeType, BRNGType]

device instance-attribute

device: Optional[BDeviceType]

batch_size class-attribute instance-attribute

batch_size: Optional[int] = None

action_space instance-attribute

action_space: Space[ActType, BDeviceType, BDtypeType, BRNGType]

observation_space instance-attribute

observation_space: Space[ObsType, BDeviceType, BDtypeType, BRNGType]

context_space class-attribute instance-attribute

context_space: Optional[Space[ContextType, BDeviceType, BDtypeType, BRNGType]] = None

rng class-attribute instance-attribute

rng: BRNGType = None

unwrapped property

unwrapped: Env

prev_wrapper_layer property

prev_wrapper_layer: Optional[Env]

step abstractmethod

step(
    action: ActType,
) -> Tuple[
    ObsType,
    Union[SupportsFloat, BArrayType],
    Union[bool, BArrayType],
    Union[bool, BArrayType],
    Dict[str, Any],
]

Advance the environment by one control step.

reset abstractmethod

reset(
    *, mask: Optional[BArrayType] = None, seed: Optional[int] = None, **kwargs
) -> Tuple[ContextType, ObsType, Dict[str, Any]]

Resets the environment to its initial state and returns the initial context and observation. If mask is provided, it will only return the masked context and observation, so the batch dimension in the output will not be same as the batch dimension in the context and observation spaces.

render abstractmethod

render() -> RenderFrame | Sequence[RenderFrame] | None

Render the current environment state using the configured render mode.

close

close()

sample_space

sample_space(space: Space) -> Any

Sample from space using and updating self.rng.

sample_action

sample_action() -> ActType

Sample one action from action_space.

sample_observation

sample_observation() -> ObsType

Sample one observation from observation_space.

sample_context

sample_context() -> Optional[ContextType]

Sample one context value if context_space is defined.

update_observation_post_reset

update_observation_post_reset(
    old_obs: ObsType, newobs_masked: ObsType, mask: BArrayType
) -> ObsType

Merge masked reset observations back into a full batched observation.

update_context_post_reset

update_context_post_reset(
    old_context: ContextType, new_context: ContextType, mask: BArrayType
) -> ContextType

Merge masked reset contexts back into a full batched context.

has_wrapper_attr

has_wrapper_attr(name: str) -> bool

Checks if the attribute name exists in the environment.

get_wrapper_attr

get_wrapper_attr(name: str) -> Any

Gets the attribute name from the environment.

set_wrapper_attr

set_wrapper_attr(name: str, value: Any)

Sets the attribute name on the environment with value.