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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mjx_safety_gym-0.1.6.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.6.tar.gz
Algorithm Hash digest
SHA256 86e9d9c2a6b0c072a7402528b3263e4e6e81e0697ca8c8fc9ba6e9f1677893ee
MD5 dba2b29de918669bd9941f8dc1bd17c1
BLAKE2b-256 0d82a599a8b1e83de35ac77e7a1f18cb194a475870c39d92cf5a330c064faa86

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mjx_safety_gym-0.1.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 45726c58da54e8a0d3c7b48a475e6a68c967d35ca3cb0ced6b72246b307bc990
MD5 8494324404096f1f4b0f23a38fc3ea9a
BLAKE2b-256 13ccfadd6ddda7d63c59d6e1a896f6b8f938a4fb71d93559d1b9eb6ca60bd33a

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