35 """Use native Grid or CVTGrid geometry with independent competition per cell.
37 In-bounds points compete using the same signed fitness. Duplicate points
38 or visits to the same cell compete once. Equal fitness keeps the existing
39 winner. Multiple cells can reference the same immutable record. The
40 archive releases the record when the last cell is taken by another elite.
42 :param geometry: Empty, parentless, best-only Grid or CVTGrid with one
43 explicit signed objective and direct fitness and descriptors.
46 def __init__(self, geometry):
47 if (type(geometry)
not in (Grid, CVTGrid)
or len(geometry)
or geometry.parents
48 or geometry.max_items_per_bin != 1
or geometry.discard_random_on_bin_overload
49 or len(geometry.fitness_domain
or ()) != 1
or len(geometry.fitness_weights
or ()) != 1
50 or not isfinite(geometry.fitness_weights[0])
or not geometry.fitness_weights[0]
51 or geometry.features_score_names
or geometry.fitness_score_names
52 or geometry.capacity < len(geometry.solutions)):
53 raise ValueError(
"Supply an empty best-only Grid or CVTGrid with one explicit signed objective.")
61 """Count distinct retained measurement records."""
65 """Iterate retained measurements once each in admission order."""
69 """Return a retained measurement by admission order."""
74 """Return a read-only view from cell indices to their measured elites."""
75 return MappingProxyType(self.
_cells)
78 def filled_bins(self):
79 """Count cells with a retained winner."""
84 """Return the fraction of geometry cells that have a winner."""
89 """Return the native descriptor bounds."""
92 def _trace_points(self, trace):
93 points = np.asarray(trace, dtype=float)
94 if points.shape == (0,):
97 or not np.isfinite(points).all()):
98 raise ValueError(
"Supply finite points of the archive descriptor dimension.")
99 return tuple(tuple(map(float, point))
for point
in points)
101 def _locate(self, points):
104 if geometry.in_bounds(point, geometry.features_domain):
105 yield geometry.index_grid(point), point
109 """Return the cell indices visited by the in-bounds points."""
114 """Use a NumPy random generator to draw a parent uniformly from the selected pool."""
116 raise ValueError(
"The trace archive is empty.")
119 return self.
_elites[rng.integers(len(self))]
122 def add(self, genome, fitness, trace):
123 """Return the cell indices won by this measurement.
125 Genome coordinates, scalar fitness and descriptor points must be
126 finite. Empty traces, out-of-domain points and out-of-domain fitness
129 values = np.asarray(genome, dtype=float)
130 score = np.asarray(fitness, dtype=float)
131 if (values.ndim != 1
or not len(values)
or not np.isfinite(values).all()
132 or score.shape
not in ((), (1,))
or not np.isfinite(score).all()):
133 raise ValueError(
"Supply a finite nonempty genome and a finite scalar fitness.")
134 fitness = float(score.item())
137 if not geometry.in_bounds((fitness,), geometry.fitness_domain):
139 direction = 1
if geometry.fitness_weights[0] > 0
else -1
140 visited = dict.fromkeys(cell
for cell, _
in self.
_locate(points))
141 winners = tuple(cell
for cell
in visited
if cell
not in self.
_cells
142 or fitness * direction > self.
_cells[cell].fitness * direction)
144 elite =
TraceElite(tuple(map(float, values)), fitness, points)
148 previous = self.
_cells.get(cell)
161 """Return a fresh native grid with one representative point per won cell."""
165 for cell, point
in self.
_locate(elite.trace):
166 if self.
_cells.get(cell)
is elite
and cell
not in inserted:
168 fitness=
Fitness((elite.fitness,), weights=geometry.fitness_weights),
175 """Count every occupied cell, including cells won by the same elite."""
177 return sum((elite.fitness
for elite
in self.
_cells.values()), 0.)
180 low, high = self.
_geometry.fitness_domain[0]
181 if not (isfinite(low)
and isfinite(high)):
183 width = high - low
if high > low
else 1.
184 if self.
_geometry.fitness_weights[0] < 0:
185 return sum((high - elite.fitness) / width
for elite
in self.
_cells.values())
186 return sum((elite.fitness - low) / width
for elite
in self.
_cells.values())