dreamerx.environments module#

Environment definitions for DreamerX.

class dreamerx.environments.VectorizedEnvironment#

Bases: object

Abstract base class for vectorized environments.

abstractmethod action_sample() → ndarray#

Sample an action from the action space.

Return type:

np.ndarray

abstractmethod copy(**kwargs: dict[str, Any]) → VectorizedEnvironment#

Copy the environment, overriding parameters in kwargs.

Parameters:

kwargs (dict[str, Any]) – Additional keyword arguments for the new environment instance.

Returns:

A copy of the environment with overridden parameters.

Return type:

VectorizedEnvironment

abstractmethod render() → ndarray#

Render the environment and return a frame.

Returns:

The rendered frame.

Return type:

np.ndarray

abstractmethod reset(seed: int = None, **kwargs: dict[str, Any]) → tuple[ndarray, dict[str, Any]]#

Reset the environment.

Parameters:
  • seed (int) – The random seed for the environment. (Default: None)

  • kwargs (dict[str, Any]) – Additional keyword arguments for resetting the environment.

Returns:

A tuple containing the initial observations and additional info.

Return type:

tuple[np.ndarray, dict[str, Any]]

abstractmethod step(actions: ndarray) → tuple[ndarray, ndarray, ndarray, ndarray, dict[str, Any]]#

Perform a step in the environment.

Parameters:

actions (np.ndarray) – The actions to perform in the environment.

Returns:

A tuple containing the next observations, rewards, terminations, truncations, and additional info.

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, dict[str, Any]]

abstract property num_envs: int#

The number of environments.

Type:

int

abstract property obs_shape: ndarray#

The shape of the observation space.

Type:

np.ndarray

abstract property render_fps: int#

The number of frames per second while rendering.

Type:

int

class dreamerx.environments.VectorizedGymEnvironment(env_name: str, num_envs: int = 1, allow_rendering: bool = False, **init_kwargs: dict[str, Any])#

Bases: VectorizedEnvironment

A vectorized environment wrapper for Gymnasium environments.

__init__(env_name: str, num_envs: int = 1, allow_rendering: bool = False, **init_kwargs: dict[str, Any]) → None#

Initialize the vectorized Gymnasium environment.

Parameters:
  • env_name (str) – The name of the Gymnasium environment to create.

  • num_envs (int) – The number of parallel environments to create. (Default: 1)

  • allow_rendering (bool) – Whether to allow rendering of the environment. (Default: False)

  • init_kwargs (dict[str, Any]) – Additional keyword arguments for initializing the Gymnasium environment.

action_sample() → ndarray#

Sample an action from the action space.

Returns:

A sampled action.

Return type:

np.ndarray

copy(**kwargs: dict[str, Any]) → VectorizedGymEnvironment#

Copy the environment, overriding parameters in kwargs.

Parameters:

kwargs (dict[str, Any]) – Additional keyword arguments for the new environment instance.

Returns:

A copy of the environment with overridden parameters.

Return type:

VectorizedGymEnvironment

render() → ndarray#

Render the environment and return a frame.

Returns:

A rendered frame.

Return type:

np.ndarray

reset(seed: int = None, **kwargs: dict[str, Any]) → tuple[ndarray, dict[str, Any]]#

Reset the environment.

Parameters:
  • seed (int) – The random seed for the environment. (Default: None)

  • kwargs (dict[str, Any]) – Additional keyword arguments for resetting the environment.

Returns:

A tuple containing the initial observations and additional info.

Return type:

tuple[np.ndarray, dict[str, Any]]

step(actions: ndarray) → tuple[ndarray, ndarray, ndarray, ndarray, dict[str, Any]]#

Take a step in the environment.

Parameters:

actions (np.ndarray) – The actions to take in the environment.

Returns:

A tuple containing the next observations, rewards, dones, truncations, and additional info.

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, dict[str, Any]]

property num_envs: int#

The number of environments.

Type:

int

property obs_shape: ndarray#

The shape of the observation space.

Type:

np.ndarray

property render_fps: int#

The number of frames per second while rendering.

Type:

int

class dreamerx.environments.VectorizedRLGymEnvironment(env_name: str = 'Soccar', num_envs: int = 1, team_size: int | list[int] = 2, frame_skip: int = 8, allow_rendering: bool = False, automatic_reset: bool = True, **init_kwargs: dict[str, Any])#

Bases: VectorizedEnvironment

A vectorized environment wrapper for RLGym environments.

__init__(env_name: str = 'Soccar', num_envs: int = 1, team_size: int | list[int] = 2, frame_skip: int = 8, allow_rendering: bool = False, automatic_reset: bool = True, **init_kwargs: dict[str, Any]) → None#

Initialize the vectorized RLGym environment.

Parameters:
  • env_name (str) – The name of the RLGym environment to create, currently unused. (Default: 'Soccar')

  • num_envs (int) – The number of parallel environments to create. (Default: 1)

  • team_size (int | list[int]) – The size of both teams. Can be an int or a list of ints for each environment. (Default: 2)

  • allow_rendering (bool) – Whether to allow rendering of the environment, frame grabbing is currently unsupported in favor of live visualization. (Default: False)

  • frame_skip (int) – The number of frames to skip for each action. (Default: 8)

  • automatic_reset (bool) – Whether to automatically reset the environment after termination. (Default: True)

  • init_kwargs (dict[str, Any]) – Additional keyword arguments for initializing the RLGym environment. (Default: {})

_construct_actions(actions: ndarray) → list[dict[str, ndarray]]#

Construct a list of dicts mapping agents to one-hot actions from an array of discrete actions.

Parameters:

actions (np.ndarray) – An array of discrete actions, with shape (num_envs, num_agents).

Returns:

A list of dicts mapping agents to one-hot actions, with length num_envs.

Return type:

list[dict[str, np.ndarray]]

action_sample() → ndarray#

Sample an action from the action space.

Returns:

An array of sampled actions, with shape (num_envs, num_actions).

Return type:

np.ndarray

copy(**kwargs: dict[str, Any]) → VectorizedRLGymEnvironment#

Copy the environment, overriding parameters in kwargs.

Parameters:

kwargs (dict[str, Any]) – Additional keyword arguments for the new environment instance.

Returns:

A copy of the environment with overridden parameters.

Return type:

VectorizedRLGymEnvironment

render(delay: bool = True, speedup: float = 1.0, warn: bool = True) → ndarray#

Render the environments and return a frame.

Live visualization is currently supported, but frame grabbing is not due no support with RLViser. This method will render the first environment to the RLViser window, but will return an empty array.

Parameters:
  • delay (bool) – Whether to delay rendering to match the render FPS. (Default: True)

  • speedup (float) – A factor to speed up rendering, applied to the delay. (Default: 1.0)

  • warn (bool) – Whether to display warnings on long computations. (Default: True)

Returns:

An array of rendered frames, with shape (num_envs, height, width, channels).

Return type:

np.ndarray

reset(seed: int = None, idx: int = None, **kwargs: dict[str, Any]) → tuple[ndarray, dict[str, Any]]#

Reset the environments.

Parameters:
  • seed (int) – An optional seed for the environments, currently unsupported. (Default: None)

  • idx (int) – An optional index to specify which environment to reset. (Default: None)

  • kwargs (dict[str, Any]) – Additional keyword arguments to pass to the environment reset method.

Returns:

A tuple containing the initial observations and an empty info dictionary.

Return type:

tuple[np.ndarray, dict[str, Any]]

step(actions: ndarray) → tuple[ndarray, ndarray, ndarray, ndarray, dict[str, Any]]#

Take a step in the environments.

Parameters:

actions (np.ndarray) – An array of actions to take in the environments, with shape (num_envs, num_agents).

Returns:

A tuple containing the next observations, rewards, terminations, truncations, and an empty info dictionary.

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, dict[str, Any]]

property num_envs: int#

The number of environments.

Type:

int

property obs_shape: ndarray#

The shape of the observation space.

Type:

np.ndarray

property render_fps: int#

The number of frames per second while rendering.

Type:

int

dreamerx.environments.register_env(env_id: str, env_class: type[VectorizedEnvironment]) → None#

Register a new environment class with a string identifier.

Parameters:
  • env_id (str) – The string identifier for the environment.

  • env_class (type[VectorizedEnvironment]) – The environment class to register, which must be a subclass of VectorizedEnvironment.