# SPDX-License-Identifier: LGPL-3.0-or-later ## @file # @brief Save a constrained search and re-check the saved genomes. """Find feasible low-cost designs across descriptor cells.""" from qdpy.algorithms import ConstrainedMAPElites, RealVariable from qdpy.containers import Grid from qdpy.results import load_grid, read_run_manifest from qdpy.simple import run def evaluate(candidate): """Return one cost, one descriptor, and the residual for x + y <= 1.""" x, y = candidate return (x**2,), (y,), {"capacity": x + y - 1.0} def main(): """Read the feasible archive and check its largest constraint residual.""" archive = Grid(shape=8, fitness_domain=((0.0, 1.0),), features_domain=((0.0, 1.0),)) search = ConstrainedMAPElites( archive, budget=64, variables=[RealVariable(0, 1), RealVariable(0, 1)], constraint_names=("capacity",), batch_size=8, optimisation_task="minimisation", ) result = run(search, evaluate, seed=42, output_dir="first-constrained") manifest = read_run_manifest(result.manifest_path) snapshot = manifest["archive"] restored = load_grid( result.manifest_path.with_name(snapshot["file"]), expected_sha256=snapshot["sha256"], ) largest_residual = max(evaluate(candidate)[2]["capacity"] for candidate in restored) print(f"Candidate attempts: {result.evaluations}") print(f"Feasible cells: {restored.filled_bins}/{restored.shape[0]}") print(f"Best feasible cost: {restored.best.fitness.values[0]:.6f}") print(f"Largest residual: {largest_residual:.6f}") if __name__ == "__main__": main()