Passed
Pull Request — master (#110)
by
unknown
02:29
created

Sphere.get_test_params()   A

Complexity

Conditions 1

Size

Total Lines 36
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 36
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import numpy as np
2
3
from hyperactive.base import BaseExperiment
4
5
6
class Sphere(BaseExperiment):
7
    """Simple Sphere function, common benchmark for optimization algorithms.
8
9
    Sphere function parameterized by the formula:
10
11
    .. math::
12
        f(x_1, x_2, \ldots, x_n) = \sum_{i=1}^n x_i^2 + c
13
14
    where :math:`c` is a constant offset added to the sum of squares,
15
    and :math:`n` is the number of dimensions.
16
    Both :math:`c` (= `const`) and :math:`n` (= `n_dim`) can be set as parameters.
17
18
    The function arguments :math:`x_1`, :math:`x_2`, ..., :math:`x_n`
19
    are the input variables of the `score` method,
20
    and are set as `x0`, `x1`, ..., `x[n]` respectively.
21
22
    This function is a common test function for optimization algorithms.
23
24
    Parameters
25
    ----------
26
    const : float, optional, default=0
27
        A constant offset added to the sum of squares.
28
    n_dim : int, optional, default=2
29
        The number of dimensions for the Sphere function. The default is 2.
30
31
    Example
32
    -------
33
    >>> from hyperactive.experiment.toy import Sphere
34
    >>> sphere = Sphere(const=0, n_dim=3)
35
    >>> params = {"x0": 1, "x1": 2, "x2": 3}
36
    >>> score, add_info = sphere.score(params)
37
38
    Quick call without metadata return or dictionary:
39
    >>> score = sphere(x0=1, x1=2, x2=3)
40
41
    Different number of dimensions changes the parameter names:
42
    >>> sphere4D = Sphere(const=0, n_dim=4)
43
    >>> score4D = sphere4D(x0=1, x1=2, x2=3, x3=4)
44
    """
45
46
    _tags = {
47
        "property:randomness": "deterministic",  # random or deterministic
48
        # if deterministic, two calls of score will result in the same value
49
        # random = two calls may result in different values; same as "stochastic"
50
    }
51
52
    def __init__(self, const=0, n_dim=2):
53
        self.const = const
54
        self.n_dim = n_dim
55
56
        super().__init__()
57
58
    def _paramnames(self):
59
        return [f"x{i}" for i in range(self.n_dim)]
60
61
    def _score(self, params):
62
        return np.sum(np.array(params) ** 2) + self.const, {}
63
64
    @classmethod
65
    def get_test_params(cls, parameter_set="default"):
66
        """Return testing parameter settings for the skbase object.
67
68
        ``get_test_params`` is a unified interface point to store
69
        parameter settings for testing purposes. This function is also
70
        used in ``create_test_instance`` and ``create_test_instances_and_names``
71
        to construct test instances.
72
73
        ``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``.
74
75
        Each ``dict`` is a parameter configuration for testing,
76
        and can be used to construct an "interesting" test instance.
77
        A call to ``cls(**params)`` should
78
        be valid for all dictionaries ``params`` in the return of ``get_test_params``.
79
80
        The ``get_test_params`` need not return fixed lists of dictionaries,
81
        it can also return dynamic or stochastic parameter settings.
82
83
        Parameters
84
        ----------
85
        parameter_set : str, default="default"
86
            Name of the set of test parameters to return, for use in tests. If no
87
            special parameters are defined for a value, will return `"default"` set.
88
89
        Returns
90
        -------
91
        params : dict or list of dict, default = {}
92
            Parameters to create testing instances of the class
93
            Each dict are parameters to construct an "interesting" test instance, i.e.,
94
            `MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
95
            `create_test_instance` uses the first (or only) dictionary in `params`
96
        """
97
        params0 = {}
98
        params1 = {"ndim": 3, "const": 1.0}
99
        return [params0, params1]
100
101
    @classmethod
102
    def _get_score_params(self):
103
        """Return settings for the score function.
104
105
        Returns a list, the i-th element corresponds to self.get_test_params()[i].
106
        It should be a valid call for self.score.
107
108
        Returns
109
        -------
110
        list of dict
111
            The parameters to be used for scoring.
112
        """
113
        score_params0 = {"x0": 0, "x1": 0}
114
        score_params1 = {"x0": 1, "x1": 2, "x2": 3}
115
        return [score_params0, score_params1]
116