OmniSafe Core Environment#

CMDP(env_id, **kwargs)

The core class of the environment.

Wrapper(env[, device])

The wrapper class of the environment.

EnvRegister()

The environment register.

CMDP#

Documentation

class omnisafe.envs.core.CMDP(env_id, **kwargs)[source]#

The core class of the environment.

The CMDP class is the core class of the environment. It defines the basic interface of the environment. The environment should inherit from this class and implement the abstract methods.

Variables:
  • need_time_limit_wrapper (bool) – Whether the environment need time limit wrapper.

  • need_auto_reset_wrapper (bool) – Whether the environment need auto reset wrapper.

Initialize an instance of CMDP.

property action_space: Box | Discrete#

The action space of the environment.

abstract close()[source]#

Close the environment.

Return type:

None

property max_episode_steps: int | None#

The max steps per episode.

property metadata: dict[str, Any]#

The metadata of the environment.

property num_envs: int#

The number of parallel environments.

property observation_space: Box | Discrete#

The observation space of the environment.

abstract render()[source]#

Compute the render frames as specified by render_mode during the initialization of the environment.

Returns:

The render frames – we recommend to use np.ndarray which could construct video by moviepy.

Return type:

Any

abstract reset(seed=None, options=None)[source]#

Reset the environment and returns an initial observation.

Parameters:
  • seed (int, optional) – The random seed. Defaults to None.

  • options (dict[str, Any], optional) – The options for the environment. Defaults to None.

Returns:
  • observation – The initial observation of the space.

  • info – Some information logged by the environment.

Return type:

tuple[torch.Tensor, dict[str, Any]]

save()[source]#

Save the important components of the environment. :rtype: dict[str, Module]

Note

The saved components will be stored in the wrapped environment. If the environment is not wrapped, the saved components will be empty dict. common wrappers are obs_normalize, reward_normalize, and cost_normalize.

Returns:

The saved components.

abstract set_seed(seed)[source]#

Set the seed for this env’s random number generator(s).

Parameters:

seed (int) – The seed to use.

Return type:

None

abstract step(action)[source]#

Run one timestep of the environment’s dynamics using the agent actions.

Parameters:

action (torch.Tensor) – The action from the agent or random.

Returns:
  • observation – The agent’s observation of the current environment.

  • reward – The amount of reward returned after previous action.

  • cost – The amount of cost returned after previous action.

  • terminated – Whether the episode has ended.

  • truncated – Whether the episode has been truncated due to a time limit.

  • info – Some information logged by the environment.

Return type:

tuple[Tensor, Tensor, Tensor, Tensor, Tensor, dict[str, Any]]

classmethod support_envs()[source]#

The supported environments.

Returns:

The supported environments.

Return type:

list[str]

property time_limit: int | None#

The time limit of the environment.

Wrapper#

Documentation

class omnisafe.envs.core.Wrapper(env, device=DEVICE_CPU)[source]#

The wrapper class of the environment.

The Wrapper class is the wrapper class of the environment. It defines the basic interface of the environment wrapper. The environment wrapper should inherit from this class and implement the abstract methods.

Parameters:
  • env (CMDP) – The environment.

  • device (torch.device) – The device to use. Defaults to torch.device('cpu').

Variables:

_env (CMDP) – The environment.

Initialize an instance of Wrapper.

close()[source]#

Close the environment.

Return type:

None

render()[source]#

Compute the render frames as specified by render_mode during the initialization of the environment.

Returns:

The render frames – we recommend to use np.ndarray which could construct video by moviepy.

Return type:

Any

reset(seed=None, options=None)[source]#

Reset the environment and returns an initial observation.

Parameters:
  • seed (int, optional) – The random seed. Defaults to None.

  • options (dict[str, Any], optional) – The options for the environment. Defaults to None.

Returns:
  • observation – The initial observation of the space.

  • info – Some information logged by the environment.

Return type:

tuple[torch.Tensor, dict[str, Any]]

save()[source]#

Save the important components of the environment. :rtype: dict[str, Module]

Note

The saved components will be stored in the wrapped environment. If the environment is not wrapped, the saved components will be empty dict. common wrappers are obs_normalize, reward_normalize, and cost_normalize.

Returns:

The saved components.

set_seed(seed)[source]#

Set the seed for this env’s random number generator(s).

Parameters:

seed (int) – The random seed to use.

Return type:

None

step(action)[source]#

Run one timestep of the environment’s dynamics using the agent actions.

Parameters:

action (torch.Tensor) – The action from the agent or random.

Returns:
  • observation – The agent’s observation of the current environment.

  • reward – The amount of reward returned after previous action.

  • cost – The amount of cost returned after previous action.

  • terminated – Whether the episode has ended.

  • truncated – Whether the episode has been truncated due to a time limit.

  • info – Some information logged by the environment.

Return type:

tuple[Tensor, Tensor, Tensor, Tensor, Tensor, dict[str, Any]]

Make an Environment#

Documentation

class omnisafe.envs.core.EnvRegister[source]#

The environment register.

The EnvRegister is used to register the environment class. It provides the method to get the environment class by the environment id.

Examples

>>> from omnisafe.envs.core import env_register
>>> from cunstom_env import CustomEnv
>>> @env_register
... class CustomEnv:
...     ...

Initialize an instance of EnvRegister.

_register(env_class)[source]#

Register the environment class.

Parameters:

env_class (type[CMDP]) – The environment class.

Return type:

None

get_class(env_id, class_name)[source]#

Get the environment class.

Parameters:
  • env_id (str) – The environment id.

  • class_name (str or None) – The environment class name.

Returns:

The environment class.

Return type:

type[CMDP]

register(env_class)[source]#

Register the environment class.

Parameters:

env_class (type[CMDP]) – The environment class.

Returns:

The environment class.

Return type:

type[CMDP]

support_envs()[source]#

The supported environments.

Returns:

The supported environments.

Return type:

list[str]

unregister(env_class)[source]#

Remove the environment from the register.

Parameters:

env_class (type[CMDP]) – The environment class.

Return type:

type[CMDP]

omnisafe.envs.core.make(env_id, class_name=None, **kwargs)[source]#

Create an environment.

Parameters:
  • env_id (str) – The environment id.

  • class_name (str or None) – The environment class name.

Keyword Arguments:
  • render_mode (str, optional) – The render mode ranges from ‘human’ to ‘rgb_array’ and ‘rgb_array_list’. Defaults to ‘rgb_array’.

  • camera_name (str, optional) – The camera name.

  • camera_id (int, optional) – The camera id.

  • width (int, optional) – The width of the rendered image. Defaults to 256.

  • height (int, optional) – The height of the rendered image. Defaults to 256.

Returns:

The environment class.

Return type:

CMDP