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

HillClimbingOptimizer._hill_climb_iter()   A

Complexity

Conditions 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 19
rs 9.7
c 0
b 0
f 0
cc 4
nop 5
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