Passed
Pull Request — master (#101)
by Simon
01:33
created

hyperactive.optimizers.search.Search._search()   B

Complexity

Conditions 5

Size

Total Lines 64
Code Lines 49

Duplication

Lines 64
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 49
nop 2
dl 64
loc 64
rs 8.2024
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
from .objective_function import ObjectiveFunction
6
from .hyper_gradient_conv import HyperGradientConv
7
from .optimizer_attributes import OptimizerAttributes
8
from .constraint import Constraint
9
10
11
class Search(OptimizerAttributes):
12
    max_time: float
13
    nth_process: int
14
15
    def __init__(self, optimizer_class, opt_params):
16
        super().__init__()
17
        self.optimizer_class = optimizer_class
18
        self.opt_params = opt_params
19
20
    def setup(
21
        self,
22
        experiment,
23
        s_space,
24
        n_iter,
25
        initialize,
26
        constraints,
27
        pass_through,
28
        callbacks,
29
        catch,
30
        max_score,
31
        early_stopping,
32
        random_state,
33
        memory,
34
        memory_warm_start,
35
        verbosity,
36
    ):
37
        self.experiment = experiment
38
        self.s_space = s_space
39
        self.n_iter = n_iter
40
41
        self.initialize = initialize
42
        self.constraints = constraints
43
        self.pass_through = pass_through
44
        self.callbacks = callbacks
45
        self.catch = catch
46
        self.max_score = max_score
47
        self.early_stopping = early_stopping
48
        self.random_state = random_state
49
        self.memory = memory
50
        self.memory_warm_start = memory_warm_start
51
        self.verbosity = verbosity
52
53
        if "progress_bar" in self.verbosity:
54
            self.verbosity = ["progress_bar"]
55
        else:
56
            self.verbosity = []
57
58
    def pass_args(self, max_time, nth_process):
59
        self.max_time = max_time
60
        self.nth_process = nth_process
61
62 View Code Duplication
    def convert_results2hyper(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
        self.eval_times = sum(self.gfo_optimizer.eval_times)
64
        self.iter_times = sum(self.gfo_optimizer.iter_times)
65
66
        if self.gfo_optimizer.best_para is not None:
67
            value = self.hg_conv.para2value(self.gfo_optimizer.best_para)
68
            position = self.hg_conv.position2value(value)
69
            best_para = self.hg_conv.value2para(position)
70
            self.best_para = best_para
71
        else:
72
            self.best_para = None
73
74
        self.best_score = self.gfo_optimizer.best_score
75
        self.positions = self.gfo_optimizer.search_data
76
        self.search_data = self.hg_conv.positions2results(self.positions)
77
78
        results_dd = self.gfo_optimizer.search_data.drop_duplicates(
79
            subset=self.s_space.dim_keys, keep="first"
80
        )
81
        self.memory_values_df = results_dd[
82
            self.s_space.dim_keys + ["score"]
83
        ].reset_index(drop=True)
84
85 View Code Duplication
    def _setup_process(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
86
        self.hg_conv = HyperGradientConv(self.s_space)
87
88
        initialize = self.hg_conv.conv_initialize(self.initialize)
89
        search_space_positions = self.s_space.positions
90
91
        # conv warm start for smbo from values into positions
92
        if "warm_start_smbo" in self.opt_params:
93
            self.opt_params["warm_start_smbo"] = (
94
                self.hg_conv.conv_memory_warm_start(
95
                    self.opt_params["warm_start_smbo"]
96
                )
97
            )
98
99
        gfo_constraints = [
100
            Constraint(constraint, self.s_space)
101
            for constraint in self.constraints
102
        ]
103
104
        self.gfo_optimizer = self.optimizer_class(
105
            search_space=search_space_positions,
106
            initialize=initialize,
107
            constraints=gfo_constraints,
108
            random_state=self.random_state,
109
            nth_process=self.nth_process,
110
            **self.opt_params,
111
        )
112
113
        self.conv = self.gfo_optimizer.conv
114
115 View Code Duplication
    def _search(self, p_bar):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
116
        self._setup_process()
117
118
        gfo_wrapper_model = ObjectiveFunction(
119
            objective_function=self.experiment.objective_function,
120
            callbacks=self.callbacks,
121
            catch=self.catch,
122
        )
123
        gfo_wrapper_model.pass_through = self.pass_through
124
125
        memory_warm_start = self.hg_conv.conv_memory_warm_start(
126
            self.memory_warm_start
127
        )
128
129
        gfo_objective_function = gfo_wrapper_model(self.s_space())
130
131
        self.gfo_optimizer.init_search(
132
            gfo_objective_function,
133
            self.n_iter,
134
            self.max_time,
135
            self.max_score,
136
            self.early_stopping,
137
            self.memory,
138
            memory_warm_start,
139
            False,
140
        )
141
        for nth_iter in range(self.n_iter):
142
            if p_bar:
143
                p_bar.set_description(
144
                    "["
145
                    + str(self.nth_process)
146
                    + "] "
147
                    + str(self.experiment.__class__.__name__)
148
                    + " ("
149
                    + self.optimizer_class.name
150
                    + ")",
151
                )
152
153
            self.gfo_optimizer.search_step(nth_iter)
154
            if self.gfo_optimizer.stop.check():
155
                break
156
157
            if p_bar:
158
                p_bar.set_postfix(
159
                    best_score=str(self.gfo_optimizer.score_best),
160
                    best_pos=str(self.gfo_optimizer.pos_best),
161
                    best_iter=str(self.gfo_optimizer.p_bar._best_since_iter),
162
                )
163
164
                p_bar.update(1)
165
                p_bar.refresh()
166
167
        self.gfo_optimizer.finish_search()
168
169
        self.convert_results2hyper()
170
171
        self._add_result_attributes(
172
            self.best_para,
173
            self.best_score,
174
            self.gfo_optimizer.p_bar._best_since_iter,
175
            self.eval_times,
176
            self.iter_times,
177
            self.search_data,
178
            self.gfo_optimizer.random_seed,
179
        )
180