12from ._population
import _attempt_budget, _finite_vector, _mean_dissimilarity, _nsga_order, _tournament
40 """Apply DE-NSGA-II to encodings with an application-defined distance.
42 Each merged population receives an extra objective equal to mean
43 behavioral dissimilarity. Its Pareto ranks guide survival and
44 tournament selection. To break rank ties, the default uses average
45 behavioral dissimilarity. A separate callback can use the genomes.
47 :param objective_count: Number of raw objectives.
48 :param sample: Callable (rng) returning a genome.
49 :param mutate: Callable (parent_genome, rng) returning an offspring.
50 :param distance: Symmetric nonnegative callable over two behavior objects.
51 Treat the genome elements and observed behavior as immutable.
52 :param crowding_distance: Optional symmetric callable over two DissimilarityEntry
53 instances. Omit it to share the diversity objective's distance matrix.
54 :param population_size: Parent and offspring population capacity.
55 :param maximize: A Boolean or one Boolean direction per objective.
56 :param standard_crowding: Use standard objective-space NSGA-II crowding for an ablation.
57 :param seed: Private sampling and evaluation seed.
60 def __init__(self, objective_count, *, sample, mutate, distance, crowding_distance=None,
61 population_size=32, maximize=True, standard_crowding=False, seed=0):
63 self.
_size = _positive_int(population_size,
"population_size")
64 if not all(map(callable, (sample, mutate, distance)))
or (
65 crowding_distance
is not None and not callable(crowding_distance)):
66 raise TypeError(
"Supply callable variation and dissimilarity functions.")
67 directions = np.asarray(maximize)
68 if directions.dtype.kind !=
"b" or directions.shape
not in ((), (self.
_objective_count,)):
69 raise ValueError(
"maximize must supply a Boolean or one Boolean per objective.")
81 """Return survivors from the last completed selection, in tournament order."""
86 """Return cumulative evaluation attempts and completed selections."""
92 diversity = _mean_dissimilarity([row.behavior
for row
in candidates], self.
_distance)
93 objectives = self.
_sign * np.asarray([row.objectives
for row
in candidates])
96 crowding = diversity
if self.
_crowding is None else _mean_dissimilarity(candidates, self.
_crowding)
97 ordered, keys = _nsga_order(np.column_stack((objectives, diversity)), self.
_size, crowding)
98 self.
_population = tuple(candidates[i]
for i
in ordered)
108 def run(self, evaluate, budget):
109 """Spend the budget and keep an unfinished offspring batch for a later call."""
110 budget = _attempt_budget(budget)
113 for _
in range(budget):
120 result = evaluate(genome, int(self.
_evaluation.integers(2**32)))
124 objectives, behavior = result
An evaluated solution with raw quality objectives and a behavior object.