Passed
Push — master ( 9ff666...1a4396 )
by Simon
03:24
created

HillClimbingPositioner.__init__()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
7
from ...base_optimizer import BaseOptimizer
8
from ...base_positioner import BasePositioner
9
10
11
class HillClimbingOptimizer(BaseOptimizer):
12
    def __init__(self, _main_args_, _opt_args_):
13
        super().__init__(_main_args_, _opt_args_)
14
15
    def _hill_climb_iter(self, _cand_, _p_):
16
        score_new = -np.inf
17
        pos_new = None
18
19
        for _ in range(self._opt_args_.n_neighbours):
20
            _p_.pos_new = _p_.move_climb(_cand_, _p_.pos_current)
21
            _p_.score_new = _cand_.eval_pos(_p_.pos_new)
22
23
            if _p_.score_new > score_new:
24
                score_new = _p_.score_new
25
                pos_new = _p_.pos_new
26
27
        if score_new > _cand_.score_best:
28
            _p_.pos_new = pos_new
29
            _p_.score_new = score_new
30
31
            _cand_, _p_ = self._update_pos(_cand_, _p_)
32
33
        return _cand_, _p_
34
35
    def _iterate(self, i, _cand_, _p_):
36
        _cand_, _p_ = self._hill_climb_iter(_cand_, _p_)
37
38
        return _cand_
39
40
    def _init_opt_positioner(self, _cand_):
41
        return super()._init_base_positioner(_cand_, positioner=HillClimbingPositioner)
42
43
44
class HillClimbingPositioner(BasePositioner):
45
    def __init__(self, *args, **kwargs):
46
        super().__init__(*args, **kwargs)
47
48
        self.epsilon = kwargs["epsilon"]
49
        self.climb_dist = kwargs["climb_dist"]
50