Passed
Push — master ( 9aa23e...87bd68 )
by Simon
01:40
created

hyperactive.optimizers.local.hill_climbing_optimizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A HillClimbingOptimizer._init_opt_positioner() 0 2 1
A HillClimbingOptimizer._iterate() 0 4 1
A HillClimbingOptimizer.__init__() 0 3 1
A HillClimbingOptimizer._hill_climb_iter() 0 19 4
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
from ...base_optimizer import BaseOptimizer
7
8
9
class HillClimbingOptimizer(BaseOptimizer):
10
    def __init__(self, *args, **kwargs):
11
        super().__init__(*args, **kwargs)
12
        self.pos_para = {"epsilon": self._arg_.epsilon}
13
14
    def _hill_climb_iter(self, _cand_, _p_, X, y):
15
        score_new = -1000
16
        pos_new = None
17
18
        for i in range(self._arg_.n_neighbours):
19
            _p_.pos_new = _p_.move_climb(_cand_, _p_.pos_current)
20
            _p_.score_new = _cand_.eval_pos(_p_.pos_new, X, y)
21
22
            if _p_.score_new > score_new:
23
                score_new = _p_.score_new
24
                pos_new = _p_.pos_new
25
26
        if score_new > _cand_.score_best:
27
            _p_.pos_new = pos_new
28
            _p_.score_new = score_new
29
30
            _cand_, _p_ = self._update_pos(_cand_, _p_)
31
32
        return _cand_, _p_
33
34
    def _iterate(self, i, _cand_, _p_, X, y):
35
        _cand_, _p_ = self._hill_climb_iter(_cand_, _p_, X, y)
36
37
        return _cand_
38
39
    def _init_opt_positioner(self, _cand_, X, y):
40
        return super()._init_base_positioner(_cand_)
41