|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import time |
|
6
|
|
|
import random |
|
7
|
|
|
import numpy as np |
|
8
|
|
|
|
|
9
|
|
|
from ..base_optimizer import BaseOptimizer |
|
10
|
|
|
from ...search import Search |
|
11
|
|
|
from ._sub_search_spaces import SubSearchSpaces |
|
12
|
|
|
from ..smb_opt import BayesianOptimizer |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class LocalBayesianOptimizer(BaseOptimizer, Search): |
|
16
|
|
|
name = "Local Bayesian Optimizer" |
|
17
|
|
|
|
|
18
|
|
|
def __init__( |
|
19
|
|
|
self, |
|
20
|
|
|
*args, |
|
21
|
|
|
max_size=300000, |
|
22
|
|
|
n_positions=20, |
|
23
|
|
|
local_range=100, |
|
24
|
|
|
**kwargs, |
|
25
|
|
|
): |
|
26
|
|
|
super().__init__(*args, **kwargs) |
|
27
|
|
|
|
|
28
|
|
|
self.max_size = max_size |
|
29
|
|
|
self.n_positions = n_positions |
|
30
|
|
|
self.local_range = local_range |
|
31
|
|
|
|
|
32
|
|
|
self.bayes_opt = BayesianOptimizer(self.conv.search_space) |
|
33
|
|
|
|
|
34
|
|
|
def create_local_smbo(self, current_position): |
|
35
|
|
|
local_ss = {} |
|
36
|
|
|
|
|
37
|
|
|
for idx, para in enumerate(self.conv.para_names): |
|
38
|
|
|
|
|
39
|
|
|
max_dim = max(0, current_position[idx] + self.local_range) |
|
40
|
|
|
min_dim = min( |
|
41
|
|
|
self.conv.dim_sizes[idx], current_position[idx] - self.local_range |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
dim_pos = np.array(self.conv.search_space_positions[idx]) |
|
45
|
|
|
|
|
46
|
|
|
dim_pos_center = np.where( |
|
47
|
|
|
np.logical_and(dim_pos >= min_dim, dim_pos <= max_dim) |
|
48
|
|
|
)[0] |
|
49
|
|
|
local_ss[para] = dim_pos_center |
|
50
|
|
|
|
|
51
|
|
|
self.bayes_opt = BayesianOptimizer(local_ss) |
|
52
|
|
|
|
|
53
|
|
|
def finish_initialization(self): |
|
54
|
|
|
self.create_local_smbo(self.pos_current) |
|
55
|
|
|
|
|
56
|
|
|
@BaseOptimizer.track_nth_iter |
|
57
|
|
|
def iterate(self): |
|
58
|
|
|
pos_loc = self.bayes_opt.iterate() |
|
59
|
|
|
pos_new = self.bayes_opt.conv.position2value(pos_loc) |
|
60
|
|
|
|
|
61
|
|
|
return pos_new |
|
62
|
|
|
|
|
63
|
|
|
def evaluate(self, score_new): |
|
64
|
|
|
self.bayes_opt.evaluate(score_new) |
|
65
|
|
|
|
|
66
|
|
|
self.score_new = score_new |
|
67
|
|
|
|
|
68
|
|
|
self._evaluate_new2current(score_new) |
|
69
|
|
|
self._evaluate_current2best() |
|
70
|
|
|
|
|
71
|
|
|
modZero = self.nth_iter % self.n_positions == 0 |
|
72
|
|
|
|
|
73
|
|
|
if modZero: |
|
74
|
|
|
self.create_local_smbo(self.pos_current) |
|
75
|
|
|
|