From 9d52953bb5ecec82df27cf77107c344bcd9e41d9 Mon Sep 17 00:00:00 2001 From: NicolasRodriguezUribe Date: Thu, 19 Nov 2026 00:18:26 +0100 Subject: [PATCH] perf(archive): the unbounded archive merges offered rows when read, with the same rows An unbounded external archive filtered and deduplicated its whole contents once per generation, so long runs spent most of their time on it (RCM25, 112 000 rows: 85 s of archive in 200 000 evaluations). Once its contents are clean (feasible, finite, mutually non-dominated, deduplicated with a zero objective tolerance), UnboundedArchive now keeps the rows each call of add() offers and merges them in one update, with moocore's non-dominated filter when moocore is installed, when contents(), constraint_values() or update() needs them, or when the kept rows outnumber twice the archive. One merge gives the rows, in the order, of one update per call: a clean archive keeps the feasible non-dominated rows in the order they were offered, and the first of equal ones. Before the first feasible row, and for rows with non-finite feasible objectives, the archive updates at once. The states' archive_X and archive_F are now ArchiveRows descriptors that merge the archive on first read, and the algorithms offer each generation with record_archive_rows(). A run that reads its archive only at the end filters it once: 0.28 s instead of 85 s on RCM25, 0.16 s instead of 11.5 s on RCM08, 0.1 s instead of 5 s on CRE21. SMPSO, which reads its leaders every step, keeps updating at once. Co-Authored-By: Claude Opus 5.5 --- docs/reference/algorithms.md | 2 + src/vamos/engine/algorithm/agemoea/agemoea.py | 3 +- src/vamos/engine/algorithm/ccmo/ccmo.py | 3 +- src/vamos/engine/algorithm/cmoeams/cmoeams.py | 3 +- .../engine/algorithm/components/archive.py | 10 +- .../algorithm/components/archive_buffer.py | 143 +++++++++++++++ .../algorithm/components/archive_core.py | 40 ++-- .../engine/algorithm/components/state.py | 77 +++++++- src/vamos/engine/algorithm/ctaea/ctaea.py | 3 +- src/vamos/engine/algorithm/ibea/ibea.py | 5 +- src/vamos/engine/algorithm/nsgaii/state.py | 7 +- src/vamos/engine/algorithm/nsgaiii/nsgaiii.py | 3 +- src/vamos/engine/algorithm/rvea/rvea.py | 3 +- src/vamos/engine/algorithm/smsemoa/smsemoa.py | 3 +- src/vamos/engine/algorithm/top/top.py | 3 +- src/vamos/engine/archive/factory.py | 4 +- tests/engine/test_buffered_archive.py | 173 ++++++++++++++++++ tests/engine/test_moead_archive_update.py | 15 +- 18 files changed, 458 insertions(+), 42 deletions(-) create mode 100644 src/vamos/engine/algorithm/components/archive_buffer.py create mode 100644 tests/engine/test_buffered_archive.py diff --git a/docs/reference/algorithms.md b/docs/reference/algorithms.md index 774312d8..c922957e 100644 --- a/docs/reference/algorithms.md +++ b/docs/reference/algorithms.md @@ -299,6 +299,8 @@ config = ( `final_selection` is `"none"` (the default: the whole archive, as before), `"farthest"`, `"crowding"`, `"hv_greedy"` or `"kmeans"`, the selections of `top_k(method=...)`; `kmeans` is seeded with the archive's `rng_seed`, which without a value derives from the run seed (see [Reproducibility](../experiment/stopping_and_archive.md#reproducibility)). `final_size` defaults to the population size. `result.X` and `result.F` hold the subset, the archive stays whole in `result.data["archive"]` (`archive_X`/`archive_F` for NSGA-III and IBEA, `external_archive` for SPEA2), and `result.data["archive_subset"]` records the selection, the size and the indices of the selected rows in the archive. A saved run stores the subset as its result and the whole archive under `archive/` (SPEA2: `external_archive/`, since its `archive/` is the environmental archive), and its resolved specification records `final_selection` and `final_size`, so a replay selects the same subset. The subset also applies to a bounded archive larger than `final_size`, not with `result_mode="population"`, and not to hypervolume termination or live callbacks, which see the whole archive during the run. +**Development after 1.4.0:** an unbounded external archive no longer filters itself once per generation. Once it holds feasible, finite rows, it keeps the rows each generation offers and merges them, with moocore's non-dominated filter when moocore is installed, only when something reads the archive (the result, a live callback in `population_archive` mode, hypervolume termination, SMPSO's leaders) or when the kept rows outnumber twice the archive. One merge gives the same rows, in the same order, as one update per generation, so results do not change; a run that reads its archive only at the end filters it once. Over the 200 000 evaluations of an NSGA-II run, the archive took 0.1 s instead of 5.0 s on CRE21 (7 000 rows), 0.16 s instead of 11.5 s on RCM08 and 0.28 s instead of 85 s on RCM25 (112 000 rows). Before the first feasible row, and after rows with non-finite feasible objectives, the archive is updated at once, as before. + NSGA-III and RVEA require a population compatible with the configured reference directions. MOEA/D requires one weight vector per population member. For a simplex lattice with `p` divisions and `m` objectives, use `comb(p + m - 1, m - 1)`; for three objectives and 12 divisions, that is **91**. Explicit incompatible cardinalities fail; they are not warning-only cases. Follow the tutorials before changing objective count or population size. **VAMOS 1.2.0:** NSGA-III needs at least one individual per reference direction (N >= H) instead of exactly one, as Deb and Jain, jMetal and pymoo do; 100 individuals with 12 divisions and three objectives run with 91 directions, and fewer than 91 fail. Without `divisions`, NSGA-III uses the complete Das-Dennis lattice with the most directions that fit in `pop_size`; VAMOS 1.1.0 and earlier cut the next larger lattice to `pop_size` directions and lost the extreme direction of the first objective. With two objectives, NSGA-III and RVEA default to `pop_size - 1` divisions, or 99 without `pop_size`, instead of 6. RVEA and MOEA/D still need exactly one individual per vector; RVEA then keeps one survivor per occupied vector, so its population can end smaller than `pop_size` (see [RVEA](../algorithms/rvea.md)). diff --git a/src/vamos/engine/algorithm/agemoea/agemoea.py b/src/vamos/engine/algorithm/agemoea/agemoea.py index 58b2d950..dc68e5d7 100644 --- a/src/vamos/engine/algorithm/agemoea/agemoea.py +++ b/src/vamos/engine/algorithm/agemoea/agemoea.py @@ -18,6 +18,7 @@ from vamos.engine.algorithm.components.hooks import get_live_viz, live_should_stop, send_live_generation from vamos.engine.algorithm.components.metrics import setup_hv_tracker from vamos.engine.algorithm.components.population import initialize_evaluated_population, resolve_bounds +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.algorithm.components.termination import ( capped_offspring_size, hv_target_reached, @@ -405,7 +406,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool else: st.selection_ranks, st.selection_crowding = selection_metrics(self.kernel, st.F, st.G, st.constraint_mode, survival_scores) if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) st.pending_offspring = None st.generation += 1 diff --git a/src/vamos/engine/algorithm/ccmo/ccmo.py b/src/vamos/engine/algorithm/ccmo/ccmo.py index 27a9d2ca..77af04d1 100644 --- a/src/vamos/engine/algorithm/ccmo/ccmo.py +++ b/src/vamos/engine/algorithm/ccmo/ccmo.py @@ -17,6 +17,7 @@ from vamos.engine.algorithm.components.hooks import get_live_viz, live_should_stop, send_live_generation from vamos.engine.algorithm.components.metrics import setup_hv_tracker from vamos.engine.algorithm.components.population import initialize_evaluated_population, resolve_bounds +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.algorithm.components.termination import capped_offspring_size, hv_target_reached, parse_termination from vamos.engine.algorithm.config.base import normalize_constraint_mode from vamos.engine.archive.factory import resolve_external_archive, setup_archive @@ -370,7 +371,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool st.n_eval += X_off.shape[0] if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) st.pending_offspring = None st.pending_from_main = 0 st.generation += 1 diff --git a/src/vamos/engine/algorithm/cmoeams/cmoeams.py b/src/vamos/engine/algorithm/cmoeams/cmoeams.py index 90dcfa91..4c816a47 100644 --- a/src/vamos/engine/algorithm/cmoeams/cmoeams.py +++ b/src/vamos/engine/algorithm/cmoeams/cmoeams.py @@ -18,6 +18,7 @@ from vamos.engine.algorithm.components.hooks import get_live_viz, live_should_stop, send_live_generation from vamos.engine.algorithm.components.metrics import setup_hv_tracker from vamos.engine.algorithm.components.population import initialize_evaluated_population, resolve_bounds +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.algorithm.components.termination import capped_offspring_size, hv_target_reached, parse_termination from vamos.engine.algorithm.config.base import normalize_constraint_mode from vamos.engine.archive.factory import resolve_external_archive, setup_archive @@ -418,7 +419,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool st.stage_b_generations += int(st.stage_b) if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) st.pending_offspring = None st.generation += 1 st.hv_reached = st.hv_reached or hv_target_reached(st) diff --git a/src/vamos/engine/algorithm/components/archive.py b/src/vamos/engine/algorithm/components/archive.py index 5046d61f..871c0fd4 100644 --- a/src/vamos/engine/algorithm/components/archive.py +++ b/src/vamos/engine/algorithm/components/archive.py @@ -260,7 +260,13 @@ def _select_subset( class UnboundedArchive(_BaseArchive): - """Archive that keeps all non-dominated solutions without size limit.""" + """Archive that keeps all non-dominated solutions without size limit. + + It is ``buffered`` unless ``buffered=False``: ``add`` keeps the rows offered while the contents are + clean and merges them, with moocore's non-dominated filter when moocore is installed, when the + contents are read or the kept rows outnumber twice the archive. The contents, their order and the + rows kept are those of one ``update`` per call. + """ def __init__( self, @@ -273,6 +279,7 @@ def __init__( decision_tolerance: float = 1e-32, n_con: int | None = None, initial_capacity: int = 256, + buffered: bool = True, ) -> None: super().__init__( None, @@ -285,6 +292,7 @@ def __init__( n_con=n_con, initial_capacity=initial_capacity, ) + self.buffered = bool(buffered) __all__ = [ diff --git a/src/vamos/engine/algorithm/components/archive_buffer.py b/src/vamos/engine/algorithm/components/archive_buffer.py new file mode 100644 index 00000000..d5abf6c7 --- /dev/null +++ b/src/vamos/engine/algorithm/components/archive_buffer.py @@ -0,0 +1,143 @@ +"""The row buffer of the unbounded archive: rows offered with ``add`` are merged when they are needed. + +``_BaseArchive`` (``archive_core``) inherits it. A buffered archive whose contents are clean keeps the +rows it is offered and merges them in one update when its contents are read, when ``update`` is +called, or when the kept rows outnumber ``max(PENDING_ROWS, 2 * size)``, with the contents of one +update per call. +""" + +from __future__ import annotations + +from typing import Any, TypeAlias + +import numpy as np +from numpy.typing import NDArray + +from vamos.foundation.constraints.utils import is_feasible + +FloatArray: TypeAlias = NDArray[np.float64] +DecisionArray: TypeAlias = NDArray[np.generic] + +# A buffered archive merges the rows offered with ``add`` once they outnumber this, or twice its size. +PENDING_ROWS = 4096 + + +class RowBuffer: + """The rows that ``add`` keeps for a later merge, and their checks; mixed into ``_BaseArchive``.""" + + buffered: bool + capacity: int | None + covers_last_input: bool + unchanged_by_last_input: bool + last_capacity_pruned: int + _pending: list[tuple[DecisionArray, FloatArray, FloatArray | None]] + _pending_rows: int + _clean: bool + _size: int + _G: FloatArray | None + _dtype: np.dtype[Any] + _n_var: int + _n_obj: int + + def update( + self, + population_X: DecisionArray, + population_F: NDArray[np.generic], + population_G: NDArray[np.generic] | None = None, + ) -> tuple[DecisionArray, FloatArray]: + raise NotImplementedError + + def _update_now( + self, + population_X: DecisionArray, + population_F: NDArray[np.generic], + population_G: NDArray[np.generic] | None = None, + ) -> tuple[DecisionArray, FloatArray]: + raise NotImplementedError + + def add( + self, + population_X: DecisionArray, + population_F: NDArray[np.generic], + population_G: NDArray[np.generic] | None = None, + ) -> None: + """Offer rows as ``update`` does, without computing the contents. + + A ``buffered`` archive with clean contents (mutually non-dominated, finite, feasible when it holds + constraint values, deduplicated with a zero objective tolerance) keeps the rows whose feasible + objective vectors are finite, and merges them when ``contents``, ``constraint_values`` or + ``update`` needs them, or when they outnumber ``max(PENDING_ROWS, 2 * size)``. One merge of many + rows gives the contents, in the order, of one ``update`` per call: a clean archive keeps the + feasible non-dominated rows in the order they were offered, and the first of equal ones. Other + rows, or rows offered to an archive that is not buffered or not clean, are merged at once. + """ + pop_X, pop_F, pop_G = self._checked_rows(population_X, population_F, population_G) + if self.buffered and pop_X.shape[0] > 0 and self._bufferable(pop_F, pop_G): + # Copies: the caller may write into its population after offering it. + self._pending.append((pop_X.copy(), pop_F.copy(), None if pop_G is None else pop_G.copy())) + self._pending_rows += pop_X.shape[0] + # As after an update of clean contents: every offered row will be archived or covered. + self.last_capacity_pruned = 0 + self.covers_last_input = True + self.unchanged_by_last_input = False + if self._pending_rows > max(PENDING_ROWS, 2 * self._size): + self._flush() + return + self.update(pop_X, pop_F, pop_G) + + def _bufferable(self, pop_F: FloatArray, pop_G: FloatArray | None) -> bool: + """Whether ``add`` may keep these rows for a later merge (see ``add``).""" + if not self._clean or self._size == 0 or self.capacity is not None or (self._G is None) != (pop_G is None): + return False + if pop_G is None: + return bool(np.isfinite(pop_F).all()) + assert self._G is not None + if pop_G.shape[1] != self._G.shape[1]: + return False + return bool(np.isfinite(pop_F[is_feasible(pop_G, n=pop_G.shape[0])]).all()) + + def _flush(self) -> None: + """Merge the rows that ``add`` kept, as one update.""" + if not self._pending: + return + pending = self._pending + self._pending = [] + self._pending_rows = 0 + X = np.concatenate([rows[0] for rows in pending]) + F = np.concatenate([rows[1] for rows in pending]) + Gs = [rows[2] for rows in pending if rows[2] is not None] + G = np.concatenate(Gs) if Gs else None + self._update_now(X, F, G) + # As after the calls that offered them: the merge covered every row. + self.covers_last_input = self._clean + self.unchanged_by_last_input = False + + def _checked_rows( + self, + population_X: DecisionArray, + population_F: NDArray[np.generic], + population_G: NDArray[np.generic] | None, + ) -> tuple[DecisionArray, FloatArray, FloatArray | None]: + pop_X = np.asarray(population_X, dtype=self._dtype, order="C") + pop_F = np.asarray(population_F, dtype=float, order="C") + if pop_X.ndim != 2: + raise ValueError("population_X must be a 2D array.") + if pop_F.ndim != 2: + raise ValueError("population_F must be a 2D array.") + if pop_X.shape[0] != pop_F.shape[0]: + raise ValueError("population_X and population_F must have the same number of rows.") + if pop_X.shape[1] != self._n_var: + raise ValueError(f"population_X has {pop_X.shape[1]} columns, expected {self._n_var}.") + if pop_F.shape[1] != self._n_obj: + raise ValueError(f"population_F has {pop_F.shape[1]} columns, expected {self._n_obj}.") + pop_G: FloatArray | None = None + if population_G is not None and pop_X.shape[0] > 0: + pop_G = np.asarray(population_G, dtype=float, order="C") + if pop_G.ndim != 2: + raise ValueError("population_G must be a 2D array when provided.") + if pop_G.shape[0] != pop_X.shape[0]: + raise ValueError("population_G must have the same number of rows as population_X.") + return pop_X, pop_F, pop_G + + +__all__ = ["PENDING_ROWS", "RowBuffer"] diff --git a/src/vamos/engine/algorithm/components/archive_core.py b/src/vamos/engine/algorithm/components/archive_core.py index 000e2bca..63df89e4 100644 --- a/src/vamos/engine/algorithm/components/archive_core.py +++ b/src/vamos/engine/algorithm/components/archive_core.py @@ -11,6 +11,7 @@ except ImportError: # pragma: no cover - optional dependency _moocore = None +from vamos.engine.algorithm.components.archive_buffer import RowBuffer from vamos.engine.algorithm.components.archive_insertion import INSERTION_ROWS, drop_rows, insertion from vamos.foundation.constraints.utils import compute_violation, is_feasible from vamos.foundation.quality_indicators.pareto import pareto_filter @@ -129,7 +130,7 @@ def _subset_arrays( return X[idx], F[idx], G[idx] if G is not None else None -class _BaseArchive: +class _BaseArchive(RowBuffer): """ Base class for bounded external archives. @@ -218,6 +219,10 @@ def __init__( self.unchanged_by_last_input = False # The configuration of the external archive this archive holds (``setup_archive``), whose final subset the result reports. self.external_config: ExternalArchiveConfig | None = None + #: Whether ``add`` keeps the rows it is offered and merges them only when needed (see ``add``). + self.buffered = False + self._pending: list[tuple[DecisionArray, FloatArray, FloatArray | None]] = [] + self._pending_rows = 0 def update( self, @@ -225,32 +230,23 @@ def update( population_F: NDArray[np.generic], population_G: NDArray[np.generic] | None = None, ) -> tuple[DecisionArray, FloatArray]: - pop_X = np.asarray(population_X, dtype=self._dtype, order="C") - pop_F = np.asarray(population_F, dtype=float, order="C") - if pop_X.ndim != 2: - raise ValueError("population_X must be a 2D array.") - if pop_F.ndim != 2: - raise ValueError("population_F must be a 2D array.") - if pop_X.shape[0] != pop_F.shape[0]: - raise ValueError("population_X and population_F must have the same number of rows.") - if pop_X.shape[1] != self._n_var: - raise ValueError(f"population_X has {pop_X.shape[1]} columns, expected {self._n_var}.") - if pop_F.shape[1] != self._n_obj: - raise ValueError(f"population_F has {pop_F.shape[1]} columns, expected {self._n_obj}.") + """Merge the rows into the archive and return its contents; rows kept by ``add`` are merged first.""" + self._flush() + return self._update_now(population_X, population_F, population_G) + + def _update_now( + self, + population_X: DecisionArray, + population_F: NDArray[np.generic], + population_G: NDArray[np.generic] | None = None, + ) -> tuple[DecisionArray, FloatArray]: + pop_X, pop_F, pop_G = self._checked_rows(population_X, population_F, population_G) self.last_capacity_pruned = 0 if pop_X.shape[0] == 0: self.covers_last_input = self._clean self.unchanged_by_last_input = True return self._snapshot() - pop_G: FloatArray | None = None - if population_G is not None: - pop_G = np.asarray(population_G, dtype=float, order="C") - if pop_G.ndim != 2: - raise ValueError("population_G must be a 2D array when provided.") - if pop_G.shape[0] != pop_X.shape[0]: - raise ValueError("population_G must have the same number of rows as population_X.") - if self._insertable(pop_X.shape[0], pop_G) and self._insert(pop_X, pop_F, pop_G): return self._snapshot() @@ -361,10 +357,12 @@ def _insert(self, pop_X: DecisionArray, pop_F: FloatArray, pop_G: FloatArray | N return True def contents(self) -> tuple[DecisionArray, FloatArray]: + self._flush() return self._snapshot() def constraint_values(self) -> FloatArray | None: """Constraint values of the archived rows, aligned with ``contents()``, or ``None`` without constraints.""" + self._flush() if self._G is None: return None return self._G[: self._size].copy() diff --git a/src/vamos/engine/algorithm/components/state.py b/src/vamos/engine/algorithm/components/state.py index 5e73fe50..2e05d7f9 100644 --- a/src/vamos/engine/algorithm/components/state.py +++ b/src/vamos/engine/algorithm/components/state.py @@ -6,8 +6,9 @@ from __future__ import annotations +import inspect from dataclasses import dataclass -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, overload import numpy as np @@ -25,6 +26,72 @@ from vamos.engine.hooks.genealogy import GenealogyTracker +_STALE = "_archive_rows_stale" + + +class ArchiveRows: + """The ``archive_X`` and ``archive_F`` fields of a state: the external archive's rows. + + :func:`record_archive_rows` offers rows to a buffered archive without merging them and marks both + fields stale; the first read of either merges the archive once (``contents()``) and keeps both. + Assigning a value sets it, as for a plain field. As a dataclass default, it is ``None``. + """ + + def __set_name__(self, owner: type[Any], name: str) -> None: + self._slot = f"_{name}_rows" + + @overload + def __get__(self, instance: None, owner: type[Any]) -> None: ... + + @overload + def __get__(self, instance: object, owner: type[Any] | None = None) -> np.ndarray[Any, Any] | None: ... + + def __get__(self, instance: object | None, owner: type[Any] | None = None) -> np.ndarray[Any, Any] | None: + if instance is None: + return None + values = instance.__dict__ + if values.get(_STALE, False): + _merge_archive_rows(instance) + rows: np.ndarray[Any, Any] | None = values.get(self._slot) + return rows + + def __set__(self, instance: object, value: np.ndarray[Any, Any] | None) -> None: + if instance.__dict__.get(_STALE, False): + _merge_archive_rows(instance) # so that the other field keeps the archive's rows + instance.__dict__[self._slot] = value + + +def _merge_archive_rows(state: object) -> None: + values = state.__dict__ + values[_STALE] = False + manager = values.get("archive_manager") + if manager is not None: + values["_archive_X_rows"], values["_archive_F_rows"] = manager.contents() + + +def record_archive_rows( + state: Any, + X: np.ndarray[Any, Any], + F: np.ndarray[Any, Any], + G: np.ndarray[Any, Any] | None, +) -> None: + """Offer rows to the state's external archive, as ``archive_manager.update`` does. + + A buffered archive (``UnboundedArchive``) keeps them until ``archive_X`` or ``archive_F`` is read, + or the result is built: a run that reads its archive only at the end filters it once instead of + once per generation, with the same rows. Other archives, and states whose archive fields are + plain attributes, are updated at once. + """ + manager = state.archive_manager + if manager is None: + return + if getattr(manager, "buffered", False) and isinstance(inspect.getattr_static(type(state), "archive_X", None), ArchiveRows): + manager.add(X, F, G) + state.__dict__[_STALE] = True + return + state.archive_X, state.archive_F = manager.update(X, F, G) + + @dataclass class AlgorithmState: """ @@ -53,10 +120,10 @@ class AlgorithmState: n_eval: int = 0 max_evals: int = 0 - # Archive (optional) + # Archive (optional). The rows of a buffered archive are merged when first read (ArchiveRows). archive_size: int | None = None - archive_X: np.ndarray[Any, Any] | None = None - archive_F: np.ndarray[Any, Any] | None = None + archive_X: ArchiveRows = ArchiveRows() + archive_F: ArchiveRows = ArchiveRows() archive_manager: ( CrowdingDistanceArchive | HypervolumeArchive | MaxMinArchive | ReferenceDirectionsArchive | SPEA2Archive | UnboundedArchive | None ) = None @@ -83,4 +150,4 @@ def hv_points(self) -> np.ndarray[Any, Any]: return self.F -__all__ = ["AlgorithmState"] +__all__ = ["AlgorithmState", "ArchiveRows", "record_archive_rows"] diff --git a/src/vamos/engine/algorithm/ctaea/ctaea.py b/src/vamos/engine/algorithm/ctaea/ctaea.py index a4b3ce50..c929004a 100644 --- a/src/vamos/engine/algorithm/ctaea/ctaea.py +++ b/src/vamos/engine/algorithm/ctaea/ctaea.py @@ -17,6 +17,7 @@ from vamos.engine.algorithm.components.hooks import get_live_viz, live_should_stop, send_live_generation from vamos.engine.algorithm.components.metrics import setup_hv_tracker from vamos.engine.algorithm.components.population import initialize_evaluated_population, resolve_bounds +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.algorithm.components.termination import capped_offspring_size, hv_target_reached, parse_termination from vamos.engine.algorithm.config.base import normalize_constraint_mode from vamos.engine.archive.factory import resolve_external_archive, setup_archive @@ -356,7 +357,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool st.n_eval += X_off.shape[0] if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) st.pending_offspring = None st.generation += 1 st.hv_reached = st.hv_reached or hv_target_reached(st) diff --git a/src/vamos/engine/algorithm/ibea/ibea.py b/src/vamos/engine/algorithm/ibea/ibea.py index b31b00ab..73e86e43 100644 --- a/src/vamos/engine/algorithm/ibea/ibea.py +++ b/src/vamos/engine/algorithm/ibea/ibea.py @@ -41,6 +41,7 @@ from vamos.foundation.eval.backends import EvaluationBackend from vamos.foundation.kernel.backend import KernelBackend from vamos.foundation.problem.types import ProblemProtocol +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.hooks.live_viz import LiveVisualization @@ -152,7 +153,7 @@ def run( # Update archive if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) # Live callback send_live_generation(live_cb, st.generation, st) @@ -340,7 +341,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool # Update archive if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) # Live callback if self._live_cb is not None: diff --git a/src/vamos/engine/algorithm/nsgaii/state.py b/src/vamos/engine/algorithm/nsgaii/state.py index fe2f63b4..fb9d92b2 100644 --- a/src/vamos/engine/algorithm/nsgaii/state.py +++ b/src/vamos/engine/algorithm/nsgaii/state.py @@ -32,6 +32,7 @@ wants_population_result, with_constraints, ) +from vamos.engine.algorithm.components.state import ArchiveRows, record_archive_rows from vamos.engine.algorithm.components.termination import HVTracker from vamos.engine.hooks.genealogy import GenealogyTracker from vamos.engine.operators.impl.real import VariationWorkspace @@ -76,8 +77,8 @@ class NSGAIIState: # Archive archive_size: int | None = None - archive_X: np.ndarray[Any, Any] | None = None - archive_F: np.ndarray[Any, Any] | None = None + archive_X: ArchiveRows = ArchiveRows() + archive_F: ArchiveRows = ArchiveRows() archive_manager: ( CrowdingDistanceArchive | HypervolumeArchive | MaxMinArchive | ReferenceDirectionsArchive | SPEA2Archive | UnboundedArchive | None ) = None @@ -386,7 +387,7 @@ def update_archives( F_use = state.F if F is None else F G_use = state.G if G is None else G if state.archive_manager is not None: - state.archive_X, state.archive_F = state.archive_manager.update(X_use, F_use, G_use) + record_archive_rows(state, X_use, F_use, G_use) if state.result_archive is state.archive_manager: return diff --git a/src/vamos/engine/algorithm/nsgaiii/nsgaiii.py b/src/vamos/engine/algorithm/nsgaiii/nsgaiii.py index 44042c5d..3ab13b87 100644 --- a/src/vamos/engine/algorithm/nsgaiii/nsgaiii.py +++ b/src/vamos/engine/algorithm/nsgaiii/nsgaiii.py @@ -37,6 +37,7 @@ from vamos.foundation.eval.backends import EvaluationBackend from vamos.foundation.kernel.backend import KernelBackend from vamos.foundation.problem.types import ProblemProtocol +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.hooks.live_viz import LiveVisualization from vamos.foundation.observer import RunContext @@ -71,7 +72,7 @@ def _archive_offspring(st: NSGAIIIState, X_off: DecisionArray, F_off: FloatArray row dominates it. """ if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(X_off, F_off, G_off) + record_archive_rows(st, X_off, F_off, G_off) class NSGAIII: diff --git a/src/vamos/engine/algorithm/rvea/rvea.py b/src/vamos/engine/algorithm/rvea/rvea.py index 724032d6..31abe226 100644 --- a/src/vamos/engine/algorithm/rvea/rvea.py +++ b/src/vamos/engine/algorithm/rvea/rvea.py @@ -20,6 +20,7 @@ from vamos.engine.algorithm.components.hooks import get_live_viz, live_should_stop, send_live_generation from vamos.engine.algorithm.components.metrics import setup_hv_tracker from vamos.engine.algorithm.components.population import initialize_evaluated_population, resolve_bounds +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.algorithm.components.termination import ( capped_offspring_size, hv_target_reached, @@ -430,7 +431,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool st.G = G_combined[survivors] if G_combined is not None else None st.n_eval += X_off.shape[0] if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) # Periodic reference-vector adaptation if st.adapt_interval is not None and st.generation % st.adapt_interval == 0 and st.nadir is not None: diff --git a/src/vamos/engine/algorithm/smsemoa/smsemoa.py b/src/vamos/engine/algorithm/smsemoa/smsemoa.py index f52dc347..26719545 100644 --- a/src/vamos/engine/algorithm/smsemoa/smsemoa.py +++ b/src/vamos/engine/algorithm/smsemoa/smsemoa.py @@ -39,6 +39,7 @@ from vamos.foundation.eval.backends import EvaluationBackend from vamos.foundation.kernel.backend import KernelBackend from vamos.foundation.problem.types import ProblemProtocol +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.hooks.live_viz import LiveVisualization from vamos.foundation.observer import RunContext @@ -55,7 +56,7 @@ def _archive_child(st: SMSEMOAState, X_child: NDArray[Any], F_child: NDArray[np. if st.archive_manager is not None: # The archive holds constraint values exactly when the population does. G_rows = np.asarray(G_child, dtype=float).reshape(X_child.shape[0], -1) if st.G is not None and G_child is not None else None - st.archive_X, st.archive_F = st.archive_manager.update(X_child, F_child, G_rows) + record_archive_rows(st, X_child, F_child, G_rows) def _check_hv_target(st: SMSEMOAState) -> bool: diff --git a/src/vamos/engine/algorithm/top/top.py b/src/vamos/engine/algorithm/top/top.py index b2e9bd2e..3dce29ae 100644 --- a/src/vamos/engine/algorithm/top/top.py +++ b/src/vamos/engine/algorithm/top/top.py @@ -18,6 +18,7 @@ from vamos.engine.algorithm.components.hooks import get_live_viz, live_should_stop, send_live_generation from vamos.engine.algorithm.components.metrics import setup_hv_tracker from vamos.engine.algorithm.components.population import initialize_evaluated_population, resolve_bounds +from vamos.engine.algorithm.components.state import record_archive_rows from vamos.engine.algorithm.components.termination import capped_offspring_size, hv_target_reached, parse_termination from vamos.engine.algorithm.config.base import normalize_constraint_mode from vamos.engine.archive.factory import resolve_external_archive, setup_archive @@ -457,7 +458,7 @@ def tell(self, eval_result: Any, problem: ProblemProtocol | None = None) -> bool st.n_eval += k if st.archive_manager is not None: - st.archive_X, st.archive_F = st.archive_manager.update(st.X, st.F, st.G) + record_archive_rows(st, st.X, st.F, st.G) st.pending_offspring = None st.pending_phase = None st.generation += 1 diff --git a/src/vamos/engine/archive/factory.py b/src/vamos/engine/archive/factory.py index 58a82916..8a604450 100644 --- a/src/vamos/engine/archive/factory.py +++ b/src/vamos/engine/archive/factory.py @@ -266,7 +266,9 @@ def update_archive( F = F_new if F_new is not None else state.F G = G_new if G_new is not None else state.G - state.archive_X, state.archive_F = state.archive_manager.update(X, F, G) + from vamos.engine.algorithm.components.state import record_archive_rows + + record_archive_rows(state, X, F, G) __all__ = [ diff --git a/tests/engine/test_buffered_archive.py b/tests/engine/test_buffered_archive.py new file mode 100644 index 00000000..22e1c5fd --- /dev/null +++ b/tests/engine/test_buffered_archive.py @@ -0,0 +1,173 @@ +"""The buffered UnboundedArchive keeps offered rows and merges them when read, with the rows of one update per call.""" + +from __future__ import annotations + +import inspect +from typing import Any + +import numpy as np +import pytest + +from vamos.engine.algorithm.components import archive_buffer +from vamos.engine.algorithm.components.archive import UnboundedArchive +from vamos.engine.algorithm.components.archive_core import _BaseArchive +from vamos.engine.algorithm.components.state import AlgorithmState, ArchiveRows, record_archive_rows +from vamos.engine.algorithm.nsgaii.state import NSGAIIState +from vamos.experiment.unified import optimize + + +def _stream(rng: np.random.Generator, rows: int, n_obj: int, *, constrained: bool, nan: bool = False) -> tuple[np.ndarray, ...]: + """Rows as an evolving population offers them: improving objectives, ties, duplicates and, optionally, NaN and infeasible rows.""" + drift = np.linspace(1.0, 0.0, rows)[:, None] + F = np.round(rng.random((rows, n_obj)) * 8.0) / 8.0 + drift # coarse values: ties and duplicates + X = rng.random((rows, 3)) + G = None + if constrained: + G = rng.normal(0.0, 1.0, size=(rows, 2)) + G[: rows // 5] = np.abs(G[: rows // 5]) + 0.1 # an infeasible start, as on a constrained problem + if nan: + F[rng.choice(rows, size=rows // 50, replace=False), 0] = np.nan + return X, F, G + + +def _offer( + archive: UnboundedArchive, X: np.ndarray, F: np.ndarray, G: np.ndarray | None, batches: list[int], reads: set[int] +) -> list[tuple[Any, ...]]: + seen = [] + start = 0 + for k, size in enumerate(batches): + rows = slice(start, start + size) + archive.add(X[rows], F[rows], None if G is None else G[rows]) + start += size + if k in reads: + seen.append((*archive.contents(), archive.constraint_values())) + seen.append((*archive.contents(), archive.constraint_values())) + return seen + + +@pytest.mark.parametrize("constrained", [False, True]) +@pytest.mark.parametrize("nan", [False, True]) +@pytest.mark.parametrize("n_obj", [2, 3, 5]) +def test_buffered_contents_equal_one_update_per_call(constrained: bool, nan: bool, n_obj: int, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(archive_buffer, "PENDING_ROWS", 64) # several merges along the stream + rng = np.random.default_rng(n_obj + 10 * constrained + 100 * nan) + X, F, G = _stream(rng, 3000, n_obj, constrained=constrained, nan=nan) + batches = [int(size) for size in rng.integers(1, 120, size=60)] + batches.append(len(F) - sum(batches)) + reads = {5, 17, 40} + expected = _offer(UnboundedArchive(3, n_obj, float, n_con=2 if constrained else None, buffered=False), X, F, G, batches, reads) + actual = _offer(UnboundedArchive(3, n_obj, float, n_con=2 if constrained else None), X, F, G, batches, reads) + for got, want in zip(actual, expected, strict=True): + for got_part, want_part in zip(got, want, strict=True): + if want_part is None: + assert got_part is None + else: + np.testing.assert_array_equal(got_part, want_part) + + +def test_rows_are_kept_only_while_the_contents_are_clean() -> None: + archive = UnboundedArchive(1, 2, float, n_con=1) + archive.add(np.zeros((2, 1)), np.array([[0.0, 1.0], [1.0, 0.0]]), np.array([[1.0], [2.0]])) # infeasible only + assert not archive._pending # no feasible row yet: merged at once + archive.add(np.zeros((1, 1)), np.array([[0.5, 0.5]]), np.array([[-1.0]])) + archive.add(np.zeros((1, 1)), np.array([[0.4, 0.4]]), np.array([[-1.0]])) + assert archive._pending_rows == 1 # the archive is feasible and clean: kept + X, F = archive.contents() + np.testing.assert_array_equal(F, [[0.4, 0.4]]) + assert not archive._pending + + +def test_kept_rows_are_copies() -> None: + archive = UnboundedArchive(1, 2, float) + archive.update(np.zeros((1, 1)), np.array([[1.0, 1.0]])) + F = np.array([[0.5, 0.5]]) + archive.add(np.zeros((1, 1)), F) + F[0] = 9.0 # a population written after it was offered + np.testing.assert_array_equal(archive.contents()[1], [[0.5, 0.5]]) + + +def test_rows_mismatching_the_constraints_raise_when_offered() -> None: + archive = UnboundedArchive(1, 2, float, n_con=1) + archive.update(np.zeros((1, 1)), np.array([[1.0, 1.0]]), np.array([[-1.0]])) + with pytest.raises(ValueError, match="constraint"): + archive.add(np.zeros((1, 1)), np.array([[0.5, 0.5]])) + + +# ---- The state's archive fields --------------------------------------------- + + +def _all_states(cls: type) -> list[type]: + return [cls, *[state for sub in cls.__subclasses__() for state in _all_states(sub)]] + + +def test_every_state_with_archive_fields_reads_them_lazily() -> None: + import vamos.engine.algorithm # noqa: F401 (imports every algorithm's state) + + for state_cls in {*_all_states(AlgorithmState), NSGAIIState}: + for name in ("archive_X", "archive_F"): + assert isinstance(inspect.getattr_static(state_cls, name), ArchiveRows), (state_cls, name) + + +def test_record_archive_rows_merges_on_first_read() -> None: + archive = UnboundedArchive(1, 2, float) + archive.update(np.zeros((1, 1)), np.array([[1.0, 1.0]])) + state = AlgorithmState(X=np.zeros((1, 1)), F=np.zeros((1, 2)), G=None, rng=np.random.default_rng(0), archive_manager=archive) + record_archive_rows(state, np.ones((2, 1)), np.array([[0.5, 0.5], [2.0, 2.0]]), None) + assert archive._pending_rows == 2 + np.testing.assert_array_equal(state.archive_F, [[0.5, 0.5]]) + np.testing.assert_array_equal(state.archive_X, [[1.0]]) + assert not archive._pending + + record_archive_rows(state, np.zeros((1, 1)), np.array([[0.1, 0.9]]), None) + state.archive_X = np.full((1, 1), 7.0) # an assignment while stale merges first: archive_F stays current + np.testing.assert_array_equal(state.archive_F, [[0.5, 0.5], [0.1, 0.9]]) + + +def test_a_bounded_archive_is_updated_at_once() -> None: + from vamos.engine.algorithm.components.archive import CrowdingDistanceArchive + + archive = CrowdingDistanceArchive(5, 1, 2, float) + state = AlgorithmState(X=np.zeros((1, 1)), F=np.zeros((1, 2)), G=None, rng=np.random.default_rng(0), archive_manager=archive) + record_archive_rows(state, np.zeros((1, 1)), np.array([[0.5, 0.5]]), None) + assert "_archive_rows_stale" not in state.__dict__ or not state.__dict__["_archive_rows_stale"] + np.testing.assert_array_equal(state.__dict__["_archive_F_rows"], [[0.5, 0.5]]) + + +# ---- Runs ---------------------------------------------------------------------- + + +ALGORITHMS = ["nsgaii", "nsgaiii", "moead", "smsemoa", "spea2", "ibea", "agemoea", "rvea", "ccmo", "ctaea", "top", "cmoeams"] + + +@pytest.mark.parametrize("problem", ["zdt1", "c1dtlz1"]) +@pytest.mark.parametrize("algorithm", ALGORITHMS) +def test_runs_with_an_unbounded_archive_match_eager_updates(algorithm: str, problem: str, monkeypatch: pytest.MonkeyPatch) -> None: + import dataclasses + + from vamos.engine.algorithm.config.defaults import build_default_algorithm_config + from vamos.engine.archive import ExternalArchiveConfig + from vamos.foundation.problem.registry import make_problem_selection + + instance = make_problem_selection(problem).instantiate() + pop_size = None if algorithm in ("moead", "nsgaiii", "rvea") else 24 + config = build_default_algorithm_config(algorithm, pop_size=pop_size, n_var=instance.n_var, n_obj=instance.n_obj) + config = dataclasses.replace(config, external_archive=ExternalArchiveConfig(capacity=None)) + + def run() -> Any: + return optimize(problem, algorithm=algorithm, algorithm_config=config, max_evaluations=1200, seed=3, engine="numpy") + + merged: list[int] = [] + flush = _BaseArchive._flush + + def counting_flush(archive: _BaseArchive) -> None: + merged.append(archive._pending_rows) + flush(archive) + + monkeypatch.setattr(_BaseArchive, "_flush", counting_flush) + buffered = run() + if problem == "zdt1": # C1-DTLZ1 finds no feasible row in this budget: nothing is kept + assert max(merged) > 0 # the run kept rows for a later merge + monkeypatch.setattr(_BaseArchive, "_bufferable", lambda self, F, G: False) + eager = run() + np.testing.assert_array_equal(buffered.X, eager.X) + np.testing.assert_array_equal(buffered.F, eager.F) diff --git a/tests/engine/test_moead_archive_update.py b/tests/engine/test_moead_archive_update.py index b9d54c2d..614edb91 100644 --- a/tests/engine/test_moead_archive_update.py +++ b/tests/engine/test_moead_archive_update.py @@ -88,14 +88,27 @@ class _OfferedRows: def __init__(self, monkeypatch: pytest.MonkeyPatch) -> None: self.calls: list[int] = [] self.active = True + self._in_add = False update = _BaseArchive.update + add = _BaseArchive.add def counting_update(archive: _BaseArchive, X: Any, F: Any, G: Any = None) -> Any: - if self.active: + if self.active and not self._in_add: self.calls.append(int(np.asarray(F).shape[0])) return update(archive, X, F, G) + # A buffered archive is offered rows with ``add``, which updates at once when it cannot keep them. + def counting_add(archive: _BaseArchive, X: Any, F: Any, G: Any = None) -> None: + if self.active: + self.calls.append(int(np.asarray(F).shape[0])) + self._in_add = True + try: + add(archive, X, F, G) + finally: + self._in_add = False + monkeypatch.setattr(_BaseArchive, "update", counting_update) + monkeypatch.setattr(_BaseArchive, "add", counting_add) def _run(cfg: dict[str, Any], problem: Any, engine: str, seed: int, termination: Any, **kwargs: Any) -> dict[str, Any]: