Builder

safety_gymnasium2.builder

class safety_gymnasium2.builder.Builder(task_id: str, config: dict | None = None, render_mode: str | None = None, width: int = 256, height: int = 256, camera_id: int | None = None, camera_name: str | None = None, *, use_task_time_limit: bool = True)

An entry point to organize different environments, while showing unified API for users.

The Builder class constructs the basic control framework of environments, while the details were hidden. There is another important parts, which is task module including all task specific operation.

Methods:

Attributes:

  • task_id (str): Task id.

  • config (dict): Pre-defined configuration of the environment, which is passed via safety_gymnasium2.register().

  • render_parameters (RenderConf): Render parameters.

  • action_space (gymnasium.spaces.Box): Action space.

  • observation_space (gymnasium.spaces.Dict): Observation space.

  • obs_space_dict (dict): Observation space dictionary.

  • done (bool): Whether the episode is done.

Initialize the builder.

Note

The camera_name parameter can be chosen from:
  • human: The camera used for freely moving around and can get input from keyboard real time.

  • vision: The camera used for vision observation, which is fixed in front of the agent’s head.

  • track: The camera used for tracking the agent.

  • fixednear: The camera used for top-down observation.

  • fixedfar: The camera used for top-down observation, but is further than fixednear.

Parameters:
  • task_id (str) – Task id.

  • config (dict) – Pre-defined configuration of the environment, which is passed via safety_gymnasium2.register().

  • render_mode (str) – Render mode, can be ‘human’, ‘rgb_array’, ‘depth_array’.

  • width (int) – Width of the rendered image.

  • height (int) – Height of the rendered image.

  • camera_id (int) – Camera id to render.

  • camera_name (str) – Camera name to render.

  • use_task_time_limit – Apply the task horizon when used without a time-limit wrapper.

DataClass

class safety_gymnasium2.builder.RenderConf(mode: str = None, width: int = 256, height: int = 256, camera_id: int = None, camera_name: str = None)

Render options.

Variables:
  • mode (str) – render mode, can be ‘human’, ‘rgb_array’, ‘depth_array’.

  • width (int) – width of the rendered image.

  • height (int) – height of the rendered image.

  • camera_id (int) – camera id to render.

  • camera_name (str) – camera name to render.

  • Note – camera_id and camera_name can only be set one of them.

Methods

safety_gymnasium2.builder.Builder.__init__(self, task_id: str, config: dict | None = None, render_mode: str | None = None, width: int = 256, height: int = 256, camera_id: int | None = None, camera_name: str | None = None, *, use_task_time_limit: bool = True) → None

Initialize the builder.

Note

The camera_name parameter can be chosen from:
  • human: The camera used for freely moving around and can get input from keyboard real time.

  • vision: The camera used for vision observation, which is fixed in front of the agent’s head.

  • track: The camera used for tracking the agent.

  • fixednear: The camera used for top-down observation.

  • fixedfar: The camera used for top-down observation, but is further than fixednear.

Parameters:
  • task_id (str) – Task id.

  • config (dict) – Pre-defined configuration of the environment, which is passed via safety_gymnasium2.register().

  • render_mode (str) – Render mode, can be ‘human’, ‘rgb_array’, ‘depth_array’.

  • width (int) – Width of the rendered image.

  • height (int) – Height of the rendered image.

  • camera_id (int) – Camera id to render.

  • camera_name (str) – Camera name to render.

  • use_task_time_limit – Apply the task horizon when used without a time-limit wrapper.

safety_gymnasium2.builder.Builder._setup_simulation(self) → None

Set up mujoco the simulation instance.

safety_gymnasium2.builder.Builder._get_task(self) → BaseTask

Instantiate a task object.

safety_gymnasium2.builder.Builder.set_seed(self, seed: int | None = None) → None

Set internal random state seeds.

safety_gymnasium2.builder.Builder.reset(self, *, seed: int | None = None, options: dict | None = None) → tuple[ndarray, dict]

Reset the environment and return observations.

safety_gymnasium2.builder.Builder.step(self, action: ndarray) → tuple[ndarray, float, float, bool, bool, dict]

Take a step and return observation, reward, cost, terminated, truncated, info.

safety_gymnasium2.builder.Builder._reward(self) → float

Calculate the current rewards.

Call exactly once per step.

safety_gymnasium2.builder.Builder._cost(self) → dict

Calculate the current costs and return a dict.

Call exactly once per step.

safety_gymnasium2.builder.Builder.render(self) → ndarray | None

Call underlying safety_gymnasium2.bases.underlying.Underlying.render() directly.

Width and height in parameters are constant defaults for rendering frames for humans. (not used for vision)

The set of supported modes varies per environment. (And some third-party environments may not support rendering at all.) By convention, if render_mode is:

  • None (default): no render is computed.

  • human: render return None. The environment is continuously rendered in the current display or terminal. Usually for human consumption.

  • rgb_array: return a single frame representing the current state of the environment. A frame is a numpy.ndarray with shape (x, y, 3) representing RGB values for an x-by-y pixel image.

  • rgb_array_list: return a list of frames representing the states of the environment since the last reset. Each frame is a numpy.ndarray with shape (x, y, 3), as with rgb_array.

  • depth_array: return a single frame representing the current state of the environment. A frame is a numpy.ndarray with shape (x, y) representing depth values for an x-by-y pixel image.

  • depth_array_list: return a list of frames representing the states of the environment since the last reset. Each frame is a numpy.ndarray with shape (x, y), as with depth_array.

Additional Methods

property Builder.action_space: Box

Helper to get action space.

property Builder.observation_space: Box | Dict

Helper to get observation space.

property Builder.obs_space_dict: dict[str, Box]

Helper to get observation space dictionary.

property Builder.done: bool

Whether this episode is ended.