32 """QDAIF with caller-supplied generation and grading.
34 Use a one-elite Grid or CVTGrid with one maximized objective. Each
35 archived individual contains one string. Propose receives a tuple
36 of parent texts and this search's NumPy generator. Judge returns a
37 scalar quality and a descriptor vector. Exact text matches reuse
38 the first successful judgment. Keep the rubric fixed across calls.
40 Set parent_count to zero for independent sampling. Otherwise
41 parents are sampled uniformly from occupied cells without
42 replacement, up to the requested number.
45 def __init__(self, archive, *, parent_count=1, seed=None):
46 if (type(archive)
not in (Grid, CVTGrid)
or archive.max_items_per_bin != 1
47 or len(archive.fitness_domain) != 1
or archive.parents):
48 raise ValueError(
"Provide a parentless scalar, one-elite Grid or CVTGrid.")
52 raise ValueError(
"parent_count must be nonnegative.")
55 if len(entry) != 1
or tuple(entry.fitness.weights) != (1.,):
56 raise ValueError(
"Existing elites must contain one text and maximize fitness.")
58 archive._set_fitness_weights((1.,))
59 self.
_rng = np.random.default_rng(seed)
64 if not isinstance(text, str)
or not text.strip():
65 raise ValueError(
"Each candidate must be a nonempty string.")
70 """Return immutable accounting through the latest callback."""
75 """Return the best archived text, or None for an empty archive."""
78 def _judge(self, text, judge):
79 text = self.
_text(text)
80 if text
in self.
_seen:
84 quality, descriptor = judge(text)
85 quality, descriptor = float(quality), tuple(map(float, descriptor))
86 if not isfinite(quality)
or not all(map(isfinite, descriptor)):
87 raise ValueError(
"Judge must return finite quality and descriptors.")
89 self.
archive._check_if_can_be_added(individual)
90 cell = self.
archive.index_grid(individual)
98 def run(self, propose, judge, *, proposal_budget, judgment_budget, initial=()):
99 """Evaluate seed texts first, then propose and judge new texts.
101 Seeds spend judgment calls only. Duplicate proposals still
102 spend proposal calls. Stop when either call budget is exhausted.
103 Malformed feedback or a callback error propagates to the caller.
105 proposals, judgments = index(proposal_budget), index(judgment_budget)
106 if min(proposals, judgments) < 0
or not callable(propose)
or not callable(judge):
107 raise ValueError(
"Use nonnegative budgets and callable propose and judge.")
108 if isinstance(initial, str):
109 raise TypeError(
"initial must be a sequence of candidate texts.")
110 initial = tuple(map(self.
_text, initial))
111 if len(set(initial) - self.
_seen) > judgments:
112 raise ValueError(
"The judgment budget cannot cover all new seed texts.")
116 for _
in range(proposals):
120 slots = self.
_rng.choice(len(self.
archive), size=count, replace=
False)
if count
else ()
121 parents = tuple(self.
archive[i][0]
for i
in slots)
123 self.
_judge(propose(parents, self.
_rng), judge)
run(self, propose, judge, *, proposal_budget, judgment_budget, initial=())
Spend additional proposal and judgment call budgets.