QDpy
Loading...
Searching...
No Matches
innovation_engine.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2
5
6"""Classifier-guided illumination with one score per goal class."""
7
8from copy import deepcopy
9from dataclasses import dataclass
10from operator import index
11
12import numpy as np
13
14from ..genealogy import GoalSwitch
15from ..lineage import LineageNode
16
17__all__ = ["ClassChampion", "ClassifierIlluminationResult", "classifier_illumination"]
18
19
20
21@dataclass(frozen=True)
23 """Store a candidate's score for this class and the corresponding lineage ID."""
24
25 candidate: object
26 score: float
27 lineage_id: int
28
29
30
31@dataclass(frozen=True)
33 """Return champions in class order, genealogy, and evaluation counters."""
34
35 champions: tuple[ClassChampion, ...]
36 nodes: dict[int, LineageNode]
37 switches: tuple[GoalSwitch, ...]
38 evaluations: int
39 champion_updates: int
40
41
42
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.
45
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.
51
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.
55 """
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 = {}, []
62 updates = 0
63 for proposal in range(budget):
64 parent_class = None
65 parents = ()
66 if proposal < initial_samples:
67 candidate = initialise(rng)
68 else:
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]
78 if improvements:
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)
88 return ClassifierIlluminationResult(tuple(champions), nodes, tuple(switches), budget, updates)
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.
Definition genealogy.py:98
One archived founder or evaluated proposal and its recorded parents.
Definition lineage.py:20