Changing constraints, structured niches, fidelity, and replay

The four workflows in this guide use the base QDpy installation. The example runs them on a six-job scheduling problem, a simulated fidelity ladder, and verified integer arithmetic. Changing constraints, structured niches, fidelity, and replay reports the comparisons and includes the per-seed data.

python -I examples/new_qd_families.py all --seeds 11 12 13 14 15

Changing score constraints

Use qdpy.algorithms.VariableConstraintMAPElites when the evaluator can measure quantities that users will later constrain. For example, a schedule evaluator can record completion times for two jobs, alongside tardiness-based fitness and a descriptor for the first scheduled job. The archive keeps one feasible elite per descriptor cell. A bounded history keeps other recent valid measurements, including candidates that missed an earlier deadline or lost to a better elite.

from qdpy.algorithms import VariableConstraintMAPElites
from qdpy.containers import Grid

archive = Grid(shape=(6,), features_domain=((0., 1.),),
               fitness_domain=((0., 1.),), fitness_weights=(1.,))
# schedule(order) returns scalar quality, a descriptor tuple, and a
# mapping with both scores. permutation(rng) and swap(parent, rng)
# return immutable tuples of job IDs.
search = VariableConstraintMAPElites(
    archive, score_names=("completion_0", "completion_4"),
    constraints={"completion_0": (None, 13)},
    history_size=100, propose=permutation, mutate=swap, seed=11,
)
search.run(schedule, budget=100)
search.set_constraints({"completion_0": (None, 9), "completion_4": (None, 9)})
print(len(archive), search.nb_evaluations)
search.run(schedule, budget=40)

The bounds are inclusive. Omit a name to remove its constraint and use None for an open bound. A call to set_constraints() filters cached measurements and repopulates the same grid, with zero evaluator calls. history_size=0 only reuses the current elites. Each cache entry assumes a stable measurement, so this workflow applies when the requirements change and the measured problem stays fixed. A quantity introduced outside the declared score schema requires a new measured run.

This workflow uses the changing requirements studied in QDA-VC. Shyne2026

Magnitude Go-Explore

Use a qdpy.algorithms.MagnitudeArchive when descriptors are objects such as permutations and you can define a dissimilarity between them. The archive places a descriptor according to the ordered indices of its closest reference landmarks. Ties follow the landmark order. Use supplied landmarks or fit them from a reference panel with magnitude-increasing swaps.

from qdpy.algorithms import MagnitudeArchive, MagnitudeGoExplore

# The reference panel contains distinct or repeated permutations.
# kendall(a, b) counts the job pairs that switch order.
archive = MagnitudeArchive.from_reference(
    reference, kendall, count=6, rank=2, capacity=12,
)
search = MagnitudeGoExplore(
    archive, propose=permutation, mutate=swap, seed=11,
)
search.optimise(lambda order: (schedule(order)[0], order), budget=128)
for cell, elite in archive.cells.items():
    print(cell, elite.fitness, archive.weights[cell])

The archive keeps a pairwise distance matrix for the retained elites. The magnitude solve uses the kernel \(\exp(-tD)\), where the automatic scale gives a strictly diagonally dominant matrix with nonnegative weights. Use a symmetric finite dissimilarity that is positive between distinct descriptors and zero for identical ones. When the archive reaches its capacity, a new niche competes with the existing elites by magnitude weight, with fitness breaking ties. Memory grows quadratically with capacity and the dense solve grows cubically, so start with a modest archive.

The bounded direct-evaluation variant uses the paper’s landmark signatures and diversity weighting. Each visit makes one offspring through the supplied mutation. The original paper also models local fitness and ranks a pool of unevaluated offspring before making a visit. Run selection="uniform" to control for parent weighting with the same archive and mutation. Huntsman2023

Verified strategy replay

qdpy.algorithms.EliteReplay builds a fixed repertoire for an external trainer. Declare the target strategies for each problem. Pass a frozen model or a deterministic generator to propose, and use an independent verifier to check correctness and label the achieved strategy. The shortest verified solution per problem and achieved cell wins. Choose the length function used by the trainer, such as the number of response tokens.

import numpy as np
from qdpy.algorithms import EliteReplay, EliteReplayDataset

search = EliteReplay({"17+19": ("direct", "compensate", "split")}, seed=11)
# propose(problem, target, parent, rng) returns solution text or None.
# verify(problem, solution) returns its achieved strategy or None.
dataset = search.run(propose, verify, budget=20)
dataset.save("arithmetic-replay.jsonl")

replay = EliteReplayDataset.load("arithmetic-replay.jsonl")
rng = np.random.default_rng(11)
batch = replay.sample(32, rng=rng)

Each replay draw samples a problem uniformly among those with solutions, then a populated cell uniformly within that problem. The caller maps problem IDs to prompts and tokenizes the saved solution text. For the EliteReplay training recipe, average the negative log probability over response tokens per example, then average over the batch. Combine that loss with the trainer’s GRPO loss using a replay coefficient.

The implementation covers search, verification, and replay data. The external trainer owns model updates. The offline example compares targeted and independent proposals with a fixed arithmetic verifier. Moussa2026