Parabola._get_score_params()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
"""2D parabola function, common benchmark for optimization algorithms."""
2
# copyright: hyperactive developers, MIT License (see LICENSE file)
3
4
from hyperactive.base import BaseExperiment
5
6
7
class Parabola(BaseExperiment):
8
    """Parabola class."""
9
10
    r"""2D parabola, common benchmark for optimization algorithms.
11
12
    Parabola parameterized by the formula:
13
14
    .. math::
15
        f(x, y) = a * (x^2 + y^2) + b * x + c * y
16
17
    where :math:`a`, :math:`b`, and :math:`c` are coefficients which can
18
    be set as parameters.
19
20
    The function arguments :math:`x` and :math:`y`
21
    are the input variables of the `score` method,
22
    and are set as `x` and `y` respectively.
23
24
    Parameters
25
    ----------
26
    a : float, default=1.0
27
        Coefficient of the parabola.
28
    b : float, default=0.0
29
        Coefficient of the parabola.
30
    c : float, default=0.0
31
        Coefficient of the parabola.
32
33
    Example
34
    -------
35
    >>> from hyperactive.experiment.bench import Parabola
36
    >>> parabola = Parabola(a=1.0, b=0.0, c=0.0)
37
    >>> params = {"x": 1, "y": 2}
38
    >>> score, add_info = parabola.score(params)
39
40
    Quick call without metadata return or dictionary:
41
    >>> score = parabola(x=1, y=2)
42
    """
43
44
    _tags = {
45
        "property:randomness": "deterministic",  # random or deterministic
46
        # if deterministic, two calls of score will result in the same value
47
        # random = two calls may result in different values; same as "stochastic"
48
        "property:higher_or_lower_is_better": "lower",
49
        # values are "higher", "lower", "mixed"
50
        # whether higher or lower scores are better
51
    }
52
53
    def __init__(self, a=1.0, b=0.0, c=0.0):
54
        self.a = a
55
        self.b = b
56
        self.c = c
57
        super().__init__()
58
59
    def _paramnames(self):
60
        return ["x", "y"]
61
62
    def _evaluate(self, params):
63
        """Evaluate the parameters.
64
65
        Parameters
66
        ----------
67
        params : dict with string keys
68
            Parameters to evaluate.
69
70
        Returns
71
        -------
72
        float
73
            The value of the parameters as per evaluation.
74
        dict
75
            Additional metadata about the search.
76
        """
77
        x = params["x"]
78
        y = params["y"]
79
80
        return self.a * (x**2 + y**2) + self.b * x + self.c * y, {}
81
82
    @classmethod
83
    def _get_score_params(self):
84
        """Return settings for testing score/evaluate functions. Used in tests only.
85
86
        Returns a list, the i-th element should be valid arguments for
87
        self.evaluate and self.score, of an instance constructed with
88
        self.get_test_params()[i].
89
90
        Returns
91
        -------
92
        list of dict
93
            The parameters to be used for scoring.
94
        """
95
        params0 = {"x": 0, "y": 0}
96
        params1 = {"x": 1, "y": 1}
97
        return [params0, params1]
98