1
|
|
|
"""2D parabola function, common benchmark for optimization algorithms.""" |
2
|
|
|
# copyright: hyperactive developers, MIT License (see LICENSE file) |
3
|
|
|
|
4
|
|
|
from hyperactive.base import BaseExperiment |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class Parabola(BaseExperiment): |
8
|
|
|
r"""2D parabola, common benchmark for optimization algorithms. |
9
|
|
|
|
10
|
|
|
Parabola parameterized by the formula: |
11
|
|
|
|
12
|
|
|
.. math:: |
13
|
|
|
f(x, y) = a * (x^2 + y^2) + b * x + c * y |
14
|
|
|
|
15
|
|
|
where :math:`a`, :math:`b`, and :math:`c` are coefficients which can |
16
|
|
|
be set as parameters. |
17
|
|
|
|
18
|
|
|
The function arguments :math:`x` and :math:`y` |
19
|
|
|
are the input variables of the `score` method, |
20
|
|
|
and are set as `x` and `y` respectively. |
21
|
|
|
|
22
|
|
|
Parameters |
23
|
|
|
---------- |
24
|
|
|
a : float, default=1.0 |
25
|
|
|
Coefficient of the parabola. |
26
|
|
|
b : float, default=0.0 |
27
|
|
|
Coefficient of the parabola. |
28
|
|
|
c : float, default=0.0 |
29
|
|
|
Coefficient of the parabola. |
30
|
|
|
|
31
|
|
|
Example |
32
|
|
|
------- |
33
|
|
|
>>> from hyperactive.experiment.toy import Parabola |
34
|
|
|
>>> parabola = Parabola(a=1.0, b=0.0, c=0.0) |
35
|
|
|
>>> params = {"x": 1, "y": 2} |
36
|
|
|
>>> score, add_info = parabola.score(params) |
37
|
|
|
|
38
|
|
|
Quick call without metadata return or dictionary: |
39
|
|
|
>>> score = parabola(x=1, y=2) |
40
|
|
|
""" |
41
|
|
|
|
42
|
|
|
_tags = { |
43
|
|
|
"property:randomness": "deterministic", # random or deterministic |
44
|
|
|
# if deterministic, two calls of score will result in the same value |
45
|
|
|
# random = two calls may result in different values; same as "stochastic" |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
def __init__(self, a=1.0, b=0.0, c=0.0): |
49
|
|
|
self.a = a |
50
|
|
|
self.b = b |
51
|
|
|
self.c = c |
52
|
|
|
super().__init__() |
53
|
|
|
|
54
|
|
|
def _paramnames(self): |
55
|
|
|
return ["x", "y"] |
56
|
|
|
|
57
|
|
|
def _score(self, params): |
58
|
|
|
x = params["x"] |
59
|
|
|
y = params["y"] |
60
|
|
|
|
61
|
|
|
return self.a * (x**2 + y**2) + self.b * x + self.c * y, {} |
62
|
|
|
|
63
|
|
|
@classmethod |
64
|
|
|
def _get_score_params(self): |
65
|
|
|
"""Return settings for testing the score function. Used in tests only. |
66
|
|
|
|
67
|
|
|
Returns a list, the i-th element corresponds to self.get_test_params()[i]. |
68
|
|
|
It should be a valid call for self.score. |
69
|
|
|
|
70
|
|
|
Returns |
71
|
|
|
------- |
72
|
|
|
list of dict |
73
|
|
|
The parameters to be used for scoring. |
74
|
|
|
""" |
75
|
|
|
params0 = {"x": 0, "y": 0} |
76
|
|
|
params1 = {"x": 1, "y": 1} |
77
|
|
|
return [params0, params1] |
78
|
|
|
|