MuJoCo problems

MuJoCo (Multi-Joint dynamics with Contact) is a physics engine for simulating robotics and other systems with articulated bodies and contact interactions. Blackbox uses MuJoCo through Gymnasium, which provides a consistent API for creating an environment, resetting it, and stepping through a simulation. The following Gymnasium MuJoCo environments are supported within Blackbox:

You must install Gymnasium and its MuJoCo dependencies separately by running following command in terminal:

pip install gymnasium[mujoco]

Basic usage

The example below demonstrates how to work with MujocoProblem class implemented in Blackbox.

from pyDOE3 import lhs
from blackbox.mujoco_problems import MujocoProblem

# initialize mujoco problem class
problem = MujocoProblem(env_name='HalfCheetah-v5', num_rollouts=5, seed=1)

# create sampling plan
x = lhs(problem.dim, 10, criterion="cm", random_state=1) 
x = problem.lb + (problem.ub - problem.lb) * x 

# evaluate sampling plan
y = problem(x)

print(f"Problem dimension: {problem.dim}")
print(f"\nSampling plan:\n{x}")
print(f"\nOutput:\n{y}")

The problem class is initialized with the environment name, number of rollouts (number of times the experiment is repeated), and a seed. The env_name must be one of the following: HalfCheetah-v5, Hopper-v5, Humanoid-v5, Swimmer-v5, Walker2d-v5. There are other arguments as well that can be provided during initialization, refer to mujoco problem API for more details. Then, a Latin hypercube sampling plan is created and scaled to appropriate bounds. In this example, the x is of shape (10,102), where 10 corresponds to number of samples and 102 denotes problem dimension. Lastly, the sampling plan is evaluated by calling the initialized class object. The call method can takes a numpy array of shape (dim,) (single sample) or (n_samples, dim) (multiple samples), and returns the value of reward function associated with each sample. You can save the x and y as an npz or mat file for later use. Similar to this example, you can initialize mujoco problem with any of the valid environments and generate datasets.