-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy path_sphere.py
More file actions
137 lines (107 loc) · 4.84 KB
/
_sphere.py
File metadata and controls
137 lines (107 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Sphere function, a common benchmark for optimization algorithms."""
# copyright: hyperactive developers, MIT License (see LICENSE file)
import numpy as np
from hyperactive.base import BaseExperiment
class Sphere(BaseExperiment):
r"""Simple Sphere function, common benchmark for optimization algorithms.
Sphere function parameterized by the formula:
.. math::
f(x_1, x_2, \ldots, x_n) = \sum_{i=1}^n x_i^2 + c
where :math:`c` is a constant offset added to the sum of squares,
and :math:`n` is the number of dimensions.
Both :math:`c` (= `const`) and :math:`n` (= `n_dim`) can be set as parameters.
The function arguments :math:`x_1`, :math:`x_2`, ..., :math:`x_n`
are the input variables of the `score` method,
and are set as `x0`, `x1`, ..., `x[n]` respectively.
This function is a common test function for optimization algorithms.
Parameters
----------
const : float, optional, default=0
A constant offset added to the sum of squares.
n_dim : int, optional, default=2
The number of dimensions for the Sphere function. The default is 2.
Example
-------
>>> from hyperactive.experiment.toy import Sphere
>>> sphere = Sphere(const=0, n_dim=3)
>>> params = {"x0": 1, "x1": 2, "x2": 3}
>>> score, add_info = sphere.score(params)
Quick call without metadata return or dictionary:
>>> score = sphere(x0=1, x1=2, x2=3)
Different number of dimensions changes the parameter names:
>>> sphere4D = Sphere(const=0, n_dim=4)
>>> score4D = sphere4D(x0=1, x1=2, x2=3, x3=4)
"""
_tags = {
"property:randomness": "deterministic", # random or deterministic
# if deterministic, two calls of score will result in the same value
# random = two calls may result in different values; same as "stochastic"
"property:higher_or_lower_is_better": "lower",
# values are "higher", "lower", "mixed"
# whether higher or lower scores are better
}
def __init__(self, const=0, n_dim=2):
self.const = const
self.n_dim = n_dim
super().__init__()
def _paramnames(self):
return [f"x{i}" for i in range(self.n_dim)]
def _evaluate(self, params):
"""Evaluate the parameters.
Parameters
----------
params : dict with string keys
Parameters to evaluate.
Returns
-------
float
The value of the parameters as per evaluation.
dict
Additional metadata about the search.
"""
params_vec = np.array([params[f"x{i}"] for i in range(self.n_dim)])
return np.sum(params_vec ** 2) + self.const, {}
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the skbase object.
``get_test_params`` is a unified interface point to store
parameter settings for testing purposes. This function is also
used in ``create_test_instance`` and ``create_test_instances_and_names``
to construct test instances.
``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``.
Each ``dict`` is a parameter configuration for testing,
and can be used to construct an "interesting" test instance.
A call to ``cls(**params)`` should
be valid for all dictionaries ``params`` in the return of ``get_test_params``.
The ``get_test_params`` need not return fixed lists of dictionaries,
it can also return dynamic or stochastic parameter settings.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
params0 = {}
params1 = {"n_dim": 3, "const": 1.0}
return [params0, params1]
@classmethod
def _get_score_params(self):
"""Return settings for testing score/evaluate functions. Used in tests only.
Returns a list, the i-th element should be valid arguments for
self.evaluate and self.score, of an instance constructed with
self.get_test_params()[i].
Returns
-------
list of dict
The parameters to be used for scoring.
"""
score_params0 = {"x0": 0, "x1": 0}
score_params1 = {"x0": 1, "x1": 2, "x2": 3}
return [score_params0, score_params1]