49 """MAP-Elites with the Gradient-Informed Discrete Emitter.
51 Return a :class:`GradientEvaluation` for each integer candidate. Its
52 Jacobian has rows for the objective followed by the descriptors, and
53 columns for adjacent one-hot blocks of the configured category counts.
54 Each call supplies the exact discrete measurements and derivatives of
55 a caller-provided smooth relaxation at that one-hot point.
57 Derivatives are cached for retained elites, so use a fixed evaluator
58 throughout a run. The entropy is controlled separately for each
59 proposal. Like the paper's softmax, each current category is included
60 in its block and may yield an unchanged proposal.
62 :param container: Best-only single-objective Grid or CVTGrid.
63 :param budget: Number of candidate calls, including the initialization.
64 :param nvec: Sequence of positive category counts, with at least two alternatives
65 in one coordinate. Categories are zero-indexed.
66 :param batch_size: Number of candidates selected from a frozen repertoire.
67 :param entropy_fraction: Desired entropy as a fraction of uniform entropy.
68 :param seed: Optional seed for initialization, parent sampling and edits.
71 _requires_proposed_genome =
True
73 def __init__(self, container, budget, nvec, batch_size=32, *, entropy_fraction=.5, seed=None, **kwargs):
74 if type(container)
not in (Grid, CVTGrid)
or container.max_items_per_bin != 1
or len(container.fitness_domain) != 1:
75 raise ValueError(
"MEGIDE requires a best-only single-objective Grid or CVTGrid.")
76 self.
nvec = tuple(_positive_int(n,
"category count")
for n
in nvec)
77 if not self.
nvec or max(self.
nvec) < 2
or not 0 <= entropy_fraction <= 1:
78 raise ValueError(
"Use a mutable categorical genome and entropy_fraction in [0, 1].")
83 kwargs.setdefault(
"optimisation_task", container.fitness_weights)
84 super().__init__(container, _positive_int(budget,
"budget"),
85 batch_size=_positive_int(batch_size,
"batch_size"),
86 dimension=len(self.
nvec), **kwargs)
89 """Reset accounting and proposals while preserving live elite gradients."""
91 self.
_rng = np.random.default_rng(self.
seed)
100 def _clear_generation(self):
101 super()._clear_generation()
107 base_ind[:] = self.
_rng.integers(self.
nvec).tolist()
110 parent = tuple(entry)
111 weights = self.
_rng.normal(size=basis.shape[0])
112 weights[0] = abs(weights[0])
113 gradient = weights @ basis
114 deltas = gradient - gradient[self.
_offsets[:-1] + parent][self.
_blocks]
118 base_ind[block] = int(edit - self.
_offsets[block])
121 def _tell_evaluation_result(self, individual, result, elapsed, verbose):
122 if not isinstance(result, GradientEvaluation):
123 raise TypeError(
"The MEGIDE evaluator must return a GradientEvaluation.")
124 descriptors = np.asarray(result.descriptors, dtype=float)
125 count = len(self.
container.features_domain)
126 jacobian = np.asarray(result.jacobian, dtype=float)
127 if (descriptors.shape != (count,)
or not np.isfinite(descriptors).all()
128 or jacobian.shape != (count + 1, self.
_offsets[-1])
or not np.isfinite(jacobian).all()):
129 raise ValueError(
"MEGIDE requires finite descriptors and one Jacobian column per category.")
130 self.
tell(individual, fitness=(float(result.fitness),), features=tuple(descriptors),
131 elapsed=elapsed, verbose=verbose)
137 for slot, individual
in enumerate(evaluated):
138 if individual
is None:
140 cell = self.
container.index_grid(individual)
142 offered[id(individual)] = slot
145 identifier = id(individual)
146 if identifier
in offered:
148 admitted.add(offered[identifier])