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