Index
utils
¶
Color
¶
Bases: StrEnum
Contains color aliases and converters to go between the various color formats. The color values are defined by the standard Tailwind CSS color theme.
MuJoCo uses a normalized rgba (all color channels clamped between 0 and 1). This enum defines with hex values, but they can be easily transformed with the provided methods (for example: Color.WHITE.rgba).
with_alpha
¶
hex_to_rgba
classmethod
¶
Converts '#RRGGBB' or 'RRGGBB' to normalized [0, 1] RGBA.
Source code in src/mujoco_mojo/utils/color.py
rgba255_to_rgba
classmethod
¶
Converts [0-255, 0-255, 0-255, 0-1] to normalized [0, 1] RGBA.
rgba_to_hex
classmethod
¶
Converts normalized RGBA to '#RRGGBB' (alpha is discarded).
Source code in src/mujoco_mojo/utils/color.py
rgba_to_rgba255
classmethod
¶
Converts normalized RGBA to [0-255, 0-255, 0-255, 0-1].
Interpolator
¶
Bases: MojoBaseModel
Utility to handle 1D lookup tables for forcing functions.
Proximity
¶
Bases: MojoBaseModel
Provide high-precision triangle-level distance queries.
geom_1
instance-attribute
¶
First geometry to perform proximity calculations for.
geom_2
instance-attribute
¶
Second geometry to perform proximity calculations for.
dist_max
instance-attribute
¶
dist_max: float
The 'cutoff' distance. If objects are further than this (as estimated by a sphere to sphere test), the sphere to sphere estimate will be returned and exit early.
algorithm
class-attribute
instance-attribute
¶
algorithm: ProximityType = CONVEX_HULL
What algorithm should be used for the narrowphase test.
visualize
class-attribute
instance-attribute
¶
visualize: bool = True
Wheter or not to visualize this proximity in the MuJoCo viewer.
get_sphere_to_sphere_proximity
¶
get_sphere_to_sphere_proximity(
mj_model: MjModel, mj_data: MjData
) -> tuple[float, Vec3, Vec3, bool]
Calculates the shortest distance between two geometries using their bounding spheres.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
The compiled MuJoCo model instance. |
required |
mj_data
|
MjData
|
The current simulation state. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, Vec3, Vec3, bool]
|
tuple[float, Vec3, Vec3, bool]: Unsigned ( |
Source code in src/mujoco_mojo/utils/proximity.py
get_convex_hull_proximity
¶
get_convex_hull_proximity(
mj_model: MjModel, mj_data: MjData
) -> tuple[float, Vec3, Vec3, ProximityType]
Calculates the shortest distance between two geometries using their convex hull.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
The compiled MuJoCo model instance. |
required |
mj_data
|
MjData
|
The current simulation state. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, Vec3, Vec3, ProximityType]
|
tuple[float, Vec3, Vec3, ProximityType]: Unsigned ( |
Source code in src/mujoco_mojo/utils/proximity.py
get_vertex_to_face_proximity
¶
get_vertex_to_face_proximity(
mj_model: MjModel, mj_data: MjData
) -> tuple[float, Vec3, Vec3, ProximityType]
Calculates the vertex to face distance using a multi-phase Bounding Volume Hierarchy (BVH) query.
Phases
- Broad Phase: Sphere-Sphere check (object level).
- Mid Phase: BVH Traversal (eliminating triangle groups). No exit here.
- Narrow Phase: Point-to-Face proximity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
mj_data
|
MjData
|
MuJoCo runtime data. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, Vec3, Vec3, ProximityType]
|
tuple[float, Vec3, Vec3, ProximityType]: Unsigned ( |
Source code in src/mujoco_mojo/utils/proximity.py
| Python | |
|---|---|
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
get_face_to_face_proximity
¶
get_face_to_face_proximity(
mj_model: MjModel, mj_data: MjData
) -> tuple[float, Vec3, Vec3, ProximityType]
Calculates the face to face distance using a multi-phase Bounding Volume Hierarchy (BVH) query.
This is more accurate than the vertex to face method, but comes at higher computational cost.
Phases
- Broad Phase: Sphere-Sphere check (object level).
- Mid Phase: BVH Traversal (eliminating triangle groups). No exit here.
- Narrow Phase: Face-to-Face proximity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
mj_data
|
MjData
|
MuJoCo runtime data. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, Vec3, Vec3, ProximityType]
|
tuple[float, Vec3, Vec3, ProximityType]: Unsigned ( |
Source code in src/mujoco_mojo/utils/proximity.py
get_proximity
¶
get_proximity(
mj_model: MjModel, mj_data: MjData
) -> tuple[float, Vec3, Vec3, ProximityType]
Calculates the shortest distance between two geometries using the specified proximity algorithm.
This is a general dispatcher method that routes to different proximity calculation algorithms based on the algorithm parameter. Each mode offers different tradeoffs between speed and precision:
Modes:
- SPHERE_TO_SPHERE: Fastest. Uses bounding sphere distance only (broadphase).
- CONVEX_HULL: Fast & accurate. Uses MuJoCo's convex hull-based distance (default).
- VERTEX_TO_FACE: Accurate. Multi-phase BVH with vertex-to-surface queries.
- FACE_TO_FACE: Most accurate but slowest. Full mesh-to-mesh distance calculation.
Phases (for non-sphere modes): 1. Broad Phase: Sphere-Sphere check (object level). 2. Narrow Phase: Algorithm-specific distance calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mj_model
|
MjModel
|
Compiled MuJoCo model. |
required |
mj_data
|
MjData
|
MuJoCo runtime data. |
required |
Returns:
| Type | Description |
|---|---|
float
|
tuple[float, ProximityType]: If fromto=False, returns the unsigned ( |
Vec3
|
tuple[tuple[float, Vec3, Vec3], ProximityType]: If fromto=True, returns the minimum distance, world location of minimum distance on geom_1, world location of minimum distance on geom_2, and which algorithm produced the result. |
Source code in src/mujoco_mojo/utils/proximity.py
request
¶
request(
signal_manager: SignalManager,
attrs: list[Literal["dist", "fromto", "prox_type"]] = [
"dist",
"prox_type",
],
)
Registers specific geom proximity attributes for logging. Please see the get_proximity method for how these outputs are calculated.
Available Requests
dist: Minimum distance as calculated by the specified algorithm. Tagged with Proximities/{pair_name}:dist.
fromto: World coordinates for where the minimum distance is estimated to occur at. Two sets of coordinates will be returned for geom_1 and geom_2. Tagged with Proximities/{pair_name}/fromto/{(geom_1 | geom_2).name}:(x | y | z).
prox_type: What type of proximity calculation the previous values are from. Using dist_max, get_proximity can return a broadphase estimate (bounding sphere to sphere) if the two geometries are distant (greater than dist_max). This telemetry will return what type of proximity calculation was performed for this timestep. It is intended to help debug to understand if a jump in telemetry (specifically sharp declines) are real or comes from the broadphase estimate. The values returned will be integer values associated with their specific ProximityType (see the enumeration for the mapping, in general a larger value will mean a more accurate one). Tagged with Proximities/{pair_name}:prox_type.
Source code in src/mujoco_mojo/utils/proximity.py
MojoRunner
dataclass
¶
MojoRunner(
generator: MojoGenerator,
generator_path: str | None = None,
runtime: MojoRuntime | None = DEFAULT_RUNTIME,
runtime_path: str | None = None,
objective: MojoObjective | None = None,
objective_path: str | None = None,
seed: int | None = DEFAULT_SEED,
workdir: Path = DEFAULT_WORKDIR,
model_config_name: str = DEFAULT_MODEL_CONFIG_NAME,
xml_name: str = DEFAULT_XML_NAME,
config: MonteCarloConfig
| OptimizerConfig = MonteCarloConfig(),
gen_args: list[Any] = list(),
gen_kwargs: dict[str, Any] = dict(),
run_args: list[Any] = list(),
run_kwargs: dict[str, Any] = dict(),
)
slurm_trial_id
property
¶
slurm_trial_id: int | None
Returns the current SLURM task ID if running as part of an array job.
run
¶
run(
global_overrides: NamedValueDict[
NDArray
] = NamedValueDict[NDArray](),
clean_workdir: bool = False,
cleanup_delay: int = 10,
execution_mode: ExecutionMode = LOCAL,
trial_nums: list[int] | None = None,
) -> bool
Vectors a job to be either computed locally or to be orchestrated by SLURM.
Source code in src/mujoco_mojo/utils/runner.py
orchestrate_slurm
¶
orchestrate_slurm(
global_overrides: NamedValueDict[NDArray],
trial_nums: list[int] | None = None,
) -> bool
Generates an sbatch script and submits the job array to SLURM for a given config.
Source code in src/mujoco_mojo/utils/runner.py
execute_single_trial
¶
execute_single_trial(
trial_num: int,
seed: int | None,
overrides_payload: dict,
) -> tuple[
MojoModel | None,
TrialStatus,
MjModel | None,
MjData | None,
]
Helper to package a Trial and run it.
Source code in src/mujoco_mojo/utils/runner.py
run_monte_carlo
¶
run_monte_carlo(
global_overrides: NamedValueDict[
NDArray
] = NamedValueDict[NDArray](),
trial_nums: list[int] | None = None,
) -> bool
Orchestrates a Monte Carlo job.
Source code in src/mujoco_mojo/utils/runner.py
| Python | |
|---|---|
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
get_slurm_array_string
staticmethod
¶
Collapses [0, 1, 2, 5, 6] into '0-2,5-6' for SLURM.
Source code in src/mujoco_mojo/utils/runner.py
get_slurm_partitions
staticmethod
¶
Queries sinfo for available partitions and identifies the default.
Source code in src/mujoco_mojo/utils/runner.py
normalize_to_mb
staticmethod
¶
Converts SLURM memory strings (e.g., '1000', '1G', '1024M') to integer MB.
Source code in src/mujoco_mojo/utils/runner.py
format_bytes
staticmethod
¶
Scales MB back up to the most readable unit (G, T, etc.).
Source code in src/mujoco_mojo/utils/runner.py
get_slurm_node_mem_limit
staticmethod
¶
Finds the MINIMUM RealMemory limit, normalized to MB.
Source code in src/mujoco_mojo/utils/runner.py
get_slurm_cpu_limit
staticmethod
¶
Finds the minimum of the max allowed CPUs (CPUTot) among nodes in a partition.
Source code in src/mujoco_mojo/utils/runner.py
slurm_time_to_seconds
staticmethod
¶
Converts SLURM time (D-HH:MM:SS or HH:MM:SS) to total seconds.
Source code in src/mujoco_mojo/utils/runner.py
get_slurm_time_limit
staticmethod
¶
Finds the MaxTime limit for a specific partition.
Source code in src/mujoco_mojo/utils/runner.py
get_max_array_size
staticmethod
¶
get_max_array_size() -> int
Queries the global SLURM configuration for MaxArraySize.
Source code in src/mujoco_mojo/utils/runner.py
orchestrate_slurm_monte_carlo
¶
orchestrate_slurm_monte_carlo(
global_overrides: NamedValueDict[
NDArray
] = NamedValueDict[NDArray](),
trial_nums: list[int] | None = None,
) -> bool
Orchestrates a Monte Carlo SLURM submission.
Source code in src/mujoco_mojo/utils/runner.py
| Python | |
|---|---|
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 | |
MonteCarloConfig
¶
Bases: BaseConfig
n_proc
class-attribute
instance-attribute
¶
n_proc: int = DEFAULT_N_PROC
Number of proccesses to allow.
This value is used to determine how many parallel jobs can be run. It is also used for the discovery of trial status. Using a value of 1 will result in the slowest runtime, but highest reliability.
Important
Be a good citizen. Use a reasonable number if you are working on a shared resource. You are a jerk if you use everything.
resume
class-attribute
instance-attribute
¶
resume: bool = DEFAULT_RESUME
Whether to resume a study if the study already exists.
padding_style
property
¶
padding_style: str
This dynamically defines the padding style for trial numbers. This is helpful to ensure the filesystem consistently sorts the trials.
Examples:
- Suppose you n_trials is 2000 (and the nominal trial_num is 0)
- This method would return 04d
- Trial number 0 maps to 0000
- Trial number 123 maps to 0123
- Trial number 1999 will still map to 1999
n_trial
class-attribute
instance-attribute
¶
n_trial: int = DEFAULT_MC_N_TRIAL
Number of trials to run.
You are able to resume a previous job and modify the number of runs desired by changing this value. A job already in progress will not be dynamically stopped though if you change this value at runtime.
OptimizerConfig
¶
Bases: BaseConfig
n_proc
class-attribute
instance-attribute
¶
n_proc: int = DEFAULT_N_PROC
Number of proccesses to allow.
This value is used to determine how many parallel jobs can be run. It is also used for the discovery of trial status. Using a value of 1 will result in the slowest runtime, but highest reliability.
Important
Be a good citizen. Use a reasonable number if you are working on a shared resource. You are a jerk if you use everything.
resume
class-attribute
instance-attribute
¶
resume: bool = DEFAULT_RESUME
Whether to resume a study if the study already exists.
padding_style
property
¶
padding_style: str
This dynamically defines the padding style for trial numbers. This is helpful to ensure the filesystem consistently sorts the trials.
Examples:
- Suppose you n_trials is 2000 (and the nominal trial_num is 0)
- This method would return 04d
- Trial number 0 maps to 0000
- Trial number 123 maps to 0123
- Trial number 1999 will still map to 1999
n_trial
class-attribute
instance-attribute
¶
n_trial: int = DEFAULT_OP_N_TRIAL
Number of trials to run.
study_name
class-attribute
instance-attribute
¶
study_name: str = DEFAULT_OP_STUDY_NAME
Unique identifier for the Optuna study.
direction
instance-attribute
¶
direction: Literal['minimize', 'maximize']
Whether we want to find the lowest or highest objective value.
timeout
class-attribute
instance-attribute
¶
timeout: float | None = DEFAULT_OP_TIMEOUT
Stop the study after this many seconds, regardless of trial count.
storage
class-attribute
instance-attribute
¶
storage: str | None = DEFAULT_OP_STORAGE
Database URL (e.g., 'sqlite:///study.db') for multi-node persistence.
sampler
class-attribute
instance-attribute
¶
The search algorithm. TPE is generally best for noisy physics.
evals_per_trial
class-attribute
instance-attribute
¶
Number of times to run the sim with different seeds per trial. The average score is returned to Optuna.
Reduces 'lucky' trials in noisy physics.
refine_search_factor
class-attribute
instance-attribute
¶
refine_search_factor: float | None = Field(
default=DEFAULT_OP_REFINE_SEARCH_FACTOR, gt=0, lt=1
)
If set (e.g., 0.5), and resume is True, the Runner will shrink the search space bounds by this factor around the current best trial to focus on local refinement.
Smaller values will more aggressively refine the search space.
Trial
dataclass
¶
Trial(
trial_num: int,
base_dir: Path,
xml_name: str,
model_config_name: str,
padding_style: str,
)
Handles the lifecycle of a single simulation run.
The Trial object is responsible for the 'dirty work' of a Monte Carlo run: - Creating directories - Writing the MJCF XML - Saving the configuration snapshot - Triggering the physics runtime.
base_dir
instance-attribute
¶
base_dir: Path
Root directory where all simulation trials are stored.
xml_name
instance-attribute
¶
xml_name: str
Filename for the generated MJCF XML (e.g., 'model.xml').
model_config_name
instance-attribute
¶
model_config_name: str
Filename for the serialized MojoModel configuration (e.g., 'config.json').
padding_style
instance-attribute
¶
padding_style: str
Format specifier for directory naming (e.g., '04d').
trial_dir
property
¶
trial_dir: Path
The absolute path to this trial's unique workspace.
Example
If base_dir is './sims' and trial_num is 7 with '03d' padding, this returns './sims/trial_007'.
model_config_path
property
¶
model_config_path: Path
The full path to the JSON configuration file for this trial.
named_value_path
property
¶
named_value_path: Path
The full path to the JSON NamedValue file for this trial.
run
¶
run(
generator: MojoGenerator,
runtime: MojoRuntime | None,
seed: int | None,
overrides: NamedValueDict[NDArray],
gen_args: list[Any],
gen_kwargs: dict[str, Any],
run_args: list[Any],
run_kwargs: dict[str, Any],
) -> tuple[
MojoModel | None,
TrialStatus,
MjModel | None,
MjData | None,
]
Executes the complete simulation pipeline for this trial.
This method coordinates three main phases:
1. Generation: Calls the user-provided generator to build a MojoModel model.
2. Persistence: Creates the workspace and writes the model/config to disk.
3. Execution: Triggers the physics runtime if one is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
MojoGenerator
|
Function that returns a |
required |
runtime
|
MojoRuntime | None
|
Optional function to run the simulation (MuJoCo). |
required |
seed
|
int | None
|
Seed to use to define the trial. |
required |
overrides
|
NamedValueDict[NDArray]
|
Key-value pairs that override random distributions. |
required |
gen_args
|
list[Any]
|
Positional arguments for the generator. |
required |
gen_kwargs
|
dict[str, Any]
|
Keyword arguments for the generator. |
required |
run_args
|
list[Any]
|
Positional arguments for the runtime. |
required |
run_kwargs
|
dict[str, Any]
|
Keyword arguments for the runtime. |
required |
Returns:
| Type | Description |
|---|---|
tuple[MojoModel | None, TrialStatus, MjModel | None, MjData | None]
|
The |
Source code in src/mujoco_mojo/utils/runner.py
| Python | |
|---|---|
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
setup_logger
¶
setup_logger(
level=INFO,
terminal: bool = True,
log_file: Path | str | None = Path("mojo.log"),
max_bytes: int = 10 * 1024 * 1024,
backup_count: int = 5,
) -> Logger
Shortcut to a sensible MuJoCo Mojo logger using Rich for the terminal.