56 """Alternate proposing task offspring and solver offspring.
58 A task offspring must be solved by at least one living solver.
59 A solver offspring must solve a living task that still has resources.
60 The first successful pairing admits the offspring and a solver
61 admission spends one resource on its supporting task. Test partners
62 in a seeded random order without replacement for each proposal.
63 Remove the oldest member of a population when it reaches capacity.
65 :param seed_tasks: Distinct immutable initial tasks, assumed viable.
66 :param seed_solvers: Distinct immutable initial solvers, assumed viable.
67 :param mutate_task: Callback taking (parent, rng) and returning a task or None.
68 :param mutate_solver: Callback taking (parent, rng) and returning a solver or None.
69 :param task_capacity: Maximum number of concurrent tasks.
70 :param solver_capacity: Maximum number of concurrent solvers.
71 :param task_resources: Number of solvers each task can admit while live.
72 None selects unlimited resources as a control.
73 :param seed: Private proposal and pair evaluation seed.
76 def __init__(self, seed_tasks, seed_solvers, *, mutate_task, mutate_solver,
77 task_capacity=32, solver_capacity=64, task_resources=4, seed=0):
78 if not callable(mutate_task)
or not callable(mutate_solver):
79 raise TypeError(
"Supply task and solver mutation functions.")
80 self.
task_capacity = _positive_int(task_capacity,
"task_capacity")
81 self.
solver_capacity = _positive_int(solver_capacity,
"solver_capacity")
82 self.
task_resources =
None if task_resources
is None else _positive_int(task_resources,
"task_resources")
86 proposals, pairings = np.random.SeedSequence(seed).spawn(2)
87 self.
_rng = np.random.default_rng(proposals)
94 def _initial(values, capacity):
95 values = tuple(values)
96 if not 1 <= len(values) <= capacity
or any(value
is None for value
in values):
97 raise ValueError(
"Seed each population with viable non-None entries within capacity.")
98 data = dict.fromkeys(values, 0)
99 if len(data) != len(values):
100 raise ValueError(
"Seeds must be distinct within each population.")
105 """Return current environments and their use in oldest-first order."""
106 return tuple(
MCCTask(task, used)
for task, used
in self.
_tasks.items())
110 """Return living solvers in oldest-first order."""
115 """Return cumulative costs and admissions."""
124 parent = tuple(population)[int(self.
_rng.integers(len(population)))]
125 child = mutate(parent, self.
_rng)
126 if child
is None or child
in population:
131 partners = tuple(task
for task, used
in self.
_tasks.items()
133 self.
_counts[
"exhausted_task_skips"] += len(self.
_tasks) - len(partners)
134 return _Trial(task_birth, child, deque(partners[i]
for i
in self.
_rng.permutation(len(partners))))
136 def _admit(self, trial, supporter):
141 self.
_tasks[supporter] += 1
142 if len(population) == capacity:
143 del population[next(iter(population))]
144 population[trial.child] = 0
152 def run(self, evaluate, budget, *, proposal_limit=None):
153 """Save pending partner tests across a budget boundary and charge calls once."""
154 if not callable(evaluate):
155 raise TypeError(
"Supply a callable evaluator.")
156 budget = _positive_int(budget,
"budget")
157 limit = 10 * budget
if proposal_limit
is None else _positive_int(proposal_limit,
"proposal_limit")
158 end = self.
stats.evaluations + budget
159 proposals_end = self.
_counts[
"proposals"] + limit
164 if self.
_counts[
"proposals"] >= proposals_end:
169 partner = trial.partners.popleft()
170 task, solver = (trial.child, partner)
if trial.task_birth
else (partner, trial.child)
173 self.
_counts[
"task_pairings" if trial.task_birth
else "solver_pairings"] += 1
174 result = evaluate(task, solver, seed)
175 if not isinstance(result, (bool, np.bool_)):
176 raise TypeError(
"The evaluator must return a boolean viability decision.")
178 self.
_admit(trial, partner)