1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
|
7
|
|
|
from ..base_optimizer import BaseOptimizer |
8
|
|
|
from ...search import Search |
9
|
|
|
from .bayesian_optimization import BayesianOptimizer |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def sort_list_idx(list_): |
13
|
|
|
list_np = np.array(list_) |
14
|
|
|
idx_sorted = list(list_np.argsort()[::-1]) |
15
|
|
|
return idx_sorted |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class PowellsMethod(BaseOptimizer, Search): |
19
|
|
|
def __init__( |
20
|
|
|
self, |
21
|
|
|
search_space, |
22
|
|
|
initialize={"grid": 4, "random": 2, "vertices": 4}, |
23
|
|
|
iters_p_dim=20, |
24
|
|
|
): |
25
|
|
|
super().__init__(search_space, initialize) |
26
|
|
|
|
27
|
|
|
self.iters_p_dim = iters_p_dim |
28
|
|
|
|
29
|
|
|
self.current_search_dim = -1 |
30
|
|
|
|
31
|
|
|
def finish_initialization(self): |
32
|
|
|
self.nth_iter_ = -1 |
33
|
|
|
self.nth_iter_current_dim = 0 |
34
|
|
|
|
35
|
|
|
def new_dim(self): |
36
|
|
|
self.current_search_dim += 1 |
37
|
|
|
|
38
|
|
|
if self.current_search_dim >= self.conv.n_dimensions: |
39
|
|
|
self.current_search_dim = 0 |
40
|
|
|
|
41
|
|
|
idx_sorted = sort_list_idx(self.scores_valid) |
42
|
|
|
self.powells_pos = [self.positions_valid[idx] for idx in idx_sorted][0] |
43
|
|
|
self.powells_scores = [self.scores_valid[idx] for idx in idx_sorted][0] |
44
|
|
|
|
45
|
|
|
self.nth_iter_current_dim = 0 |
46
|
|
|
|
47
|
|
|
min_pos = [] |
48
|
|
|
max_pos = [] |
49
|
|
|
center_pos = [] |
50
|
|
|
|
51
|
|
|
search_space_1D = {} |
52
|
|
|
for idx, para_name in enumerate(self.conv.para_names): |
53
|
|
|
if self.current_search_dim == idx: |
54
|
|
|
# fill with range of values |
55
|
|
|
search_space_pos = self.conv.search_space_positions[idx] |
56
|
|
|
search_space_1D[para_name] = search_space_pos |
57
|
|
|
|
58
|
|
|
min_pos.append(int(np.amin(search_space_pos))) |
59
|
|
|
max_pos.append(int(np.amax(search_space_pos))) |
60
|
|
|
center_pos.append(int(np.median(search_space_pos))) |
61
|
|
|
else: |
62
|
|
|
# fill with single value |
63
|
|
|
search_space_1D[para_name] = np.array([self.powells_pos[idx]]) |
64
|
|
|
|
65
|
|
|
min_pos.append(self.powells_pos[idx]) |
66
|
|
|
max_pos.append(self.powells_pos[idx]) |
67
|
|
|
center_pos.append(self.powells_pos[idx]) |
68
|
|
|
|
69
|
|
|
self.init_positions_ = [min_pos, center_pos, max_pos] |
70
|
|
|
|
71
|
|
|
self.bayes_opt = BayesianOptimizer( |
72
|
|
|
search_space=search_space_1D, initialize={"vertices": 2, "random": 3} |
73
|
|
|
) |
74
|
|
|
|
75
|
|
|
@BaseOptimizer.track_nth_iter |
76
|
|
|
def iterate(self): |
77
|
|
|
self.nth_iter_ += 1 |
78
|
|
|
self.nth_iter_current_dim += 1 |
79
|
|
|
|
80
|
|
|
modZero = self.nth_iter_ % self.iters_p_dim == 0 |
81
|
|
|
# nonZero = self.nth_iter_ != 0 |
82
|
|
|
|
83
|
|
|
if modZero: |
84
|
|
|
self.new_dim() |
85
|
|
|
|
86
|
|
|
if self.nth_iter_current_dim < 5: |
87
|
|
|
pos_new = self.bayes_opt.init_pos( |
88
|
|
|
self.bayes_opt.init_positions[self.nth_iter_current_dim] |
89
|
|
|
) |
90
|
|
|
else: |
91
|
|
|
pos_new = self.bayes_opt.iterate() |
92
|
|
|
pos_new = self.bayes_opt.conv.position2value(pos_new) |
93
|
|
|
|
94
|
|
|
return pos_new |
95
|
|
|
|
96
|
|
|
def evaluate(self, score_new): |
97
|
|
|
self.score_new = score_new |
98
|
|
|
|
99
|
|
|
if self.current_search_dim == -1: |
100
|
|
|
BaseOptimizer.evaluate(self, score_new) |
101
|
|
|
else: |
102
|
|
|
self.bayes_opt.evaluate(score_new) |
103
|
|
|
|