5"""Record a mutation forest and plot the ancestors of one crossover elite."""
8from pathlib
import Path
10import matplotlib.pyplot
as plt
12from qdpy.algorithms import IsoLineCrossMAPElites, RandomSearchMutPolyBounded
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
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)
42 print(f
"Selected crossover elite {chosen}. The two lineage logs are in {output}.")
45if __name__ ==
"__main__":
46 parser = argparse.ArgumentParser(description=__doc__)
47 parser.add_argument(
"output", type=Path)
48 main(parser.parse_args().output)