BaseOptimizationStrategy.setup_search()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 39
Code Lines 34

Duplication

Lines 39
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 34
nop 15
dl 39
loc 39
rs 9.064
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
from .optimizer_attributes import OptimizerAttributes
7
8
9
class BaseOptimizationStrategy(OptimizerAttributes):
10
    def __init__(self):
11
        super().__init__()
12
13 View Code Duplication
    def setup_search(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
        self,
15
        objective_function,
16
        s_space,
17
        n_iter,
18
        initialize,
19
        constraints,
20
        pass_through,
21
        callbacks,
22
        catch,
23
        max_score,
24
        early_stopping,
25
        random_state,
26
        memory,
27
        memory_warm_start,
28
        verbosity,
29
    ):
30
        self.objective_function = objective_function
31
        self.s_space = s_space
32
        self.n_iter = n_iter
33
34
        self.initialize = initialize
35
        self.constraints = constraints
36
        self.pass_through = pass_through
37
        self.callbacks = callbacks
38
        self.catch = catch
39
        self.max_score = max_score
40
        self.early_stopping = early_stopping
41
        self.random_state = random_state
42
        self.memory = memory
43
        self.memory_warm_start = memory_warm_start
44
        self.verbosity = verbosity
45
46
        self._max_time = None
47
48
        if "progress_bar" in self.verbosity:
49
            self.verbosity = []
50
        else:
51
            self.verbosity = []
52
53
    @property
54
    def max_time(self):
55
        return self._max_time
56
57
    @max_time.setter
58
    def max_time(self, value):
59
        self._max_time = value
60
61
        for optimizer_setup in self.optimizer_setup_l:
62
            optimizer_setup["optimizer"].max_time = value
63
64
    def search(self, nth_process, p_bar):
65
        for optimizer_setup in self.optimizer_setup_l:
66
            hyper_opt = optimizer_setup["optimizer"]
67
            duration = optimizer_setup["duration"]
68
            opt_strat_early_stopping = optimizer_setup["early_stopping"]
69
70
            if opt_strat_early_stopping:
71
                early_stopping = opt_strat_early_stopping
72
            else:
73
                early_stopping = self.early_stopping
74
75
            n_iter = round(self.n_iter * duration / self.duration_sum)
76
77
            # initialize
78
            if self.best_para is not None:
79
                initialize = {}
80
                if "warm_start" in initialize:
81
                    initialize["warm_start"].append(self.best_para)
82
                else:
83
                    initialize["warm_start"] = [self.best_para]
84
            else:
85
                initialize = dict(self.initialize)
86
87
            # memory_warm_start
88
            if self.search_data is not None:
89
                memory_warm_start = self.search_data
90
            else:
91
                memory_warm_start = self.memory_warm_start
92
93
            # warm_start_smbo
94
            if (
95
                hyper_opt.optimizer_class.optimizer_type == "sequential"
96
                and self.search_data is not None
97
            ):
98
                hyper_opt.opt_params["warm_start_smbo"] = self.search_data
99
100
            hyper_opt.setup_search(
101
                objective_function=self.objective_function,
102
                s_space=self.s_space,
103
                n_iter=n_iter,
104
                initialize=initialize,
105
                constraints=self.constraints,
106
                pass_through=self.pass_through,
107
                callbacks=self.callbacks,
108
                catch=self.catch,
109
                max_score=self.max_score,
110
                early_stopping=early_stopping,
111
                random_state=self.random_state,
112
                memory=self.memory,
113
                memory_warm_start=memory_warm_start,
114
                verbosity=self.verbosity,
115
            )
116
117
            hyper_opt.search(nth_process, p_bar)
118
119
            self._add_result_attributes(
120
                hyper_opt.best_para,
121
                hyper_opt.best_score,
122
                hyper_opt.best_since_iter,
123
                hyper_opt.eval_times,
124
                hyper_opt.iter_times,
125
                hyper_opt.search_data,
126
                hyper_opt.gfo_optimizer.random_seed,
127
            )
128