nmoo.lambda

A wrapper that applies a given function to the wrapped problem's output.

 1"""
 2A wrapper that applies a given function to the wrapped problem's output.
 3"""
 4__docformat__ = "google"
 5
 6from typing import Callable, Dict
 7
 8import numpy as np
 9from loguru import logger as logging
10from pymoo.core.problem import Problem
11
12from .wrapped_problem import WrappedProblem
13
14
15class Lambda(WrappedProblem):
16    """
17    A wrapper that applies a given function to the wrapped problem's output.
18    """
19
20    _functions: Dict[str, Callable[[np.ndarray], np.ndarray]]
21    """Functions to apply."""
22
23    def __init__(
24        self,
25        problem: Problem,
26        functions: Dict[str, Callable[[np.ndarray], np.ndarray]],
27        *,
28        name: str = "lambda",
29        **kwargs,
30    ):
31        """
32        Constructor.
33
34        Args:
35            name (str): An optional name for this problem. This will be used
36                when creating history dump files. Defaults to `lambda`.
37            problem (pymoo `Problem`): A non-noisy pymoo problem (or
38                `nmoo.wrapped_problem.WrappedProblem`).
39            parameters (dict): Functions to apply, in the form of a dict
40                mapping the name of an objective to a callable object. The set
41                of keys should be a subset of the final `out` dictionary keys
42                in the wrapped problem's `_evaluate` method.
43
44        Example:
45            Assume that the output of the wrapped problem has an `F`
46            numerical objective. The following multiplies its value by 2:
47
48                problem2 = nmoo.Lambda(
49                    problem,
50                    {
51                        "F": lambda x: 2. * x,
52                    },
53                )
54        """
55        super().__init__(problem, name=name, **kwargs)
56        self._functions = functions
57
58    def _evaluate(self, x, out, *args, **kwargs):
59        self._problem._evaluate(x, out, *args, **kwargs)
60        for k, function in self._functions.items():
61            try:
62                out[k] = function(out[k])
63            except KeyError:
64                logging.error(
65                    "Callable key {} is not present in objective "
66                    "function output keys. This objective will not be "
67                    "modified. Objective function keys: {}. ",
68                    k,
69                    str(list(out.keys())),
70                )
71        self.add_to_history_x_out(x, out)
class Lambda(nmoo.wrapped_problem.WrappedProblem):
16class Lambda(WrappedProblem):
17    """
18    A wrapper that applies a given function to the wrapped problem's output.
19    """
20
21    _functions: Dict[str, Callable[[np.ndarray], np.ndarray]]
22    """Functions to apply."""
23
24    def __init__(
25        self,
26        problem: Problem,
27        functions: Dict[str, Callable[[np.ndarray], np.ndarray]],
28        *,
29        name: str = "lambda",
30        **kwargs,
31    ):
32        """
33        Constructor.
34
35        Args:
36            name (str): An optional name for this problem. This will be used
37                when creating history dump files. Defaults to `lambda`.
38            problem (pymoo `Problem`): A non-noisy pymoo problem (or
39                `nmoo.wrapped_problem.WrappedProblem`).
40            parameters (dict): Functions to apply, in the form of a dict
41                mapping the name of an objective to a callable object. The set
42                of keys should be a subset of the final `out` dictionary keys
43                in the wrapped problem's `_evaluate` method.
44
45        Example:
46            Assume that the output of the wrapped problem has an `F`
47            numerical objective. The following multiplies its value by 2:
48
49                problem2 = nmoo.Lambda(
50                    problem,
51                    {
52                        "F": lambda x: 2. * x,
53                    },
54                )
55        """
56        super().__init__(problem, name=name, **kwargs)
57        self._functions = functions
58
59    def _evaluate(self, x, out, *args, **kwargs):
60        self._problem._evaluate(x, out, *args, **kwargs)
61        for k, function in self._functions.items():
62            try:
63                out[k] = function(out[k])
64            except KeyError:
65                logging.error(
66                    "Callable key {} is not present in objective "
67                    "function output keys. This objective will not be "
68                    "modified. Objective function keys: {}. ",
69                    k,
70                    str(list(out.keys())),
71                )
72        self.add_to_history_x_out(x, out)

A wrapper that applies a given function to the wrapped problem's output.

Lambda( problem: pymoo.core.problem.Problem, functions: Dict[str, Callable[[numpy.ndarray], numpy.ndarray]], *, name: str = 'lambda', **kwargs)
24    def __init__(
25        self,
26        problem: Problem,
27        functions: Dict[str, Callable[[np.ndarray], np.ndarray]],
28        *,
29        name: str = "lambda",
30        **kwargs,
31    ):
32        """
33        Constructor.
34
35        Args:
36            name (str): An optional name for this problem. This will be used
37                when creating history dump files. Defaults to `lambda`.
38            problem (pymoo `Problem`): A non-noisy pymoo problem (or
39                `nmoo.wrapped_problem.WrappedProblem`).
40            parameters (dict): Functions to apply, in the form of a dict
41                mapping the name of an objective to a callable object. The set
42                of keys should be a subset of the final `out` dictionary keys
43                in the wrapped problem's `_evaluate` method.
44
45        Example:
46            Assume that the output of the wrapped problem has an `F`
47            numerical objective. The following multiplies its value by 2:
48
49                problem2 = nmoo.Lambda(
50                    problem,
51                    {
52                        "F": lambda x: 2. * x,
53                    },
54                )
55        """
56        super().__init__(problem, name=name, **kwargs)
57        self._functions = functions

Constructor.

Arguments:
  • name (str): An optional name for this problem. This will be used when creating history dump files. Defaults to lambda.
  • problem (pymoo Problem): A non-noisy pymoo problem (or nmoo.wrapped_problem.WrappedProblem).
  • parameters (dict): Functions to apply, in the form of a dict mapping the name of an objective to a callable object. The set of keys should be a subset of the final out dictionary keys in the wrapped problem's _evaluate method.
Example:

Assume that the output of the wrapped problem has an F numerical objective. The following multiplies its value by 2:

problem2 = nmoo.Lambda(
    problem,
    {
        "F": lambda x: 2. * x,
    },
)
Inherited Members
nmoo.wrapped_problem.WrappedProblem
add_to_history
add_to_history_x_out
all_layers
depth
dump_all_histories
dump_history
ground_problem
innermost_wrapper
reseed
start_new_run
pymoo.core.problem.Problem
evaluate
do
nadir_point
ideal_point
pareto_front
pareto_set
has_bounds
has_constraints
bounds
name
calc_constraint_violation