OrthogonalGridSearchOptimizer.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 20
rs 9.55
c 0
b 0
f 0
cc 1
nop 8

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
import numpy as np
6
7
from ..base_optimizer import BaseOptimizer
8
9
10
class OrthogonalGridSearchOptimizer(BaseOptimizer):
11
    def __init__(
12
        self,
13
        search_space,
14
        initialize={"grid": 4, "random": 2, "vertices": 4},
15
        constraints=[],
16
        random_state=None,
17
        rand_rest_p=0,
18
        nth_process=None,
19
        step_size=1,
20
    ):
21
        super().__init__(
22
            search_space=search_space,
23
            initialize=initialize,
24
            constraints=constraints,
25
            random_state=random_state,
26
            rand_rest_p=rand_rest_p,
27
            nth_process=nth_process,
28
        )
29
30
        self.step_size = step_size
31
32
    def grid_move(self):
33
        mod_tmp = self.nth_trial * self.step_size + int(
34
            self.nth_trial * self.step_size / self.conv.search_space_size
35
        )
36
        div_tmp = self.nth_trial * self.step_size + int(
37
            self.nth_trial * self.step_size / self.conv.search_space_size
38
        )
39
        flipped_new_pos = []
40
41
        for dim_size in self.conv.dim_sizes:
42
            mod = mod_tmp % dim_size
43
            div = int(div_tmp / dim_size)
44
45
            flipped_new_pos.append(mod)
46
47
            mod_tmp = div
48
            div_tmp = div
49
50
        return np.array(flipped_new_pos)
51
52
    @BaseOptimizer.track_new_pos
53
    def iterate(self):
54
        pos_new = self.grid_move()
55
        pos_new = self.conv2pos(pos_new)
56
        return pos_new
57
58
    @BaseOptimizer.track_new_score
59
    def evaluate(self, score_new):
60
        BaseOptimizer.evaluate(self, score_new)
61