gradient_free_optimizers.optimizers.global_opt.random_restart_hill_climbing   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 45
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A RandomRestartHillClimbingOptimizer.iterate() 0 13 3
A RandomRestartHillClimbingOptimizer.__init__() 0 28 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
from ..local_opt import HillClimbingOptimizer
7
8
9
class RandomRestartHillClimbingOptimizer(HillClimbingOptimizer):
10
    name = "Random Restart Hill Climbing"
11
    _name_ = "random_restart_hill_climbing"
12
    __name__ = "RandomRestartHillClimbingOptimizer"
13
14
    optimizer_type = "global"
15
    computationally_expensive = False
16
17
    def __init__(
18
        self,
19
        search_space,
20
        initialize={"grid": 4, "random": 2, "vertices": 4},
21
        constraints=[],
22
        random_state=None,
23
        rand_rest_p=0,
24
        nth_process=None,
25
        epsilon=0.03,
26
        distribution="normal",
27
        n_neighbours=3,
28
        n_iter_restart=10,
29
    ):
30
        super().__init__(
31
            search_space=search_space,
32
            initialize=initialize,
33
            constraints=constraints,
34
            random_state=random_state,
35
            rand_rest_p=rand_rest_p,
36
            nth_process=nth_process,
37
            epsilon=epsilon,
38
            distribution=distribution,
39
            n_neighbours=n_neighbours,
40
        )
41
        self.epsilon = epsilon
42
        self.distribution = distribution
43
        self.n_neighbours = n_neighbours
44
        self.n_iter_restart = n_iter_restart
45
46
    @HillClimbingOptimizer.track_new_pos
47
    @HillClimbingOptimizer.random_iteration
48
    def iterate(self):
49
        notZero = self.nth_trial != 0
50
        modZero = self.nth_trial % self.n_iter_restart == 0
51
52
        if notZero and modZero:
53
            return self.move_random()
54
        else:
55
            return self.move_climb(
56
                self.pos_current,
57
                epsilon=self.epsilon,
58
                distribution=self.distribution,
59
            )
60