Passed
Pull Request — master (#110)
by
unknown
12:02 queued 10:25
created

hyperactive.base._optimizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 27
dl 0
loc 71
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
    }
12
13
    def __init__(self):
14
        super().__init__()
15
        assert hasattr(self, "experiment"), "Optimizer must have an experiment."
16
        search_config = self.get_params()
17
        self._experiment = search_config.pop("experiment", None)
18
19
    def add_search(self, experiment, **search_config):
20
        """Add a new optimization search process with specified parameters.
21
22
        Parameters
23
        ----------
24
        experiment : BaseExperiment
25
            The experiment to optimize parameters for.
26
        search_config : dict with str keys
27
            Key/value pairs may be any subset of the parameters of the class.
28
        """
29
        self._experiment = experiment
30
        if not hasattr(self, "_search_config_update"):
31
            self._search_config_update = search_config
32
        else:
33
            self._search_config_update.update(search_config)
34
35
    def get_search_config(self):
36
        """Get the search configuration.
37
38
        Returns
39
        -------
40
        dict with str keys
41
            The search configuration dictionary.
42
        """
43
        search_config = self.get_params(deep=False)
44
        search_config.pop("experiment", None)
45
        if hasattr(self, "_search_config_update"):
46
            search_config.update(self._search_config_update)
47
        return search_config
48
49
    def get_experiment(self):
50
        """Get the experiment.
51
52
        Returns
53
        -------
54
        BaseExperiment
55
            The experiment to optimize parameters for.
56
        """
57
        return self._experiment
58
59
    def run(self):
60
        """Run the optimization search process.
61
62
        Returns
63
        -------
64
        best_params : dict
65
            The best parameters found during the optimization process.
66
        """
67
        experiment = self.get_experiment()
68
        search_config = self.get_search_config()
69
70
        return self._run(experiment, **search_config)
71