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

hyperactive.base._optimizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 28
dl 0
loc 73
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
# 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