Passed
Pull Request — master (#110)
by
unknown
11:00 queued 09:30
created

hyperactive.experiment.toy._parabola   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 22
dl 0
loc 73
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
    Parameters
18
    ----------
19
    a : float, default=1.0
20
        Coefficient of the parabola.
21
    b : float, default=0.0
22
        Coefficient of the parabola.
23
    c : float, default=0.0
24
        Coefficient of the parabola.
25
26
    Example
27
    -------
28
    >>> from hyperactive.experiment.toy import Parabola
29
    >>> parabola = Parabola(a=1.0, b=0.0, c=0.0)
30
    >>> params = {"x": 1, "y": 2}
31
    >>> score, add_info = parabola.score(params)
32
33
    Quick call without metadata return or dictionary:
34
    >>> score = parabola(x=1, y=2)
35
    """
36
37
    _tags = {
38
        "property:randomness": "deterministic",  # random or deterministic
39
        # if deterministic, two calls of score will result in the same value
40
        # random = two calls may result in different values; same as "stochastic"
41
    }
42
43
    def __init__(self, a=1.0, b=0.0, c=0.0):
44
        self.a = a
45
        self.b = b
46
        self.c = c
47
        super().__init__()
48
49
    def _paramnames(self):
50
        return ["x", "y"]
51
52
    def _score(self, params):
53
        x = params["x"]
54
        y = params["y"]
55
56
        return self.a * (x**2 + y**2) + self.b * x + self.c * y, {}
57
58
    @classmethod
59
    def _get_score_params(self):
60
        """Return settings for the score function.
61
62
        Returns a list, the i-th element corresponds to self.get_test_params()[i].
63
        It should be a valid call for self.score.
64
65
        Returns
66
        -------
67
        list of dict
68
            The parameters to be used for scoring.
69
        """
70
        params0 = {"x": 0, "y": 0}
71
        params1 = {"x": 1, "y": 1}
72
        return [params0, params1]
73