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

hyperactive.optimizers.local.stochastic_hill_climbing   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 31
dl 0
loc 50
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A StochasticHillClimbingOptimizer._score_norm() 0 5 1
A StochasticHillClimbingOptimizer.__init__() 0 2 1
A StochasticHillClimbingOptimizer._accept() 0 2 1
A StochasticHillClimbingOptimizer._consider() 0 6 2
A StochasticHillClimbingOptimizer._stochastic_hill_climb_iter() 0 8 2
A StochasticHillClimbingOptimizer._iterate() 0 4 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A sigmoid() 0 2 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import math
6
import random
7
import numpy as np
8
9
from . import HillClimbingOptimizer
10
11
12
def sigmoid(x):
13
    return 1 / (1 + math.exp(-x))
14
15
16
class StochasticHillClimbingOptimizer(HillClimbingOptimizer):
17
    def __init__(self, *args, **kwargs):
18
        super().__init__(*args, **kwargs)
19
20
    def _consider(self, _p_, p_accept):
21
        rand = random.uniform(0, self._arg_.p_down)
22
23
        if p_accept > rand:
24
            _p_.score_current = _p_.score_new
25
            _p_.pos_current = _p_.pos_new
26
27
    def _score_norm(self, _p_):
28
        return (
29
            100
30
            * (_p_.score_current - _p_.score_new)
31
            / (_p_.score_current + _p_.score_new)
32
        )
33
34
    def _accept(self, _p_):
35
        return np.exp(-self._score_norm(_p_))
36
37
    def _stochastic_hill_climb_iter(self, _cand_, _p_, X, y):
38
        _cand_, _p_ = self._hill_climb_iter(_cand_, _p_, X, y)
39
40
        if _p_.score_new <= _cand_.score_best:
41
            p_accept = self._accept(_p_)
42
            self._consider(_p_, p_accept)
43
44
        return _cand_
45
46
    def _iterate(self, i, _cand_, _p_, X, y):
47
        _cand_ = self._stochastic_hill_climb_iter(_cand_, _p_, X, y)
48
49
        return _cand_
50