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
|
|
|
params_vec = np.array([params[f"x{i}"] for i in range(self.n_dim)]) |
63
|
|
|
return np.sum(params_vec ** 2) + self.const, {} |
64
|
|
|
|
65
|
|
|
@classmethod |
66
|
|
|
def get_test_params(cls, parameter_set="default"): |
67
|
|
|
"""Return testing parameter settings for the skbase object. |
68
|
|
|
|
69
|
|
|
``get_test_params`` is a unified interface point to store |
70
|
|
|
parameter settings for testing purposes. This function is also |
71
|
|
|
used in ``create_test_instance`` and ``create_test_instances_and_names`` |
72
|
|
|
to construct test instances. |
73
|
|
|
|
74
|
|
|
``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``. |
75
|
|
|
|
76
|
|
|
Each ``dict`` is a parameter configuration for testing, |
77
|
|
|
and can be used to construct an "interesting" test instance. |
78
|
|
|
A call to ``cls(**params)`` should |
79
|
|
|
be valid for all dictionaries ``params`` in the return of ``get_test_params``. |
80
|
|
|
|
81
|
|
|
The ``get_test_params`` need not return fixed lists of dictionaries, |
82
|
|
|
it can also return dynamic or stochastic parameter settings. |
83
|
|
|
|
84
|
|
|
Parameters |
85
|
|
|
---------- |
86
|
|
|
parameter_set : str, default="default" |
87
|
|
|
Name of the set of test parameters to return, for use in tests. If no |
88
|
|
|
special parameters are defined for a value, will return `"default"` set. |
89
|
|
|
|
90
|
|
|
Returns |
91
|
|
|
------- |
92
|
|
|
params : dict or list of dict, default = {} |
93
|
|
|
Parameters to create testing instances of the class |
94
|
|
|
Each dict are parameters to construct an "interesting" test instance, i.e., |
95
|
|
|
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. |
96
|
|
|
`create_test_instance` uses the first (or only) dictionary in `params` |
97
|
|
|
""" |
98
|
|
|
params0 = {} |
99
|
|
|
params1 = {"n_dim": 3, "const": 1.0} |
100
|
|
|
return [params0, params1] |
101
|
|
|
|
102
|
|
|
@classmethod |
103
|
|
|
def _get_score_params(self): |
104
|
|
|
"""Return settings for the score function. |
105
|
|
|
|
106
|
|
|
Returns a list, the i-th element corresponds to self.get_test_params()[i]. |
107
|
|
|
It should be a valid call for self.score. |
108
|
|
|
|
109
|
|
|
Returns |
110
|
|
|
------- |
111
|
|
|
list of dict |
112
|
|
|
The parameters to be used for scoring. |
113
|
|
|
""" |
114
|
|
|
score_params0 = {"x0": 0, "x1": 0} |
115
|
|
|
score_params1 = {"x0": 1, "x1": 2, "x2": 3} |
116
|
|
|
return [score_params0, score_params1] |
117
|
|
|
|