81 """Run fixed-budget per-cell evolution strategies on real parameter vectors.
83 :param archive: Nonempty fixed Grid or CVTGrid with one signed objective.
84 The best member of each occupied cell seeds the search.
85 :param steps: Gaussian rank-gradient updates per target.
86 :param population_size: Independent perturbations per update, at least two.
87 :param sigma: Parameter perturbation standard deviation.
88 :param learning_rate: Gradient ascent step size.
89 :param samples: Fresh rollouts used to place each final center by its mean.
90 :param ind_domain: Optional shared finite bounds for parameter coordinates.
91 :param complete: Continue from refined targets into adjacent empty bins.
92 :param seed: Seed for perturbations, frontier selection, and rollout seeds.
95 def __init__(self, archive, *, steps=10, population_size=32, sigma=.05,
96 learning_rate=.02, samples=16, ind_domain=None, complete=True, seed=0):
97 if (type(archive)
not in (Grid, CVTGrid)
or not len(archive)
98 or len(archive.fitness_domain
or ()) != 1
or len(archive.fitness_weights
or ()) != 1
99 or not np.isfinite(archive.fitness_weights[0])
or not archive.fitness_weights[0]
100 or not np.isfinite(archive.features_domain).all()
101 or archive.fitness_score_names):
102 raise ValueError(
"ARIA requires a nonempty fixed Grid/CVTGrid with finite features "
103 "and one native signed fitness objective.")
104 self.
_initial = {cell: deepcopy(_incumbent(archive, cell))
105 for cell, entries
in archive.solutions.items()
if entries}
106 vectors = np.asarray([list(ind)
for ind
in self.
_initial.values()], dtype=float)
107 self.
_bounds =
None if ind_domain
is None else _bounds(ind_domain)
108 if (vectors.ndim != 2
or not vectors.shape[1]
or not np.isfinite(vectors).all()
109 or not np.array_equal(vectors, self.
_clip(vectors))):
110 raise ValueError(
"Warm-start elites must be finite vectors with the same shape and bounds.")
111 if not 0 < sigma < np.inf
or not 0 < learning_rate < np.inf:
112 raise ValueError(
"sigma and learning_rate must be positive and finite.")
113 self.
steps = _positive_int(steps,
"steps")
114 self.
population_size = _positive_int(population_size,
"population_size")
116 raise ValueError(
"Use at least two perturbations per target update.")
117 self.
samples = _positive_int(samples,
"samples")
119 self.
_source = _stage_archive(archive)
121 self.
_source._max_items_per_bin = 1
123 self.
_source.discard_random_on_bin_overload =
False
124 self.
_direction = np.sign(archive.fitness_weights[0])
126 def _clip(self, vectors):
127 return vectors
if self.
_bounds is None else np.clip(vectors, *self.
_bounds)
129 def _refine(self, original, target, center, runner, rng):
130 point = np.asarray(original, dtype=float)
131 for _
in range(self.
steps):
134 for vector
in self.
_clip(point + self.
sigma * noise):
135 candidate = _fresh(original)
136 candidate[:] = vector.tolist()
137 candidates.append(candidate)
138 observations = runner.evaluate_groups([(candidate, 1)
for candidate
in candidates])
140 for rows
in observations:
142 ranks.append((-1, 0.))
146 inside = self.
_source.index_grid(row.features) == target
149 ranks.append((1, self.
_direction * row.fitness[0])
if inside
else
150 (0, -np.linalg.norm(np.asarray(row.features) - center)))
152 distinct = {value: rank
for rank, value
in enumerate(sorted(set(ranks)))}
153 utilities = (rankdata([distinct[value]
for value
in ranks]) - 1) / (len(ranks) - 1) - .5
155 candidate = _fresh(original)
156 candidate[:] = point.tolist()
157 rows, = runner.evaluate_groups([(candidate, self.
samples)])
158 measurement = _aggregate(self.
_source, candidate, rows, estimator=
lambda x: np.mean(x, axis=0))
if rows
else None
159 return candidate, measurement
166 def run(self, evaluate, budget, *, executor=None):
167 """Return a new archive. Untouched source cells retain their original elites."""
168 budget = _positive_int(budget,
"budget")
170 targets = budget // per_target
171 output = _stage_archive(self.
_source)
173 rng = np.random.default_rng([self.
seed, 2])
176 refined = completed = accepted = 0
178 def process(original, target):
180 candidate, measured = self.
_refine(original, target, centers[target], runner, rng)
181 visited[target] = candidate
182 if measured
is not None and _cell(output, measured) == target:
184 output._check_if_can_be_added(measured)
185 except (IndexError, ValueError):
187 accepted += output.add(measured)
is not None
189 for target, original
in self.
_initial.items():
190 if refined < targets:
191 process(original, target)
194 output.add(deepcopy(original))
196 frontier = {(source, target)
for source
in visited
for target
in neighbors[source]
197 if target
not in visited}
198 while frontier
and refined + completed < targets:
199 pairs = sorted(frontier)
200 source, target = pairs[rng.integers(len(pairs))]
201 process(visited[source], target)
202 frontier.difference_update((neighbor, target)
for neighbor
in neighbors[target])
203 frontier.update((target, neighbor)
for neighbor
in neighbors[target]
if neighbor
not in visited)
205 return ARIAResult(output, runner.attempted, runner.failed, refined, completed, accepted)