diff --git a/docs/algorithms/moead.md b/docs/algorithms/moead.md index 523ed72a5..6090d48cb 100644 --- a/docs/algorithms/moead.md +++ b/docs/algorithms/moead.md @@ -84,6 +84,8 @@ For two objectives, `MOEADConfig.default(pop_size=100, n_obj=2, ...)` requests 9 - **Packaged weights:** with three or more objectives and a `pop_size` that no complete lattice matches, `MOEADConfig.default(pop_size=...)` and `optimize` use the weight file VAMOS packages for that size, as jMetal does (for example `W3D_100.dat`, so 100 individuals with three objectives work). In VAMOS 1.1.0 this failed with a lattice-size error. - **Defaults:** the default DE crossover with PBI aggregation is a VAMOS choice; `aggregation("tchebycheff")` with the other defaults reproduces jMetal's MOEA/D-DE. +**Development after 1.4.0:** four objectives and 100 individuals run the packaged `W4D_100.dat`, which held its vectors twice and was skipped, so VAMOS 1.4.0 ran 84 individuals for any population from 100 to 119. Every packaged weight file now loads, also from the command line, and weight files may separate their components by whitespace or commas; see the [configuration reference](../reference/api/algorithms/moead.md). + **VAMOS 1.4.0:** MOEA/D gains the choices of Evolver's MOEA/D, with jMetal's definitions. Each is off by default, and a configuration without them runs as in 1.3.0: | Choice | How to set it | Values | diff --git a/docs/algorithms/nsgaiii.md b/docs/algorithms/nsgaiii.md index f02ae35ad..01b08f0da 100644 --- a/docs/algorithms/nsgaiii.md +++ b/docs/algorithms/nsgaiii.md @@ -84,7 +84,9 @@ For each generation, NSGA-III combines parents and offspring, sorts candidates i | Result mode | `population` returns all survivors; `non_dominated` filters the selected result source. | | Budget | Cover initialization and report the exact number of evaluations, rather than assuming a whole number of generations. | -For explicit reference-direction files, each row must be non-negative, sum to one, have one column per objective and match the configured population count. Consult the [configuration reference](../reference/api/algorithms/nsgaiii.md) for the file option. +For explicit reference-direction files, each row must be non-negative, sum to one and have one column per objective, and the population needs at least one individual per row. Consult the [configuration reference](../reference/api/algorithms/nsgaiii.md) for the file option. + +**Development after 1.4.0:** `reference_directions(path=...)` reads the file as MOEA/D reads its weight files, with whitespace or commas between the components, so jMetal's weight files, such as the `W3D_100.dat` that VAMOS ships, can be passed as they are; VAMOS 1.4.0 read comma-separated files only. A path that is not an existing file raises `ConfigurationError` when the run starts; VAMOS 1.4.0 generated the lattice instead and saved it at that path. ### Variants and their effect diff --git a/docs/reference/api/algorithms/moead.md b/docs/reference/api/algorithms/moead.md index 6df8924ff..4259c5a61 100644 --- a/docs/reference/api/algorithms/moead.md +++ b/docs/reference/api/algorithms/moead.md @@ -26,6 +26,27 @@ lattice by about `1e-16`, so a tool that reads the file can drift from VAMOS wit the same seed. `vamos tune` records the population a trial ran when it differs from the sampled one (see [Tuning](../../../topics/tuning.md#populations-that-depend-on-the-number-of-objectives)). +**Development after 1.4.0:** every weight file that VAMOS ships loads, from +`optimize` and from the packaged directory that the command line passes as +`weight_vectors(path=...)`. Fourteen of the 31 did not: + +- `W4D_100.dat` held its 100 vectors twice, as the jMetalPy file it comes from + (jMetal has no such file), so VAMOS skipped it, and MOEA/D with four + objectives and 100 to 119 individuals ran the 84 vectors of the next smaller + lattice; it now runs the file's 100 vectors, and the command line, which + failed, runs them too. +- `W4D_165.dat` is comma-separated in jMetal, whose own reader expects + whitespace; VAMOS ships it space-separated, and reads either separator in + every weight file. +- jMetal rounds its files: the rows of `W3D_500.dat` to `W3D_1200.dat` and of + `W5D_1000.dat` to `W5D_2500.dat` sum to one within `2e-6`, and `W3D_136.dat` + has components of `-5e-17`. A file's rows may now depart from the simplex by + `1e-5` (`1e-6` before), and a negative component within it reads as zero; the + rows are not renormalized, so a file that loaded before gives the same vectors. + +With `optimize`, a complete lattice still comes before a packaged file of the +same size, and the populations of the other sizes are unchanged. + **VAMOS 1.4.0:** the options of Evolver's MOEA/D. Each is off by default, so a configuration that leaves them out runs exactly as in VAMOS 1.3.0. diff --git a/docs/reference/api/algorithms/nsgaiii.md b/docs/reference/api/algorithms/nsgaiii.md index 0aa4982b4..0d8fb7bfd 100644 --- a/docs/reference/api/algorithms/nsgaiii.md +++ b/docs/reference/api/algorithms/nsgaiii.md @@ -19,6 +19,14 @@ emits a `FutureWarning`; both fields are planned for removal in 2.0 (see `NSGAIIIConfig.default()` does not warn. `mutation_prob_factor` now sets the mutation probability to `factor / n_var`, as in NSGA-II. +**Development after 1.4.0:** the file of `reference_directions(path=...)` is +read as MOEA/D's weight files: one direction per line, the components separated +by whitespace or by commas (VAMOS 1.4.0 read commas only, so jMetal's +`W3D_100.dat` failed), every row non-negative and summing to one within the +rounding of jMetal's files (`1e-5`). A path that is not an existing file raises +`ConfigurationError` when the run starts; VAMOS 1.4.0 generated the lattice +instead and saved it at that path. + ::: vamos.algorithms.NSGAIIIConfig options: heading_level: 2 diff --git a/docs/topics/tuning.md b/docs/topics/tuning.md index 0a6886efe..96371a1a9 100644 --- a/docs/topics/tuning.md +++ b/docs/topics/tuning.md @@ -847,6 +847,12 @@ built. The `effective` record compares with that fixed population: with `--pop-size 150` and three objectives, every MOEA/D trial records `{"pop_size": 136}`. +**Development after 1.4.0:** with four objectives and a `pop_size` from 100 +to 119, MOEA/D runs the 100 vectors of the packaged `W4D_100.dat`, which VAMOS +1.4.0 skipped because the file held them twice: it ran 84 individuals, the +lattice of six divisions. A saved assignment with such a population now builds +100 individuals; the other populations are unchanged. + `optuna` and `bohb_optuna` suggest parents before the parameters that depend on them, whatever their declaration order, and record only active suggestions in `trial.params`. `smac3` and `bohb` receive the dependencies as ConfigSpace diff --git a/src/vamos/engine/algorithm/components/weight_vectors.py b/src/vamos/engine/algorithm/components/weight_vectors.py index 36f621f19..696893cf7 100644 --- a/src/vamos/engine/algorithm/components/weight_vectors.py +++ b/src/vamos/engine/algorithm/components/weight_vectors.py @@ -6,6 +6,31 @@ import numpy as np from vamos.engine.reference_directions import FloatArray, das_dennis, largest_lattice_divisions, lattice_size +from vamos.foundation.exceptions import ConfigurationError + +# How far a weight file may depart from the simplex through rounding: jMetal's files round each component, so +# their rows sum to 1 only within about 2e-6, and a few components that should be zero are about -5e-17. +_ROUNDING_TOLERANCE = 1e-5 + + +def read_weight_vectors(path: str, n_obj: int) -> FloatArray: + """Read the weight vectors of a file, one per line, with ``n_obj`` components each. + + The components are separated by whitespace or by commas: jMetal's weight files use spaces, except + ``W4D_165.dat``, and NSGA-III's reference-direction files have been comma-separated. Every component + must be non-negative and every row must sum to 1, both within the rounding of jMetal's files (1e-5); + a negative component within that rounding reads as 0. The rows are returned as they are, not + renormalized. A path that is not a file raises :class:`~vamos.exceptions.ConfigurationError`. + + **Development after 1.4.0:** commas and whitespace in every file, and the rounding tolerance of 1e-5 + (1e-6 before, without the negative rounding). + """ + if not os.path.isfile(path): + raise ConfigurationError( + f"Weight vector file '{path}' does not exist or is not a file.", + suggestion="Pass the path of an existing file, one weight vector per line.", + ) + return _validated_weights(_load_weights(path), n_obj) def load_or_generate_weight_vectors( @@ -17,8 +42,9 @@ def load_or_generate_weight_vectors( mode: str = "auto", ) -> FloatArray: """ - Load weight vectors from CSV if available, otherwise generate a simplex-lattice - design and persist it when a path is provided. + Load weight vectors from a file if available, otherwise generate a simplex-lattice + design and persist it when a path is provided. Files are read by + :func:`read_weight_vectors`, with whitespace or commas between the components. mode="auto" generates the complete Das-Dennis lattice: with ``divisions``, all of its vectors; without, the largest lattice with at most ``pop_size`` vectors. The @@ -32,8 +58,7 @@ def load_or_generate_weight_vectors( if mode == "jmetalpy": return _load_or_generate_jmetalpy_weights(pop_size, n_obj, path, divisions) if path and os.path.exists(path): - weights = _load_weights(path, delimiter=",") - _assert_valid_weights(weights, n_obj) + weights = read_weight_vectors(path, n_obj) if weights.shape[0] != pop_size: raise ValueError( f"Weight file '{path}' contains {weights.shape[0]} vectors but pop_size={pop_size} requires exactly that many." @@ -49,13 +74,12 @@ def load_or_generate_weight_vectors( return weights -def _load_weights(path: str, delimiter: str | None = ",") -> FloatArray: - if delimiter is None: - arr = np.loadtxt(path) - else: - arr = np.loadtxt(path, delimiter=delimiter) - arr = np.atleast_2d(arr).astype(float, copy=False) - return arr +def _load_weights(path: str) -> FloatArray: + """The rows of a weight file whose components are separated by whitespace or commas.""" + with open(path, encoding="utf-8") as handle: + text = handle.read() + arr = np.loadtxt(io.StringIO(text.replace(",", " ")), ndmin=2) + return np.asarray(arr, dtype=float) def _load_or_generate_jmetalpy_weights(pop_size: int, n_obj: int, path: str | None, divisions: int | None) -> FloatArray: @@ -76,8 +100,7 @@ def _load_or_generate_jmetalpy_weights(pop_size: int, n_obj: int, path: str | No if packaged is not None: return packaged if weight_path is not None and os.path.exists(weight_path): - weights = _load_weights(weight_path, delimiter=None) - _assert_valid_weights(weights, n_obj) + weights = read_weight_vectors(weight_path, n_obj) if weights.shape[0] != pop_size: raise ValueError(f"Expected {pop_size} weight vectors in '{weight_path}', got {weights.shape[0]}.") return weights @@ -212,11 +235,11 @@ def _packaged_weights(n_obj: int, pop_size: int) -> FloatArray | None: path = weight_path(f"W{n_obj}D_{pop_size}.dat") except ValueError: return None - weights = _load_weights(str(path), delimiter=None) + weights = _load_weights(str(path)) if weights.shape != (pop_size, n_obj): return None + weights = _validated_weights(weights, n_obj) normalized: FloatArray = weights / weights.sum(axis=1, keepdims=True) - _assert_valid_weights(normalized, n_obj) return normalized @@ -229,17 +252,19 @@ def _resolve_jmetalpy_weight_path(path: str | None, n_obj: int, pop_size: int) - return os.path.join(path, filename) -def _assert_valid_weights(weights: FloatArray, n_obj: int) -> None: +def _validated_weights(weights: FloatArray, n_obj: int) -> FloatArray: + """``weights`` with the negative rounding of a zero component set to 0, or ``ValueError`` off the simplex.""" if weights.ndim != 2: raise ValueError("Weight matrix must be 2D.") if weights.shape[1] != n_obj: raise ValueError(f"Expected weight vectors with {n_obj} columns, got {weights.shape[1]}.") - if np.any(weights < 0.0): + if not np.all(np.isfinite(weights)): + raise ValueError("Weight vectors must be finite.") + if np.any(weights < -_ROUNDING_TOLERANCE): raise ValueError("Weight vectors must be non-negative.") - rows_sum = weights.sum(axis=1) - # Allow very small numerical drift - if np.any(np.abs(rows_sum - 1.0) > 1e-6): + if np.any(np.abs(weights.sum(axis=1) - 1.0) > _ROUNDING_TOLERANCE): raise ValueError("Each weight vector must sum to 1.") + return np.asarray(np.maximum(weights, 0.0), dtype=float) def _generate_weights(pop_size: int, n_obj: int, divisions: int | None, *, strict: bool = False) -> FloatArray: diff --git a/src/vamos/engine/algorithm/config/nsgaiii.py b/src/vamos/engine/algorithm/config/nsgaiii.py index 3fd0a593b..d9573bd5b 100644 --- a/src/vamos/engine/algorithm/config/nsgaiii.py +++ b/src/vamos/engine/algorithm/config/nsgaiii.py @@ -146,6 +146,17 @@ def reference_directions( path: str | None = None, divisions: int | None = None, ) -> _NSGAIIIConfigBuilder: + """The reference directions: a file, or the complete Das-Dennis lattice of ``divisions``. + + ``path`` names a file with one direction per line, its components separated by whitespace or + by commas, such as jMetal's weight files (``W3D_100.dat``, which VAMOS ships). Without either, + NSGA-III runs the complete lattice with the most directions that fit in ``pop_size``. The + population needs at least one individual per direction. + + **Development after 1.4.0:** the file may use whitespace as well as commas, and a ``path`` + that is not a file raises :class:`~vamos.exceptions.ConfigurationError` when the run starts; + VAMOS 1.4.0 read commas only, and generated the lattice and saved it at a missing path. + """ self._cfg["reference_directions"] = {"path": path, "divisions": divisions} return self diff --git a/src/vamos/engine/algorithm/nsgaiii/initialization.py b/src/vamos/engine/algorithm/nsgaiii/initialization.py index 7db528eaa..57f8f4261 100644 --- a/src/vamos/engine/algorithm/nsgaiii/initialization.py +++ b/src/vamos/engine/algorithm/nsgaiii/initialization.py @@ -10,7 +10,6 @@ from __future__ import annotations -import os from typing import TYPE_CHECKING, Any import numpy as np @@ -22,7 +21,7 @@ from vamos.engine.algorithm.components.population import initialize_evaluated_population from vamos.engine.algorithm.components.termination import parse_termination, validate_initial_budget from vamos.engine.algorithm.components.utils import resolve_bounds_array -from vamos.engine.algorithm.components.weight_vectors import load_or_generate_weight_vectors +from vamos.engine.algorithm.components.weight_vectors import load_or_generate_weight_vectors, read_weight_vectors from vamos.engine.algorithm.config.base import normalize_constraint_mode from vamos.engine.archive.factory import resolve_external_archive, setup_archive from vamos.engine.operators.policies.nsgaiii import build_variation_operators @@ -124,16 +123,10 @@ def initialize_nsgaiii_run( dir_cfg = config.get("reference_directions", {}) or {} ref_path = dir_cfg.get("path") ref_divisions = dir_cfg.get("divisions") - if ref_path and os.path.exists(ref_path): - ref_dirs = np.loadtxt(ref_path, delimiter=",") - ref_dirs = np.atleast_2d(ref_dirs).astype(float, copy=False) - if ref_dirs.ndim != 2 or ref_dirs.shape[1] != n_obj: - raise ValueError("Reference directions file has invalid shape.") - if np.any(ref_dirs < 0.0): - raise ValueError("Reference directions must be non-negative.") - row_sums = ref_dirs.sum(axis=1) - if np.any(np.abs(row_sums - 1.0) > 1e-6): - raise ValueError("Each reference direction must sum to 1.") + if ref_path: + # Development after 1.4.0: the file is read as MOEA/D reads its weights, with whitespace or commas + # between the components, and a path that is not a file raises instead of generating a lattice there. + ref_dirs = read_weight_vectors(str(ref_path), n_obj) source = f"NSGA-III reference directions in '{ref_path}' ({ref_dirs.shape[0]})" elif ref_divisions is not None: source = f"NSGA-III reference directions for divisions={ref_divisions}" @@ -145,14 +138,14 @@ def initialize_nsgaiii_run( f"{source} ({n_directions}) need pop_size >= {n_directions} (pop_size={pop_size}). " "Increase pop_size or choose fewer reference directions." ) - ref_dirs = load_or_generate_weight_vectors(pop_size, n_obj, path=ref_path, divisions=ref_divisions) + ref_dirs = load_or_generate_weight_vectors(pop_size, n_obj, divisions=ref_divisions) source = f"{source} ({ref_dirs.shape[0]})" else: if n_obj >= 2 and pop_size < n_obj: raise ValueError( f"NSGA-III needs pop_size >= n_obj={n_obj} for its smallest reference-direction lattice (pop_size={pop_size})." ) - ref_dirs = load_or_generate_weight_vectors(pop_size, n_obj, path=ref_path, divisions=None) + ref_dirs = load_or_generate_weight_vectors(pop_size, n_obj, divisions=None) source = f"NSGA-III reference directions ({ref_dirs.shape[0]})" if ref_dirs.shape[0] > pop_size: raise ValueError( diff --git a/src/vamos/resources/weights/W4D_100.dat b/src/vamos/resources/weights/W4D_100.dat index 5437be355..ba5ebd43b 100644 --- a/src/vamos/resources/weights/W4D_100.dat +++ b/src/vamos/resources/weights/W4D_100.dat @@ -98,104 +98,3 @@ 8.429740529506624114e-01 0.000000000000000000e+00 1.570259470493376441e-01 0.000000000000000000e+00 8.440820263497530451e-01 0.000000000000000000e+00 0.000000000000000000e+00 1.559179736502469826e-01 1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 -0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00 -0.000000000000000000e+00 0.000000000000000000e+00 1.517456609315984972e-01 8.482543390684015305e-01 -0.000000000000000000e+00 0.000000000000000000e+00 3.089549470541857468e-01 6.910450529458143087e-01 -0.000000000000000000e+00 0.000000000000000000e+00 5.466295666186646862e-01 4.533704333813353138e-01 -0.000000000000000000e+00 0.000000000000000000e+00 6.947742391618196844e-01 3.052257608381802600e-01 -0.000000000000000000e+00 0.000000000000000000e+00 8.479254959354245447e-01 1.520745040645754831e-01 -0.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00 -0.000000000000000000e+00 1.412384089809661236e-01 5.624229748565522380e-01 2.963386161624817494e-01 -0.000000000000000000e+00 1.423629748171094112e-01 7.087007729124448208e-01 1.489362522704458791e-01 -0.000000000000000000e+00 1.441214435901828073e-01 8.558785564098172483e-01 0.000000000000000000e+00 -0.000000000000000000e+00 1.458767324714570213e-01 4.156843258946683606e-01 4.384389416338745904e-01 -0.000000000000000000e+00 1.495294486434081971e-01 0.000000000000000000e+00 8.504705513565917752e-01 -0.000000000000000000e+00 1.578213537503010411e-01 2.731490650017161514e-01 5.690295812479828630e-01 -0.000000000000000000e+00 1.701299158218504970e-01 1.295402394617766184e-01 7.003298447163728291e-01 -0.000000000000000000e+00 2.810246413803882404e-01 4.274212471470156260e-01 2.915541114725960781e-01 -0.000000000000000000e+00 2.840648299873700933e-01 5.694408386062640437e-01 1.464943314063659185e-01 -0.000000000000000000e+00 2.856392317485221821e-01 7.143607682514777624e-01 0.000000000000000000e+00 -0.000000000000000000e+00 3.370977727138081970e-01 0.000000000000000000e+00 6.629022272861918585e-01 -0.000000000000000000e+00 3.428413274124658128e-01 1.473628042216627854e-01 5.097958683658714296e-01 -0.000000000000000000e+00 3.994430312256416205e-01 2.795659417121782453e-01 3.209910270621801898e-01 -0.000000000000000000e+00 4.240578223303994587e-01 5.759421776696005413e-01 0.000000000000000000e+00 -0.000000000000000000e+00 4.246086456342148541e-01 4.244496077704697501e-01 1.509417465953153958e-01 -0.000000000000000000e+00 5.070995911406093182e-01 0.000000000000000000e+00 4.929004088593907373e-01 -0.000000000000000000e+00 5.401344914838474009e-01 1.476893612652001309e-01 3.121761472509524404e-01 -0.000000000000000000e+00 5.625778083857098100e-01 4.374221916142901900e-01 0.000000000000000000e+00 -0.000000000000000000e+00 5.632819504494038787e-01 2.833586250189266131e-01 1.533594245316695082e-01 -0.000000000000000000e+00 6.630528350202686827e-01 0.000000000000000000e+00 3.369471649797312618e-01 -0.000000000000000000e+00 6.989035290008067669e-01 1.391044667651976652e-01 1.619920042339955124e-01 -0.000000000000000000e+00 7.046893185563298800e-01 2.953106814436701755e-01 0.000000000000000000e+00 -0.000000000000000000e+00 8.439219537004628569e-01 0.000000000000000000e+00 1.560780462995371431e-01 -0.000000000000000000e+00 8.504298799135847631e-01 1.495701200864152369e-01 0.000000000000000000e+00 -0.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 -9.332109925208263712e-02 2.410487703783708580e-01 2.601181327546606137e-01 4.055119976148859329e-01 -1.212740413880061957e-01 0.000000000000000000e+00 3.647959707366896787e-01 5.139299878753040840e-01 -1.369935295075066228e-01 0.000000000000000000e+00 1.566151949709478353e-01 7.063912755215455697e-01 -1.421361736538943876e-01 1.714205361240309644e-01 1.356319197625622808e-01 5.508113704595123394e-01 -1.436625582169020476e-01 4.144242224102873684e-01 2.779839751900682332e-01 1.639292441827423230e-01 -1.450022980917531623e-01 3.829565605806742323e-01 1.389527029319885809e-01 3.330884383955841632e-01 -1.465596222526458769e-01 1.502564688895632605e-01 0.000000000000000000e+00 7.031839088577908070e-01 -1.487231962470322855e-01 5.526325005461416584e-01 1.394248464386060038e-01 1.592194567682200246e-01 -1.489306359046610462e-01 6.939000551211376155e-01 0.000000000000000000e+00 1.571693089742013660e-01 -1.500358520459580680e-01 8.499641479540419597e-01 0.000000000000000000e+00 0.000000000000000000e+00 -1.501335485134633330e-01 7.043126614102592775e-01 1.455537900762775005e-01 0.000000000000000000e+00 -1.507142706495444850e-01 0.000000000000000000e+00 6.940758176753867303e-01 1.552099116750688679e-01 -1.519930840468924349e-01 5.602772864737478598e-01 2.877296294793597053e-01 0.000000000000000000e+00 -1.523420244303578808e-01 5.257209554206587754e-01 0.000000000000000000e+00 3.219370201489833438e-01 -1.524078606640256461e-01 0.000000000000000000e+00 0.000000000000000000e+00 8.475921393359744371e-01 -1.533521647494212881e-01 4.209290337311242802e-01 4.257188015194544595e-01 0.000000000000000000e+00 -1.538210881409899922e-01 0.000000000000000000e+00 8.461789118590100633e-01 0.000000000000000000e+00 -1.559895216299089504e-01 1.460337020044912559e-01 3.961563965134911047e-01 3.018203798521087444e-01 -1.566976092850831948e-01 0.000000000000000000e+00 5.289109745988019862e-01 3.143914161161148191e-01 -1.568836053200171610e-01 3.413435528577909994e-01 0.000000000000000000e+00 5.017728418221919506e-01 -1.568845799015602671e-01 1.373091060518208950e-01 5.524613883817918181e-01 1.533449256648268255e-01 -1.571075456338251186e-01 1.405083587593243255e-01 7.023840956068505559e-01 0.000000000000000000e+00 -1.588512186002686011e-01 2.792591813400139844e-01 4.156031404318044076e-01 1.462864596279130902e-01 -1.613886171348187515e-01 2.784840738855490216e-01 5.601273089796322546e-01 0.000000000000000000e+00 -2.365134513390303728e-01 9.090793825365048320e-02 2.540151534230933872e-01 4.185634569842257013e-01 -2.404347199519812905e-01 2.536559335784447367e-01 2.501133217967827505e-01 2.557960246727911113e-01 -2.629799327445451196e-01 2.288445742774933012e-01 1.065792872857167090e-01 4.015962056922448009e-01 -2.721534083761060208e-01 0.000000000000000000e+00 1.606546080760528072e-01 5.671919835478411720e-01 -2.900501965976872709e-01 1.479287288160751890e-01 0.000000000000000000e+00 5.620210745862375123e-01 -2.952343245699609042e-01 5.449582419458804949e-01 0.000000000000000000e+00 1.598074334841586008e-01 -2.956716328119778914e-01 3.994714888615369386e-01 1.465170901760065314e-01 1.583397881504785831e-01 -2.987972120709255219e-01 5.503631658960104200e-01 1.508396220330641413e-01 0.000000000000000000e+00 -2.995714301255981704e-01 3.807114452386846648e-01 0.000000000000000000e+00 3.197171246357172203e-01 -2.999701763798350296e-01 7.000298236201649704e-01 0.000000000000000000e+00 0.000000000000000000e+00 -3.046201048185363525e-01 0.000000000000000000e+00 0.000000000000000000e+00 6.953798951814637030e-01 -3.061894102691745667e-01 0.000000000000000000e+00 3.812755900699066336e-01 3.125349996609188552e-01 -3.105781443925872409e-01 0.000000000000000000e+00 5.370094640114077000e-01 1.524123915960051145e-01 -3.108289349820835268e-01 1.460563518652158721e-01 3.895117232969453336e-01 1.536029898557552120e-01 -3.138582953296881750e-01 0.000000000000000000e+00 6.861417046703118805e-01 0.000000000000000000e+00 -3.209403561001402116e-01 3.281779765690368000e-01 3.508816673308229328e-01 0.000000000000000000e+00 -3.213934688366851811e-01 1.510159286089505226e-01 5.275906025543644073e-01 0.000000000000000000e+00 -3.910715527510944534e-01 1.040886070656628637e-01 2.329239847295487553e-01 2.719158554536937888e-01 -3.947924188830133074e-01 2.470890433411349063e-01 2.361979545699287864e-01 1.219205832059229305e-01 -4.081993048877988750e-01 2.476787651899039211e-01 8.768052442063588303e-02 2.564414055016613347e-01 -4.102336777214580787e-01 0.000000000000000000e+00 1.598616460422517260e-01 4.299046762362901952e-01 -4.208429267198299795e-01 1.604390692320503131e-01 0.000000000000000000e+00 4.187180040481197074e-01 -4.375560233977611735e-01 4.039415425099037171e-01 1.585024340923351094e-01 0.000000000000000000e+00 -4.399469464187258927e-01 4.032830980024528778e-01 0.000000000000000000e+00 1.567699555788212573e-01 -4.431091761516853245e-01 5.568908238483146755e-01 0.000000000000000000e+00 0.000000000000000000e+00 -4.564548153198588198e-01 0.000000000000000000e+00 3.815814211605010331e-01 1.619637635196401748e-01 -4.690946983303491691e-01 1.541207371814345417e-01 3.767845644882162337e-01 0.000000000000000000e+00 -4.837886685645650942e-01 0.000000000000000000e+00 5.162113314354348503e-01 0.000000000000000000e+00 -5.373476860169809077e-01 0.000000000000000000e+00 0.000000000000000000e+00 4.626523139830191478e-01 -5.453144677206371860e-01 9.495948820996948092e-02 2.309597454833887120e-01 1.287662985860044962e-01 -5.463839790674551722e-01 2.438395463904204641e-01 2.097764745421244470e-01 0.000000000000000000e+00 -5.529952958900917759e-01 0.000000000000000000e+00 1.545369651557695068e-01 2.924677389541386341e-01 -5.623868732886577737e-01 2.598427723837856695e-01 5.546029346365282525e-02 1.223100608639037246e-01 -5.626430685116043939e-01 1.521743563794337173e-01 0.000000000000000000e+00 2.851825751089618333e-01 -5.844674952632212817e-01 4.155325047367787183e-01 0.000000000000000000e+00 0.000000000000000000e+00 -6.563690883274037047e-01 0.000000000000000000e+00 3.436309116725962953e-01 0.000000000000000000e+00 -6.886068974557958411e-01 0.000000000000000000e+00 0.000000000000000000e+00 3.113931025442042144e-01 -6.996476200456240857e-01 0.000000000000000000e+00 1.493473002001867722e-01 1.510050797541890866e-01 -7.040069821035184194e-01 1.407880700084996606e-01 1.552049478879818922e-01 0.000000000000000000e+00 -7.098710837998043477e-01 1.411430187093619393e-01 0.000000000000000000e+00 1.489858974908336853e-01 -7.253557383391620039e-01 2.746442616608380516e-01 0.000000000000000000e+00 0.000000000000000000e+00 -8.429740529506624114e-01 0.000000000000000000e+00 1.570259470493376441e-01 0.000000000000000000e+00 -8.440820263497530451e-01 0.000000000000000000e+00 0.000000000000000000e+00 1.559179736502469826e-01 -1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 - diff --git a/src/vamos/resources/weights/W4D_165.dat b/src/vamos/resources/weights/W4D_165.dat index aff7d976d..144b98127 100644 --- a/src/vamos/resources/weights/W4D_165.dat +++ b/src/vamos/resources/weights/W4D_165.dat @@ -1,165 +1,165 @@ -0.0,0.0,0.0,1.0 -0.0,0.0,0.125,0.875 -0.0,0.0,0.25,0.75 -0.0,0.0,0.375,0.625 -0.0,0.0,0.5,0.5 -0.0,0.0,0.625,0.375 -0.0,0.0,0.75,0.25 -0.0,0.0,0.875,0.125 -0.0,0.0,1.0,0.0 -0.0,0.125,0.0,0.875 -0.0,0.125,0.125,0.75 -0.0,0.125,0.25,0.625 -0.0,0.125,0.375,0.5 -0.0,0.125,0.5,0.375 -0.0,0.125,0.625,0.25 -0.0,0.125,0.75,0.125 -0.0,0.125,0.875,0.0 -0.0,0.25,0.0,0.75 -0.0,0.25,0.125,0.625 -0.0,0.25,0.25,0.5 -0.0,0.25,0.375,0.375 -0.0,0.25,0.5,0.25 -0.0,0.25,0.625,0.125 -0.0,0.25,0.75,0.0 -0.0,0.375,0.0,0.625 -0.0,0.375,0.125,0.5 -0.0,0.375,0.25,0.375 -0.0,0.375,0.375,0.25 -0.0,0.375,0.5,0.125 -0.0,0.375,0.625,0.0 -0.0,0.5,0.0,0.5 -0.0,0.5,0.125,0.375 -0.0,0.5,0.25,0.25 -0.0,0.5,0.375,0.125 -0.0,0.5,0.5,0.0 -0.0,0.625,0.0,0.375 -0.0,0.625,0.125,0.25 -0.0,0.625,0.25,0.125 -0.0,0.625,0.375,0.0 -0.0,0.75,0.0,0.25 -0.0,0.75,0.125,0.125 -0.0,0.75,0.25,0.0 -0.0,0.875,0.0,0.125 -0.0,0.875,0.125,0.0 -0.0,1.0,0.0,0.0 -0.125,0.0,0.0,0.875 -0.125,0.0,0.125,0.75 -0.125,0.0,0.25,0.625 -0.125,0.0,0.375,0.5 -0.125,0.0,0.5,0.375 -0.125,0.0,0.625,0.25 -0.125,0.0,0.75,0.125 -0.125,0.0,0.875,0.0 -0.125,0.125,0.0,0.75 -0.125,0.125,0.125,0.625 -0.125,0.125,0.25,0.5 -0.125,0.125,0.375,0.375 -0.125,0.125,0.5,0.25 -0.125,0.125,0.625,0.125 -0.125,0.125,0.75,0.0 -0.125,0.25,0.0,0.625 -0.125,0.25,0.125,0.5 -0.125,0.25,0.25,0.375 -0.125,0.25,0.375,0.25 -0.125,0.25,0.5,0.125 -0.125,0.25,0.625,0.0 -0.125,0.375,0.0,0.5 -0.125,0.375,0.125,0.375 -0.125,0.375,0.25,0.25 -0.125,0.375,0.375,0.125 -0.125,0.375,0.5,0.0 -0.125,0.5,0.0,0.375 -0.125,0.5,0.125,0.25 -0.125,0.5,0.25,0.125 -0.125,0.5,0.375,0.0 -0.125,0.625,0.0,0.25 -0.125,0.625,0.125,0.125 -0.125,0.625,0.25,0.0 -0.125,0.75,0.0,0.125 -0.125,0.75,0.125,0.0 -0.125,0.875,0.0,0.0 -0.25,0.0,0.0,0.75 -0.25,0.0,0.125,0.625 -0.25,0.0,0.25,0.5 -0.25,0.0,0.375,0.375 -0.25,0.0,0.5,0.25 -0.25,0.0,0.625,0.125 -0.25,0.0,0.75,0.0 -0.25,0.125,0.0,0.625 -0.25,0.125,0.125,0.5 -0.25,0.125,0.25,0.375 -0.25,0.125,0.375,0.25 -0.25,0.125,0.5,0.125 -0.25,0.125,0.625,0.0 -0.25,0.25,0.0,0.5 -0.25,0.25,0.125,0.375 -0.25,0.25,0.25,0.25 -0.25,0.25,0.375,0.125 -0.25,0.25,0.5,0.0 -0.25,0.375,0.0,0.375 -0.25,0.375,0.125,0.25 -0.25,0.375,0.25,0.125 -0.25,0.375,0.375,0.0 -0.25,0.5,0.0,0.25 -0.25,0.5,0.125,0.125 -0.25,0.5,0.25,0.0 -0.25,0.625,0.0,0.125 -0.25,0.625,0.125,0.0 -0.25,0.75,0.0,0.0 -0.375,0.0,0.0,0.625 -0.375,0.0,0.125,0.5 -0.375,0.0,0.25,0.375 -0.375,0.0,0.375,0.25 -0.375,0.0,0.5,0.125 -0.375,0.0,0.625,0.0 -0.375,0.125,0.0,0.5 -0.375,0.125,0.125,0.375 -0.375,0.125,0.25,0.25 -0.375,0.125,0.375,0.125 -0.375,0.125,0.5,0.0 -0.375,0.25,0.0,0.375 -0.375,0.25,0.125,0.25 -0.375,0.25,0.25,0.125 -0.375,0.25,0.375,0.0 -0.375,0.375,0.0,0.25 -0.375,0.375,0.125,0.125 -0.375,0.375,0.25,0.0 -0.375,0.5,0.0,0.125 -0.375,0.5,0.125,0.0 -0.375,0.625,0.0,0.0 -0.5,0.0,0.0,0.5 -0.5,0.0,0.125,0.375 -0.5,0.0,0.25,0.25 -0.5,0.0,0.375,0.125 -0.5,0.0,0.5,0.0 -0.5,0.125,0.0,0.375 -0.5,0.125,0.125,0.25 -0.5,0.125,0.25,0.125 -0.5,0.125,0.375,0.0 -0.5,0.25,0.0,0.25 -0.5,0.25,0.125,0.125 -0.5,0.25,0.25,0.0 -0.5,0.375,0.0,0.125 -0.5,0.375,0.125,0.0 -0.5,0.5,0.0,0.0 -0.625,0.0,0.0,0.375 -0.625,0.0,0.125,0.25 -0.625,0.0,0.25,0.125 -0.625,0.0,0.375,0.0 -0.625,0.125,0.0,0.25 -0.625,0.125,0.125,0.125 -0.625,0.125,0.25,0.0 -0.625,0.25,0.0,0.125 -0.625,0.25,0.125,0.0 -0.625,0.375,0.0,0.0 -0.75,0.0,0.0,0.25 -0.75,0.0,0.125,0.125 -0.75,0.0,0.25,0.0 -0.75,0.125,0.0,0.125 -0.75,0.125,0.125,0.0 -0.75,0.25,0.0,0.0 -0.875,0.0,0.0,0.125 -0.875,0.0,0.125,0.0 -0.875,0.125,0.0,0.0 -1.0,0.0,0.0,0.0 +0.0 0.0 0.0 1.0 +0.0 0.0 0.125 0.875 +0.0 0.0 0.25 0.75 +0.0 0.0 0.375 0.625 +0.0 0.0 0.5 0.5 +0.0 0.0 0.625 0.375 +0.0 0.0 0.75 0.25 +0.0 0.0 0.875 0.125 +0.0 0.0 1.0 0.0 +0.0 0.125 0.0 0.875 +0.0 0.125 0.125 0.75 +0.0 0.125 0.25 0.625 +0.0 0.125 0.375 0.5 +0.0 0.125 0.5 0.375 +0.0 0.125 0.625 0.25 +0.0 0.125 0.75 0.125 +0.0 0.125 0.875 0.0 +0.0 0.25 0.0 0.75 +0.0 0.25 0.125 0.625 +0.0 0.25 0.25 0.5 +0.0 0.25 0.375 0.375 +0.0 0.25 0.5 0.25 +0.0 0.25 0.625 0.125 +0.0 0.25 0.75 0.0 +0.0 0.375 0.0 0.625 +0.0 0.375 0.125 0.5 +0.0 0.375 0.25 0.375 +0.0 0.375 0.375 0.25 +0.0 0.375 0.5 0.125 +0.0 0.375 0.625 0.0 +0.0 0.5 0.0 0.5 +0.0 0.5 0.125 0.375 +0.0 0.5 0.25 0.25 +0.0 0.5 0.375 0.125 +0.0 0.5 0.5 0.0 +0.0 0.625 0.0 0.375 +0.0 0.625 0.125 0.25 +0.0 0.625 0.25 0.125 +0.0 0.625 0.375 0.0 +0.0 0.75 0.0 0.25 +0.0 0.75 0.125 0.125 +0.0 0.75 0.25 0.0 +0.0 0.875 0.0 0.125 +0.0 0.875 0.125 0.0 +0.0 1.0 0.0 0.0 +0.125 0.0 0.0 0.875 +0.125 0.0 0.125 0.75 +0.125 0.0 0.25 0.625 +0.125 0.0 0.375 0.5 +0.125 0.0 0.5 0.375 +0.125 0.0 0.625 0.25 +0.125 0.0 0.75 0.125 +0.125 0.0 0.875 0.0 +0.125 0.125 0.0 0.75 +0.125 0.125 0.125 0.625 +0.125 0.125 0.25 0.5 +0.125 0.125 0.375 0.375 +0.125 0.125 0.5 0.25 +0.125 0.125 0.625 0.125 +0.125 0.125 0.75 0.0 +0.125 0.25 0.0 0.625 +0.125 0.25 0.125 0.5 +0.125 0.25 0.25 0.375 +0.125 0.25 0.375 0.25 +0.125 0.25 0.5 0.125 +0.125 0.25 0.625 0.0 +0.125 0.375 0.0 0.5 +0.125 0.375 0.125 0.375 +0.125 0.375 0.25 0.25 +0.125 0.375 0.375 0.125 +0.125 0.375 0.5 0.0 +0.125 0.5 0.0 0.375 +0.125 0.5 0.125 0.25 +0.125 0.5 0.25 0.125 +0.125 0.5 0.375 0.0 +0.125 0.625 0.0 0.25 +0.125 0.625 0.125 0.125 +0.125 0.625 0.25 0.0 +0.125 0.75 0.0 0.125 +0.125 0.75 0.125 0.0 +0.125 0.875 0.0 0.0 +0.25 0.0 0.0 0.75 +0.25 0.0 0.125 0.625 +0.25 0.0 0.25 0.5 +0.25 0.0 0.375 0.375 +0.25 0.0 0.5 0.25 +0.25 0.0 0.625 0.125 +0.25 0.0 0.75 0.0 +0.25 0.125 0.0 0.625 +0.25 0.125 0.125 0.5 +0.25 0.125 0.25 0.375 +0.25 0.125 0.375 0.25 +0.25 0.125 0.5 0.125 +0.25 0.125 0.625 0.0 +0.25 0.25 0.0 0.5 +0.25 0.25 0.125 0.375 +0.25 0.25 0.25 0.25 +0.25 0.25 0.375 0.125 +0.25 0.25 0.5 0.0 +0.25 0.375 0.0 0.375 +0.25 0.375 0.125 0.25 +0.25 0.375 0.25 0.125 +0.25 0.375 0.375 0.0 +0.25 0.5 0.0 0.25 +0.25 0.5 0.125 0.125 +0.25 0.5 0.25 0.0 +0.25 0.625 0.0 0.125 +0.25 0.625 0.125 0.0 +0.25 0.75 0.0 0.0 +0.375 0.0 0.0 0.625 +0.375 0.0 0.125 0.5 +0.375 0.0 0.25 0.375 +0.375 0.0 0.375 0.25 +0.375 0.0 0.5 0.125 +0.375 0.0 0.625 0.0 +0.375 0.125 0.0 0.5 +0.375 0.125 0.125 0.375 +0.375 0.125 0.25 0.25 +0.375 0.125 0.375 0.125 +0.375 0.125 0.5 0.0 +0.375 0.25 0.0 0.375 +0.375 0.25 0.125 0.25 +0.375 0.25 0.25 0.125 +0.375 0.25 0.375 0.0 +0.375 0.375 0.0 0.25 +0.375 0.375 0.125 0.125 +0.375 0.375 0.25 0.0 +0.375 0.5 0.0 0.125 +0.375 0.5 0.125 0.0 +0.375 0.625 0.0 0.0 +0.5 0.0 0.0 0.5 +0.5 0.0 0.125 0.375 +0.5 0.0 0.25 0.25 +0.5 0.0 0.375 0.125 +0.5 0.0 0.5 0.0 +0.5 0.125 0.0 0.375 +0.5 0.125 0.125 0.25 +0.5 0.125 0.25 0.125 +0.5 0.125 0.375 0.0 +0.5 0.25 0.0 0.25 +0.5 0.25 0.125 0.125 +0.5 0.25 0.25 0.0 +0.5 0.375 0.0 0.125 +0.5 0.375 0.125 0.0 +0.5 0.5 0.0 0.0 +0.625 0.0 0.0 0.375 +0.625 0.0 0.125 0.25 +0.625 0.0 0.25 0.125 +0.625 0.0 0.375 0.0 +0.625 0.125 0.0 0.25 +0.625 0.125 0.125 0.125 +0.625 0.125 0.25 0.0 +0.625 0.25 0.0 0.125 +0.625 0.25 0.125 0.0 +0.625 0.375 0.0 0.0 +0.75 0.0 0.0 0.25 +0.75 0.0 0.125 0.125 +0.75 0.0 0.25 0.0 +0.75 0.125 0.0 0.125 +0.75 0.125 0.125 0.0 +0.75 0.25 0.0 0.0 +0.875 0.0 0.0 0.125 +0.875 0.0 0.125 0.0 +0.875 0.125 0.0 0.0 +1.0 0.0 0.0 0.0 diff --git a/tests/engine/test_moead_weight_vectors.py b/tests/engine/test_moead_weight_vectors.py index 4f661473c..cf9b3ab95 100644 --- a/tests/engine/test_moead_weight_vectors.py +++ b/tests/engine/test_moead_weight_vectors.py @@ -110,12 +110,114 @@ def test_packaged_weights_load_despite_their_rounding(n_obj: int, pop_size: int) np.testing.assert_allclose(weights, stored, atol=1e-5) -def test_packaged_weights_with_another_size_are_not_used() -> None: - from vamos.engine.algorithm.components.weight_vectors import load_or_generate_weight_vectors +def _packaged_weight_files() -> list[tuple[str, int, int]]: + import re + from importlib import resources + + names = (entry.name for entry in resources.files("vamos.resources").joinpath("weights").iterdir()) + matches = (re.fullmatch(r"W(\d+)D_(\d+)\.dat", name) for name in names) + return sorted((match.group(0), int(match.group(1)), int(match.group(2))) for match in matches if match is not None) + + +def test_vamos_ships_the_jmetal_weight_files() -> None: + files = _packaged_weight_files() + + # jMetal's 29 files, jMetalPy's W4D_100.dat and W3D_120.dat. + assert len(files) == 31 + assert ("W4D_100.dat", 4, 100) in files and ("W4D_165.dat", 4, 165) in files + + +@pytest.mark.parametrize(("name", "n_obj", "pop_size"), _packaged_weight_files()) +def test_every_packaged_weight_file_loads_with_one_vector_per_individual(name: str, n_obj: int, pop_size: int) -> None: + from vamos.engine.algorithm.components.weight_vectors import _packaged_weights, read_weight_vectors + from vamos.resources import weight_path + + packaged = _packaged_weights(n_obj, pop_size) + read = read_weight_vectors(str(weight_path(name)), n_obj) + + assert packaged is not None and packaged.shape == (pop_size, n_obj) + assert read.shape == (pop_size, n_obj) + np.testing.assert_allclose(read.sum(axis=1), 1.0, atol=1e-5) + assert read.min() >= 0.0 + np.testing.assert_allclose(packaged, read, atol=1e-5) + + +def test_moead_with_four_objectives_and_100_individuals_runs_the_packaged_weights() -> None: + from vamos.resources import weight_path + + weights = load_or_generate_weight_vectors(100, 4, mode="jmetalpy") + + stored = np.loadtxt(weight_path("W4D_100.dat")) + assert stored.shape == (100, 4) and len(np.unique(stored, axis=0)) == 100 + np.testing.assert_allclose(weights, stored, atol=1e-12) + assert largest_weighted_population(100, 4) == 100 + assert largest_weighted_population(119, 4) == 100 + assert largest_weighted_population(99, 4) == 84 + + +@pytest.mark.parametrize("pop_size", [91, 100, 136, 1000]) +def test_the_packaged_directory_reads_as_any_jmetal_directory(pop_size: int) -> None: + # The command line passes the packaged directory as weight_vectors.path: its files must load from there too, + # W3D_136.dat with components of -5e-17 and W3D_1000.dat with rows that sum to 1 within 1e-6. + from vamos.resources import weight_path + + directory = str(weight_path("W3D_91.dat").parent) + + weights = load_or_generate_weight_vectors(pop_size, 3, path=directory, mode="jmetalpy") + + assert weights.shape == (pop_size, 3) and weights.min() >= 0.0 + np.testing.assert_allclose(weights, np.loadtxt(weight_path(f"W3D_{pop_size}.dat")), atol=1e-12) + + +@pytest.mark.parametrize(("pop_size", "n_obj"), [(100, 4), (165, 4)]) +def test_the_four_objective_files_load_from_the_packaged_directory(pop_size: int, n_obj: int) -> None: + from vamos.resources import weight_path + + directory = str(weight_path("W3D_91.dat").parent) + + assert load_or_generate_weight_vectors(pop_size, n_obj, path=directory, mode="jmetalpy").shape == (pop_size, n_obj) + + +@pytest.mark.parametrize("separator", [" ", ",", ", ", "\t"]) +def test_a_weight_file_may_separate_its_components_with_whitespace_or_commas(tmp_path, separator: str) -> None: + from vamos.engine.algorithm.components.weight_vectors import read_weight_vectors + from vamos.engine.reference_directions import das_dennis + + lattice = das_dennis(3, 4) + path = tmp_path / "W3D_15.dat" + path.write_text("\n".join(separator.join(repr(float(value)) for value in row) for row in lattice) + "\n", encoding="utf-8") + + np.testing.assert_array_equal(read_weight_vectors(str(path), 3), lattice) + np.testing.assert_array_equal(load_or_generate_weight_vectors(15, 3, path=str(path), mode="jmetalpy"), lattice) + + +@pytest.mark.parametrize( + ("rows", "message"), + [ + ([[0.5, 0.5, 0.1]], "sum to 1"), + ([[1.2, -0.2, 0.0]], "non-negative"), + ([[0.5, 0.5]], "3 columns"), + ([[float("nan"), 0.5, 0.5]], "finite"), + ], +) +def test_a_weight_file_off_the_simplex_is_rejected(tmp_path, rows: list[list[float]], message: str) -> None: + from vamos.engine.algorithm.components.weight_vectors import read_weight_vectors + + path = tmp_path / "weights.dat" + np.savetxt(path, np.asarray(rows)) + + with pytest.raises(ValueError, match=message): + read_weight_vectors(str(path), 3) + + +def test_a_missing_weight_file_is_a_configuration_error(tmp_path) -> None: + from vamos.engine.algorithm.components.weight_vectors import read_weight_vectors + from vamos.exceptions import ConfigurationError - # W4D_100.dat holds 200 vectors: 100 individuals with four objectives keep the lattice error. - with pytest.raises(ValueError, match="pop_size=100"): - load_or_generate_weight_vectors(100, 4, mode="jmetalpy") + with pytest.raises(ConfigurationError, match="does not exist"): + read_weight_vectors(str(tmp_path / "missing.dat"), 3) + with pytest.raises(ConfigurationError, match="not a file"): + read_weight_vectors(str(tmp_path), 3) def test_largest_weighted_population_keeps_sizes_with_weights_and_lowers_the_others() -> None: diff --git a/tests/engine/test_nsgaiii_reference_direction_files.py b/tests/engine/test_nsgaiii_reference_direction_files.py new file mode 100644 index 000000000..4d1a1e68a --- /dev/null +++ b/tests/engine/test_nsgaiii_reference_direction_files.py @@ -0,0 +1,64 @@ +"""NSGA-III's ``reference_directions={"path": ...}``: jMetal's weight files, with whitespace or commas, and a missing file.""" + +from __future__ import annotations + +from pathlib import Path + +import numpy as np +import pytest + +from vamos.engine.algorithm.config import NSGAIIIConfig +from vamos.engine.algorithm.nsgaiii import NSGAIII +from vamos.exceptions import ConfigurationError +from vamos.foundation.kernel.numpy_backend import NumPyKernel +from vamos.foundation.problem.dtlz import DTLZ2Problem +from vamos.resources import weight_path + + +def _config(pop_size: int, path: str) -> dict[str, object]: + return ( + NSGAIIIConfig.builder() + .pop_size(pop_size) + .crossover("sbx", prob=0.9, eta=30.0) + .mutation("pm", prob="1/n", eta=20.0) + .selection("tournament") + .reference_directions(path=path) + .build() + ).to_dict() + + +def _run(pop_size: int, path: str) -> dict[str, object]: + algorithm = NSGAIII(_config(pop_size, path), kernel=NumPyKernel()) + return algorithm.run(DTLZ2Problem(n_var=12, n_obj=3), termination=("max_evaluations", 2 * pop_size), seed=1) + + +def test_nsgaiii_reads_the_whitespace_separated_jmetal_file() -> None: + result = _run(100, str(weight_path("W3D_100.dat"))) + + np.testing.assert_array_equal(result["reference_directions"], np.loadtxt(weight_path("W3D_100.dat"))) + assert result["evaluations"] == 200 + + +@pytest.mark.parametrize("separator", [",", " "]) +def test_nsgaiii_reads_comma_and_whitespace_separated_files_alike(tmp_path: Path, separator: str) -> None: + stored = np.loadtxt(weight_path("W3D_91.dat")) + path = tmp_path / "directions.csv" + np.savetxt(path, stored, delimiter=separator) + + result = _run(91, str(path)) + + np.testing.assert_array_equal(result["reference_directions"], stored) + + +def test_a_missing_reference_direction_file_raises_and_writes_nothing(tmp_path: Path) -> None: + missing = tmp_path / "missing" / "W3D_91.dat" + + with pytest.raises(ConfigurationError, match="does not exist"): + _run(91, str(missing)) + # VAMOS 1.4.0 generated the lattice and saved it there. + assert not missing.exists() and not missing.parent.exists() + + +def test_a_reference_direction_file_with_more_directions_than_individuals_raises() -> None: + with pytest.raises(ValueError, match="pop_size >= 100"): + _run(91, str(weight_path("W3D_100.dat")))