45 """Run Dean and Cheney's MOVE with caller-supplied genomes and objective functions.
47 The initial population assigns one candidate to each slot. Within a
48 generation, all offspring use the incumbent snapshot, then compete
49 in slot order. Each objective casts a vote only for a strict
50 improvement. Slots keep their incumbent on a tied or losing vote.
52 :param objective_count: Number of values returned by the evaluator.
53 :param sample: Callable (rng) returning a genome iterable.
54 :param mutate: Callable (parent_genome, rng) returning an offspring iterable.
55 :param slots: Number of independently sampled subsets if subsets is omitted.
56 :param subset_size: Number of distinct indices per sampled subset.
57 Defaults to five or the full objective count, whichever is smaller.
58 :param subsets: Optional explicit nonempty objective index subsets in slot order.
59 :param maximize: One Boolean direction for all objectives or one per objective.
60 :param seed: Seed for geometry, variation, and independent evaluations.
63 def __init__(self, objective_count, *, sample, mutate, slots=50, subset_size=None,
64 subsets=None, maximize=True, seed=0):
65 count = _positive_int(objective_count,
"objective_count")
66 if not callable(sample)
or not callable(mutate):
67 raise TypeError(
"sample and mutate must be callable.")
69 np.random.default_rng, np.random.SeedSequence(seed).spawn(3))
71 size = min(5, count)
if subset_size
is None else _positive_int(subset_size,
"subset_size")
73 raise ValueError(
"subset_size cannot exceed objective_count.")
74 subsets = (geometry.choice(count, size, replace=
False)
for _
in range(_positive_int(slots,
"slots")))
75 self.
_subsets = tuple(tuple(sorted(map(index, subset)))
for subset
in subsets)
76 if not self.
_subsets or any(
not s
or s[0] < 0
or s[-1] >= count
or len(set(s)) != len(s)
78 raise ValueError(
"Each slot needs distinct objective indices in range.")
79 directions = np.asarray(maximize)
80 if directions.dtype.kind !=
"b" or directions.shape
not in ((), (count,)):
81 raise ValueError(
"maximize must supply a Boolean or one Boolean per objective.")
82 self.
_directions = np.broadcast_to(directions, (count,))
91 """Return the objective indices in each slot, including repeated subsets."""
96 """Return the current champions in slot order, with None for empty slots."""
101 """Return cumulative telemetry."""
104 def _compete(self, child, slot, source):
106 if incumbent
is not None:
108 votes = sum((child.objectives[i] > incumbent.objectives[i])
if self.
_directions[i]
109 else (child.objectives[i] < incumbent.objectives[i])
for i
in subset)
110 if votes * 2 <= len(subset):
114 self.
_switches += source
is not None and source != slot
120 def run(self, evaluate, budget):
121 """Spend the additional budget. A subsequent call continues the current generation."""
122 for _
in range(_attempt_budget(budget)):
128 objectives = evaluate(genome, int(self.
_evaluation.integers(2**32)))
129 if objectives
is None:
132 values = _finite_vector(objectives,
"Objectives", len(self.
_directions))
133 child =
MOVEEntry(genome, values, self.
_evaluations,
None if parent
is None else parent.evaluation_id,
134 None if parent
is None else slot)
138 for target
in range(len(self.
_subsets)):
139 self.
_compete(child, target, child.parent_slot)
An evaluated candidate, shared by every slot it wins.