QDpy
Loading...
Searching...
No Matches
map_elites_lineage.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
4
5"""Record a mutation forest and plot the ancestors of one crossover elite."""
6
7import argparse
8from pathlib import Path
9
10import matplotlib.pyplot as plt
11
12from qdpy.algorithms import IsoLineCrossMAPElites, RandomSearchMutPolyBounded
13from qdpy.benchmarks import illumination_rastrigin_normalised
14from qdpy.containers import Grid
15from qdpy.lineage import LineageRecorder, read_lineage
16from qdpy.plots import plot_lineage
17from qdpy.simple import run
18
19
20def record(method, path, **options):
21 """Measure a small seeded search and return its log and retained IDs."""
22 archive = Grid(shape=(6, 6), features_domain=((0., 1.),) * 2, fitness_domain=((0., 1.),))
23 search = method(archive, budget=36, dimension=4, batch_size=4, **options)
24 with LineageRecorder(path).attach(search) as recorder:
25 run(search, illumination_rastrigin_normalised, seed=34)
26 retained = [recorder.id_for(elite) for elite in archive]
27 return read_lineage(path), retained
28
29
30def main(output):
31 """Draw the mutation forest beside the ancestry of a retained crossover elite."""
32 output.mkdir(parents=True, exist_ok=True)
33 mutation, _ = record(RandomSearchMutPolyBounded, output / "mutation.jsonl", sel_pb=1., init_pb=0., mut_pb=.45)
34 crossover, retained = record(IsoLineCrossMAPElites, output / "crossover.jsonl", crossover_probability=1.)
35 multi_parent = [node for node in retained if len(set(crossover[node].parents)) > 1]
36 chosen = max(multi_parent or retained)
37 fig, axes = plt.subplots(1, 2, figsize=(10.5, 4.9), layout="constrained")
38 plot_lineage(mutation, ax=axes[0])
39 plot_lineage(crossover, elite=chosen, ax=axes[1])
40 fig.savefig(output / "map-elites-lineage.png", dpi=140)
41 plt.close(fig)
42 print(f"Selected crossover elite {chosen}. The two lineage logs are in {output}.")
43
44
45if __name__ == "__main__":
46 parser = argparse.ArgumentParser(description=__doc__)
47 parser.add_argument("output", type=Path)
48 main(parser.parse_args().output)