# SPDX-License-Identifier: LGPL-3.0-or-later ## @file # @brief Use a separate Pareto front in each descriptor cell. """Use a separate Pareto front in each descriptor cell.""" from qdpy.algorithms import MOME from qdpy.containers import ParetoGrid from qdpy.simple import run def evaluate(candidate): """Return two conflicting costs and one descriptor.""" x, descriptor = candidate return (x ** 2, (1 - x) ** 2), (descriptor,) if __name__ == "__main__": unit = (0.0, 1.0) grid = ParetoGrid( shape=4, max_items_per_bin=5, fitness_domain=(unit, unit), features_domain=(unit,), fitness_weights=(-1.0, -1.0), ) search = MOME(grid, budget=24, dimension=2, batch_size=4) result = run(search, evaluate, seed=42) cell = max(grid.solutions, key=lambda index: len(grid.front(index))) reference = (1.0, 1.0) tradeoffs = sorted(tuple(round(float(cost), 4) for cost in ind.fitness.values) for ind in grid.front(cell)) print(f"Evaluations: {result.evaluations}") print(f"Occupied cells: {grid.filled_bins}/{len(grid.solutions)}") print(f"Raw cost pairs in cell {cell}: {tradeoffs}") print(f"Cell hypervolume: {grid.cell_hypervolume(cell, reference):.4f}") print(f"QD hypervolume: {grid.qd_hypervolume(reference):.4f}") print(f"Global hypervolume: {grid.global_hypervolume(reference):.4f}")