Completed
Push — master ( e76f99...13bbbc )
by Simon
01:30
created

  A

Complexity

Conditions 3

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
from scipy.spatial.distance import cdist
9
10
11
from .sbom import SBOM
12
13
14
class BayesianOptimizer(SBOM):
15
    def __init__(self, _opt_args_):
16
        super().__init__(_opt_args_)
17
        self.regr = self._opt_args_.gpr
18
        self.new_positions = []
19
20
    def expected_improvement(self):
21
        all_pos_comb_sampled = self.get_random_sample()
22
23
        mu, sigma = self.regr.predict(all_pos_comb_sampled, return_std=True)
24
        mu_sample = self.regr.predict(self.X_sample)
25
26
        mu = mu.reshape(-1, 1)
27
        sigma = sigma.reshape(-1, 1)
28
        mu_sample = mu_sample.reshape(-1, 1)
29
30
        mu_sample_opt = np.max(mu_sample)
31
        imp = mu - mu_sample_opt - self._opt_args_.xi
32
33
        Z = np.divide(imp, sigma, out=np.zeros_like(sigma), where=sigma != 0)
34
        exp_imp = imp * norm.cdf(Z) + sigma * norm.pdf(Z)
35
        exp_imp[sigma == 0.0] = 0.0
36
37
        return exp_imp
38
39
    def propose_location(self, i, _cand_):
40
        self.regr.fit(self.X_sample, self.Y_sample)
41
42
        exp_imp = self.expected_improvement()
43
        exp_imp = exp_imp[:, 0]
44
45
        index_best = list(exp_imp.argsort()[::-1])
46
        all_pos_comb_sorted = self.all_pos_comb[index_best]
47
48
        pos_best = [all_pos_comb_sorted[0]]
49
50
        while len(pos_best) < self._opt_args_.skip_retrain(i):
51
            if all_pos_comb_sorted.shape[0] == 0:
52
                break
53
54
            dists = cdist(all_pos_comb_sorted, [pos_best[-1]], metric="cityblock")
55
            dists_norm = dists / dists.max()
56
            bool = np.squeeze(dists_norm > 0.25)
57
            all_pos_comb_sorted = all_pos_comb_sorted[bool]
58
59
            if len(all_pos_comb_sorted) > 0:
60
                pos_best.append(all_pos_comb_sorted[0])
61
62
        return pos_best
63
64
    def _iterate(self, i, _cand_):
65
        if i < self._opt_args_.start_up_evals:
66
            self.p_list[0].move_random(_cand_)
67
            self._optimizer_eval(_cand_, self.p_list[0])
68
            self._update_pos(_cand_, self.p_list[0])
69
        else:
70
            if len(self.new_positions) == 0:
71
                self.new_positions = self.propose_location(i, _cand_)
72
73
            self.p_list[0].pos_new = self.new_positions[0]
74
            self._optimizer_eval(_cand_, self.p_list[0])
75
            self._update_pos(_cand_, self.p_list[0])
76
77
            self.new_positions.pop(0)
78
79
        self.X_sample = np.vstack((self.X_sample, self.p_list[0].pos_new))
80
        self.Y_sample = np.vstack((self.Y_sample, self.p_list[0].score_new))
81
82
        return _cand_
83