QDpy
Loading...
Searching...
No Matches
me_gide.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5import argparse
6import json
7from time import perf_counter
8
9import numpy as np
10
11from qdpy.algorithms import GradientEvaluation, MEGIDE
12from qdpy.containers import Grid
13
14
15def benchmark(seed, budget):
16 """Compare entropy-guided and uniform category selection on one problem."""
17 rng = np.random.default_rng(seed)
18 nvec = (2, 3, 4, 2, 3, 4, 4, 2, 3, 2, 4, 3)
19 offsets = np.cumsum((0, *nvec[:-1]))
20 quality = rng.uniform(0, 1, sum(nvec)) / len(nvec)
21 first = np.concatenate([np.linspace(0, 1, n) for n in nvec[:6]]) / 6
22 second = np.concatenate([np.linspace(0, 1, n) for n in nvec[6:]]) / 6
23 jacobian = np.stack([quality, np.pad(first, (0, len(second))), np.pad(second, (len(first), 0))])
24
25 def evaluate(candidate):
26 # The exact discrete evaluations sum the chosen columns. These same
27 # columns are derivatives of the linear one-hot relaxation.
28 fitness, *descriptor = jacobian[:, offsets + candidate].sum(axis=1)
29 return GradientEvaluation(fitness, np.clip(descriptor, 0, 1), jacobian)
30
31 for method, entropy in (("me-gide", .65), ("uniform-edits", 1.)):
32 grid = Grid(shape=(6, 6), fitness_domain=((0, 1),), features_domain=((0, 1),) * 2,
33 fitness_weights=(1.,))
34 search = MEGIDE(grid, budget, nvec, 16, entropy_fraction=entropy, seed=seed)
35 start = perf_counter()
36 search.optimise(evaluate)
37 yield dict(method=method, seed=seed, evaluations=search.nb_evaluations,
38 coverage=grid.filled_bins / 36, qd_score=sum(ind.fitness.values[0] for ind in grid),
39 best=grid.best.fitness.values[0], seconds=perf_counter() - start)
40
41
42if __name__ == "__main__":
43 parser = argparse.ArgumentParser(description=__doc__)
44 parser.add_argument("--budget", type=int, default=768)
45 parser.add_argument("--seeds", nargs="+", type=int, default=[11, 22, 33])
46 args = parser.parse_args()
47 for seed in args.seeds:
48 for result in benchmark(seed, args.budget):
49 print(json.dumps(result, sort_keys=True))