QDpy
Loading...
Searching...
No Matches
discount_model_search.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Compare learned discounts and cell thresholds on shared CVT geometry."""
6
7import argparse
8
9from scipy.stats import qmc
10
11from qdpy.algorithms import CMAMAE, DiscountModelSearch
12from qdpy.benchmarks import rastrigin_normalised
13from qdpy.containers import CVTGrid
14from qdpy.simple import run
15
16UNIT = (0.0, 1.0)
17DESCRIPTOR_DIMENSION = 6
18
19
20def evaluate(candidate):
21 """Use normalised Rastrigin and the first six genome coordinates."""
22 return rastrigin_normalised(candidate), tuple(candidate[:DESCRIPTOR_DIMENSION])
23
24
25def run_search(algorithm, centroids):
26 """Build a fresh reporting archive and run a seeded search."""
27 archive = CVTGrid(
28 shape=len(centroids), centroids=centroids, fitness_domain=(UNIT,),
29 features_domain=(UNIT,) * DESCRIPTOR_DIMENSION,
30 )
31 search = algorithm(
32 archive, budget=24, x0=[0.5] * 8, sigma0=0.15, batch_size=4,
33 threshold_min=-1.0, learning_rate=0.1, ind_domain=UNIT,
34 )
35 result = run(search, evaluate, seed=42)
36 print(f"{algorithm.__name__}: {result.evaluations} evaluations, "
37 f"{archive.filled_bins}/{len(archive.solutions)} cells, "
38 f"QD score {archive.qd_score():.4f}, best raw fitness {result.best.fitness.values[0]:.4f}")
39 return search
40
41
42if __name__ == "__main__":
43 parser = argparse.ArgumentParser(description=__doc__)
44 parser.add_argument("--compare-cma-mae", action="store_true", help="Run CMA-MAE on the same CVT centroids.")
45 args = parser.parse_args()
46
47 centroids = qmc.Sobol(DESCRIPTOR_DIMENSION, scramble=True, seed=42).random_base2(5)
48 search = run_search(DiscountModelSearch, centroids)
49
50 probes = [centroids[0], 0.95 * centroids[0] + 0.05 * centroids[1]]
51 cells = [int(search.container.index_grid(point)[0]) for point in probes]
52 print(f"Predicted discounts at points in cells {cells}: {search.predict_discount(probes).round(4)}")
53 if args.compare_cma_mae:
54 run_search(CMAMAE, centroids)