Passed
Push — master ( 4a40b1...18f8a0 )
by Simon
01:39
created

Parabola._evaluate()   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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