QDpy
Loading...
Searching...
No Matches
bop_elites.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Run a small Bayesian search with genuine evaluations and a fixed grid."""
6
7from qdpy.algorithms import BOPElites
8from qdpy.containers import Grid
9from qdpy.simple import run
10
11
12def evaluate(candidate):
13 """Return the raw cost and the product descriptor."""
14 x, y = candidate
15 return ((x - 0.2) ** 2 + (y - 0.4) ** 2,), (x * y,)
16
17
18if __name__ == "__main__":
19 unit = (0.0, 1.0)
20 archive = Grid(shape=4, fitness_domain=((0, 2),), features_domain=(unit,))
21 search = BOPElites(
22 archive, budget=24, dimension=2, ind_domain=unit, quality_floor=-2,
23 initial_samples=8, candidate_pool=16, restarts=2,
24 )
25 result = run(search, evaluate, seed=42)
26 print(f"Evaluations: {result.evaluations}")
27 print(f"Finite observations: {search.n_observations}")
28 print(f"Occupied cells: {archive.filled_bins}/{len(archive.solutions)}")
29 for cell, entries in archive.solutions.items():
30 if entries:
31 elite = entries[0]
32 print(f"Cell {cell}: descriptor {elite.features[0]:.4f}, raw cost {elite.fitness.values[0]:.4f}")