# 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}")