|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
import numpy as np |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
from ...base_optimizer import BaseOptimizer |
|
10
|
|
|
from ...base_positioner import BasePositioner |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def _split_into_subcubes(data, split_per_dim=2): |
|
14
|
|
|
n_dim = data.shape[1] |
|
15
|
|
|
subcubes = [] |
|
16
|
|
|
|
|
17
|
|
|
data_list = [data] |
|
18
|
|
|
|
|
19
|
|
|
for dim in range(n_dim): |
|
20
|
|
|
print("\n") |
|
21
|
|
|
subdata_list = [] |
|
22
|
|
|
|
|
23
|
|
|
if dim == 0: |
|
24
|
|
|
data_list = [data] |
|
25
|
|
|
|
|
26
|
|
|
for data in data_list: |
|
27
|
|
|
print("data", data.shape) |
|
28
|
|
|
data_sorted = data[data[:, dim].argsort()] |
|
29
|
|
|
|
|
30
|
|
|
subdata = np.array_split(data_sorted, 2, axis=0) |
|
31
|
|
|
|
|
32
|
|
|
subdata_list = subdata_list + subdata |
|
33
|
|
|
|
|
34
|
|
|
data_list = subdata_list |
|
35
|
|
|
|
|
36
|
|
|
print("subdata_list", len(subdata_list)) |
|
37
|
|
|
|
|
38
|
|
|
return subcubes |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class SBOM(BaseOptimizer): |
|
42
|
|
|
def __init__(self, _opt_args_): |
|
43
|
|
|
super().__init__(_opt_args_) |
|
44
|
|
|
self.n_positioners = 1 |
|
45
|
|
|
|
|
46
|
|
|
def get_random_sample(self): |
|
47
|
|
|
sample_size = self._sample_size() |
|
48
|
|
|
if sample_size > self.all_pos_comb.shape[0]: |
|
49
|
|
|
sample_size = self.all_pos_comb.shape[0] |
|
50
|
|
|
|
|
51
|
|
|
row_sample = np.random.choice( |
|
52
|
|
|
self.all_pos_comb.shape[0], size=(sample_size,), replace=False |
|
53
|
|
|
) |
|
54
|
|
|
return self.all_pos_comb[row_sample] |
|
55
|
|
|
|
|
56
|
|
|
def _sample_size(self): |
|
57
|
|
|
n = self._opt_args_.max_sample_size |
|
58
|
|
|
return int(n * np.tanh(self.all_pos_comb.size / n)) |
|
59
|
|
|
|
|
60
|
|
|
def _all_possible_pos(self, cand): |
|
61
|
|
|
pos_space = [] |
|
62
|
|
|
for dim_ in cand._space_.dim: |
|
63
|
|
|
pos_space.append(np.arange(dim_ + 1)) |
|
64
|
|
|
|
|
65
|
|
|
self.n_dim = len(pos_space) |
|
66
|
|
|
self.all_pos_comb = np.array(np.meshgrid(*pos_space)).T.reshape(-1, self.n_dim) |
|
67
|
|
|
|
|
68
|
|
|
print("\n\nself.all_pos_comb", self.all_pos_comb.shape, "\n") |
|
69
|
|
|
|
|
70
|
|
|
_split_into_subcubes(self.all_pos_comb) |
|
71
|
|
|
|
|
72
|
|
|
def _init_iteration(self, _cand_): |
|
73
|
|
|
p = SbomPositioner(self._opt_args_) |
|
74
|
|
|
p.move_random(_cand_) |
|
75
|
|
|
|
|
76
|
|
|
self._optimizer_eval(_cand_, p) |
|
77
|
|
|
self._update_pos(_cand_, p) |
|
78
|
|
|
|
|
79
|
|
|
self._all_possible_pos(_cand_) |
|
80
|
|
|
|
|
81
|
|
|
if self._opt_args_.warm_start_smbo: |
|
82
|
|
|
self.X_sample = _cand_.mem._get_para() |
|
83
|
|
|
self.Y_sample = _cand_.mem._get_score() |
|
84
|
|
|
else: |
|
85
|
|
|
self.X_sample = _cand_.pos_best.reshape(1, -1) |
|
86
|
|
|
self.Y_sample = np.array(_cand_.score_best).reshape(1, -1) |
|
87
|
|
|
|
|
88
|
|
|
return p |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class SbomPositioner(BasePositioner): |
|
92
|
|
|
def __init__(self, _opt_args_): |
|
93
|
|
|
super().__init__(_opt_args_) |
|
94
|
|
|
|