CMA-MAEGA with analytic gradients

Minimise x**2 + y**2 while finding solutions at different values of the descriptor y. qdpy.algorithms.CMAMAEGA evaluates derivatives at its current centre and uses those directions to propose offspring. The ordinary evaluator measures the offspring.

Install the cma extra and run the script from the repository root.

python examples/cma_maega.py
# SPDX-License-Identifier: LGPL-3.0-or-later
## @file
# @brief Use analytic gradients in CMA-MAEGA.

"""Run a differentiable search with one gradient call per centre."""

import numpy as np

from qdpy.algorithms import CMAMAEGA, GradientEvaluation
from qdpy.containers import Grid
from qdpy.simple import run


def evaluate(candidate):
    """Return the raw sphere objective and the second coordinate as the descriptor."""
    return (sum(x ** 2 for x in candidate),), (candidate[1],)


def evaluate_gradient(candidate):
    """Return the measurements and one derivative row per value."""
    fitness, features = evaluate(candidate)
    x, y = candidate
    jacobian = np.array([[2 * x, 2 * y], [0.0, 1.0]])
    return GradientEvaluation(fitness[0], features, jacobian)


if __name__ == "__main__":
    unit = (0.0, 1.0)
    archive = Grid(shape=4, fitness_domain=((0, 2),), features_domain=(unit,))
    search = CMAMAEGA(
        archive, budget=24, x0=[0.5, 0.5], sigma0=0.1, batch_size=4,
        threshold_min=-2, gradient_evaluate=evaluate_gradient, ind_domain=unit,
    )
    result = run(search, evaluate, seed=42)
    print(f"Evaluations: {result.evaluations}")
    print(f"Centre evaluations: {search.gradient_evaluations}")
    print(f"Offspring evaluations: {result.evaluations - search.gradient_evaluations}")
    print(f"Occupied cells: {archive.filled_bins}/{len(archive.solutions)}")
    print(f"Best raw fitness: {result.best.fitness.values[0]:.6g}")

Return a qdpy.algorithms.GradientEvaluation with the raw objective, descriptors, and a Jacobian. The Jacobian has one column per genome coordinate. The first row contains the objective derivatives [2*x, 2*y], and the next row contains the derivatives of y, [0, 1]. Append one row per additional descriptor, in the same order as the descriptor values. CMA-MAEGA applies the objective sign and normalises nonzero rows before sampling coefficients.

Each centre call counts as one evaluation, including failures. With this budget the script reports five centre evaluations and 19 offspring evaluations. The last offspring batch has three slots. It can update the archive and cell thresholds, and the centre move and covariance adaptation use full offspring batches.

Manual scheduling

For one centre and offspring cycle, keep the constructor and replace the call to run() and the reporting block with this code.

centre = search.ask_dqd()
search.tell_dqd(evaluate_gradient(centre))

candidates = search.ask_batch()
search.tell_batch([evaluate(ind) for ind in candidates])
print(search.nb_evaluations, search.gradient_evaluations)

Report None through tell_dqd() if the centre evaluator fails. Request a new centre before asking for offspring. With the managed runner, a packed evaluator receives groups of offspring and the centre callback still receives one individual at a time.

See Fontaine and Nikolaidis (2023), Appendix D.