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) = -a \cdot \exp(-\frac{b}{\sqrt{d}\left\|x\right\|}) - \exp(\frac{1}{d} \sum_{i=1}^d\cos (c x_i) ) + a + \exp(1) |
14
|
|
|
|
15
|
|
|
where :math:`a` (= `a`), :math:`b` (= `b`), and :math:`c` (= `c`) are constants, |
16
|
|
|
:math:`d` (= `d`) is the number of dimensions of the real input vector :math:`x`, |
17
|
|
|
and :math:`\left\|x\right\|` is the Euclidean norm of the vector :math:`x`. |
18
|
|
|
|
19
|
|
|
The components of the function argument :math:`x` |
20
|
|
|
are the input variables of the `score` method, |
21
|
|
|
and are set as `x0`, `x1`, ..., `x[d]` respectively. |
22
|
|
|
|
23
|
|
|
Parameters |
24
|
|
|
---------- |
25
|
|
|
a : float, optional, default=20 |
26
|
|
|
Amplitude constant used in the calculation of the Ackley function. |
27
|
|
|
b : float, optional, default=0.2 |
28
|
|
|
Decay constant used in the calculation of the Ackley function. |
29
|
|
|
c : float, optional, default=2*pi |
30
|
|
|
Frequency constant used in the calculation of the Ackley function. |
31
|
|
|
d : int, optional, default=2 |
32
|
|
|
Number of dimensions for the Ackley function. The default is 2. |
33
|
|
|
|
34
|
|
|
Example |
35
|
|
|
------- |
36
|
|
|
>>> from hyperactive.experiment.toy import Ackley |
37
|
|
|
>>> ackley = Ackley(a=20) |
38
|
|
|
>>> params = {"x0": 1, "x1": 2} |
39
|
|
|
>>> score, add_info = ackley.score(params) |
40
|
|
|
|
41
|
|
|
Quick call without metadata return or dictionary: |
42
|
|
|
>>> score = ackley(x0=1, x1=2) |
43
|
|
|
""" # noqa: E501 |
44
|
|
|
|
45
|
|
|
_tags = { |
46
|
|
|
"property:randomness": "deterministic", # random or deterministic |
47
|
|
|
# if deterministic, two calls of score will result in the same value |
48
|
|
|
# random = two calls may result in different values; same as "stochastic" |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
def __init__(self, a=20, b=0.2, c=2 * np.pi, d=2): |
52
|
|
|
self.a = a |
53
|
|
|
self.b = b |
54
|
|
|
self.c = c |
55
|
|
|
self.d = d |
56
|
|
|
super().__init__() |
57
|
|
|
|
58
|
|
|
def _paramnames(self): |
59
|
|
|
return [f"x{i}" for i in range(self.d)] |
60
|
|
|
|
61
|
|
|
def _score(self, params): |
62
|
|
|
x_vec = np.array([params[f"x{i}"] for i in range(self.d)]) |
63
|
|
|
|
64
|
|
|
loss1 = -self.a * np.exp(-self.b * np.sqrt(np.sum(x_vec**2) / self.d)) |
65
|
|
|
loss2 = -np.exp(np.sum(np.cos(self.c * x_vec)) / self.d) |
66
|
|
|
loss3 = np.exp(1) |
67
|
|
|
loss4 = self.a |
68
|
|
|
|
69
|
|
|
loss = loss1 + loss2 + loss3 + loss4 |
70
|
|
|
|
71
|
|
|
return loss, {} |
72
|
|
|
|
73
|
|
|
@classmethod |
74
|
|
|
def get_test_params(cls, parameter_set="default"): |
75
|
|
|
"""Return testing parameter settings for the skbase object. |
76
|
|
|
|
77
|
|
|
``get_test_params`` is a unified interface point to store |
78
|
|
|
parameter settings for testing purposes. This function is also |
79
|
|
|
used in ``create_test_instance`` and ``create_test_instances_and_names`` |
80
|
|
|
to construct test instances. |
81
|
|
|
|
82
|
|
|
``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``. |
83
|
|
|
|
84
|
|
|
Each ``dict`` is a parameter configuration for testing, |
85
|
|
|
and can be used to construct an "interesting" test instance. |
86
|
|
|
A call to ``cls(**params)`` should |
87
|
|
|
be valid for all dictionaries ``params`` in the return of ``get_test_params``. |
88
|
|
|
|
89
|
|
|
The ``get_test_params`` need not return fixed lists of dictionaries, |
90
|
|
|
it can also return dynamic or stochastic parameter settings. |
91
|
|
|
|
92
|
|
|
Parameters |
93
|
|
|
---------- |
94
|
|
|
parameter_set : str, default="default" |
95
|
|
|
Name of the set of test parameters to return, for use in tests. If no |
96
|
|
|
special parameters are defined for a value, will return `"default"` set. |
97
|
|
|
|
98
|
|
|
Returns |
99
|
|
|
------- |
100
|
|
|
params : dict or list of dict, default = {} |
101
|
|
|
Parameters to create testing instances of the class |
102
|
|
|
Each dict are parameters to construct an "interesting" test instance, i.e., |
103
|
|
|
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. |
104
|
|
|
`create_test_instance` uses the first (or only) dictionary in `params` |
105
|
|
|
""" |
106
|
|
|
return [{"a": 0}, {"a": 20, "d": 42}, {"a": -42, "b": 0.5, "c": 1, "d": 10}] |
107
|
|
|
|
108
|
|
|
@classmethod |
109
|
|
|
def _get_score_params(self): |
110
|
|
|
"""Return settings for the score function. |
111
|
|
|
|
112
|
|
|
Returns a list, the i-th element corresponds to self.get_test_params()[i]. |
113
|
|
|
It should be a valid call for self.score. |
114
|
|
|
|
115
|
|
|
Returns |
116
|
|
|
------- |
117
|
|
|
list of dict |
118
|
|
|
The parameters to be used for scoring. |
119
|
|
|
""" |
120
|
|
|
params0 = {"x0": 0, "x1": 0} |
121
|
|
|
params1 = {f"x{i}": i + 3 for i in range(42)} |
122
|
|
|
params2 = {f"x{i}": i**2 for i in range(10)} |
123
|
|
|
return [params0, params1, params2] |
124
|
|
|
|