26 """Illuminate a design space between feedback rounds.
28 Call record_feedback() with occupied cells chosen from the current
29 display. Ridge regression estimates the likelihood of a selection
30 from the descriptors (or caller-supplied features). The tabular
31 alternative uses selection counts. Both models use softmax sampling
32 with a uniform exploration component. Fitness and the descriptors
33 that determine cell membership are supplied by the evaluator.
35 :param container: Fixed, parentless, best-only Grid or CVTGrid.
36 :param budget: Native managed evaluation budget.
37 :param dimension: Number of real coordinates in the candidate genome.
38 :param ind_domain: Shared finite lower and upper genome bounds.
39 :param mode: "ridge", "tabular", or "uniform" for a control.
40 :param feedback_window: Most recent rounds used for learning.
41 :param regularization: Positive ridge penalty.
42 :param temperature: Positive softmax temperature in model-score units.
43 :param exploration: Probability of a uniform draw in the automated emitter.
44 :param features: Optional function mapping an evaluated elite to a finite feature vector.
45 :param iso_sigma: Independent Iso+LineDD noise scale.
46 :param line_sigma: Iso+LineDD scale along the direction between sampled elites.
47 :param batch_size: Number of proposals per fixed generation.
48 :param seed: Private proposal seed.
49 :param kwargs: Native algorithm settings, including objective direction.
52 _requires_proposed_genome =
True
54 def __init__(self, container, budget, dimension, ind_domain=(0., 1.), *,
55 mode=
"ridge", feedback_window=8, regularization=.1, temperature=.25,
56 exploration=.15, features=
None, iso_sigma=.05, line_sigma=.2,
57 batch_size=8, seed=0, **kwargs):
58 if (type(container)
not in (Grid, CVTGrid)
or container.parents
or container.max_items_per_bin != 1
59 or container.discard_random_on_bin_overload):
60 raise ValueError(
"Use a fixed parentless one-elite Grid or CVTGrid.")
61 bounds = np.asarray(container.features_domain, dtype=float)
62 if (bounds.ndim != 2
or bounds.shape[1] != 2
or not np.isfinite(bounds).all()
63 or not np.isfinite(bounds[:, 1] - bounds[:, 0]).all()
or np.any(bounds[:, 1] < bounds[:, 0])):
64 raise ValueError(
"Use finite descriptor domains with finite spans.")
65 if (mode
not in (
"ridge",
"tabular",
"uniform")
66 or not 0 < regularization < np.inf
or not 0 < temperature < np.inf
67 or not 0 <= exploration <= 1
or (features
is not None and not callable(features))):
68 raise ValueError(
"Use a valid model, finite positive regularization and temperature, and exploration in [0, 1].")
69 self.
ind_domain = _variation_settings(ind_domain, iso_sigma, line_sigma)
71 raise ValueError(
"Use a finite genome width.")
74 self.
_span = np.where(bounds[:, 0] == bounds[:, 1], 1., bounds[:, 1] - bounds[:, 0])
77 self.
feedback_window = _positive_int(feedback_window,
"feedback_window")
79 if container.fitness_weights
is not None:
80 kwargs.setdefault(
"optimisation_task", tuple(container.fitness_weights))
81 super().__init__(container, _positive_int(budget,
"budget"),
82 dimension=_positive_int(dimension,
"dimension"),
83 batch_size=_positive_int(batch_size,
"batch_size"), **kwargs)
84 for elite
in container:
88 """Reset feedback, the parent model, and native evaluation counters."""
90 self.
_rng = np.random.default_rng(self.
seed)
98 """Return cumulative feedback rounds, including rounds with no selections."""
102 return {cell: entries[0]
for cell, entries
in self.
container.solutions.items()
if entries}
104 def _vector(self, elite):
107 if vector.ndim != 1
or not len(vector)
or not np.isfinite(vector).all():
108 raise ValueError(
"Preference features must be a finite nonempty vector.")
115 """Use selected cells as positives and other displayed cells as negatives."""
117 raise RuntimeError(
"Record preference feedback between complete generations.")
119 shown = tuple(occupied
if displayed
is None else map(tuple, displayed))
120 selected = frozenset(map(tuple, selected))
121 if (
not shown
or len(shown) != len(set(shown))
or any(cell
not in occupied
for cell
in shown)
122 or not selected.issubset(shown)):
123 raise ValueError(
"Supply distinct occupied displayed cells and select only from that display.")
124 vectors = np.stack([self.
_vector(occupied[cell])
for cell
in shown])
if self.
mode ==
"ridge" else None
125 round_data = (selected, shown, vectors)
127 history.append(round_data)
128 if self.
mode ==
"ridge":
129 x = np.concatenate([rows
for _, _, rows
in history])
130 y = [float(cell
in chosen)
for chosen, display, _
in history
for cell
in display]
132 elif self.
mode ==
"tabular":
133 self.
_table = Counter(cell
for chosen, _, _
in history
for cell
in chosen)
137 def _weights(self, occupied):
138 if self.
mode ==
"ridge" and self.
_model is not None:
139 logits = self.
_model.predict(np.stack([self.
_vector(ind)
for ind
in occupied.values()]))
140 elif self.
mode ==
"tabular":
141 logits = np.array([self.
_table[cell]
for cell
in occupied])
143 logits = np.zeros(len(occupied))
145 with np.errstate(over=
"ignore"):
146 weights = softmax((logits - logits.max()) / self.
temperature)
151 """Return a mapping in native cell order. The empty archive returns an empty mapping."""
153 return dict(zip(occupied, self.
_weights(occupied)))
if occupied
else {}
169 base_ind[:] = list(vector)
172 def _validate_genome(self, genome):
173 vector = np.asarray(genome, dtype=float)
174 if (vector.shape != (self.
dimension,)
or not np.isfinite(vector).all()
176 raise ValueError(
"Supply a finite genome of the configured size and interval.")
178 def _check_candidate(self, individual):
180 _check_measurements(self.
container, individual)