Optimization¶
Abstract
While a Monte Carlo campaign explores the "what if" of random chance, Optimization helps answer the "what is best." Instead of drawing from static distributions, Mojo uses Optuna to intelligently navigate your design space, evolving your model parameters to minimize or maximize a specific physical outcome.
Design Variables vs. Distributions¶
In a standard Monte Carlo script, you use mojo_model.sample_dist() to represent uncertainty. In an optimization study, you replace these with Design Variables.
Defining the Search Space¶
Mojo supports two primary types of design variables within your generate script:
DesignFloat: For continuous parameters like spring stiffness, mass, or damping ratios.DesignCategorical: For discrete choices like material types (['steel', 'aluminum']) or solver methods.
Example: Defining Design Variables
Note: Random Values and Overrides
Just because you are using design variables doesn't restrict you from defining distributions! You can very much still add "chaos" to your runs with the random values as you would for a Monte Carlo.
If override named values are also provided, those will be used over a design value just like for stochastic values.
The Objective Function Contract¶
The Objective Function is the "grade" you give to a simulation. It is a standalone function that Mojo calls after the runtime script finishes. Its job is to ingest the simulation results and return a single float.
Example: MojoObjective Handle
A valid objective function must accept the MojoModel, the Path to the telemetry outputs requested during runtime, and the MuJoCo mj_model/mj_data objects.
Example: Scoring Performance
Running the Optimizer¶
Optimization jobs are launched via the mujoco-mojo run optimize command (instead of monte-carlo). This engine orchestrates the feedback loop between your generate script, your runtime script, and your objective function.
Key Command Line Arguments¶
| Argument | Shortcut | Description |
|---|---|---|
--direction |
-d |
Whether to minimize (e.g., error) or maximize (e.g., efficiency). |
--sampler |
-sm |
The search algorithm. tpe is the workhorse; cmaes is for local refinement. |
--storage |
-st |
Defines if a storage database will be placed in the workdir. |
--evals-per-trial |
-ept |
Runs the sim N times with different seeds and averages the score. |
--refine-search-factor |
-rsf |
Aggressive Refinement. On resume, shrinks bounds around the current best. |
# Launch a 400-trial study with 10 parallel workers
mujoco-mojo run optimiztion \
-g sim.generate \
-r sim.runtime \
--objective sim.objective \
--n-trial 400 \
--n-proc 10 \
--seed 42 \
--storage \
--direction minimize
Advanced Workflows: Zooming and Robustness¶
Stochastic Robustness (--evals-per-trial)¶
In MuJoCo, a "lucky" seed can sometimes produce a great score that isn't actually robust. By setting --evals-per-trial 5, Mojo runs every trial 5 times with different joint noise and returns the mean score. This ensures the optimizer finds stable designs, not just lucky ones.
Adaptive Refinement (--refine-search-factor)¶
Once you find a promising "neighborhood," you can resume the job with --refine-search-factor 0.2 --resume. This physically shrinks the search bounds by 80% around your current best trial, allowing the solver to find the absolute peak with high precision.
Post-Processing¶
One difference you may notice with mujoco-mojo dojo is the new Morph tab not present for Monte-Carlo jobs. This new tab allows you to view the history of your optimization using optuna-dashboard.
To use this you must provide a storage database argument for you optimization job configuration.
Success
The Optimization toolkit transforms MuJoCo Mojo from a diagnostic simulator into an automated engineering design tool. By replacing manual parameter sweeps with a closed-loop search, you can efficiently navigate high-dimensional design spaces where physical intuition often hits a ceiling.
Whether you are filtering out physics noise with multi-evaluation trials or "zooming in" on a performance sweet spot with adaptive refinement, the Morph toolkit ensures that your final design is backed by rigorous convergence, not just a lucky seed.
Example: Full Optimization Script
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 | |