Total Complexity | 4 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 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 | |||
9 | |||
10 | def normalize(array): |
||
11 | num = array - array.min() |
||
12 | den = array.max() - array.min() |
||
13 | |||
14 | if den == 0: |
||
15 | return np.random.random_sample(array.shape) |
||
16 | else: |
||
17 | return ((num / den) + 0) / 1 |
||
18 | |||
19 | |||
20 | class ExpectedImprovement: |
||
21 | def __init__(self, surrogate_model, position_l, xi): |
||
22 | self.surrogate_model = surrogate_model |
||
23 | self.position_l = position_l |
||
24 | self.xi = xi |
||
25 | |||
26 | def calculate(self, X_sample, Y_sample): |
||
27 | mu, sigma = self.surrogate_model.predict(self.position_l, return_std=True) |
||
28 | # TODO mu_sample = self.surrogate_model.predict(X_sample) |
||
29 | mu = mu.reshape(-1, 1) |
||
30 | sigma = sigma.reshape(-1, 1) |
||
31 | |||
32 | # with normalization this is always 1 |
||
33 | Y_sample = normalize(np.array(Y_sample)).reshape(-1, 1) |
||
34 | |||
35 | imp = mu - np.max(Y_sample) - self.xi |
||
36 | Z = np.divide(imp, sigma, out=np.zeros_like(sigma), where=sigma != 0) |
||
37 | |||
38 | exploit = imp * norm.cdf(Z) |
||
39 | explore = sigma * norm.pdf(Z) |
||
40 | |||
41 | aqu_func = exploit + explore |
||
42 | aqu_func[sigma == 0.0] = 0.0 |
||
43 | |||
44 | return aqu_func[:, 0] |
||
45 |