Completed
Push — master ( 734fb3...5d2776 )
by Simon
04:12 queued 11s
created

sigmoid()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 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 _iterate(self, i, _cand_, _p_, X, y):
38
        _p_.pos_new = _p_.move_climb(_cand_, _p_.pos_current)
39
        _p_.score_new = _cand_.eval_pos(_p_.pos_new, X, y)
40
41
        if _p_.score_new > _cand_.score_best:
42
            _cand_, _p_ = self._update_pos(_cand_, _p_)
43
        else:
44
            p_accept = self._accept(_p_)
45
            self._consider(_p_, p_accept)
46
47
        return _cand_
48