34 """Run MAP-Elites with UCB parent selection and one-parent variation.
36 :param container: Grid with one retained elite per cell.
37 :param budget: Candidate evaluation budget.
38 :param dimension: Number of real genome coordinates.
39 :param arms: Use an independent history per "individual" or retain
40 counts by "cell" as elites are replaced.
41 :param exploration: Nonnegative coefficient of the UCB exploration term.
42 :param mutate: Optional callable taking an immutable parent genome and
43 the private generator. The default adds bounded Gaussian noise.
44 :param seed: Seed for initialization, mutation and tie breaks.
47 def __init__(self, container, budget, dimension, *, arms="individual", exploration=1 / np.sqrt(2),
48 sigma=.05, ind_domain=(0., 1.), mutate=
None, seed=0, **kwargs):
49 if not isinstance(container, Grid)
or container.max_items_per_bin != 1:
50 raise ValueError(
"MonteCarloElites needs a best-only Grid or CVTGrid.")
51 if arms
not in (
"individual",
"cell")
or not 0 <= exploration < np.inf
or not 0 <= sigma < np.inf:
52 raise ValueError(
"Use individual or cell arms and finite nonnegative exploration and sigma.")
53 if mutate
is not None and not callable(mutate):
54 raise TypeError(
"mutate must be callable.")
57 if container.fitness_weights
is not None:
58 kwargs.setdefault(
"optimisation_task", container.fitness_weights)
59 super().__init__(container, _positive_int(budget,
"budget"),
60 dimension=_positive_int(dimension,
"dimension"), **kwargs)
63 """Reset arm histories and the seeded generator while retaining elites."""
65 self.
_rng = np.random.default_rng(self.
seed)
74 if self.
arms ==
"cell":
81 choice = self.
_rng.choice(np.flatnonzero(scores == scores.max()))
82 parent, arm = tuple(elites[choice]), arms[choice]
87 genome = np.asarray(self.
mutate(parent, self.
_rng), dtype=float)
88 if genome.shape != (self.
dimension,)
or not np.all(
90 raise ValueError(
"Mutation must return a finite genome in the configured bounds.")
94 self.
_pending[id(base_ind)] = (base_ind, arm)
98 arm = self.
_pending.pop(id(individual),
None)
100 self.
_pending[id(evaluated)] = (evaluated, arm[1])
102 def _forget_suggestion(self, individual):
103 self.
_pending.pop(id(individual),
None)
106 arm = self.
_pending.pop(id(individual),
None)
107 if arm
is not None and added_to_container:
108 cell = self.
container.index_grid(individual)
109 arm[1].successes += any(elite
is individual
for elite
in self.
container.solutions[cell])
111 def __getstate__(self):
112 state = super().__getstate__()
113 state[
"_pending"] = {}
118 def __setstate__(self, state):