3"""Run five small CPU searches and their comparison workflows."""
6from itertools
import combinations
14 DiversityEnhancingNSGAII,
23 return tuple(rng.uniform(-1., 1., 2))
26def real_mutation(genome, rng):
27 return tuple(np.clip(np.asarray(genome) + rng.normal(0., .2, 2), -1., 1.))
31 rng = np.random.default_rng(534)
32 targets = rng.uniform(-1., 1., (10, 2))
33 triples = list(combinations(range(len(targets)), 3))
34 subsets = tuple(triples[i]
for i
in rng.choice(len(triples), 11, replace=
False)) + (tuple(range(10)),)
36 def objectives(genome, rollout_seed):
37 return np.exp(-2. * np.square(targets - genome).sum(axis=1))
39 move =
MOVE(len(targets), sample=real_sample, mutate=real_mutation, subsets=subsets, seed=seed)
40 cmoea =
CMOEA(len(targets), sample=real_sample, mutate=real_mutation, subsets=subsets,
41 bin_capacity=1, batch_size=len(subsets), seed=seed)
42 move.run(objectives, 360)
43 cmoea.run(
lambda genome, rollout_seed: (objectives(genome, rollout_seed), genome), 360)
45 "move_evaluations": move.stats.evaluations,
46 "cmoea_evaluations": cmoea.stats.evaluations,
47 "move_mean_best_objective": float(np.max([row.objectives
for row
in move.champions], axis=0).mean()),
48 "cmoea_mean_best_objective": float(np.max([rows[0].task_scores
for rows
in cmoea.bins.values()], axis=0).mean()),
49 "cross_slot_replacements": move.stats.goal_switches,
55 return tuple(map(int, rng.integers(0, 2, size=12)))
57 def mutate(genome, rng):
59 flip = int(rng.integers(len(child)))
60 child[flip] = 1 - child[flip]
63 def evaluate(genome, rollout_seed):
64 selected = frozenset(i
for i, enabled
in enumerate(genome)
if enabled)
65 quality = sum((i % 4) + 1
for i
in selected)
66 return (float(quality), -float(len(selected))), selected
72 for name, standard
in ((
"dissimilarity",
False), (
"objective_crowding",
True)):
74 standard_crowding=standard, population_size=16, seed=seed)
75 search.run(evaluate, 240)
77 "evaluations": search.stats.evaluations,
78 "mean_dissimilarity": float(np.mean([distance(a.behavior, b.behavior)
79 for a, b
in combinations(search.population, 2)])),
80 "best_quality": max(row.objectives[0]
for row
in search.population),
86 def evaluate(genome, rollout_seed):
88 reward = max(.84 - 2. * (x + .72) ** 2, 1. - 2. * (x - .72) ** 2) - .25 * y ** 2
91 rng = np.random.default_rng(seed)
92 initial = [real_sample(rng), real_sample(rng)]
93 method =
EDOCS(2, archive_size=8, clusters=2, probes=6, es_steps=2, ind_domain=(-1., 1.), seed=seed)
94 baseline =
DvDES(initial, pairs=3, ind_domain=(-1., 1.), seed=seed)
95 method.run(evaluate, 268)
96 baseline.run(evaluate, 268)
98 "edo_evaluations": method.stats.evaluations,
99 "dvd_evaluations": baseline.stats.evaluations,
100 "edo_best_reward": max(row.reward
for row
in method.archive),
101 "dvd_best_reward": max(row.reward
for row
in baseline.population),
102 "edo_arm_observations": method.stats.arm_counts,
107 x_train = np.linspace(-2., 2., 28)
108 x_test = np.linspace(-1.98, 1.98, 45)
111 return np.cos(1.7 * x) + .3 * np.sin(3. * x)
113 def predictions(genome, x):
114 amplitude, phase, offset = genome
115 return amplitude * np.cos(1.7 * x + phase) + offset
118 return tuple(rng.uniform(-1., 1., 3))
120 def mutate(genome, rng):
121 return tuple(np.clip(np.asarray(genome) + rng.normal(0., .2, 3), -1.5, 1.5))
123 labels = target(x_train)
126 def evaluate(genome, rollout_seed):
127 prediction = predictions(genome, x_train)
128 loss = float(np.square(labels - prediction).mean())
129 measured.append((loss, genome))
130 return loss, prediction
132 def crossover(parent, donor, rng):
133 return tuple(np.where(rng.random(3) < .5, parent, donor))
135 search =
CosineMAPElites(labels, sample=sample, mutate=mutate, crossover=crossover,
136 population_size=24, shape=(8, 8), seed=seed)
137 search.run(evaluate, 288)
138 size = min(5, len(search.archive))
139 evolved = search.predict(
lambda genome: predictions(genome, x_test), size=size)
141 unique = {genome: loss
for loss, genome
in measured}
142 best = sorted(unique, key=unique.get)[:size]
143 quality_only = np.mean([predictions(genome, x_test)
for genome
in best], axis=0)
145 "evaluations": search.stats.evaluations,
146 "archive_size": len(search.archive),
147 "ensemble_size": size,
148 "cosine_test_mse": float(np.square(target(x_test) - evolved).mean()),
149 "quality_only_test_mse": float(np.square(target(x_test) - quality_only).mean()),
154 def simulate(genome, rollout_seed):
156 quality = .8 + .2 * x - .25 * y ** 2
157 return quality, (x, y)
159 def target(genome, rollout_seed):
161 return (x, y - .44 * (x + 1.) / 2.)
164 for name, training_calls
in ((
"calibrated", 6), (
"simulation_only", 0)):
165 search =
TBREvolution(sample=real_sample, mutate=real_mutation, population_size=12, neighbors=4,
166 novelty_threshold=.24, transfer_threshold=.26, transfer_period=2, seed=seed)
167 search.run(simulate, 192, evaluate_target=target, target_budget=training_calls)
169 audit_rng = np.random.default_rng(731)
170 indices = audit_rng.choice(len(search.archive), min(8, len(search.archive)), replace=
False)
171 entries = [search.archive[i]
for i
in indices]
172 measurements = [target(row.genome, 0)
for row
in entries]
173 errors = [np.linalg.norm(np.asarray(row.descriptor) - actual)
174 for row, actual
in zip(entries, measurements)]
175 coordinates = (np.asarray(measurements) - (-1., -1.44)) / (2., 2.44)
176 occupied = {tuple(cell)
for cell
in np.clip((5 * coordinates).astype(int), 0, 4)}
178 "simulator_calls": search.stats.simulation_evaluations,
179 "target_training_calls": search.stats.target_evaluations,
180 "target_audit_calls": len(errors),
181 "transferable_fraction": float(np.mean(np.asarray(errors) <= .26)),
182 "mean_target_error": float(np.mean(errors)),
183 "target_cells": len(occupied),
190 "structural": structural,
191 "clusters": clusters,
192 "ensemble": ensemble,
193 "transfer": transfer,
198 parser = argparse.ArgumentParser(description=__doc__)
199 parser.add_argument(
"--case", choices=(
"all", *CASES), default=
"all")
200 parser.add_argument(
"--seed", type=int, default=42)
201 args = parser.parse_args()
202 selected = CASES
if args.case ==
"all" else {args.case: CASES[args.case]}
203 report = {name: run(args.seed)
for name, run
in selected.items()}
204 print(json.dumps({
"seed": args.seed, **report}, indent=2, allow_nan=
False))
207if __name__ ==
"__main__":
Preserve solutions to task combinations via local nondominated survival.
Fit cosine-kernel PCA over symmetric prediction references and retain one elite per cell.
Rank by quality and average diversity, then crowd by dissimilarity.
Optimize multiple centers with local rewards and shared population volumes.
Cluster live descriptors and independently update each representative with ES.
Breed one child per slot and compare it against all slots by strict majority.
Breed by novelty, local quality, and local predicted transferability.