Skip to main content

MJX Safety Gym Environments

Project description

mjx-safety-gym

Open-source MJX implementation of OpenAI Safety Gym for accelerated safe reinforcement learning.
Provides lightweight safety environments with JAX + MuJoCo that can run both interactively (for visualization and debugging) or fully on GPU (for large-scale RL training).

This codebase is modeled after DeepMind’s mujoco_playground. You can use it in a similar way — for example, by creating a Brax wrapper around the environments and training them directly with Brax.


Installation

This package requires Python 3.11 or above.

You can install it in two ways:

Option 1 — Local development (from source)

# Create and activate a virtual environment with Python ≥3.11
python -m venv .venv
source .venv/bin/activate  # (Windows: .venv\Scripts\activate)

# Install mjx-safety-gym in editable mode
pip install -e .

Option 2 - Direct Install from Pypi

pip install mjx-safety-gym

How to Use

For now, we have only implemented the simple Go-To-Goal environment.

Most users will want to JIT-compile and vectorize (vmap) the environment’s reset and step functions in their training pipelines, allowing them to scale to thousands of parallel environments on GPU/TPU.

Quick Start

Verify your install by creating, resetting, and stepping an environment:

from mjx_safety_gym.envs.go_to_goal import GoToGoal
import jax
from jax import numpy as jp

# Create environment
env = GoToGoal()
rng = jax.random.PRNGKey(0)

# Reset environment
rng, rng_reset = jax.random.split(rng)
state = env.reset(rng_reset)
print("Initial observation shape:", state.obs.shape)

# Step environment once with zero action
action = jp.zeros((2,))
state = env.step(state, action)
print("Next reward:", state.reward)

Interactive Viewer

Alternatively, the repository includes an interactive viewer (scripts/interactive.py) that lets you manually control an agent with keyboard input (the agent is controlled by the arrow keys).

For MacOS, we need special privileges to capture keyboard input and run the interactive viewer

sudo mjpython scripts/interactive.py

Otherwise, simply run

python scripts/interactive.py

Madrona

This repository could work for vision-based observations (included, but untested). For this, we need to install Madrona.

Madrona can be installed on the ETH Zurich cluster as follows:

chmod +x vision_setup.bash
./vision_setup.bash

Other users can inspect it to see the dependencies required for vision-based support. Setup requires Linux with an NVIDIA GPU and may take several minutes.

Repository Structure

mjx-safety-gym/
├── mjx_safety_gym
│   ├── __init__.py              # Package entry
│   ├── collision.py             # Collision handling
│   ├── envs/
│   │   ├── go_to_goal.py        # Example environment
│   │   └── xmls/                # MuJoCo XML models
│   │       └── point.xml
│   ├── lidar.py                 # Lidar sensor simulation
│   ├── mjx_env.py               # Core MJX environment wrapper
│   └── world.py                 # World generation
├── scripts/
│   └── interactive.py           # Interactive viewer (keyboard control)
├── vision_setup.bash            # Vision-based setup (ETH Euler cluster specific)
├── pyproject.toml               # Build + metadata
├── LICENSE
└── README.md

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mjx_safety_gym-0.1.5.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mjx_safety_gym-0.1.5-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file mjx_safety_gym-0.1.5.tar.gz.

File metadata

  • Download URL: mjx_safety_gym-0.1.5.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for mjx_safety_gym-0.1.5.tar.gz
Algorithm Hash digest
SHA256 dfa75adf0eb5b62baee7d0ead357ad56f946782c0efd07635bfa1f5816d067d0
MD5 38f2c025e5794070502cbf5f7edb5dcd
BLAKE2b-256 41f7272823d7b8887b1b9fd93bf5dcc02f50de154e860c7ca808d22e374e8098

See more details on using hashes here.

File details

Details for the file mjx_safety_gym-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: mjx_safety_gym-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for mjx_safety_gym-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8f217430836f92129dd618c3c1ba1d5576ad7b1dbb7e1e4eef92d74f2d17a0cb
MD5 5537ac8d70e5789574a33cafaa5c28e2
BLAKE2b-256 296b9cb69a06fc7bb04b4bb648f53e4cb523a755bd86496634bb73084b6afc94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page