RandomAnnealingOptimizer.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 31
Code Lines 29

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 29
dl 31
loc 31
rs 9.184
c 0
b 0
f 0
cc 1
nop 12

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 ..local_opt import HillClimbingOptimizer
7
8
9
class RandomAnnealingOptimizer(HillClimbingOptimizer):
10
    name = "Random Annealing"
11
    _name_ = "random_annealing"
12
    __name__ = "RandomAnnealingOptimizer"
13
14 View Code Duplication
    def __init__(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
15
        self,
16
        search_space,
17
        initialize={"grid": 4, "random": 2, "vertices": 4},
18
        constraints=[],
19
        random_state=None,
20
        rand_rest_p=0,
21
        nth_process=None,
22
        epsilon=0.03,
23
        distribution="normal",
24
        n_neighbours=3,
25
        annealing_rate=0.98,
26
        start_temp=10,
27
    ):
28
        super().__init__(
29
            search_space=search_space,
30
            initialize=initialize,
31
            constraints=constraints,
32
            random_state=random_state,
33
            rand_rest_p=rand_rest_p,
34
            nth_process=nth_process,
35
            epsilon=epsilon,
36
            distribution=distribution,
37
            n_neighbours=n_neighbours,
38
        )
39
        self.epsilon = epsilon
40
        self.distribution = distribution
41
        self.n_neighbours = n_neighbours
42
        self.annealing_rate = annealing_rate
43
        self.start_temp = start_temp
44
        self.temp = start_temp
45
46
    @HillClimbingOptimizer.track_new_pos
47
    @HillClimbingOptimizer.random_iteration
48
    def iterate(self):
49
        pos = self.move_climb(
50
            self.pos_current,
51
            epsilon=self.epsilon,
52
            distribution=self.distribution,
53
            epsilon_mod=self.temp,
54
        )
55
        self.temp = self.temp * self.annealing_rate
56
57
        return pos
58