117 def __init__(self, probabilities, labels, *, configurations=None, loss=None, bins=7,
185 def fit(self, budget, *, mode="qdo"):
186 """Fit using validation predictions only. Evaluate test predictions separately."""
187 budget = _positive_int(budget,
"budget")
188 if mode
not in (
"qdo",
"quality",
"greedy"):
189 raise ValueError(
"mode must be qdo, quality, or greedy.")
192 fitness_domain=((0., np.inf),), fitness_weights=(-1.,),
194 if mode ==
"qdo" else None)
195 rng = np.random.default_rng(self.
seed)
196 best_counts, best_loss =
None, np.inf
201 nonlocal best_counts, best_loss, calls
205 best_counts, best_loss = tuple(map(int, counts)), loss
209 parent = np.zeros(models, dtype=int)
210 while calls < budget:
212 for i
in rng.permutation(models)[:budget - calls]:
213 child = parent.copy()
215 trials.append((score(child), child))
216 _, parent = min(trials, key=
lambda trial: trial[0])
219 def offer(counts, loss):
220 if archive
is not None:
221 archive.add(
Individual(counts.tolist(), fitness=
Fitness((loss,), weights=(-1.,)),
224 quality[tuple(counts)] = loss
225 if len(quality) > self.
bins ** 2:
226 del quality[max(quality, key=quality.get)]
228 for i
in rng.permutation(models)[:budget]:
229 counts = np.zeros(models, dtype=int)
231 offer(counts, score(counts))
235 while calls < budget:
236 pool = ([(tuple(ind), ind.fitness.values[0])
for ind
in archive]
237 if archive
is not None else list(quality.items()))
238 ordered = [counts
for counts, _
in sorted(pool, key=
lambda pair: pair[1])]
239 batch, history = [], [[], [], []]
240 for _
in range(min(self.
batch_size, budget - calls)):
241 random_parent = bool(rng.random() < sampling.probability)
242 use_cross = self.
crossover !=
"none" and len(ordered) > 1
and rng.random() < crossover.probability
243 count = 2
if use_cross
else 1
244 indices = rng.choice(len(ordered), count, replace=
False)
if random_parent
else range(count)
245 parents = [np.asarray(ordered[i])
for i
in indices]
246 child = self.
_cross(*parents, rng)
if use_cross
else parents[0].copy()
247 mutate = bool(use_cross
and rng.random() < post_mutation.probability)
248 if not use_cross
or mutate:
249 tried = expanded.setdefault(tuple(child), Counter())
250 least = min(tried[i]
for i
in range(models))
251 available = [i
for i
in range(models)
if tried[i] == least]
252 i = int(rng.choice(available))
256 batch.append((child, loss))
257 history[0].append((random_parent, loss))
258 history[1].append((use_cross, loss))
260 history[2].append((mutate, loss))
261 for counts, loss
in batch:
263 for choice, outcomes
in zip((sampling, crossover, post_mutation), history):
264 choice.update(outcomes)