QDpy
Loading...
Searching...
No Matches
structured_bop_elites.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Run paired Bayesian searches on an evaluation-limited constrained task."""
6
7from argparse import ArgumentParser
8import json
9from math import pi, sin
10from time import perf_counter
11
12import numpy as np
13
14from qdpy.algorithms import BOPElites
15from qdpy.containers import Grid
16
17
18def descriptors(genomes, latents):
19 """Use the same latent scalar in both descriptor coordinates."""
20 (source,) = latents.T
21 return np.column_stack((source, source + 0.22 * (genomes[:, 0] - 0.5)))
22
23
24def evaluate(genome):
25 """Report a known invalid point or a measured objective and one latent."""
26 x, y = genome
27 if x + 0.8 * y > 0.96 or y < 0.12 + 0.09 * sin(2 * pi * x):
28 return None
29 quality = 2 - (x - 0.7) ** 2 - (y - 0.75) ** 2
30 latent = 0.08 + 0.7 * y + 0.12 * (1 + sin(2 * pi * x)) / 2
31 return (quality,), (latent,)
32
33
34def comparison(budget, seed):
35 """Compare the four combinations at the same proposal budget."""
36 for name, feasibility, structured in [("default", False, False), ("feasibility", True, False),
37 ("structured", False, True), ("both", True, True)]:
38 archive = Grid(shape=(8, 8), fitness_domain=((0., 2.),), features_domain=((0., 1.),) * 2)
39 options = dict(descriptor_transform=descriptors, latent_size=1, posterior_samples=128) if structured else {}
40 search = BOPElites(archive, budget, 2, (0., 1.), 0., initial_samples=12,
41 candidate_pool=32, restarts=1, seed=seed, model_feasibility=feasibility,
42 optimisation_task="maximisation", **options)
43 invalid = 0
44 model_seconds = 0.
45 start = perf_counter()
46 for _ in range(budget):
47 modelled = search.nb_suggestions >= search.initial_samples and search.n_observations >= 2
48 before_ask = perf_counter()
49 (genome,) = search.ask_batch()
50 if modelled:
51 model_seconds += perf_counter() - before_ask
52 measurement = evaluate(genome)
53 if measurement is None:
54 invalid += 1
55 feedback = (None, None, {"bop_valid": False}) if feasibility else None
56 else:
57 fitness, latent = measurement
58 if structured:
59 feedback = (fitness, None, {"bop_latents": latent})
60 else:
61 measured = descriptors(np.array([genome]), np.array([latent]))[0]
62 feedback = fitness, tuple(measured)
63 search.tell_batch([feedback])
64 yield dict(method=name, seed=seed, attempts=search.nb_evaluations, invalid_attempts=invalid,
65 feasible_observations=search.n_observations, coverage=archive.filled_bins / 64,
66 qd_score=archive.qd_score(normalized=False), model_seconds=model_seconds,
67 seconds=perf_counter() - start)
68
69
70if __name__ == "__main__":
71 parser = ArgumentParser(description=__doc__)
72 parser.add_argument("--budget", type=int, default=96)
73 parser.add_argument("--seeds", type=int, nargs="+", default=[11, 29, 47])
74 args = parser.parse_args()
75 for seed in args.seeds:
76 for row in comparison(args.budget, seed):
77 print(json.dumps(row, sort_keys=True, allow_nan=False))