99 def __init__(self, grid_factory, mutate, *, task_budget, offspring=4,
112 def adapt(self, prior, task, evaluate, budget=None, *, seed=0):
113 """Run mutation-only MAP-Elites under a separate evaluation budget.
115 evaluate(task, genome) returns (scalar fitness, descriptors, success).
116 The success flag is independent of archive admission.
118 prior = _prior_population(prior)
119 budget = self.
task_budget if budget
is None else _positive_int(budget,
"budget")
120 if budget < len(prior)
or not callable(evaluate):
121 raise ValueError(
"Provide an evaluator and a budget that covers all prior genomes.")
123 _validate_grid(archive)
124 weights = tuple(archive.fitness_weights
or ())
125 if (archive.parents
or len(weights) != 1
or not isfinite(weights[0])
or weights[0] == 0
126 or archive.fitness_score_names
or archive.features_score_names):
127 raise ValueError(
"Use a parentless archive with a finite signed scalar fitness weight and native scores.")
128 rng = np.random.default_rng(seed)
132 for attempt
in range(budget):
133 if attempt < len(prior):
134 root, depth, genome = attempt, 0, prior[attempt]
139 parent = archive[rng.integers(len(archive))]
140 root, depth = parent.scores[
"faery_root"], parent.scores[
"faery_depth"] + 1
142 root = int(rng.integers(len(prior)))
143 parent, depth = prior[root], 1
144 genome = tuple(self.
_mutate(tuple(parent), rng))
146 raise ValueError(
"Mutation must return a nonempty genome.")
148 quality, features, solved = evaluate(task, genome)
149 quality, features = float(quality), tuple(map(float, features))
150 if (
not isfinite(quality)
or len(features) != len(archive.features_domain)
151 or not all(map(isfinite, features))
or not isinstance(solved, (bool, np.bool_))):
152 raise ValueError(
"Return finite fitness and descriptors, and a boolean success flag.")
154 if first_success
is None:
155 first_success = attempt + 1
156 successes.setdefault(genome,
FAERYSuccess(genome, root, depth))
157 if (archive.in_bounds((quality,), archive.fitness_domain)
158 and archive.in_bounds(features, archive.features_domain)):
159 cell = archive.index_grid(features)
160 if archive.free
or archive.solutions[cell]:
161 individual =
Individual(list(genome), fitness=
Fitness((quality,), weights=weights),
163 individual.scores[
"faery_root"] = root
164 individual.scores[
"faery_depth"] = depth
165 archive.add(individual)
167 return FAERYTaskResult(archive, tuple(successes.values()), budget, first_success)
168 return FAERYTaskResult(archive, tuple(successes.values()), attempt, first_success)
171 def fit(self, initial, tasks, evaluate, *, generations):
172 """Reassess the combined parent and offspring candidates on shared task panels."""
173 prior = _prior_population(initial)
175 generations = _positive_int(generations,
"generations")
177 if not 0 < sample_size <= len(tasks)
or not callable(evaluate):
178 raise ValueError(
"Provide enough training tasks and an evaluator.")
180 raise ValueError(
"task_budget must cover the prior and all outer offspring.")
182 rng = np.random.default_rng(self.
seed)
184 for _
in range(generations):
185 pool = dict.fromkeys(prior)
187 child = tuple(self.
_mutate(prior[rng.integers(size)], rng))
189 raise ValueError(
"Mutation must return a nonempty genome.")
190 pool.setdefault(child)
191 genomes = tuple(pool)
192 count = [0] * len(genomes)
193 depth_sum = [0] * len(genomes)
195 for task_index
in rng.choice(len(tasks), size=sample_size, replace=
False):
196 result = self.
adapt(genomes, tasks[task_index], evaluate, seed=int(rng.integers(2**32)))
197 calls += result.evaluations
198 for success
in result.successes:
199 count[success.root] += 1
200 depth_sum[success.root] += success.depth
201 depths = tuple(total / number
if number
else None for total, number
in zip(depth_sum, count))
202 speed = np.asarray([-depth
if depth
is not None else -self.
task_budget for depth
in depths])
203 utility = _pareto_utilities(np.asarray(count), speed)
204 selected = tuple(map(int, np.lexsort((rng.random(len(genomes)), -utility))[:size]))
205 prior = tuple(genomes[i]
for i
in selected)
206 history.append(
FAERYGeneration(genomes, tuple(count), depths, selected, calls))
207 return FAERYResult(prior, tuple(history), sum(row.evaluations
for row
in history))