38def _discount(point, front, gain, rate):
56 """Two-objective MO-CMA-MAE with a dynamic threshold archive.
58 Each candidate is scored against its cell's current threshold front, in
59 proposal order. The learning rate determines how much hypervolume of
60 an improvement is inserted into the threshold front. The reporting
61 ParetoGrid stores original, nondominated measurements.
63 :param container: Two-objective ParetoGrid with declared objective directions.
64 :param budget: Number of attempted candidate evaluations.
65 :param x0: Initial genome centre, with at least two coordinates.
66 :param sigma0: Initial CMA step size.
67 :param batch_size: Population size, at least two.
68 :param reference: Hypervolume reference in raw objective units. Candidate
69 objectives must be at least as good as the reference in both coordinates.
70 :param learning_rate: Fraction of the hypervolume improvement applied to the
71 threshold front, in [0, 1]. Zero freezes thresholds and one applies full gain.
72 :param ind_domain: Optional shared genome bounds.
73 :param seed: Optional private random seed for CMA and restarts.
76 def __init__(self, container, budget, x0, sigma0, batch_size, reference, *,
77 learning_rate=.1, ind_domain=None, seed=None, **kwargs):
80 except ImportError
as error:
81 raise ImportError(
"MOCMAMAE requires the QDpy 'cma' extra.")
from error
82 if not isinstance(container, ParetoGrid)
or len(container.fitness_domain) != 2:
83 raise ValueError(
"MOCMAMAE requires a two-objective ParetoGrid.")
84 self.
x0 = np.asarray(x0, dtype=float).copy()
85 if self.
x0.ndim != 1
or self.
x0.size < 2
or not np.isfinite(self.
x0).all():
86 raise ValueError(
"x0 must be a finite vector with at least two coordinates.")
87 if not (0 < sigma0 < np.inf
and 0 <= learning_rate <= 1):
88 raise ValueError(
"Use a positive finite sigma0 and learning_rate in [0, 1].")
89 batch_size = _positive_int(batch_size,
"batch_size")
91 raise ValueError(
"MOCMAMAE needs at least two candidates per population.")
92 reference = np.asarray(reference, dtype=float)
93 if reference.shape != (2,)
or not np.isfinite(reference).all():
94 raise ValueError(
"Use a finite two-objective reference point.")
96 self.
ind_domain =
None if ind_domain
is None else _bounds(ind_domain)
98 raise ValueError(
"x0 lies outside the genome bounds.")
100 kwargs.setdefault(
"optimisation_task", container.fitness_weights)
101 super().__init__(container, _positive_int(budget,
"budget"), batch_size=batch_size,
102 dimension=len(self.
x0), **kwargs)
103 for individual
in container:
107 """Reset the emitter and threshold fronts while retaining reported elites."""
113 self.
_rng = random.Random(self.
seed)
if self.
seed is not None else None
117 """Return the threshold points in the original objective units."""
119 return tuple(tuple(ref + sign * value
for ref, sign, value
in zip(self.
reference, direction, point))
122 def _restart_center(self):
128 def _point(self, individual):
129 values = self.
container.get_ind_fitness(individual).values
130 point = tuple(float(np.sign(weight)) * (float(value) - ref)
132 if len(point) != 2
or min(point) < 0
or not isfinite(point[0] * point[1]):
133 raise ValueError(
"Both objectives must dominate the reference and give finite hypervolume.")
136 def _check_candidate(self, individual):
137 super()._check_candidate(individual)
141 self.
_gains = np.full(len(evaluated), -np.inf)
143 for slot, individual
in enumerate(evaluated):
144 if individual
is None:
146 cell = self.
container.index_grid(individual)
147 point = self.
_point(individual)
149 gain = self.
_gains[slot] = _gain(point, front)
151 new[id(individual)] = slot
155 discounted = _discount(point, front, gain, self.
learning_rate)
156 if discounted != (0., 0.):
158 admitted = self.
container._merge_fronts((evaluated[slot]
for slot
in new.values()),
162 return {new[key]
for key
in admitted}