nmoo.indicators.delta_f

ΔF performance indicator, which measures how accurately a problem estimates the true objective. To measure accuracy for Pareto individuals only, see nmoo.indicators.delta_f_pareto.DeltaFPareto.

 1"""
 2ΔF performance indicator, which measures how accurately a problem estimates the
 3true objective. To measure accuracy for Pareto individuals only, see
 4`nmoo.indicators.delta_f_pareto.DeltaFPareto`.
 5"""
 6__docformat__ = "google"
 7
 8from pathlib import Path
 9from typing import Any, Dict, Optional, Union
10
11import numpy as np
12from pymoo.core.indicator import Indicator
13from pymoo.core.problem import Problem
14
15from nmoo.wrapped_problem import WrappedProblem
16
17
18class DeltaF(Indicator):
19    """
20    The ΔF performance indicator. Given
21    * a `nmoo.wrapped_problem.WrappedProblem` with `g` its ground problem,
22    * a population `X`,
23    * and their image (under the objective function) array `F`,
24
25    calculates the vectorized average Euclidean distance between `F` and `Y`,
26    where `Y` is obtained by averaging `g(X)` a given number of times.
27
28    You can use this PI in a benchmark as
29    ```py
30    Benchmark(
31        ...
32        performance_indicators=[..., "df", ...]
33    )
34    ```
35    """
36
37    _ground_problem: Problem
38    """The ground problem."""
39
40    _history: Dict[str, np.ndarray]
41    """
42    Contains all `X` values submitted to this indicator, and all denoised `F`
43    values.
44    """
45
46    _history_path: Optional[Path]
47
48    _n_evals: int
49    """
50    Number of evaluations of the ground problem that will be averaged in order
51    to approximate the ground Pareto front.
52    """
53
54    def __init__(
55        self,
56        problem: WrappedProblem,
57        n_evals: int = 1,
58        history_path: Optional[Union[str, Path]] = None,
59        **kwargs,
60    ):
61        super().__init__(zero_to_one=False, **kwargs)
62        self._ground_problem = problem.ground_problem()
63        self._history_path = (
64            Path(history_path)
65            if isinstance(history_path, str)
66            else history_path
67        )
68        self._history = {}
69        if n_evals < 1:
70            raise ValueError("n_evals must be >= 1")
71        self._n_evals = n_evals
72
73    def _do(self, F: np.ndarray, *args, **kwargs) -> float:
74        """
75        Calculates ΔF. See class documentation.
76        """
77        if not args:
78            raise ValueError(
79                "Need a second argument (namely the X array) when calling "
80                "DeltaF.do"
81            )
82        X, fs = args[0], []
83        for _ in range(self._n_evals):
84            out: Dict[str, Any] = {}
85            self._ground_problem._evaluate(X, out, *args, **kwargs)
86            fs.append(out["F"])
87        self._history = {"X": X, "F": np.mean(np.array(fs), axis=0)}
88        self.dump_history()
89        return np.mean(np.linalg.norm(F - self._history["F"], axis=-1))
90
91    def dump_history(self):
92        """Dumps the history into an NPZ archive"""
93        if isinstance(self._history_path, Path):
94            np.savez_compressed(self._history_path, **self._history)
class DeltaF(pymoo.core.indicator.Indicator):
19class DeltaF(Indicator):
20    """
21    The ΔF performance indicator. Given
22    * a `nmoo.wrapped_problem.WrappedProblem` with `g` its ground problem,
23    * a population `X`,
24    * and their image (under the objective function) array `F`,
25
26    calculates the vectorized average Euclidean distance between `F` and `Y`,
27    where `Y` is obtained by averaging `g(X)` a given number of times.
28
29    You can use this PI in a benchmark as
30    ```py
31    Benchmark(
32        ...
33        performance_indicators=[..., "df", ...]
34    )
35    ```
36    """
37
38    _ground_problem: Problem
39    """The ground problem."""
40
41    _history: Dict[str, np.ndarray]
42    """
43    Contains all `X` values submitted to this indicator, and all denoised `F`
44    values.
45    """
46
47    _history_path: Optional[Path]
48
49    _n_evals: int
50    """
51    Number of evaluations of the ground problem that will be averaged in order
52    to approximate the ground Pareto front.
53    """
54
55    def __init__(
56        self,
57        problem: WrappedProblem,
58        n_evals: int = 1,
59        history_path: Optional[Union[str, Path]] = None,
60        **kwargs,
61    ):
62        super().__init__(zero_to_one=False, **kwargs)
63        self._ground_problem = problem.ground_problem()
64        self._history_path = (
65            Path(history_path)
66            if isinstance(history_path, str)
67            else history_path
68        )
69        self._history = {}
70        if n_evals < 1:
71            raise ValueError("n_evals must be >= 1")
72        self._n_evals = n_evals
73
74    def _do(self, F: np.ndarray, *args, **kwargs) -> float:
75        """
76        Calculates ΔF. See class documentation.
77        """
78        if not args:
79            raise ValueError(
80                "Need a second argument (namely the X array) when calling "
81                "DeltaF.do"
82            )
83        X, fs = args[0], []
84        for _ in range(self._n_evals):
85            out: Dict[str, Any] = {}
86            self._ground_problem._evaluate(X, out, *args, **kwargs)
87            fs.append(out["F"])
88        self._history = {"X": X, "F": np.mean(np.array(fs), axis=0)}
89        self.dump_history()
90        return np.mean(np.linalg.norm(F - self._history["F"], axis=-1))
91
92    def dump_history(self):
93        """Dumps the history into an NPZ archive"""
94        if isinstance(self._history_path, Path):
95            np.savez_compressed(self._history_path, **self._history)

The ΔF performance indicator. Given

calculates the vectorized average Euclidean distance between F and Y, where Y is obtained by averaging g(X) a given number of times.

You can use this PI in a benchmark as

Benchmark(
    ...
    performance_indicators=[..., "df", ...]
)
DeltaF( problem: nmoo.wrapped_problem.WrappedProblem, n_evals: int = 1, history_path: Union[str, pathlib.Path, NoneType] = None, **kwargs)
55    def __init__(
56        self,
57        problem: WrappedProblem,
58        n_evals: int = 1,
59        history_path: Optional[Union[str, Path]] = None,
60        **kwargs,
61    ):
62        super().__init__(zero_to_one=False, **kwargs)
63        self._ground_problem = problem.ground_problem()
64        self._history_path = (
65            Path(history_path)
66            if isinstance(history_path, str)
67            else history_path
68        )
69        self._history = {}
70        if n_evals < 1:
71            raise ValueError("n_evals must be >= 1")
72        self._n_evals = n_evals
def dump_history(self):
92    def dump_history(self):
93        """Dumps the history into an NPZ archive"""
94        if isinstance(self._history_path, Path):
95            np.savez_compressed(self._history_path, **self._history)

Dumps the history into an NPZ archive

Inherited Members
pymoo.core.indicator.Indicator
do