60 """Run counterexample-guided MAP-Elites over a fixed finite scenario universe.
62 :param archive: Empty, best-only scalar Grid or CVTGrid. This archive
63 is owned by the search for the duration of verification.
64 :param scenarios: Nonempty ordered collection of distinct hashable cases.
65 :param propose: Callback (rng) producing an initial iterable genome.
66 :param mutate: Callback (parent_tuple, rng) producing an offspring genome.
67 :param measure: Callback (genome) returning ((fitness,), descriptors).
68 The measurement fixes the candidate's niche and its quality.
69 :param verify: Callback (genome, scenario) returning True on success,
70 False on a counterexample, or None when the result is inconclusive.
71 :param initial_scenarios: Cases to check before unknown cases.
72 :param seed: Private proposal and parent selection seed.
74 Verification should be deterministic. Only candidates that can improve
75 the archive are checked. A candidate enters the archive only after
76 passing the complete scenario universe. A failed scenario moves ahead
77 of unseen cases for later candidates. A candidate interrupted by the
78 check budget or an inconclusive result resumes on the next run call.
81 def __init__(self, archive, scenarios, *, propose, mutate, measure, verify, initial_scenarios=(), seed=0):
82 _validate_grid(archive)
83 if not all(map(callable, (propose, mutate, measure, verify))):
84 raise TypeError(
"Supply callable propose, mutate, measure, and verify functions.")
85 cases = tuple(scenarios)
87 if not cases
or len(universe) != len(cases):
88 raise ValueError(
"scenarios must contain distinct cases.")
89 known = dict.fromkeys(initial_scenarios)
90 if not universe.issuperset(known):
91 raise ValueError(
"initial_scenarios must belong to the verification universe.")
96 self.
_rng = np.random.default_rng(seed)
103 """Return an immutable snapshot of cumulative search work."""
108 """Return discovered counterexamples in first-discovery order."""
113 """Return the genome awaiting verification, or None."""
114 return None if self.
_pending is None else tuple(self.
_pending.measurement[0])
116 def _prioritized_cases(self):
119 def _propose_candidate(self):
126 self.
_counts[
"measurements"] += 1
127 measurement = _measurement(self.
archive, self.
_measure(genome), genome)
128 candidate, cell = measurement
131 front = self.
archive.solutions[cell]
133 if self.
archive._replacement_in_bin(candidate, front)
is None:
139 def _check_case(self):
141 case = pending.scenarios[pending.position]
143 passed = self.
_verify(tuple(pending.measurement[0]), case)
145 self.
_counts[
"inconclusive_checks"] += 1
147 if not isinstance(passed, (bool, np.bool_)):
148 raise TypeError(
"verify must return True, False, or None.")
150 if case
not in self.
_known:
154 self.
_counts[
"counterexamples"] += 1
158 pending.position += 1
159 if pending.position == len(pending.scenarios):
163 _offer(self.
archive, pending.measurement)
168 def run(self, *, proposals, checks):
169 """Return cumulative stats and retain a partial verification for the next call.
171 Zero proposals allows finishing the pending candidate. Reaching
172 an inconclusive case pauses this call. A callback exception
173 propagates after charging the attempted operation.
175 proposal_limit = self.
_counts[
"proposals"] + _nonnegative_limit(proposals,
"proposals")
176 check_limit = self.
_counts[
"checks"] + _nonnegative_limit(checks,
"checks")
179 if self.
_counts[
"proposals"] >= proposal_limit: