# SPDX-License-Identifier: LGPL-3.0-or-later ## @file # @brief Use a learned discount and a fixed CVT result archive. """Compare learned discounts and cell thresholds on shared CVT geometry.""" import argparse from scipy.stats import qmc from qdpy.algorithms import CMAMAE, DiscountModelSearch from qdpy.benchmarks import rastrigin_normalised from qdpy.containers import CVTGrid from qdpy.simple import run UNIT = (0.0, 1.0) DESCRIPTOR_DIMENSION = 6 def evaluate(candidate): """Use normalised Rastrigin and the first six genome coordinates.""" return rastrigin_normalised(candidate), tuple(candidate[:DESCRIPTOR_DIMENSION]) def run_search(algorithm, centroids): """Build a fresh reporting archive and run a seeded search.""" archive = CVTGrid( shape=len(centroids), centroids=centroids, fitness_domain=(UNIT,), features_domain=(UNIT,) * DESCRIPTOR_DIMENSION, ) search = algorithm( archive, budget=24, x0=[0.5] * 8, sigma0=0.15, batch_size=4, threshold_min=-1.0, learning_rate=0.1, ind_domain=UNIT, ) result = run(search, evaluate, seed=42) print(f"{algorithm.__name__}: {result.evaluations} evaluations, " f"{archive.filled_bins}/{len(archive.solutions)} cells, " f"QD score {archive.qd_score():.4f}, best raw fitness {result.best.fitness.values[0]:.4f}") return search if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--compare-cma-mae", action="store_true", help="Run CMA-MAE on the same CVT centroids.") args = parser.parse_args() centroids = qmc.Sobol(DESCRIPTOR_DIMENSION, scramble=True, seed=42).random_base2(5) search = run_search(DiscountModelSearch, centroids) probes = [centroids[0], 0.95 * centroids[0] + 0.05 * centroids[1]] cells = [int(search.container.index_grid(point)[0]) for point in probes] print(f"Predicted discounts at points in cells {cells}: {search.predict_discount(probes).round(4)}") if args.compare_cma_mae: run_search(CMAMAE, centroids)