|
1
|
|
|
"""Adapter for gfo package.""" |
|
2
|
|
|
# copyright: hyperactive developers, MIT License (see LICENSE file) |
|
3
|
|
|
|
|
4
|
|
|
from gradient_free_optimizers import HillClimbingOptimizer |
|
5
|
|
|
from hyperactive.base import BaseOptimizer |
|
6
|
|
|
from skbase.utils.stdout_mute import StdoutMute |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class _BaseGFOadapter(BaseOptimizer): |
|
10
|
|
|
"""Adapter base class for gradient-free-optimizers. |
|
11
|
|
|
|
|
12
|
|
|
* default tag setting |
|
13
|
|
|
* Handles defaults for "initialize" |
|
14
|
|
|
* provides default get_search_config |
|
15
|
|
|
* provides default get_test_params |
|
16
|
|
|
* extension interface: _get_gfo_class |
|
17
|
|
|
""" |
|
18
|
|
|
|
|
19
|
|
|
_tags = { |
|
20
|
|
|
"authors": "SimonBlanke", |
|
21
|
|
|
"python_dependencies": ["gradient-free-optimizers>=1.5.0"], |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
def __init__(self): |
|
25
|
|
|
|
|
26
|
|
|
super().__init__() |
|
27
|
|
|
|
|
28
|
|
|
if self.initialize is None: |
|
29
|
|
|
self._initialize = {"grid": 4, "random": 2, "vertices": 4} |
|
30
|
|
|
else: |
|
31
|
|
|
self._initialize = self.initialize |
|
32
|
|
|
|
|
33
|
|
|
def _get_gfo_class(self): |
|
34
|
|
|
"""Get the GFO class to use. |
|
35
|
|
|
|
|
36
|
|
|
Returns |
|
37
|
|
|
------- |
|
38
|
|
|
class |
|
39
|
|
|
The GFO class to use. One of the concrete GFO classes |
|
40
|
|
|
""" |
|
41
|
|
|
raise NotImplementedError( |
|
42
|
|
|
"This method should be implemented in a subclass." |
|
43
|
|
|
) |
|
44
|
|
|
|
|
45
|
|
|
def get_search_config(self): |
|
46
|
|
|
"""Get the search configuration. |
|
47
|
|
|
|
|
48
|
|
|
Returns |
|
49
|
|
|
------- |
|
50
|
|
|
dict with str keys |
|
51
|
|
|
The search configuration dictionary. |
|
52
|
|
|
""" |
|
53
|
|
|
search_config = super().get_search_config() |
|
54
|
|
|
search_config["initialize"] = self._initialize |
|
55
|
|
|
del search_config["verbose"] |
|
56
|
|
|
return search_config |
|
57
|
|
|
|
|
58
|
|
|
@classmethod |
|
59
|
|
|
def get_test_params(cls, parameter_set="default"): |
|
60
|
|
|
"""Return testing parameter settings for the skbase object. |
|
61
|
|
|
|
|
62
|
|
|
``get_test_params`` is a unified interface point to store |
|
63
|
|
|
parameter settings for testing purposes. This function is also |
|
64
|
|
|
used in ``create_test_instance`` and ``create_test_instances_and_names`` |
|
65
|
|
|
to construct test instances. |
|
66
|
|
|
|
|
67
|
|
|
``get_test_params`` should return a single ``dict``, or a ``list`` of ``dict``. |
|
68
|
|
|
|
|
69
|
|
|
Each ``dict`` is a parameter configuration for testing, |
|
70
|
|
|
and can be used to construct an "interesting" test instance. |
|
71
|
|
|
A call to ``cls(**params)`` should |
|
72
|
|
|
be valid for all dictionaries ``params`` in the return of ``get_test_params``. |
|
73
|
|
|
|
|
74
|
|
|
The ``get_test_params`` need not return fixed lists of dictionaries, |
|
75
|
|
|
it can also return dynamic or stochastic parameter settings. |
|
76
|
|
|
|
|
77
|
|
|
Parameters |
|
78
|
|
|
---------- |
|
79
|
|
|
parameter_set : str, default="default" |
|
80
|
|
|
Name of the set of test parameters to return, for use in tests. If no |
|
81
|
|
|
special parameters are defined for a value, will return `"default"` set. |
|
82
|
|
|
|
|
83
|
|
|
Returns |
|
84
|
|
|
------- |
|
85
|
|
|
params : dict or list of dict, default = {} |
|
86
|
|
|
Parameters to create testing instances of the class |
|
87
|
|
|
Each dict are parameters to construct an "interesting" test instance, i.e., |
|
88
|
|
|
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance. |
|
89
|
|
|
`create_test_instance` uses the first (or only) dictionary in `params` |
|
90
|
|
|
""" |
|
91
|
|
|
import numpy as np |
|
92
|
|
|
from hyperactive.experiment.integrations import SklearnCvExperiment |
|
93
|
|
|
|
|
94
|
|
|
sklearn_exp = SklearnCvExperiment.create_test_instance() |
|
95
|
|
|
params_sklearn = { |
|
96
|
|
|
"experiment": sklearn_exp, |
|
97
|
|
|
"search_space": { |
|
98
|
|
|
"C": np.array([0.01, 0.1, 1, 10]), |
|
99
|
|
|
"gamma": np.array([0.0001, 0.01, 0.1, 1, 10]), |
|
100
|
|
|
}, |
|
101
|
|
|
"n_iter": 100, |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
from hyperactive.experiment.toy import Ackley |
|
105
|
|
|
|
|
106
|
|
|
ackley_exp = Ackley.create_test_instance() |
|
107
|
|
|
params_ackley = { |
|
108
|
|
|
"experiment": ackley_exp, |
|
109
|
|
|
"search_space": { |
|
110
|
|
|
"x0": np.linspace(-5, 5, 10), |
|
111
|
|
|
"x1": np.linspace(-5, 5, 10), |
|
112
|
|
|
}, |
|
113
|
|
|
"n_iter": 100, |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return [params_sklearn, params_ackley] |
|
117
|
|
|
|