Implementation:Farama Foundation Gymnasium MuJoCo Utils
| Knowledge Sources | |
|---|---|
| Domains | Reinforcement_Learning, MuJoCo_Environments |
| Last Updated | 2026-02-15 03:00 GMT |
Overview
A set of MujocoEnv-related utility functions provided by Gymnasium, primarily used for testing and state management purposes.
Description
The MuJoCo utilities module provides three functions for working with MuJoCo environment state: get_state retrieves the full physics state of an environment, set_state restores a previously captured state, and check_mujoco_reset_state verifies that environment reset is deterministic and unaffected by previous steps. These utilities require mujoco >= 2.3.6 and leverage the mjtState API for precise state serialization. The state type can be configured to capture either the full physics state (mjSTATE_FULLPHYSICS) or just the integration state (mjSTATE_INTEGRATION), which is useful for distinguishing between training and validation use cases.
Usage
Use these utilities for testing MuJoCo environments to ensure deterministic behavior, for implementing state save/restore functionality in planning algorithms, or for debugging environment state issues. The check_mujoco_reset_state function is particularly useful for validating that custom MuJoCo environments properly reset their state.
Code Reference
Source Location
- Repository: Farama_Foundation_Gymnasium
- File: gymnasium/envs/mujoco/utils.py
Signature
def get_state(
env: gymnasium.envs.mujoco.MujocoEnv,
state_type: mujoco.mjtState = mujoco.mjtState.mjSTATE_FULLPHYSICS,
) -> np.ndarray
def set_state(
env: gymnasium.envs.mujoco.MujocoEnv,
state: np.ndarray,
state_type: mujoco.mjtState = mujoco.mjtState.mjSTATE_FULLPHYSICS,
) -> np.ndarray
def check_mujoco_reset_state(
env: gymnasium.envs.mujoco.MujocoEnv,
seed=1234,
state_type: mujoco.mjtState = mujoco.mjtState.mjSTATE_INTEGRATION,
) -> None
Import
from gymnasium.envs.mujoco.utils import get_state, set_state, check_mujoco_reset_state
I/O Contract
get_state
| Name | Type | Required | Description |
|---|---|---|---|
| env | gymnasium.envs.mujoco.MujocoEnv | Yes | The MuJoCo environment whose state to retrieve |
| state_type | mujoco.mjtState | No | Type of state to capture (default: mjSTATE_FULLPHYSICS) |
Returns: np.ndarray containing the serialized state.
set_state
| Name | Type | Required | Description |
|---|---|---|---|
| env | gymnasium.envs.mujoco.MujocoEnv | Yes | The MuJoCo environment whose state to set |
| state | np.ndarray | Yes | State array previously generated from get_state |
| state_type | mujoco.mjtState | No | Type of state to restore (default: mjSTATE_FULLPHYSICS) |
Returns: np.ndarray of the state that was set.
check_mujoco_reset_state
| Name | Type | Required | Description |
|---|---|---|---|
| env | gymnasium.envs.mujoco.MujocoEnv | Yes | The MuJoCo environment to test |
| seed | int | No | Seed used for env.reset() (default: 1234) |
| state_type | mujoco.mjtState | No | Type of state to compare (default: mjSTATE_INTEGRATION) |
Returns: None. Raises AssertionError if reset is not deterministic.
Usage Examples
import gymnasium as gym
from gymnasium.envs.mujoco.utils import get_state, set_state, check_mujoco_reset_state
# Save and restore state
env = gym.make("Ant-v5")
observation, info = env.reset(seed=42)
state = get_state(env)
observation, reward, terminated, truncated, info = env.step(env.action_space.sample())
# Restore the previous state
set_state(env, state)
# Verify deterministic reset
check_mujoco_reset_state(env)
env.close()