Passed
Pull Request — master (#110)
by
unknown
02:46 queued 01:17
created

AckleyFunction._score()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 10
loc 10
rs 10
c 0
b 0
f 0
1
import numpy as np
2
3
from hyperactive.base import BaseExperiment
4
5
6
class AckleyFunction(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
    Parameters
18
    ----------
19
    A : float
20
        Amplitude constant used in the calculation of the Ackley function.
21
22
    Example
23
    -------
24
    >>> from hyperactive.experiment.toy import AckleyFunction
25
    >>> ackley = AckleyFunction(A=20)
26
    >>> params = {"x0": 1, "x1": 2}
27
    >>> score, add_info = ackley.score(params)
28
29
    Quick call without metadata return or dictionary:
30
    >>> score = ackley(x0=1, x1=2)
31
    """  # noqa: E501
32
33
    _tags = {
34
        "property:randomness": "deterministic",  # random or deterministic
35
        # if deterministic, two calls of score will result in the same value
36
        # random = two calls may result in different values; same as "stochastic"
37
    }
38
39
    def __init__(self, A):
40
        self.A = A
41
        super().__init__()
42
43
    def _paramnames(self):
44
        return ["x0", "x1"]
45
46 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...
47
        x = params["x0"]
48
        y = params["x1"]
49
50
        loss1 = -self.A * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y)))
51
        loss2 = -np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
52
        loss3 = np.exp(1)
53
        loss4 = self.A
54
55
        return -(loss1 + loss2 + loss3 + loss4), {}
56
57
    @classmethod
58
    def get_test_params(cls, parameter_set="default"):
59
        """Return testing parameter settings for the skbase object.
60
61
        ``get_test_params`` is a unified interface point to store
62
        parameter settings for testing purposes. This function is also
63
        used in ``create_test_instance`` and ``create_test_instances_and_names``
64
        to construct test instances.
65
66
        ``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``.
67
68
        Each ``dict`` is a parameter configuration for testing,
69
        and can be used to construct an "interesting" test instance.
70
        A call to ``cls(**params)`` should
71
        be valid for all dictionaries ``params`` in the return of ``get_test_params``.
72
73
        The ``get_test_params`` need not return fixed lists of dictionaries,
74
        it can also return dynamic or stochastic parameter settings.
75
76
        Parameters
77
        ----------
78
        parameter_set : str, default="default"
79
            Name of the set of test parameters to return, for use in tests. If no
80
            special parameters are defined for a value, will return `"default"` set.
81
82
        Returns
83
        -------
84
        params : dict or list of dict, default = {}
85
            Parameters to create testing instances of the class
86
            Each dict are parameters to construct an "interesting" test instance, i.e.,
87
            `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
88
            `create_test_instance` uses the first (or only) dictionary in `params`
89
        """
90
        return [{"A": 0}, {"A": 20}, {"A": -42}]
91
92
    @classmethod
93
    def _get_score_params(self):
94
        """Return settings for the score function.
95
96
        Returns a list, the i-th element corresponds to self.get_test_params()[i].
97
        It should be a valid call for self.score.
98
99
        Returns
100
        -------
101
        list of dict
102
            The parameters to be used for scoring.
103
        """
104
        params0 = {"x0": 0, "x1": 0}
105
        params1 = {"x0": 1, "x1": 1}
106
        return [params0, params1]
107