|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
from scipy.stats import norm |
|
7
|
|
|
|
|
8
|
|
|
from ..smb_opt.smbo import SMBO |
|
9
|
|
|
from ..smb_opt.surrogate_models import EnsembleRegressor |
|
10
|
|
|
from ..smb_opt.acquisition_function import ExpectedImprovement |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
from sklearn.tree import DecisionTreeRegressor |
|
14
|
|
|
from sklearn.ensemble import GradientBoostingRegressor |
|
15
|
|
|
from sklearn.svm import SVR |
|
16
|
|
|
from sklearn.gaussian_process import GaussianProcessRegressor |
|
17
|
|
|
from sklearn.neural_network import MLPRegressor |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def normalize(array): |
|
21
|
|
|
num = array - array.min() |
|
22
|
|
|
den = array.max() - array.min() |
|
23
|
|
|
|
|
24
|
|
|
if den == 0: |
|
25
|
|
|
return np.random.random_sample(array.shape) |
|
26
|
|
|
else: |
|
27
|
|
|
return ((num / den) + 0) / 1 |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class EnsembleOptimizer(SMBO): |
|
31
|
|
|
name = "Ensemble Optimizer" |
|
32
|
|
|
|
|
33
|
|
|
def __init__( |
|
34
|
|
|
self, |
|
35
|
|
|
search_space, |
|
36
|
|
|
initialize={"grid": 4, "random": 2, "vertices": 4}, |
|
37
|
|
|
constraints=[], |
|
38
|
|
|
random_state=None, |
|
39
|
|
|
rand_rest_p=0, |
|
40
|
|
|
nth_process=None, |
|
41
|
|
|
epsilon=0.03, |
|
42
|
|
|
distribution="normal", |
|
43
|
|
|
n_neighbours=3, |
|
44
|
|
|
estimators=[ |
|
45
|
|
|
GradientBoostingRegressor(n_estimators=5), |
|
46
|
|
|
# DecisionTreeRegressor(), |
|
47
|
|
|
# MLPRegressor(), |
|
48
|
|
|
GaussianProcessRegressor(), |
|
49
|
|
|
], |
|
50
|
|
|
xi=0.01, |
|
51
|
|
|
warm_start_smbo=None, |
|
52
|
|
|
max_sample_size=10000000, |
|
53
|
|
|
sampling={"random": 1000000}, |
|
54
|
|
|
replacement=True, |
|
55
|
|
|
warnings=100000000, |
|
56
|
|
|
**kwargs, |
|
57
|
|
|
): |
|
58
|
|
|
super().__init__( |
|
59
|
|
|
search_space=search_space, |
|
60
|
|
|
initialize=initialize, |
|
61
|
|
|
constraints=constraints, |
|
62
|
|
|
random_state=random_state, |
|
63
|
|
|
rand_rest_p=rand_rest_p, |
|
64
|
|
|
nth_process=nth_process, |
|
65
|
|
|
epsilon=epsilon, |
|
66
|
|
|
distribution=distribution, |
|
67
|
|
|
n_neighbours=n_neighbours, # |
|
68
|
|
|
warm_start_smbo=warm_start_smbo, |
|
69
|
|
|
max_sample_size=max_sample_size, |
|
70
|
|
|
sampling=sampling, |
|
71
|
|
|
replacement=replacement, |
|
72
|
|
|
) |
|
73
|
|
|
self.estimators = estimators |
|
74
|
|
|
self.regr = EnsembleRegressor(estimators) |
|
75
|
|
|
self.xi = xi |
|
76
|
|
|
self.warm_start_smbo = warm_start_smbo |
|
77
|
|
|
self.max_sample_size = max_sample_size |
|
78
|
|
|
self.sampling = sampling |
|
79
|
|
|
self.warnings = warnings |
|
80
|
|
|
|
|
81
|
|
|
self.init_warm_start_smbo() |
|
82
|
|
|
|
|
83
|
|
|
def finish_initialization(self): |
|
84
|
|
|
self.all_pos_comb = self._all_possible_pos() |
|
85
|
|
|
return super().finish_initialization() |
|
86
|
|
|
|
|
87
|
|
|
def _expected_improvement(self): |
|
88
|
|
|
self.pos_comb = self._sampling(self.all_pos_comb) |
|
89
|
|
|
|
|
90
|
|
|
acqu_func = ExpectedImprovement(self.regr, self.pos_comb, self.xi) |
|
91
|
|
|
return acqu_func.calculate(self.X_sample, self.Y_sample) |
|
92
|
|
|
|
|
93
|
|
|
def _training(self): |
|
94
|
|
|
X_sample = np.array(self.X_sample) |
|
95
|
|
|
Y_sample = np.array(self.Y_sample) |
|
96
|
|
|
|
|
97
|
|
|
if len(Y_sample) == 0: |
|
98
|
|
|
return self.move_random() |
|
99
|
|
|
|
|
100
|
|
|
Y_sample = normalize(Y_sample).reshape(-1, 1) |
|
101
|
|
|
self.regr.fit(X_sample, Y_sample) |
|
102
|
|
|
|