66 """Search with interpretable descriptors and reuse historical elite measurements.
68 Evaluators receive a real-valued genome and private integer seed.
69 Return scalar fitness and a mapping of named finite measurements.
70 Include all active axes and optionally any measurements for future
71 swaps. The search owns its Grid and replaces it after a successful
72 rebuild. Retaining a reference to the old Grid gives a snapshot.
74 :param axes: Nonempty sequence of distinct DescriptorAxis objects.
75 :param dimension: Number of real genome coordinates.
76 :param fitness_domain: Inclusive scalar reporting fitness bounds.
77 :param maximize: True to prefer higher fitness.
78 :param ind_domain: Shared finite interval for all genome coordinates.
79 :param iso_sigma: Independent mutation standard deviation.
80 :param line_sigma: Shared directional mutation standard deviation.
81 :param seed: Seed for proposals, evaluations, and separate refreshes.
84 def __init__(self, axes, dimension, *, fitness_domain, maximize=True, ind_domain=(0., 1.),
85 iso_sigma=.05, line_sigma=.2, seed=0):
87 self.
_dimension = _positive_int(dimension,
"dimension")
88 self.
_bounds = _search_bounds(_variation_settings(ind_domain, iso_sigma, line_sigma))
89 self.
_iso, self.
_line = iso_sigma, line_sigma
90 low, high = map(float, fitness_domain)
92 raise ValueError(
"Use an increasing fitness interval.")
94 self.
_weights = (1.,)
if maximize
else (-1.,)
97 np.random.default_rng, np.random.SeedSequence(seed).spawn(3))
101 def _checked_axes(axes):
103 if (
not axes
or any(
not isinstance(axis, DescriptorAxis)
for axis
in axes)
104 or len({axis.name
for axis
in axes}) != len(axes)):
105 raise ValueError(
"Supply distinct named DescriptorAxis objects.")
108 def _grid(self, axes):
109 return Grid(shape=tuple(axis.bins
for axis
in axes), features_domain=tuple(axis.bounds
for axis
in axes),
114 """Return the active native grid."""
119 """Return the active named axes in grid order."""
124 """Snapshot cumulative evaluation and switch counts."""
128 def _put(archive, axes, genome, fitness, scores):
129 if (
not isinstance(scores, Mapping)
130 or any(
not isinstance(name, str)
or not isinstance(value, Real)
or not isfinite(value)
131 for name, value
in scores.items())):
132 raise ValueError(
"Return a mapping from names to finite measurements.")
133 missing = [axis.name
for axis
in axes
if axis.name
not in scores]
135 raise ValueError(f
"Missing active descriptor measurements: {', '.join(missing)}.")
136 measurement = _measurement(archive, ((fitness,), tuple(scores[axis.name]
for axis
in axes)), genome)
137 measurement[0].scores.update({name: float(value)
for name, value
in scores.items()})
138 _offer(archive, measurement)
142 return int(rng.integers(2**32))
145 def run(self, evaluate, budget):
146 """Return cumulative statistics. Evaluator errors propagate to the caller."""
147 if not callable(evaluate):
148 raise TypeError(
"Provide a callable evaluator.")
149 budget = _positive_int(budget,
"budget")
151 for _
in range(budget):
153 first, second = rng.integers(len(archive), size=2)
154 genome = iso_line_dd(archive[first], archive[second], bounds=self.
_bounds,
155 iso_sigma=self.
_iso, line_sigma=self.
_line, rng=rng)
158 genome = tuple(map(float, genome))
161 self.
_put(archive, self.
_axes, genome, fitness, scores)
166 """Project current elites onto supplied axes without new evaluator calls."""
168 report = self.
_grid(axes)
170 self.
_put(report, axes, elite, elite.fitness.values[0], elite.scores)
174 def switch_axis(self, name, replacement, *, refresh=None, refresh_budget=0):
175 """Replace an axis and rebuild the active grid as one operation.
177 Keep the best elite in each cell of the unaffected projection.
178 With one axis, keep the single best elite. Reuse cached values
179 for the replacement axis. If a survivor lacks that measurement,
180 refresh(genome, seed) must return its new scalar descriptor. The
181 refresh budget must cover all missing values before any call is
182 made. On failure the original grid remains active and attempted
183 refreshes are still counted.
185 if not isinstance(replacement, DescriptorAxis):
186 raise TypeError(
"replacement must be a DescriptorAxis.")
187 names = [axis.name
for axis
in self.
_axes]
188 if name
not in names:
189 raise ValueError(
"The requested descriptor axis is not active.")
190 slot = names.index(name)
191 previous = self.
_axes[slot]
193 if previous == replacement:
194 raise ValueError(
"The replacement must change the active axis.")
195 budget = index(refresh_budget)
196 if budget < 0
or (refresh
is not None and not callable(refresh)):
197 raise ValueError(
"Use a nonnegative refresh budget and a callable refresh.")
198 other_axes = tuple(axis
for i, axis
in enumerate(self.
_axes)
if i != slot)
200 survivors = self.
report(other_axes)
if other_axes
else ((source.best,)
if source
else ())
201 missing = sum(replacement.name
not in elite.scores
for elite
in survivors)
202 if missing
and (refresh
is None or missing > budget):
203 raise ValueError(f
"The rebuild needs {missing} refresh evaluations.")
204 destination = self.
_grid(axes)
206 for elite
in survivors:
207 scores = dict(elite.scores)
208 if replacement.name
not in scores:
210 scores[replacement.name] = refresh(tuple(elite), self.
_seed(self.
_refresh))
211 self.
_put(destination, axes, elite, elite.fitness.values[0], scores)
214 return DescriptorSwitch(previous, replacement, len(source), len(survivors), len(destination),