Elite Archive (CPU)¶
The MAP-Elites survey over tree space. run_archive fills the archive niche by
niche and halts on discovery saturation. EliteArchive holds the elites and
renders coordinates, surfaces, and dataframes.
run_archive
¶
run_archive(r1: HifukuTree, r2: HifukuTree, r3: HifukuTree, alignment, model, start_trees, triangle: AnchorTriangle, taxon_table: TaxonTable, cell: float = ARCHIVE_CELL, origin: float = 0.0, n_iters: int = ARCHIVE_N_ITERS, seed: int = 0, sigma_slide: float = DEFAULT_SIGMA_SLIDE, scale_width: float = DEFAULT_SCALE_WIDTH, batch: int = ARCHIVE_BATCH, converge: bool = True, force_full_run: bool = False, gate_window: int = ARCHIVE_GATE_WINDOW, coverage_eps: float = ARCHIVE_COVERAGE_EPS, precision_eps: float = ARCHIVE_PRECISION_EPS, logl_margin: float | None = ARCHIVE_LOGL_MARGIN, logl_floor: float | None = None, monitor=False) -> ArchiveResult
Run a CPU elite-archive survey for one alignment.
The three anchor trees fix the chart. The archive is seeded with the anchors and the start trees. The survey then repeats a pass: select a random occupied niche, vary its elite, evaluate the offspring fitness and niche, and place it. Fitness is the alignment log-likelihood.
This function builds a :class:~hifuku.cpu_backend.CpuBackend and gives it
to :func:~hifuku.driver.run_survey. The loop and the halt rule are in the
driver, which the GPU survey also uses. One rule then has one
implementation.
The survey runs batch variations between two checkpoints, up to
n_iters. When converge is set, it halts on discovery saturation: the
trailing-window coverage rate and precision rate both fall to or below their
thresholds. Coverage is the crisp signal. Precision uses a loose threshold
because refinement inside a niche never fully stops. force_full_run
turns the halt off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r1
|
HifukuTree
|
Anchor trees that fix the chart. Used only for CV distances. |
required |
r2
|
HifukuTree
|
Anchor trees that fix the chart. Used only for CV distances. |
required |
r3
|
HifukuTree
|
Anchor trees that fix the chart. Used only for CV distances. |
required |
alignment
|
The alignment surveyed and its substitution model. |
required | |
model
|
The alignment surveyed and its substitution model. |
required | |
start_trees
|
list[HifukuTree]
|
Extra seed trees, normally the anchor tree for the surveyed gene. |
required |
triangle
|
AnchorTriangle
|
Anchor geometry. It carries the chart metric. |
required |
taxon_table
|
TaxonTable
|
Shared namespace. |
required |
cell
|
float
|
Niche side in barycentric units. The grid is unbounded. |
ARCHIVE_CELL
|
origin
|
float
|
Grid origin on both axes. |
0.0
|
n_iters
|
int
|
The survey budget cap in variations. |
ARCHIVE_N_ITERS
|
seed
|
int
|
RNG seed. |
0
|
sigma_slide
|
float
|
Proposal widths for the branch moves. |
DEFAULT_SIGMA_SLIDE
|
scale_width
|
float
|
Proposal widths for the branch moves. |
DEFAULT_SIGMA_SLIDE
|
batch
|
int
|
Variations between two checkpoints. |
ARCHIVE_BATCH
|
converge
|
bool
|
Halt on discovery saturation when set. |
True
|
force_full_run
|
bool
|
Run the full budget and ignore the halt. |
False
|
gate_window
|
int
|
Trailing window in checkpoints for the halt rates. |
ARCHIVE_GATE_WINDOW
|
coverage_eps
|
float
|
Halt thresholds on the windowed coverage and precision rates. |
ARCHIVE_COVERAGE_EPS
|
precision_eps
|
float
|
Halt thresholds on the windowed coverage and precision rates. |
ARCHIVE_COVERAGE_EPS
|
logl_margin
|
float
|
Relative log-likelihood gate: a variation makes a niche only when its tree is within this many nats of the best tree found. The floor follows the best, and the driver reads it once per checkpoint. Pass None for a survey that only the budget limits. |
ARCHIVE_LOGL_MARGIN
|
logl_floor
|
float
|
Absolute log-likelihood gate. Useful for a common domain across genes.
Not permitted together with |
None
|
monitor
|
(callable, None or False)
|
A checkpoint callback. False, the default, reports nothing. None
selects the default reporter. See :mod: |
False
|
Returns:
| Type | Description |
|---|---|
ArchiveResult
|
The archive, its per-checkpoint history, and the halt state. |
Source code in src/hifuku/archive.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
EliteArchive
dataclass
¶
A map from niche to elite over an unbounded 2D chart grid.
A niche is a square cell of side cell, indexed by integer coordinates
(i, j) from origin. The grid has no window: any chart point has a
niche. Exploration is bounded by the log-likelihood gate in the survey, not
by the grid, and the filled niches form the map's footprint.
anchors (an AnchorTriangle), taxon_table, and metric hold the
context the archive was built against. The accessors fall back to anchors
when their argument is omitted. :meth:locate uses all three to place a new
tree in the same frame as the map. A later re-anchoring step can use the
anchors to stitch archives on different anchor planes into one frame.
Source code in src/hifuku/archive.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
tree_at
¶
Return the elite tree at niche key, parsing its stored newick on
first access. For a live archive the tree is already present; for a
loaded archive it is materialized from tree_source and cached.
Source code in src/hifuku/archive.py
trees
¶
niche_of
¶
Return the (i, j) niche of a chart point. Unbounded: never None.
niche_center
¶
place
¶
Store the elite when its niche is empty or the elite is better.
Source code in src/hifuku/archive.py
place_batch
¶
Merge a batch of candidates and return (n_new, gain).
items is a sequence of (key, Elite). The method reduces the
batch to one winner per niche, then compares each winner against the
incumbent. n_new counts the niches that were empty. gain sums
the improvement of each winner over its incumbent. A new niche adds no
gain.
The tie rules follow the device place in gpu_archive, which packs a
fitness in the high bits and a slot index in the low bits, then takes an
atomic maximum. The incumbent holds the largest slot, so it wins a tie
against a candidate. Among candidates of equal fitness, the last one
wins.
One call with one candidate gives the same result as :meth:place, so
a one-walker run reproduces a sequential run.
Source code in src/hifuku/archive.py
best_logL
¶
The largest finite log-likelihood held, or -inf if none is.
Non-finite values are skipped rather than compared. max keeps the
first item a comparison cannot beat, so a NaN in the sequence returns
either itself or its neighbor depending on where it sits, which would
make the answer a function of insertion order.
Source code in src/hifuku/archive.py
index_bounds
¶
(i_min, i_max, j_min, j_max) over filled niches, or None if empty.
Source code in src/hifuku/archive.py
filled_bounds
¶
Chart bounding box (lam1_lo, lam1_hi, lam2_lo, lam2_hi) of the
filled niches, or None if empty.
Source code in src/hifuku/archive.py
elevation_grid
¶
Elite log-likelihood over the filled bounding box, a masked array.
Source code in src/hifuku/archive.py
points
¶
Scattered elite points as (x, y, logL, z) arrays.
coords="barycentric" returns the niche centers (lam1, lam2);
coords="cartesian" maps them through the anchor triangle into the
Euclidean CBS plane. anchors defaults to the archive's stored
anchors; a cartesian request with neither is an error.
Source code in src/hifuku/archive.py
records
¶
Scattered elevation records as an (N, 3) array.
Each row is one elite (coord1, coord2, logL), in barycentric
(lam1, lam2, logL) or Cartesian (x, y, logL) coordinates per
coords. Unlike :meth:surface, this does no interpolation; it hands
the raw scattered records to a plotting utility that triangulates them,
for example ax.tricontourf(*archive.records(coords="cartesian",
anchors=tri).T).
Source code in src/hifuku/archive.py
locate
¶
Return the chart coordinates of a tree as (x, y, z).
This method places tree in the same frame as the map. It uses the
archive's own anchors, taxon table, and metric. A feature tree, such as
the maximum-likelihood tree or a ufboot replicate, lands where it belongs
on the map. coords is "barycentric" or "cartesian". z is the
out-of-plane residual.
Source code in src/hifuku/archive.py
frame
¶
Tidy dataframe of the elites (niche indices, coordinates, logL, z).
Includes Cartesian x, y columns when anchors is given or the
archive has stored anchors.
Source code in src/hifuku/archive.py
surface
¶
Interpolated elevation surface as (X, Y, Z) meshgrids.
Shepard-interpolates the elite log-likelihoods over a regular n by
n grid spanning the filled region, in barycentric or Cartesian
coordinates. Z is masked beyond the support radius. The result is a
plain gridded field: it drops into ax.contourf(X, Y, Z),
ax.pcolormesh(X, Y, Z), ax.plot_surface(X, Y, Z), and the like.
Source code in src/hifuku/archive.py
locate_tree
¶
Return the chart coordinates of a tree as (x, y, z).
This function aligns the leaves of the tree to taxon_table. It measures
the distance from the tree to each anchor of triangle with the chart's
own metric, then solves for the chart position.
coords="barycentric" returns (lambda1, lambda2, z).
coords="cartesian" returns the point in the Euclidean metric plane.
z is the out-of-plane residual. It measures how far the tree sits off
the anchor plane.
triangle provides the anchor trees, their embedded geometry, and the
metric. The chart owns its metric so a tree cannot be placed under a metric
inconsistent with the chart it lands on.