18from .task_search
import _measurement, _offer, _search_bounds, _validate_grid
47 """Use the active CVT archive as the parent pool for one phase.
49 At a phase boundary, re-bin each current elite into the incoming
50 archive using the measurements cached on the original evaluation.
51 The incoming archive also keeps any elites already found in earlier
52 phases. Transfer costs zero evaluator calls. Each view owns its
53 fitness weights and descriptor geometry.
55 :param archives: Ordered mapping of names to empty, distinct scalar CVT grids.
56 :param dimension: Number of real decision coordinates.
57 :param phase_evaluations: Number of evaluation attempts per active view.
58 :param shared_feedback: Also offer each evaluation to inactive views for a control.
59 :param ind_domain: Increasing finite genome bounds shared by all coordinates.
60 :param iso_sigma: Independent Iso+LineDD mutation scale.
61 :param line_sigma: Directional Iso+LineDD mutation scale.
62 :param seed: Seed for private variation and evaluator streams.
65 def __init__(self, archives, dimension, *, phase_evaluations, shared_feedback=False,
66 ind_domain=(0., 1.), iso_sigma=.03, line_sigma=.2, seed=0):
67 if (
not isinstance(archives, Mapping)
or not archives
68 or any(
not isinstance(name, str)
or not name
for name
in archives)
69 or len({id(grid)
for grid
in archives.values()}) != len(archives)):
70 raise ValueError(
"Provide a nonempty mapping of names to distinct CVTGrid instances.")
71 for archive
in archives.values():
72 if type(archive)
is not CVTGrid:
73 raise ValueError(
"Each view must be a CVTGrid.")
74 _validate_grid(archive)
75 if type(shared_feedback)
is not bool:
76 raise TypeError(
"shared_feedback must be a bool.")
78 self.
_names = tuple(archives)
79 self.
_dimension = _positive_int(dimension,
"dimension")
80 self.
_phase = _positive_int(phase_evaluations,
"phase_evaluations")
81 self.
_bounds = _search_bounds(_variation_settings(ind_domain, iso_sigma, line_sigma))
83 variation, measurement = np.random.SeedSequence(seed).spawn(2)
84 self.
_rng = np.random.default_rng(variation)
85 self.
_seed = int(measurement.generate_state(1, dtype=np.uint64)[0])
92 """Return a mapping of names to the live CVT grids owned by the search."""
97 """Return the active parent pool."""
102 """Return cumulative evaluator and phase counts."""
107 def _project(archive, view, genome, fitness, views):
108 measured = _measurement(archive, ((fitness,), views[view]), genome)
109 measured[0]._descriptor_views = views
115 target = self.
_names[slot]
117 previous = archive.nb_added
119 fitness = self.
_archives[source].get_ind_fitness(elite).values[0]
120 _offer(archive, self.
_project(archive, target, elite, fitness, elite._descriptor_views))
128 def run(self, evaluate, budget):
129 """Return a scalar fitness and one descriptor vector per configured view."""
130 if not callable(evaluate):
131 raise TypeError(
"Supply a callable evaluator.")
132 for _
in range(_positive_int(budget,
"budget")):
140 a, b = self.
_rng.integers(len(archive), size=2)
141 point = iso_line_dd(archive[int(a)], archive[int(b)], bounds=self.
_bounds,
145 genome = tuple(map(float, point))
149 fitness, reported = evaluate(genome, seed)
150 if not isinstance(reported, Mapping)
or reported.keys() != self.
_archives.keys():
151 raise ValueError(
"Return descriptors for each configured view.")
152 views = {name: tuple(row)
for name, row
in reported.items()}
153 measurements = {name: self.
_project(grid, name, genome, fitness, views)
154 for name, grid
in self.
_archives.items()}
156 for name, grid
in self.
_archives.items():
157 _offer(grid, measurements[name])
159 _offer(archive, measurements[active])