26 """Draw Gaussian coefficients and use the absolute coefficient for fitness.
28 One combined evaluator call yields a GradientEvaluation for each
29 candidate. Its normalized Jacobian is stored with the candidate if
30 admitted, so future offspring reuse the measured basis. Descriptor
31 coefficients remain signed. The fitness row follows the configured
32 optimization direction.
34 :param container: Empty parentless best-only Grid or CVTGrid with one objective.
35 :param budget: Combined fitness, descriptor, and Jacobian calls.
36 :param dimension: Number of decision variables.
37 :param sigma: Standard deviation of the sampled coefficients.
38 :param batch_size: Number of candidates proposed before a generation commits.
39 :param ind_domain: Shared decision bounds for sampling and clipping.
40 :param seed: Private seed for initialization, selection, and gradient coefficients.
41 :param kwargs: Native QDAlgorithm settings.
44 _requires_proposed_genome =
True
46 def __init__(self, container, budget, dimension, *, sigma=.1, batch_size=1,
47 ind_domain=(0., 1.), seed=0, **kwargs):
48 _validate_grid(container)
49 if container.parents
or container.fitness_score_names
or container.features_score_names:
50 raise ValueError(
"Use a parentless archive with native fitness and descriptors.")
53 raise ValueError(
"Use finite decision spans and a positive finite sigma.")
55 if container.fitness_weights
is not None:
56 kwargs.setdefault(
"optimisation_task", tuple(container.fitness_weights))
57 super().__init__(container, _positive_int(budget,
"budget"),
58 dimension=_positive_int(dimension,
"dimension"),
59 batch_size=_positive_int(batch_size,
"batch_size"), **kwargs)
61 raise ValueError(
"A nonzero scalar fitness weight is required.")
64 """Reset the proposal stream while retaining measured elite bases."""
66 self.
_rng = np.random.default_rng(self.
seed)
70 """Return combined evaluation attempts, including failed gradient calls."""
76 basis = np.asarray(parent.scores[_BASIS_SCORE])
77 coefficients = self.
_rng.normal(0., self.
sigma, len(basis))
78 coefficients[0] = abs(coefficients[0])
79 base_ind[:] = np.clip(np.asarray(parent) + coefficients @ basis, *self.
ind_domain)
84 def _payload(self, result):
85 if not isinstance(result, GradientEvaluation):
86 raise TypeError(
"The evaluator must return a GradientEvaluation.")
87 fitness = float(result.fitness)
88 descriptors = np.asarray(result.descriptors, dtype=float)
89 jacobian = np.asarray(result.jacobian, dtype=float)
90 width = len(self.
container.features_domain)
91 if (descriptors.shape != (width,)
or jacobian.shape != (width + 1, self.
dimension)
92 or not isfinite(fitness)
or not np.isfinite(descriptors).all()
or not np.isfinite(jacobian).all()):
93 raise ValueError(
"Supply finite fitness, descriptors, and a Jacobian matching the archive and genome.")
95 return (fitness,), descriptors, {_BASIS_SCORE: tuple(tuple(row)
for row
in basis)}
97 def _check_candidate(self, individual):
98 super()._check_candidate(individual)
103 for individual, result, elapsed
in records:
104 if result
is not None:
107 except (TypeError, ValueError, OverflowError)
as error:
108 warnings.warn(f
"Invalid gradient evaluation: {error}", stacklevel=2)
110 staged.append((individual, result, elapsed))