5"""Run an inspectable MAP-Elites experiment or regenerate figures from it."""
8from pathlib
import Path
11from qdpy.bundles import RunBundleOptions, open_run_bundle
16def figures(bundle, output):
17 """Generate a paired archive and ancestry figure and a GIF time-lapse."""
18 import matplotlib.pyplot
as plt
21 from qdpy.plots import animate_archives, plot_archive, plot_lineage
23 output.mkdir(parents=
True, exist_ok=
True)
24 if any(output.iterdir()):
25 raise FileExistsError(f
"Figure directory must be empty: {output}")
26 nodes = read_lineage(bundle.lineage_path)
27 resumed_from = bundle.manifest[
"bundle"].get(
"resumed_from")
29 if resumed_from
is not None:
30 crossings = [(parent, child.id)
for child
in nodes.values()
31 if child.evaluation
is not None and child.evaluation > resumed_from
32 for parent
in child.parents
33 if nodes[parent].evaluation
is not None and nodes[parent].evaluation <= resumed_from]
34 fig, axes = plt.subplots(1, 2, figsize=(8.7, 4.1), layout=
"constrained")
35 plot_archive(bundle.archive_path, ax=axes[0])
36 plot_lineage(nodes, elite=bundle.best_lineage_id, highlight_edges=crossings, ax=axes[1])
37 axes[0].set_title(f
"Archive at {bundle.manifest['evaluation']['attempted']} evaluations")
38 fig.savefig(output /
"archive-and-lineage.png", dpi=130)
40 animate_archives(bundle.history_paths, output /
"history.gif", fps=4, encoder=
"pillow",
41 show_progress=
True, highlight_changes=
True)
46 """Keep search and plot-only entry points available from any working directory."""
47 parser = argparse.ArgumentParser(description=__doc__)
48 parser.add_argument(
"directory", type=Path)
49 parser.add_argument(
"--budget", type=int, default=96)
50 parser.add_argument(
"--seed", type=int)
51 parser.add_argument(
"--stop-after", type=int)
52 mode = parser.add_mutually_exclusive_group()
53 mode.add_argument(
"--resume-trusted", action=
"store_true")
54 mode.add_argument(
"--plot-only", action=
"store_true")
55 parser.add_argument(
"--figures", type=Path)
56 args = parser.parse_args(argv)
57 if args.plot_only
and args.stop_after
is not None:
58 parser.error(
"--stop-after applies to a search.")
60 if not args.plot_only:
61 parameters = dict(dimension=4, budget=args.budget, shape=(8, 8), batch_size=8,
62 fitness_domain=((0., 1.),), features_domain=((0., 1.),) * 2,
63 optimisation_task=
"maximisation")
64 options = RunBundleOptions(history_every=2, lineage=
True, checkpoint_every=2,
65 stop_after=args.stop_after, resume=args.resume_trusted,
66 trusted=args.resume_trusted)
67 seed = args.seed
if args.resume_trusted
or args.seed
is not None else 329
68 result = run(map_elites(**parameters), illumination_rastrigin_normalised,
69 seed=seed, output_dir=args.directory, configuration=parameters, bundle=options)
70 print(f
"{result.evaluations} evaluations in this invocation")
72 bundle = open_run_bundle(args.directory)
73 status = bundle.manifest[
"bundle"][
"status"]
74 attempts = bundle.manifest[
"evaluation"][
"attempted"]
75 print(f
"{status.capitalize()} at {attempts} evaluations. Verified {bundle.manifest_path}")
76 if args.plot_only
or args.figures
is not None:
77 figures(bundle, args.figures
or args.directory /
"figures")
80if __name__ ==
"__main__":