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

Ackley._paramnames()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import numpy as np
2
3
from hyperactive.base import BaseExperiment
4
5
6
class Ackley(BaseExperiment):
7
    r"""Ackley function, common benchmark for optimization algorithms.
8
9
    The Ackley function is a non-convex function used to test optimization algorithms.
10
    It is defined as:
11
12
    .. math::
13
        f(x, y) = -A \cdot \exp(-0.2 \sqrt{0.5 (x^2 + y^2)}) - \exp(0.5 (\cos(2 \pi x) + \cos(2 \pi y))) + \exp(1) + A
14
15
    where A is a constant.
16
17
    The function arguments :math:`x` and :math:`y`
18
    are the input variables of the `score` method,
19
    and are set as `x0` and `x1` respectively.
20
21
    Parameters
22
    ----------
23
    A : float
24
        Amplitude constant used in the calculation of the Ackley function.
25
26
    Example
27
    -------
28
    >>> from hyperactive.experiment.toy import Ackley
29
    >>> ackley = Ackley(A=20)
30
    >>> params = {"x0": 1, "x1": 2}
31
    >>> score, add_info = ackley.score(params)
32
33
    Quick call without metadata return or dictionary:
34
    >>> score = ackley(x0=1, x1=2)
35
    """  # noqa: E501
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):
44
        self.A = A
45
        super().__init__()
46
47
    def _paramnames(self):
48
        return ["x0", "x1"]
49
50 View Code Duplication
    def _score(self, params):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
        x = params["x0"]
52
        y = params["x1"]
53
54
        loss1 = -self.A * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y)))
55
        loss2 = -np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
56
        loss3 = np.exp(1)
57
        loss4 = self.A
58
59
        return -(loss1 + loss2 + loss3 + loss4), {}
60
61
    @classmethod
62
    def get_test_params(cls, parameter_set="default"):
63
        """Return testing parameter settings for the skbase object.
64
65
        ``get_test_params`` is a unified interface point to store
66
        parameter settings for testing purposes. This function is also
67
        used in ``create_test_instance`` and ``create_test_instances_and_names``
68
        to construct test instances.
69
70
        ``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``.
71
72
        Each ``dict`` is a parameter configuration for testing,
73
        and can be used to construct an "interesting" test instance.
74
        A call to ``cls(**params)`` should
75
        be valid for all dictionaries ``params`` in the return of ``get_test_params``.
76
77
        The ``get_test_params`` need not return fixed lists of dictionaries,
78
        it can also return dynamic or stochastic parameter settings.
79
80
        Parameters
81
        ----------
82
        parameter_set : str, default="default"
83
            Name of the set of test parameters to return, for use in tests. If no
84
            special parameters are defined for a value, will return `"default"` set.
85
86
        Returns
87
        -------
88
        params : dict or list of dict, default = {}
89
            Parameters to create testing instances of the class
90
            Each dict are parameters to construct an "interesting" test instance, i.e.,
91
            `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
92
            `create_test_instance` uses the first (or only) dictionary in `params`
93
        """
94
        return [{"A": 0}, {"A": 20}, {"A": -42}]
95
96
    @classmethod
97
    def _get_score_params(self):
98
        """Return settings for the score function.
99
100
        Returns a list, the i-th element corresponds to self.get_test_params()[i].
101
        It should be a valid call for self.score.
102
103
        Returns
104
        -------
105
        list of dict
106
            The parameters to be used for scoring.
107
        """
108
        params0 = {"x0": 0, "x1": 0}
109
        params1 = {"x0": 1, "x1": 1}
110
        return [params0, params1]
111