Dynamic MAP-Elites

Refresh elites when the same genome can have a different objective or descriptor later in the run. The example changes the centre of a sphere and shifts its two descriptors at ticks 128 and 256.

Run from the repository root with the base package.

python examples/dynamic_map_elites.py
# SPDX-License-Identifier: LGPL-3.0-or-later
## @file
# @brief Search a shifting sphere and audit its archive under the final conditions.

"""Refresh elites as a four-variable sphere and its descriptor map shift."""

import numpy as np

from qdpy.algorithms import dynamic_map_elites, reevaluate_archive
from qdpy.containers import Grid


def make_archive():
    """Build a grid used by search and independent reporting."""
    return Grid(shape=(8, 8), fitness_domain=((0, 1),),
                features_domain=((0, 1), (0, 1)))


def evaluate(genome, tick):
    """Change objective centre and descriptor offsets at ticks 128 and 256."""
    centre, offsets = (
        (0.25, (0.0, 0.0)),
        (0.75, (0.20, -0.15)),
        (0.35, (-0.15, 0.20)),
    )[min(tick // 128, 2)]
    point = np.asarray(genome)
    fitness = float(np.mean((point - centre) ** 2))
    descriptors = np.clip(point.reshape(2, 2).mean(axis=1) + offsets, 0, 1)
    return (fitness,), tuple(descriptors)


def audit_at(archive, tick):
    """Return the share of unchanged entries and an independently rescored grid."""
    stored = {
        tuple(ind): (archive.get_ind_fitness(ind).values, tuple(archive.get_ind_features(ind)))
        for ind in archive
    }
    unchanged = 0

    def reevaluate(individual, _seed):
        nonlocal unchanged
        measurement = evaluate(individual, tick)
        unchanged += measurement == stored[tuple(individual)]
        return measurement

    audit = reevaluate_archive(archive, reevaluate, samples=1)
    return unchanged / len(archive) if archive else 0.0, audit


if __name__ == "__main__":
    budget = 384
    result = dynamic_map_elites(make_archive(), evaluate, dimension=4, budget=budget, seed=42)
    freshness, audit = audit_at(result.archive, budget)
    print(f"Attempts: {result.offspring_attempts} offspring + {result.refresh_attempts} refreshes")
    print(f"Detected changes: {len(result.detected_change_ticks)}")
    print(f"Archive freshness: {freshness:.1%}")
    print(f"Final audit: {audit.rollouts} evaluations, {audit.corrected_coverage:.1%} coverage, "
          f"QD score {audit.corrected_qd_score:.3f}")

Pass an empty, parentless, best-only grid to qdpy.algorithms.dynamic_map_elites(). The evaluator receives an immutable genome and a tick, which is the number of attempts already spent when a search step starts. Return ((fitness,), descriptors). An offspring and its refresh calls use the same tick.

With refresh="local", the runner reevaluates an occupied destination before comparing the offspring with its incumbent. If the incumbent’s descriptors move it to another occupied cell, that cell’s incumbent is also reevaluated. The runner follows the original grid and visits each incumbent once per step, including when moves form a cycle. Once the measurements are ready, it reinserts the refreshed entries in original archive order and offers the offspring last. Existing entries win ties at each insertion.

The budget covers both offspring and refresh attempts. If the runner cannot finish a refresh, it leaves the grid as it was at the start of that step and increments incomplete_updates. The calls already made still count. A detected change identifies a mismatch with the stored measurement. Several stale cells can reveal the same environmental shift on different ticks. Use a deterministic evaluator at a given tick, since the runner compares stored and fresh values exactly.

The example independently reevaluates the final grid at tick 384. Its freshness is the share of entries whose stored values are still correct. The corrected QD score and coverage use the descriptors measured at that tick. With seed 42, the search spent 203 attempts on offspring and 181 on refreshes. The audit used 50 additional calls and measured 50.0% freshness and 40.6% corrected coverage.

QDpy uses the replacee refresh procedure from Gallotta et al. (2024), with sequential Gaussian proposals and age-biased parent selection. See Fixed-task and dynamic QD pilots for comparisons with refresh="all" and refresh="none".