Constrained MAP-Elites with mixed variables¶
Find feasible designs across a grid when the input combines real values, integer counts, and categorical choices. The example varies size, layer count, and material. It reports one low-cost design per cell of the size and layer grid.
Run from the repository root with the base package.
python examples/constrained_map_elites.py
# SPDX-License-Identifier: LGPL-3.0-or-later
## @file
# @brief Find feasible designs across a grid using mixed variables.
"""Search a synthetic design space with real, integer, and categorical variables."""
from qdpy.algorithms import CategoricalVariable, ConstrainedMAPElites, IntegerVariable, RealVariable
from qdpy.containers import Grid
from qdpy.simple import run
def make_search(budget, *, batch_size=1, infeasible_parent_probability=0.5):
"""Build the same design space and reporting grid for each run."""
grid = Grid(shape=(8, 4), fitness_domain=((0.0, 2.0),),
features_domain=((0.0, 1.0), (0.5, 4.5)))
return ConstrainedMAPElites(
grid, budget, [RealVariable(0, 1), IntegerVariable(1, 4),
CategoricalVariable(("light", "reinforced"))],
constraint_names=("strength", "clearance"), batch_size=batch_size,
infeasible_parent_probability=infeasible_parent_probability,
)
def evaluate_design(design):
"""Return a cost, two descriptors, and residuals for this synthetic task."""
size, layers, material = design
reinforced = material == "reinforced"
cost = (size - 0.35) ** 2 + 0.08 * layers + 0.15 * reinforced
return (cost,), (size, layers), {
"strength": 0.6 - (size + 0.15 * layers + 0.2 * reinforced),
"clearance": size + 0.10 * layers - 1.1,
}
if __name__ == "__main__":
search = make_search(24)
def evaluate(candidate):
"""Translate the numeric genome before evaluating it."""
return evaluate_design(search.decode(candidate))
result = run(search, evaluate, seed=7)
print(f"Attempts: {result.evaluations}")
print(f"Feasible cells: {result.archive.filled_bins}/{len(result.archive.solutions)}")
print(f"Near-feasible cells: {len(search.infeasible_elites)}")
print(f"Best design: {search.decode(result.best)}, cost {result.best.fitness.values[0]:.4f}")
qdpy.algorithms.ConstrainedMAPElites starts with an empty,
best-only grid and one objective. Declare the variables in genome
order. Candidates store integer category indices. Call
search.decode(candidate) to recover the original labels.
Return a fitness tuple, the descriptors, and a dictionary with one finite residual per configured constraint. Each residual must be at most zero for the candidate to enter the result grid. For a lower strength bound, subtract measured strength from required strength. Use separate score names for the objective and the residuals.
The search keeps one near-feasible candidate per cell, ranked by the
sum of positive residuals and then by weighted fitness. Scale
residuals to comparable units if they should have similar influence.
The default draws from either archive with equal probability when
both have entries. Set infeasible_parent_probability=0 to use
feasible parents exclusively.
With seed 7, the example reports seven feasible cells, three
near-feasible cells, and a best raw cost of about 0.1603. Read
search.infeasible_elites to inspect the near-feasible candidates.
The feasible grid supplies coverage, QD score, and the best result.
If that grid is empty after the budget is spent, run() raises
RuntimeError. The counters and near-feasible archive remain
available on the search object.
Use ask_batch() and tell_batch(results) for manual evaluation.
Each batch draws parents from the archives at the start of that batch.
Missing or nonfinite residuals consume an attempt and trigger a warning.
The mixed-variable search follows Brevault and Balesdent (2024). For the two-archive strategy, see Zammit et al. (2023). See Constrained search and GAME pilots for the three-way comparison.