gradient_free_optimizers.optimizers.exp_opt.random_annealing   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 53.45 %

Importance

Changes 0
Metric Value
wmc 2
eloc 45
dl 31
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A RandomAnnealingOptimizer.iterate() 0 12 1
A RandomAnnealingOptimizer.__init__() 31 31 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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