Passed
Pull Request — master (#110)
by
unknown
01:48
created

hyperactive.base._optimizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 28
dl 0
loc 72
rs 10
c 0
b 0
f 0

5 Methods

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