File size: 6,762 Bytes
ee93ecd | 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 | Domain Randomization
====================
Domain randomization varies physical parameters during training so that policies
are robust to modeling errors and real-world variation. This guide shows
how to attach randomization terms to your environment using ``EventTerm`` and
``mdp.randomize_field``.
TL;DR
-----
Use an ``EventTerm`` that calls ``mdp.randomize_field`` with a target **field**, a
**value range** (or per-axis ranges), and an **operation** describing how to
apply the draw.
.. code-block:: python
from mjlab.managers.event_manager import EventTermCfg
from mjlab.managers.scene_entity_config import SceneEntityCfg
from mjlab.envs import mdp
foot_friction: EventTermCfg = EventTermCfg(
mode="reset", # randomize each episode
func=mdp.randomize_field,
domain_randomization=True, # marks this as domain randomization
params={
"asset_cfg": SceneEntityCfg("robot", geom_names=[".*_foot.*"]),
"field": "geom_friction",
"ranges": (0.3, 1.2),
"operation": "abs",
},
)
Domain Randomization Flag
-------------------------
When creating an ``EventTermCfg`` for domain randomization, set ``domain_randomization=True``.
This allows the environment to track which fields are being randomized:
.. code-block:: python
EventTermCfg(
mode="reset",
func=mdp.randomize_field,
domain_randomization=True, # required for DR tracking
params={"field": "geom_friction", ...},
)
This flag is especially useful when using custom class-based event terms instead of
``mdp.randomize_field``.
Event Modes
-----------
* ``"startup"`` - randomize once at initialization
* ``"reset"`` - randomize at every episode reset
* ``"interval"`` - randomize at regular time intervals
Available Fields
----------------
**Joint/DOF:** ``dof_armature``, ``dof_frictionloss``, ``dof_damping``, ``jnt_range``,
``jnt_stiffness``, ``qpos0``
**Body:** ``body_mass``, ``body_ipos``, ``body_iquat``, ``body_inertia``, ``body_pos``,
``body_quat``
**Geom:** ``geom_friction``, ``geom_pos``, ``geom_quat``, ``geom_rgba``
**Site:** ``site_pos``, ``site_quat``
Randomization Parameters
------------------------
**Distribution:** ``"uniform"`` (default), ``"log_uniform"`` (values must be > 0),
``"gaussian"`` (``mean, std``)
**Operation:** ``"abs"`` (default, set), ``"scale"`` (multiply), ``"add"`` (offset)
Axis selection
^^^^^^^^^^^^^^
Multi-dimensional fields can be randomized per-axis.
**Friction.** Geoms have three coefficients ``[tangential, torsional, rolling]``.
For ``condim=3`` (standard frictional contact), only **axis 0 (tangential)**
affects contact behavior:
.. code-block:: python
# Tangential friction (affects condim=3)
params={"field": "geom_friction", "ranges": {0: (0.3, 1.2)}}
# Tangential + torsional (torsional matters for condim >= 4)
params={"field": "geom_friction", "ranges": {0: (0.5, 1.0), 1: (0.001, 0.01)}}
# X and Y position
params={"field": "body_pos", "axes": [0, 1], "ranges": (-0.1, 0.1)}
Examples
--------
Friction (reset)
^^^^^^^^^^^^^^^^
.. code-block:: python
foot_friction: EventTermCfg = EventTermCfg(
mode="reset",
func=mdp.randomize_field,
domain_randomization=True,
params={
"asset_cfg": SceneEntityCfg("robot", geom_names=[".*_foot.*"]),
"field": "geom_friction",
"ranges": (0.3, 1.2),
"operation": "abs",
},
)
.. note::
Give your robot's collision geoms higher **priority** than terrain
(geom priority defaults to 0). Then you only need to randomize robot friction.
MuJoCo will use the higher-priority geom's friction in (robot, terrain)
contacts.
.. code-block:: python
from mjlab.utils.spec_config import CollisionCfg
robot_collision = CollisionCfg(
geom_names_expr=[".*_foot.*"],
priority=1,
friction=(0.6,),
condim=3,
)
Joint Offset (startup)
^^^^^^^^^^^^^^^^^^^^^^
Randomize default joint positions to simulate joint offset calibration errors:
.. code-block:: python
joint_offset: EventTermCfg = EventTermCfg(
mode="startup",
func=mdp.randomize_field,
domain_randomization=True,
params={
"asset_cfg": SceneEntityCfg("robot", joint_names=[".*"]),
"field": "qpos0",
"ranges": (-0.01, 0.01),
"operation": "add",
},
)
Center of Mass (COM) (startup)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
com: EventTermCfg = EventTermCfg(
mode="startup",
func=mdp.randomize_field,
domain_randomization=True,
params={
"asset_cfg": SceneEntityCfg("robot", body_names=["torso"]),
"field": "body_ipos",
"ranges": {0: (-0.02, 0.02), 1: (-0.02, 0.02)},
"operation": "add",
},
)
Custom Class-Based Event Terms
------------------------------
You can create custom event terms using classes instead of functions. This is useful
for event terms that need to maintain state or perform initialization logic:
.. code-block:: python
class RandomizeTerrainFriction:
"""Custom event term that randomizes terrain friction."""
def __init__(self, cfg, env):
# Find the terrain geom index during initialization
self._terrain_idx = None
for idx, geom in enumerate(env.scene.spec.geoms):
if geom.name == "terrain":
self._terrain_idx = idx
if self._terrain_idx is None:
raise ValueError("Terrain geom not found in the model.")
def __call__(self, env, env_ids, ranges):
"""Called each time the event is triggered."""
from mjlab.utils.math import sample_uniform
env.sim.model.geom_friction[env_ids, self._terrain_idx, 0] = sample_uniform(
ranges[0], ranges[1], len(env_ids), env.device
)
# Use the custom class in your environment config
terrain_friction: EventTermCfg = EventTermCfg(
mode="reset",
func=RandomizeTerrainFriction,
domain_randomization=True,
params={"field": "geom_friction", "ranges": (0.3, 1.2)},
)
Migrating from Isaac Lab
------------------------
Isaac Lab exposes explicit friction combination modes (``multiply``, ``average``,
``min``, ``max``). MuJoCo instead uses **priority-based selection**: if one
contacting geom has higher ``priority``, its friction is used; otherwise the
**element-wise maximum** is used. See the
`MuJoCo contact documentation <https://mujoco.readthedocs.io/en/stable/computation/index.html#contact>`_
for details.
|