1
|
|
|
"""Base class for optimizer.""" |
2
|
|
|
# copyright: hyperactive developers, MIT License (see LICENSE file) |
3
|
|
|
|
4
|
|
|
from skbase.base import BaseObject |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class BaseOptimizer(BaseObject): |
8
|
|
|
"""Base class for optimizer.""" |
9
|
|
|
|
10
|
|
|
_tags = { |
11
|
|
|
"object_type": "optimizer", |
12
|
|
|
"python_dependencies": None, |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
def __init__(self): |
16
|
|
|
super().__init__() |
17
|
|
|
assert hasattr(self, "experiment"), "Optimizer must have an experiment." |
18
|
|
|
search_config = self.get_params() |
19
|
|
|
self._experiment = search_config.pop("experiment", None) |
20
|
|
|
|
21
|
|
|
def add_search(self, experiment, **search_config): |
22
|
|
|
"""Add a new optimization search process with specified parameters. |
23
|
|
|
|
24
|
|
|
Parameters |
25
|
|
|
---------- |
26
|
|
|
experiment : BaseExperiment |
27
|
|
|
The experiment to optimize parameters for. |
28
|
|
|
search_config : dict with str keys |
29
|
|
|
Key/value pairs may be any subset of the parameters of the class. |
30
|
|
|
""" |
31
|
|
|
self._experiment = experiment |
32
|
|
|
if not hasattr(self, "_search_config_update"): |
33
|
|
|
self._search_config_update = search_config |
34
|
|
|
else: |
35
|
|
|
self._search_config_update.update(search_config) |
36
|
|
|
|
37
|
|
|
def get_search_config(self): |
38
|
|
|
"""Get the search configuration. |
39
|
|
|
|
40
|
|
|
Returns |
41
|
|
|
------- |
42
|
|
|
dict with str keys |
43
|
|
|
The search configuration dictionary. |
44
|
|
|
""" |
45
|
|
|
search_config = self.get_params(deep=False) |
46
|
|
|
search_config.pop("experiment", None) |
47
|
|
|
if hasattr(self, "_search_config_update"): |
48
|
|
|
search_config.update(self._search_config_update) |
49
|
|
|
return search_config |
50
|
|
|
|
51
|
|
|
def get_experiment(self): |
52
|
|
|
"""Get the experiment. |
53
|
|
|
|
54
|
|
|
Returns |
55
|
|
|
------- |
56
|
|
|
BaseExperiment |
57
|
|
|
The experiment to optimize parameters for. |
58
|
|
|
""" |
59
|
|
|
return self._experiment |
60
|
|
|
|
61
|
|
|
def run(self): |
62
|
|
|
"""Run the optimization search process. |
63
|
|
|
|
64
|
|
|
Returns |
65
|
|
|
------- |
66
|
|
|
best_params : dict |
67
|
|
|
The best parameters found during the optimization process. |
68
|
|
|
""" |
69
|
|
|
experiment = self.get_experiment() |
70
|
|
|
search_config = self.get_search_config() |
71
|
|
|
|
72
|
|
|
return self._run(experiment, **search_config) |
73
|
|
|
|