38 """Keep separate subpopulations for good designs and near-feasible ones.
40 Positive constraint residuals indicate violations. Feasible individuals
41 compete on user-provided fitness, infeasible individuals on total positive
42 residual. Both grids retain the configured number of breeders per cell.
43 Use hashable genome sequences and emit measurements for prospective axes.
56 def __init__(self, axes, *, fitness_domain, sample, mutate, crossover=None, maximize=True, cell_capacity=4,
57 initial_samples=8, anchor_probability=.2, seed=0):
58 axes = DescriptorMutationMAPElites._checked_axes(axes)
59 self.
_capacity = _positive_int(cell_capacity,
"cell_capacity")
60 self.
_initial = _attempt_budget(initial_samples)
61 low, high = map(float, fitness_domain)
62 if (
not low < high
or not all(map(callable, (sample, mutate)))
63 or crossover
is not None and not callable(crossover)
64 or not 0. <= anchor_probability <= 1.):
65 raise ValueError(
"Supply ordered fitness bounds, callable operators, and a probability in [0, 1].")
70 for child
in np.random.SeedSequence(seed).spawn(2))
77 def _grids(self, axes):
78 options = dict(shape=tuple(axis.bins
for axis
in axes), features_domain=tuple(axis.bounds
for axis
in axes),
79 storage_type=
"orderedset", max_items_per_bin=self.
_capacity)
80 return (
Grid(fitness_domain=(self.
_domain,), fitness_weights=(self.
_weight,), **options),
81 Grid(fitness_domain=((-np.inf, 0.),), fitness_weights=(1.,), **options))
90 def infeasible_archive(self):
109 def _evaluate(self, genome, evaluate):
111 seed = int(self.
_rollouts.integers(2**32))
113 result = evaluate(genome, seed)
117 fitness, scores, residuals = result
118 residuals = _finite_vector(residuals,
"constraint residuals", self.
_residual_count)
119 if not isfinite(fitness):
120 raise ValueError(
"Return finite scalar fitness.")
121 violation = fsum(max(0., residual)
for residual
in residuals)
123 before = grid.nb_added
124 DescriptorMutationMAPElites._put(grid, self.
_axes, genome, -violation
if violation
else fitness, scores)
137 if not callable(evaluate):
138 raise TypeError(
"Supply a callable evaluator.")
139 genome = tuple(genome)
148 """Reuse cached measurements. A failed rebuild leaves both live archives intact."""
149 axes = DescriptorMutationMAPElites._checked_axes(axes)
150 if axes == self.
_axes:
155 DescriptorMutationMAPElites._put(destination, axes, elite, elite.fitness.values[0], elite.scores)
161 def _parent(self, grid):
162 cells = np.flatnonzero(grid.nb_items_per_bin)
163 cell = np.unravel_index(cells[self.
_variation.integers(len(cells))], grid.shape)
164 members = grid.solutions[cell]
165 contestants = (members[i]
for i
in self.
_variation.integers(len(members), size=2))
166 return tuple(max(contestants, key=
lambda row: grid.fitness_weights[0] * row.fitness.values[0]))
171 def run(self, evaluate, budget):
172 if not callable(evaluate):
173 raise TypeError(
"Supply a callable evaluator.")
175 for _
in range(_attempt_budget(budget)):
182 grid = available[int(rng.integers(len(available)))]
186 genome = self.
_mutate(parent, rng)
__init__(self, axes, *, fitness_domain, sample, mutate, crossover=None, maximize=True, cell_capacity=4, initial_samples=8, anchor_probability=.2, seed=0)