59 return np.partition(matrix, k, axis=1)[:, :k]
63def _cpd(distances, dominates, sigma, neighbors):
66 convergence = np.sum(np.where(dominates.T, weights, 0.), axis=1)
84 """Run the steady-state algorithm of Liu et al. on bounded real decisions.
86 Penalize decision distances by the local presence of objective-space
87 dominators. Update a separate nondominated archive using both decision
88 and objective distances. Use population tournaments in the first half
89 of the budget, then also mate sparse archive elites with nearby designs.
91 :param genome_bounds: Finite increasing (lower, upper) intervals, one per variable.
92 :param fitness_weights: At least two signs. Positive maximizes and negative minimizes.
93 :param population_size: Size of the breeding population and cap of the archive.
94 :param neighbors: Number of nearest other solutions used in either density.
95 :param eta: Scale of the local-convergence bandwidth in Eq. 17.
96 :param seed: Private seed for initialization, mating, and variation.
99 def __init__(self, genome_bounds, fitness_weights, *, population_size=60, neighbors=3, eta=2., seed=0):
101 self.
_direction = np.sign(np.asarray(fitness_weights, dtype=float))
103 or not np.isfinite(fitness_weights).all()
or np.any(self.
_direction == 0)):
104 raise ValueError(
"Provide at least two finite, nonzero fitness weights.")
105 self.
population_size = _positive_int(population_size,
"population_size")
106 self.
neighbors = _positive_int(neighbors,
"neighbors")
108 raise ValueError(
"Use a population of at least two and finite positive eta.")
113 if not isfinite(self.
_sigma * sqrt(2 * pi))
or not 0 < self.
_sigma or not isfinite(1. / self.
_sigma):
114 raise ValueError(
"Use bounds and eta that give a finite nonzero convergence bandwidth.")
116 def _measure(self, evaluate, normalized):
118 objectives = np.asarray(evaluate(genome), dtype=float)
119 if objectives.shape != self.
_direction.shape
or not np.isfinite(objectives).all():
120 raise ValueError(
"Return one finite value per configured objective.")
121 return CPDEAPoint(genome, tuple(map(float, objectives)))
123 def _objectives(self, points):
124 return np.asarray([point.fitness
for point
in points]) * self.
_direction
126 def _archive_update(self, archive, candidate):
127 if candidate
in archive:
129 pool = [*archive, candidate]
130 front = [point
for point, dominated
in zip(pool, _dominates(self.
_objectives(pool)).any(axis=0))
135 front.pop(len(front) - 1 - np.argmax(density[::-1]))
138 def _mating(self, population, archive, rng, explore):
140 distance = _distance([point.genome
for point
in population])
142 tournaments = rng.integers(len(population), size=(2, 2))
143 return tuple(population[contest[np.argmin(costs[contest])]]
for contest
in tournaments)
145 first = archive[np.argmin(density)]
146 pool = tuple(point
for point
in dict.fromkeys([*population, *archive])
if point.genome != first.genome)
149 distances = cdist([first.genome], [point.genome
for point
in pool])[0]
150 closest = np.argsort(distances, kind=
"stable")[:len(first.genome)]
151 return first, pool[rng.choice(closest)]
155 """Call evaluate(genome) for raw objectives and return a :class:`CPDEAResult`."""
156 if not callable(evaluate):
157 raise TypeError(
"evaluate must be callable.")
158 budget = _positive_int(budget,
"budget")
160 raise ValueError(
"The budget must cover the entire initial population.")
161 rng = np.random.default_rng(self.
seed)
163 population = [self.
_measure(evaluate, point)
166 for point
in population:
169 first, second = self.
_mating(population, archive, rng, used < budget / 2
or rng.random() < .5)
170 parents = self.
_coordinates.normalize([first.genome, second.genome])
171 child = _sbx(*parents, rng, eta=20)
172 mut_polynomial_bounded(child, 20, 0., 1., 1. / dimension, rng=rng)
173 candidate = self.
_measure(evaluate, child)
174 population.append(candidate)
175 distance = _distance([point.genome
for point
in population])
177 population.pop(len(population) - 1 - np.argmax(costs[::-1]))
179 return CPDEAResult(tuple(archive), tuple(population), budget)