nmoo.evaluators.penalized_evaluator

A custom evaluator that modifies the perceived number of evaluations (n_eval).

 1"""
 2A custom evaluator that modifies the perceived number of evaluations
 3(`n_eval`).
 4"""
 5__docformat__ = "google"
 6
 7import numpy as np
 8from pymoo.core.evaluator import Evaluator
 9
10
11class PenalizedEvaluator(Evaluator):
12    """
13    A custom evaluator that multiplies the perceived number of evaluations
14    (`n_eval`).
15
16    For example, an pymoo algorithm using the following evaluator
17
18        PenalizedEvaluator(5)
19
20    will perceive every evaluation of the underlying problem as `5`
21    evaluations. In other words, if the algorithm's maximum evaluation count is
22    `N`, then the problem's `_evaluate` method will be called on at most `N /
23    5` values.
24    """
25
26    _multiplier: int
27
28    def __init__(self, multiplier: int = 5):
29        """
30        Args:
31            multiplier (int): multiplier pertaining to the penalty being
32                applied.
33        """
34        super().__init__()
35        self._multiplier = multiplier
36
37    def eval(self, *args, **kwargs):  # pylint: disable=signature-differs
38        """
39        Calls `Evaluator.eval` and modifies `n_eval` according to the penalty
40        parameters.
41        """
42        result = super().eval(*args, **kwargs)
43        if isinstance(result, np.ndarray):
44            self.n_eval += (self._multiplier - 1) * result.shape[0]
45        else:
46            self.n_eval += self._multiplier - 1
47        return result
class PenalizedEvaluator(pymoo.core.evaluator.Evaluator):
12class PenalizedEvaluator(Evaluator):
13    """
14    A custom evaluator that multiplies the perceived number of evaluations
15    (`n_eval`).
16
17    For example, an pymoo algorithm using the following evaluator
18
19        PenalizedEvaluator(5)
20
21    will perceive every evaluation of the underlying problem as `5`
22    evaluations. In other words, if the algorithm's maximum evaluation count is
23    `N`, then the problem's `_evaluate` method will be called on at most `N /
24    5` values.
25    """
26
27    _multiplier: int
28
29    def __init__(self, multiplier: int = 5):
30        """
31        Args:
32            multiplier (int): multiplier pertaining to the penalty being
33                applied.
34        """
35        super().__init__()
36        self._multiplier = multiplier
37
38    def eval(self, *args, **kwargs):  # pylint: disable=signature-differs
39        """
40        Calls `Evaluator.eval` and modifies `n_eval` according to the penalty
41        parameters.
42        """
43        result = super().eval(*args, **kwargs)
44        if isinstance(result, np.ndarray):
45            self.n_eval += (self._multiplier - 1) * result.shape[0]
46        else:
47            self.n_eval += self._multiplier - 1
48        return result

A custom evaluator that multiplies the perceived number of evaluations (n_eval).

For example, an pymoo algorithm using the following evaluator

PenalizedEvaluator(5)

will perceive every evaluation of the underlying problem as 5 evaluations. In other words, if the algorithm's maximum evaluation count is N, then the problem's _evaluate method will be called on at most N / 5 values.

PenalizedEvaluator(multiplier: int = 5)
29    def __init__(self, multiplier: int = 5):
30        """
31        Args:
32            multiplier (int): multiplier pertaining to the penalty being
33                applied.
34        """
35        super().__init__()
36        self._multiplier = multiplier
Arguments:
  • multiplier (int): multiplier pertaining to the penalty being applied.
def eval(self, *args, **kwargs):
38    def eval(self, *args, **kwargs):  # pylint: disable=signature-differs
39        """
40        Calls `Evaluator.eval` and modifies `n_eval` according to the penalty
41        parameters.
42        """
43        result = super().eval(*args, **kwargs)
44        if isinstance(result, np.ndarray):
45            self.n_eval += (self._multiplier - 1) * result.shape[0]
46        else:
47            self.n_eval += self._multiplier - 1
48        return result

Calls Evaluator.eval and modifies n_eval according to the penalty parameters.