31 """Independently measure selection and archive descriptors.
33 The third item of the evaluator result is a score mapping. Store
34 the numeric selection vector under ``selection_score``. The archive
35 uses the ordinary feature vector and scalar fitness. All parents in
36 a batch come from the same measured archive snapshot.
38 :param container: Parentless best-only Grid or CVTGrid.
39 :param budget: Managed evaluation attempts.
40 :param dimension: Length of each real vector.
41 :param selection_size: Number of coordinates in the selection vector.
42 :param selection_score: Name of the vector in candidate scores.
43 :param mode: ``uniform``, ``novelty``, ``surprise``, ``blend``, or ``pareto``.
44 :param neighbors: Number of closest other elites for novelty.
45 :param clusters: Maximum number of cluster centers for forecasting surprise.
46 :param surprise_neighbors: Forecast centers averaged in the surprise score.
47 :param novelty_weight: Weight of novelty in the blended score.
48 :param ind_domain: Common interval for all genome coordinates.
49 :param iso_sigma: Independent Gaussian variation per coordinate.
50 :param line_sigma: Gaussian variation along the difference of two parents.
51 :param seed: Private seed for selection, variation, and forecasting.
52 :param batch_size: Evaluations per archive snapshot.
53 :param kwargs: Native optimiser options such as the objective direction.
56 _requires_proposed_genome =
True
58 def __init__(self, container, budget, dimension, *, selection_size, selection_score="selection",
59 mode="novelty", neighbors=15, clusters=12, surprise_neighbors=2, novelty_weight=.6,
60 ind_domain=(0., 1.), iso_sigma=.05, line_sigma=.2, seed=0, batch_size=16, **kwargs):
61 if (
not isinstance(container, Grid)
or container.parents
or container.max_items_per_bin != 1
62 or container.discard_random_on_bin_overload
or len(container.fitness_weights
or ()) != 1
63 or not np.isfinite(container.fitness_weights[0])
or not container.fitness_weights[0]):
64 raise ValueError(
"Supply a parentless best-only grid with one signed objective.")
65 if mode
not in (
"uniform",
"novelty",
"surprise",
"blend",
"pareto"):
66 raise ValueError(
"Choose uniform, novelty, surprise, blend, or pareto selection.")
67 if not isinstance(selection_score, str)
or not selection_score
or not 0. <= novelty_weight <= 1.:
68 raise ValueError(
"Supply a score name and a novelty weight in [0, 1].")
69 self.
ind_domain = _variation_settings(ind_domain, iso_sigma, line_sigma)
70 self.
_width = _positive_int(selection_size,
"selection_size")
72 self.
_neighbors = _positive_int(neighbors,
"neighbors")
73 self.
_clusters = _positive_int(clusters,
"clusters")
75 self.
_iso, self.
_line, self.
seed = iso_sigma, line_sigma, seed
76 dimension = _positive_int(dimension,
"dimension")
80 raise ValueError(
"Warm-start genomes must match dimension and bounds.")
81 kwargs.setdefault(
"optimisation_task", container.fitness_weights)
82 super().__init__(container, _positive_int(budget,
"budget"), dimension=dimension,
83 batch_size=_positive_int(batch_size,
"batch_size"), **kwargs)
86 """Reset forecasting and private random streams."""
88 self.
_rng, self.
_model_rng = map(np.random.default_rng, np.random.SeedSequence(self.
seed).spawn(2))
96 """Return the number of forecasts with two observed archive snapshots."""
99 def _descriptor(self, row):
100 if self.
_score not in row.scores:
101 raise ValueError(f
"Missing selection descriptor {self._score!r}.")
102 return _finite_vector(row.scores[self.
_score],
"Selection descriptor", self.
_width)
104 def _check_candidate(self, individual):
105 super()._check_candidate(individual)
108 def _selection_scores(self, points):
112 scale = max(1., float(np.max(np.abs(points))),
113 0.
if previous
is None else float(np.max(np.abs(previous))))
114 scaled = points / scale
115 novelty = np.zeros(len(points))
116 surprise = np.zeros(len(points))
117 if len(points) > 1
and self.
mode in (
"novelty",
"blend",
"pareto"):
118 distances = cdist(scaled, scaled)
119 np.fill_diagonal(distances, np.inf)
120 novelty = _nearest_means(distances, min(len(points) - 1, self.
_neighbors))
121 if self.
mode in (
"surprise",
"blend",
"pareto"):
122 centers = np.unique(scaled, axis=0)
124 with threadpool_limits(limits=1):
125 centers = KMeans(self.
_clusters, n_init=1, random_state=int(
126 self.
_model_rng.integers(2**31))).fit(scaled).cluster_centers_
127 if previous
is not None:
128 old = previous / scale
129 predicted = 2. * centers - old[cdist(centers, old).argmin(axis=1)]
130 surprise = _nearest_means(cdist(scaled, predicted), min(self.
_surprise_neighbors, len(centers)))
132 self.
_previous_centers = np.clip(centers, scaled.min(axis=0), scaled.max(axis=0)) * scale
133 return novelty, surprise
141 if self.
mode ==
"pareto":
142 rank, crowding = _order(np.column_stack((novelty, surprise)))
143 self.
_keys = tuple(zip(rank, -crowding))
145 if self.
mode ==
"novelty":
147 elif self.
mode ==
"surprise":
151 self.
_probabilities = weights / weights.sum()
if weights.any()
else None
153 def _clear_generation(self):
154 super()._clear_generation()
159 if self.
_keys is not None:
170 base_ind[:] = iso_line_dd(first, second, bounds=self.
ind_domain, iso_sigma=self.
_iso,