Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Dm control Variation Base

From Leeroopedia
Knowledge Sources
Domains Reinforcement_Learning, Domain_Randomization
Last Updated 2026-02-15 04:00 GMT

Overview

Abstract base class and operator infrastructure for composable variation objects that enable algebraic expressions over randomized values in the dm_control Composer framework.

Description

The Variation class is an abstract base class (ABC) that defines the fundamental interface for all variation objects in the dm_control variation system. It requires subclasses to implement a __call__ method accepting three arguments: initial_value (the original attribute value), current_value (the current attribute value), and random_state (a NumPy random state for sampling). This callable protocol allows variation objects to be used interchangeably wherever stochastic or deterministic value generation is needed.

The class overloads all standard Python arithmetic operators (+, -, *, /, //, **, unary -, and [] indexing) to return composed variation objects. When two variations are combined with an operator, a _BinaryOperation is created that evaluates both operands and applies the operator to the results. Similarly, _UnaryOperation wraps single-operand operations like negation, and _GetItemOperation supports indexing into array-valued variation outputs. All composed operations use variation_values.evaluate for recursive resolution, enabling deeply nested expression trees.

This design allows users to write natural mathematical expressions such as distributions.Uniform(0.8, 1.2) * initial_mass or base_position + distributions.Normal(0, 0.1), where the result is itself a Variation object that can be further composed or evaluated.

Usage

Use this class as the base for all custom variation implementations. Subclass Variation and implement the __call__ method to define how values are generated. The operator overloading is used implicitly whenever arithmetic is performed between variation objects or between a variation and a constant value.

Code Reference

Source Location

Signature

class Variation(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def __call__(self, initial_value, current_value, random_state):
        ...

class _UnaryOperation(Variation):
    def __init__(self, op, variation):
        ...

class _BinaryOperation(Variation):
    def __init__(self, op, first, second):
        ...

class _GetItemOperation(Variation):
    def __init__(self, variation, index):
        ...

Import

from dm_control.composer.variation import base
from dm_control.composer.variation.base import Variation

I/O Contract

Inputs

Name Type Required Description
initial_value any No The original value of the attribute being varied; absolute variations may ignore this
current_value any No The current value of the attribute being varied; absolute variations may ignore this
random_state numpy.RandomState No A random state for generating stochastic values; deterministic variations may ignore this

Outputs

Name Type Description
return any The next value for this variation, type depends on the concrete subclass

Supported Operators

Operator Python Method Result Type Description
a + b __add__ / __radd__ _BinaryOperation Addition of two variation outputs
a - b __sub__ / __rsub__ _BinaryOperation Subtraction of two variation outputs
a * b __mul__ / __rmul__ _BinaryOperation Multiplication of two variation outputs
a / b __truediv__ / __rtruediv__ _BinaryOperation True division of two variation outputs
a // b __floordiv__ / __rfloordiv__ _BinaryOperation Floor division of two variation outputs
a ** b __pow__ / __rpow__ _BinaryOperation Exponentiation of two variation outputs
-a __neg__ _UnaryOperation Negation of a variation output
a[i] __getitem__ _GetItemOperation Index into an array-valued variation output

Usage Examples

from dm_control.composer.variation.base import Variation
from dm_control.composer.variation import distributions

# Compose variations using arithmetic operators
uniform = distributions.Uniform(low=0.8, high=1.2)
normal = distributions.Normal(loc=0.0, scale=0.01)

# Create a composed variation: uniform * 2.0 + normal
composed = uniform * 2.0 + normal

# Evaluate the composed variation
import numpy as np
rng = np.random.RandomState(42)
value = composed(initial_value=1.0, current_value=1.0, random_state=rng)

# Index into an array-valued variation
point = distributions.UniformPointOnSphere()
x_component = point[0]  # Returns a _GetItemOperation

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment