Skip to content

randomization

cosmic_warp.core.randomization

Domain randomization of per-entity vehicle parameters.

Parameters are resampled entirely on the GPU whenever entities are reset, from counter-based per-entity streams like ResetManager's (on their own stream id, cosmic_warp.core.rng), so randomization stays valid inside CUDA graph captures (reset_terminated / simulate(auto_reset_terminated=True)).

Sampling respects the physical couplings
  • Propellant is sampled, not total mass: state mass = mass_dry + propellant, so entities can never spawn below dry mass (which the dynamics would silently clamp to "out of propellant").
  • Mass randomization preserves the velocity, not the momentum: the state's momentum is rescaled by new_mass / old_mass, so the sampled trajectory (in particular an orbit drawn in orbital-element reset mode) is unchanged by how much propellant the vehicle carries.
  • The inertia tensor is randomized through a scalar scale factor applied to the body's nominal tensor, preserving symmetry, positive definiteness and the triangle inequality of the principal moments.

RandomizationConfig

Per-body uniform sampling bounds. A field is active when its flag is nonzero.

DomainRandomizer

DomainRandomizer(
    num_envs: int,
    vehicle: VehicleManager,
    device: str = "cuda",
    num_bodies: int = 1,
    seed: int = 1234,
)

Resamples vehicle parameters (and propellant load) per entity on reset.

Bounds are per body and broadcast across environments; every entity draws its own values from its body's bounds. Inertia scaling applies to each entity's own configured (nominal) tensor, so per-environment inertia is kept; a later vehicle.set_config takes effect at the next sim.reset(), reset_terminated(), simulate() or RL VecEnv.step().

Parameters:

Name Type Description Default
num_envs int

Number of parallel environments

required
vehicle VehicleManager

The simulator's VehicleManager

required
device str

Device to run on

'cuda'
num_bodies int

Number of bodies per environment

1
seed int

RNG seed for parameter sampling

1234
Source code in cosmic_warp/core/randomization.py
def __init__(
    self,
    num_envs: int,
    vehicle: VehicleManager,
    device: str = "cuda",
    num_bodies: int = 1,
    seed: int = 1234
):
    """
    Args:
        num_envs: Number of parallel environments
        vehicle: The simulator's VehicleManager
        device: Device to run on
        num_bodies: Number of bodies per environment
        seed: RNG seed for parameter sampling
    """
    self.num_envs = num_envs
    self.num_bodies = num_bodies
    self.num_entities = num_envs * num_bodies
    self.device = device
    self._vehicle = vehicle
    self._vehicle_configs = vehicle.configs

    # Host-side per-body bounds mirror (merged on partial overrides).
    # Keys: "mass_dry", "propellant", "inertia_scale" -> (min, max).
    self._bounds = [dict() for _ in range(num_bodies)]

    # Per-entity nominal inertia (and inverse) that inertia_scale applies
    # to, refreshed from the vehicle's host mirror when it changes
    self._inertia_nominal = wp.zeros(self.num_entities, dtype=wp.mat33d, device=device)
    self._inertia_inv_nominal = wp.zeros(self.num_entities, dtype=wp.mat33d, device=device)
    self._nominal_version = None

    # All flags zero-initialized = nothing randomized until set_bounds
    self.config = wp.zeros(num_bodies, dtype=RandomizationConfig, device=device)
    self.enabled = False

    self.rng_state = wp.empty(self.num_entities, dtype=RNG_STATE, device=device)
    self.reseed(seed)

reseed

reseed(seed: int) -> None

Restart the per-entity RNG streams at draw 0 of seed (an integer in [0, 2**64)).

Source code in cosmic_warp/core/randomization.py
def reseed(self, seed: int) -> None:
    """Restart the per-entity RNG streams at draw 0 of ``seed`` (an
    integer in [0, 2**64))."""
    seed_states(self.rng_state, validation.seed("randomization seed", seed))

set_bounds

set_bounds(
    *,
    mass_dry: tuple = None,
    propellant: tuple = None,
    inertia_scale: tuple = None,
    body_ids: list = None,
) -> None

Enable randomization of vehicle parameters for specific bodies.

Parameters:

Name Type Description Default
mass_dry tuple

(min, max) dry mass [kg]. Requires propellant for the same bodies so total mass stays above dry mass.

None
propellant tuple

(min, max) propellant mass [kg], sampled on top of the (possibly randomized) dry mass: state mass = dry + propellant.

None
inertia_scale tuple

(min, max) scale factor applied to each entity's configured (nominal) inertia tensor.

None
body_ids list

List of body indices to configure (None = all bodies)

None
Source code in cosmic_warp/core/randomization.py
def set_bounds(
    self,
    *,
    mass_dry: tuple = None,
    propellant: tuple = None,
    inertia_scale: tuple = None,
    body_ids: list = None
) -> None:
    """
    Enable randomization of vehicle parameters for specific bodies.

    Args:
        mass_dry: (min, max) dry mass [kg]. Requires `propellant` for the
            same bodies so total mass stays above dry mass.
        propellant: (min, max) propellant mass [kg], sampled on top of the
            (possibly randomized) dry mass: state mass = dry + propellant.
        inertia_scale: (min, max) scale factor applied to each entity's
            configured (nominal) inertia tensor.
        body_ids: List of body indices to configure (None = all bodies)
    """
    body_ids = (range(self.num_bodies) if body_ids is None
                else check_ids("body_ids", body_ids, self.num_bodies))

    if mass_dry is not None:
        mass_dry = _checked_bounds(mass_dry, "mass_dry", positive=True)
    if propellant is not None:
        propellant = _checked_bounds(propellant, "propellant")
    if inertia_scale is not None:
        inertia_scale = _checked_bounds(inertia_scale, "inertia_scale", positive=True)

    # Validate the merged bounds of every body before mutating anything
    merged = {}
    for body_id in body_ids:
        b = dict(self._bounds[body_id])
        if mass_dry is not None:
            b["mass_dry"] = mass_dry
        if propellant is not None:
            b["propellant"] = propellant
        if inertia_scale is not None:
            b["inertia_scale"] = inertia_scale
        if "mass_dry" in b and "propellant" not in b:
            raise ValueError(
                f"randomization of mass_dry (body {body_id}) requires "
                "propellant bounds too, so total mass = dry + propellant "
                "stays above dry mass."
            )
        merged[body_id] = b

    for body_id, b in merged.items():
        self._bounds[body_id] = b
        self._write_config(body_id)

    self.enabled = any(self._bounds)
    self.sync_nominal()

get_bounds

get_bounds(body_id: int = 0) -> dict

The bounds set for one body, as {name: (min, max)}; a parameter absent from the dict is not randomized. A copy: edit with set_bounds.

Source code in cosmic_warp/core/randomization.py
def get_bounds(self, body_id: int = 0) -> dict:
    """The bounds set for one body, as {name: (min, max)}; a parameter
    absent from the dict is not randomized. A copy: edit with set_bounds."""
    (body_id,) = check_ids("body_id", [body_id], self.num_bodies)
    return dict(self._bounds[body_id])

sync_nominal

sync_nominal() -> None

Upload the configured inertia as the nominal for inertia scaling, if it changed since the last upload (host-side check, no device read). Called by set_bounds, sim.reset(), reset_terminated() (outside a capture), simulate() and the RL VecEnv.step().

Source code in cosmic_warp/core/randomization.py
def sync_nominal(self) -> None:
    """Upload the configured inertia as the nominal for inertia scaling,
    if it changed since the last upload (host-side check, no device read).
    Called by set_bounds, sim.reset(), reset_terminated() (outside a
    capture), simulate() and the RL VecEnv.step()."""
    if self._nominal_version == self._vehicle.inertia_version:
        return
    nominal = self._vehicle.inertia_host
    self._inertia_nominal.assign(nominal)
    self._inertia_inv_nominal.assign(np.linalg.inv(nominal) if nominal.any()
                                     else nominal)
    self._nominal_version = self._vehicle.inertia_version

randomize_all

randomize_all(states: array) -> None

Resample randomized parameters for all entities.

Source code in cosmic_warp/core/randomization.py
def randomize_all(self, states: wp.array) -> None:
    """Resample randomized parameters for all entities."""
    wp.launch(
        kernel=randomize_kernel,
        dim=self.num_entities,
        inputs=[states, self._vehicle_configs, self.config, self.rng_state,
                self._inertia_nominal, self._inertia_inv_nominal, self.num_bodies],
        device=self.device
    )

randomize_masked

randomize_masked(states: array, mask: array) -> None

Resample randomized parameters for masked entities (graph-safe).

Source code in cosmic_warp/core/randomization.py
def randomize_masked(self, states: wp.array, mask: wp.array) -> None:
    """Resample randomized parameters for masked entities (graph-safe)."""
    wp.launch(
        kernel=randomize_masked_kernel,
        dim=self.num_entities,
        inputs=[states, self._vehicle_configs, self.config, self.rng_state,
                self._inertia_nominal, self._inertia_inv_nominal, mask, self.num_bodies],
        device=self.device
    )

randomize_kernel

randomize_kernel(
    states: array(dtype=State),
    vehicles: array(dtype=VehicleConfig),
    config: array(dtype=RandomizationConfig),
    rng_state: array(dtype=RNG_STATE),
    inertia_nominal: array(dtype=mat33d),
    inertia_inv_nominal: array(dtype=mat33d),
    num_bodies: int32,
) -> None

Resample randomized parameters for all entities.

Source code in cosmic_warp/core/randomization.py
@wp.kernel
def randomize_kernel(
    states: wp.array(dtype=State),
    vehicles: wp.array(dtype=VehicleConfig),
    config: wp.array(dtype=RandomizationConfig),
    rng_state: wp.array(dtype=RNG_STATE),
    inertia_nominal: wp.array(dtype=wp.mat33d),
    inertia_inv_nominal: wp.array(dtype=wp.mat33d),
    num_bodies: wp.int32
) -> None:
    """Resample randomized parameters for all entities."""
    tid = wp.tid()
    _randomize_entity(config[tid % num_bodies], states, vehicles, rng_state,
                      inertia_nominal, inertia_inv_nominal, tid)

randomize_masked_kernel

randomize_masked_kernel(
    states: array(dtype=State),
    vehicles: array(dtype=VehicleConfig),
    config: array(dtype=RandomizationConfig),
    rng_state: array(dtype=RNG_STATE),
    inertia_nominal: array(dtype=mat33d),
    inertia_inv_nominal: array(dtype=mat33d),
    mask: array(dtype=bool),
    num_bodies: int32,
) -> None

Resample randomized parameters for masked entities only.

Source code in cosmic_warp/core/randomization.py
@wp.kernel
def randomize_masked_kernel(
    states: wp.array(dtype=State),
    vehicles: wp.array(dtype=VehicleConfig),
    config: wp.array(dtype=RandomizationConfig),
    rng_state: wp.array(dtype=RNG_STATE),
    inertia_nominal: wp.array(dtype=wp.mat33d),
    inertia_inv_nominal: wp.array(dtype=wp.mat33d),
    mask: wp.array(dtype=wp.bool),
    num_bodies: wp.int32
) -> None:
    """Resample randomized parameters for masked entities only."""
    tid = wp.tid()
    if mask[tid]:
        _randomize_entity(config[tid % num_bodies], states, vehicles, rng_state,
                          inertia_nominal, inertia_inv_nominal, tid)