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.4.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.4-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mjx_safety_gym-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 88d1ece62e506b2b94c6a7b3b0e3997786a641134ca90d71fe926ef2957555ae
MD5 daf5f5deb537ed4df8e65e5ba598cff2
BLAKE2b-256 5efdae935fd62fc1bb2d17a232baa7297a99b1280ac7460d4f09aca15d9ac031

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mjx_safety_gym-0.1.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 125ebc90e4c1225133520ade32f903edd1b165d7048c178e9ca63f6d9b31487c
MD5 e5cc6a36e18c150b5a095910e248bfc3
BLAKE2b-256 c33cc398c61bdef8c3940588864d8d13ba08cbce793565707c5f62a3493a37a0

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