1
|
|
|
"""Base class for optimizer.""" |
2
|
|
|
|
3
|
|
|
# copyright: hyperactive developers, MIT License (see LICENSE file) |
4
|
|
|
|
5
|
|
|
from skbase.base import BaseObject |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class BaseOptimizer(BaseObject): |
9
|
|
|
"""Base class for optimizer.""" |
10
|
|
|
|
11
|
|
|
_tags = { |
12
|
|
|
"object_type": "optimizer", |
13
|
|
|
"python_dependencies": None, |
14
|
|
|
# properties of the optimizer |
15
|
|
|
"info:name": None, # str |
16
|
|
|
"info:local_vs_global": "mixed", # "local", "mixed", "global" |
17
|
|
|
"info:explore_vs_exploit": "mixed", # "explore", "exploit", "mixed" |
18
|
|
|
"info:compute": "middle", # "low", "middle", "high" |
19
|
|
|
# see here for explanation of the tags: |
20
|
|
|
# https://simonblanke.github.io/gradient-free-optimizers-documentation/1.5/optimizers/ # noqa: E501 |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
def __init__(self): |
24
|
|
|
super().__init__() |
25
|
|
|
assert hasattr(self, "experiment"), "Optimizer must have an experiment." |
26
|
|
|
search_config = self.get_params() |
27
|
|
|
self._experiment = search_config.pop("experiment", None) |
28
|
|
|
|
29
|
|
|
if self.get_tag("info:name") is None: |
30
|
|
|
self.set_tags(**{"info:name": self.__class__.__name__}) |
31
|
|
|
|
32
|
|
|
def get_search_config(self): |
33
|
|
|
"""Get the search configuration. |
34
|
|
|
|
35
|
|
|
Returns |
36
|
|
|
------- |
37
|
|
|
dict with str keys |
38
|
|
|
The search configuration dictionary. |
39
|
|
|
""" |
40
|
|
|
search_config = self.get_params(deep=False) |
41
|
|
|
search_config.pop("experiment", None) |
42
|
|
|
return search_config |
43
|
|
|
|
44
|
|
|
def get_experiment(self): |
45
|
|
|
"""Get the experiment. |
46
|
|
|
|
47
|
|
|
Returns |
48
|
|
|
------- |
49
|
|
|
BaseExperiment |
50
|
|
|
The experiment to optimize parameters for. |
51
|
|
|
""" |
52
|
|
|
return self._experiment |
53
|
|
|
|
54
|
|
|
def run(self): |
55
|
|
|
"""Run the optimization search process to maximize the experiment's score. |
56
|
|
|
|
57
|
|
|
The optimization searches for a maximizer of the experiment's |
58
|
|
|
``score`` method. |
59
|
|
|
|
60
|
|
|
Depending on the tag ``property:higher_or_lower_is_better`` being |
61
|
|
|
set to ``higher`` or ``lower``, the ``run`` method will search for: |
62
|
|
|
|
63
|
|
|
* the minimizer of the ``evaluate`` method if the tag is ``lower`` |
64
|
|
|
* the maximizer of the ``evaluate`` method if the tag is ``higher`` |
65
|
|
|
|
66
|
|
|
Returns |
67
|
|
|
------- |
68
|
|
|
best_params : dict |
69
|
|
|
The best parameters found during the optimization process. |
70
|
|
|
The dict ``best_params`` can be used in ``experiment.score`` or |
71
|
|
|
``experiment.evaluate`` directly. |
72
|
|
|
""" |
73
|
|
|
experiment = self.get_experiment() |
74
|
|
|
search_config = self.get_search_config() |
75
|
|
|
|
76
|
|
|
best_params = self._run(experiment, **search_config) |
77
|
|
|
self.best_params_ = best_params |
78
|
|
|
return best_params |
79
|
|
|
|