Passed
Pull Request — master (#110)
by
unknown
01:45
created

hyperactive.experiment.toy._parabola   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Parabola.__init__() 0 5 1
A Parabola._paramnames() 0 2 1
A Parabola._get_score_params() 0 15 1
A Parabola._score() 0 5 1
1
"""2D parabola."""
2
3
from hyperactive.base import BaseExperiment
4
5
6
class Parabola(BaseExperiment):
7
    """2D parabola, common benchmark for optimization algorithms.
8
9
    Parabola parameterized by the formula:
10
11
    .. math::
12
        f(x, y) = a * (x^2 + y^2) + b * x + c * y
13
14
    where :math:`a`, :math:`b`, and :math:`c` are coefficients which can
15
    be set as parameters.
16
17
    The function arguments :math:`x` and :math:`y`
18
    are the input variables of the `score` method,
19
    and are set as `x` and `y` respectively.
20
21
    Parameters
22
    ----------
23
    a : float, default=1.0
24
        Coefficient of the parabola.
25
    b : float, default=0.0
26
        Coefficient of the parabola.
27
    c : float, default=0.0
28
        Coefficient of the parabola.
29
30
    Example
31
    -------
32
    >>> from hyperactive.experiment.toy import Parabola
33
    >>> parabola = Parabola(a=1.0, b=0.0, c=0.0)
34
    >>> params = {"x": 1, "y": 2}
35
    >>> score, add_info = parabola.score(params)
36
37
    Quick call without metadata return or dictionary:
38
    >>> score = parabola(x=1, y=2)
39
    """
40
41
    _tags = {
42
        "property:randomness": "deterministic",  # random or deterministic
43
        # if deterministic, two calls of score will result in the same value
44
        # random = two calls may result in different values; same as "stochastic"
45
    }
46
47
    def __init__(self, a=1.0, b=0.0, c=0.0):
48
        self.a = a
49
        self.b = b
50
        self.c = c
51
        super().__init__()
52
53
    def _paramnames(self):
54
        return ["x", "y"]
55
56
    def _score(self, params):
57
        x = params["x"]
58
        y = params["y"]
59
60
        return self.a * (x**2 + y**2) + self.b * x + self.c * y, {}
61
62
    @classmethod
63
    def _get_score_params(self):
64
        """Return settings for the score function.
65
66
        Returns a list, the i-th element corresponds to self.get_test_params()[i].
67
        It should be a valid call for self.score.
68
69
        Returns
70
        -------
71
        list of dict
72
            The parameters to be used for scoring.
73
        """
74
        params0 = {"x": 0, "y": 0}
75
        params1 = {"x": 1, "y": 1}
76
        return [params0, params1]
77