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