6"""Classifier-guided illumination with one score per goal class."""
8from copy
import deepcopy
9from dataclasses
import dataclass
10from operator
import index
14from ..genealogy
import GoalSwitch
15from ..lineage
import LineageNode
17__all__ = [
"ClassChampion",
"ClassifierIlluminationResult",
"classifier_illumination"]
21@dataclass(frozen=True)
23 """Store a candidate's score for this class and the corresponding lineage ID."""
31@dataclass(frozen=True)
33 """Return champions in class order, genealogy, and evaluation counters."""
35 champions: tuple[ClassChampion, ...]
36 nodes: dict[int, LineageNode]
37 switches: tuple[GoalSwitch, ...]
43def classifier_illumination(initialise, mutate, classify, *, classes, budget, initial_samples=1, seed=0):
44 """Run the fixed-class MAP-Elites variant of an Innovation Engine.
46 ``initialise(rng)`` returns a candidate, and ``mutate(candidate, rng)``
47 varies a copy of one uniformly sampled class champion. ``classify(candidate)``
48 returns one finite score per class in a stable order. Each evaluation
49 competes independently against all class champions. An accepted update
50 in a class other than the sampled parent class is one goal switch.
52 The callbacks define the representation, renderer, and classifier.
53 Candidates can win several classes in one call, which counts as one
54 evaluation. Ties retain the earlier champion. Scores are maximized.
56 classes, budget, initial_samples = index(classes), index(budget), index(initial_samples)
57 if classes < 1
or not 1 <= initial_samples <= budget:
58 raise ValueError(
"Use positive class and budget counts with initial_samples within the budget.")
59 rng = np.random.default_rng(seed)
60 champions = [
None] * classes
61 nodes, switches = {}, []
63 for proposal
in range(budget):
66 if proposal < initial_samples:
67 candidate = initialise(rng)
69 parent_class = int(rng.integers(classes))
70 champion = champions[parent_class]
71 parents = (champion.lineage_id,)
72 candidate = mutate(deepcopy(champion.candidate), rng)
73 scores = np.asarray(classify(candidate), dtype=float)
74 if scores.shape != (classes,)
or not np.isfinite(scores).all():
75 raise ValueError(
"The classifier must return one finite score per class.")
76 improvements = [label
for label, score
in enumerate(scores)
77 if champions[label]
is None or score > champions[label].score]
79 retained = deepcopy(candidate)
80 for label
in improvements:
81 champions[label] =
ClassChampion(retained, float(scores[label]), proposal)
82 if parent_class
is not None and label != parent_class:
83 switches.append(
GoalSwitch(parents[0], proposal, parent_class, label))
84 updates += len(improvements)
85 operator =
"initialise" if not parents
else "mutation"
86 status =
"accepted" if improvements
else "rejected"
87 nodes[proposal] =
LineageNode(proposal, parents, operator, proposal + 1, status, proposal + 1)
A class's best candidate and its proposal identity.
Champions and observed goal switches from a fixed-budget run.
An observed parent-to-child transition between distinct goals.
One archived founder or evaluated proposal and its recorded parents.