Passed
Push — master ( be0089...415349 )
by Simon
01:18
created

BayesianOptimizer.propose_location()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
import numpy as np
7
from scipy.stats import norm
8
9
10
from .sbom import SBOM
11
12
13
class BayesianOptimizer(SBOM):
14
    def __init__(self, _main_args_, _opt_args_):
15
        super().__init__(_main_args_, _opt_args_)
16
        self.gpr = self._opt_args_.gpr
17
18
    def expected_improvement(self):
19
        mu, sigma = self.gpr.predict(self.all_pos_comb)
20
        mu_sample, _ = self.gpr.predict(self.X_sample)
21
22
        sigma = sigma.reshape(-1, 1)
23
        mu_sample_opt = np.max(mu_sample)
24
25
        imp = mu - mu_sample_opt - self._opt_args_.xi
26
        Z = np.divide(imp, sigma, out=np.zeros_like(sigma), where=sigma != 0)
27
        exp_imp = imp * norm.cdf(Z) + sigma * norm.pdf(Z)
28
        exp_imp[sigma == 0.0] = 0.0
29
30
        return exp_imp
31
32
    def propose_location(self, cand):
33
        self.gpr.fit(self.X_sample, self.Y_sample)
34
        exp_imp = self.expected_improvement()
35
        exp_imp = exp_imp[:, 0]
36
37
        index_best = list(exp_imp.argsort()[::-1])
38
39
        all_pos_comb_sorted = self.all_pos_comb[index_best]
40
        pos_best = all_pos_comb_sorted[0]
41
42
        return pos_best
43
44
    def _iterate(self, i, _cand_, _p_):
45
        _p_.pos_new = self.propose_location(_cand_)
46
        _p_.score_new = _cand_.eval_pos(_p_.pos_new)
47
48
        if _p_.score_new > _cand_.score_best:
49
            _cand_, _p_ = self._update_pos(_cand_, _p_)
50
51
        self.X_sample = np.vstack((self.X_sample, _p_.pos_new))
52
        self.Y_sample = np.vstack((self.Y_sample, _p_.score_new))
53
54
        return _cand_
55