5"""Compare LM-MA-MAE with CMA-MAE, and DDE-Elites with Iso+LineDD."""
10from time
import perf_counter
18def run(method, dimension, budget, seed):
19 """Run one bounded, shifted quadratic problem and report native QD metrics."""
22 archive = Grid(shape=(10, 10), max_items_per_bin=1, fitness_domain=((0., 1.),),
23 features_domain=((-1., 1.), (-1., 1.)))
24 target = np.linspace(-.5, .5, dimension)
25 weights = np.geomspace(.1, 1., dimension)
26 options = dict(container=archive, budget=budget, batch_size=16, ind_domain=(-1., 1.),
27 optimisation_task=
"max")
28 if method
in (
"lm-ma-mae",
"cma-mae"):
29 cls = LMMAMAE
if method ==
"lm-ma-mae" else CMAMAE
30 search = cls(x0=np.zeros(dimension), sigma0=.45, threshold_min=0., seed=seed, **options)
31 elif method ==
"dde-elites":
32 search = DDEElites(dimension=dimension, seed=seed, fit_epochs=2, fit_interval=2,
33 latent_size=4, iso_sigma=.05, **options)
35 search = IsoLineMAPElites(dimension=dimension, iso_sigma=.05, **options)
37 def evaluate(individual):
38 x = np.asarray(individual, dtype=float)
39 error = np.mean(weights * (x - target) ** 2)
40 return (float(np.exp(-error)),), x[:2]
42 start = perf_counter()
43 search.optimise(evaluate)
44 seconds = perf_counter() - start
47 if method ==
"lm-ma-mae":
48 matrix_bytes = search._strategy.paths.nbytes
49 elif method ==
"cma-mae":
50 matrix_bytes = search._strategy.sm.C.nbytes
52 scores = [individual.fitness.values[0]
for individual
in archive]
53 return dict(method=method, seed=seed, dimension=dimension, evaluations=search.nb_evaluations,
54 cells=len(archive), qd_score=sum(scores), best=max(scores), seconds=seconds,
55 adaptation_array_bytes=matrix_bytes, model_fits=getattr(search,
"model_fits", 0),
56 operators=getattr(search,
"operator_counts", {}))
59if __name__ ==
"__main__":
60 parser = argparse.ArgumentParser(description=__doc__)
61 parser.add_argument(
"methods", nargs=
"+", choices=(
"lm-ma-mae",
"cma-mae",
"dde-elites",
"iso-line"))
62 parser.add_argument(
"--dimensions", type=int, nargs=
"+", default=[32])
63 parser.add_argument(
"--budget", type=int, default=256)
64 parser.add_argument(
"--seeds", type=int, nargs=
"+", default=[11, 22, 33])
65 args = parser.parse_args()
66 for dimension
in args.dimensions:
67 for method
in args.methods:
68 for seed
in args.seeds:
69 print(json.dumps(run(method, dimension, args.budget, seed), sort_keys=
True))