Repository navigation
Expand file tree
/
Copy path_optimizer.py
More file actions
101 lines (81 loc) · 3.33 KB
/
Copy path_optimizer.py
File metadata and controls
101 lines (81 loc) · 3.33 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
"""Base class for optimizer."""
# copyright: hyperactive developers, MIT License (see LICENSE file)
from skbase.base import BaseObject
class BaseOptimizer(BaseObject):
"""Base class for optimizer."""
_tags = {
"object_type": "optimizer",
"python_dependencies": None,
# properties of the optimizer
"info:name": None, # str
"info:local_vs_global": "mixed", # "local", "mixed", "global"
"info:explore_vs_exploit": "mixed", # "explore", "exploit", "mixed"
"info:compute": "middle", # "low", "middle", "high"
# see here for explanation of the tags:
# https://simonblanke.github.io/gradient-free-optimizers-documentation/1.5/optimizers/ # noqa: E501
}
def __init__(self):
super().__init__()
assert hasattr(self, "experiment"), "Optimizer must have an experiment."
search_config = self.get_params()
self._experiment = search_config.pop("experiment", None)
if self.get_tag("info:name") is None:
self.set_tags(**{"info:name": self.__class__.__name__})
def get_search_config(self):
"""Get the search configuration.
Returns
-------
dict with str keys
The search configuration dictionary.
"""
search_config = self.get_params(deep=False)
search_config.pop("experiment", None)
return search_config
def get_experiment(self):
"""Get the experiment.
Returns
-------
BaseExperiment
The experiment to optimize parameters for.
"""
return self._experiment
def solve(self):
"""Run the optimization search process to maximize the experiment's score.
The optimization searches for a maximizer of the experiment's
``score`` method.
Depending on the tag ``property:higher_or_lower_is_better`` being
set to ``higher`` or ``lower``, the ``run`` method will search for:
* the minimizer of the ``evaluate`` method if the tag is ``lower``
* the maximizer of the ``evaluate`` method if the tag is ``higher``
Returns
-------
best_params : dict
The best parameters found during the optimization process.
The dict ``best_params`` can be used in ``experiment.score`` or
``experiment.evaluate`` directly.
"""
experiment = self.get_experiment()
search_config = self.get_search_config()
best_params = self._solve(experiment, **search_config)
self.best_params_ = best_params
return best_params
def _solve(self, experiment, *args, **kwargs):
"""Run the optimization search process.
Parameters
----------
experiment : BaseExperiment
The experiment to optimize parameters for.
*args : tuple
Positional arguments specific to the optimization backend.
**kwargs : dict
Keyword arguments specific to the optimization backend.
Returns
-------
dict with str keys
The best parameters found during the search.
Must have keys a subset or identical to experiment.paramnames().
"""
raise NotImplementedError(
"abstract method, BaseOptimizer._solve should be implemented by "
"descendant classes"
)