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
|
|
|
class SBOM(BaseOptimizer): |
14
|
|
|
def __init__(self, _opt_args_): |
15
|
|
|
super().__init__(_opt_args_) |
16
|
|
|
self.n_positioners = 1 |
17
|
|
|
|
18
|
|
|
def get_random_sample(self): |
19
|
|
|
sample_size = self._sample_size() |
20
|
|
|
if sample_size > self.all_pos_comb.shape[0]: |
21
|
|
|
sample_size = self.all_pos_comb.shape[0] |
22
|
|
|
|
23
|
|
|
row_sample = np.random.choice( |
24
|
|
|
self.all_pos_comb.shape[0], size=(sample_size,), replace=False |
25
|
|
|
) |
26
|
|
|
return self.all_pos_comb[row_sample] |
27
|
|
|
|
28
|
|
|
def _sample_size(self): |
29
|
|
|
n = self._opt_args_.max_sample_size |
30
|
|
|
return int(n * np.tanh(self.all_pos_comb.size / n)) |
31
|
|
|
|
32
|
|
|
def _all_possible_pos(self, cand): |
33
|
|
|
pos_space = [] |
34
|
|
|
for dim_ in cand._space_.dim: |
35
|
|
|
pos_space.append(np.arange(dim_ + 1)) |
36
|
|
|
|
37
|
|
|
self.n_dim = len(pos_space) |
38
|
|
|
self.all_pos_comb = np.array(np.meshgrid(*pos_space)).T.reshape(-1, self.n_dim) |
39
|
|
|
|
40
|
|
|
def _init_iteration(self, _cand_): |
41
|
|
|
p = SbomPositioner() |
42
|
|
|
p.move_random(_cand_) |
43
|
|
|
|
44
|
|
|
self._optimizer_eval(_cand_, p) |
45
|
|
|
self._update_pos(_cand_, p) |
46
|
|
|
|
47
|
|
|
self._all_possible_pos(_cand_) |
48
|
|
|
|
49
|
|
|
if self._opt_args_.warm_start_smbo: |
50
|
|
|
self.X_sample = _cand_.mem._get_para() |
51
|
|
|
self.Y_sample = _cand_.mem._get_score() |
52
|
|
|
else: |
53
|
|
|
self.X_sample = _cand_.pos_best.reshape(1, -1) |
54
|
|
|
self.Y_sample = np.array(_cand_.score_best).reshape(1, -1) |
55
|
|
|
|
56
|
|
|
return p |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
class SbomPositioner(BasePositioner): |
60
|
|
|
def __init__(self, *args, **kwargs): |
61
|
|
|
super().__init__(*args, **kwargs) |
62
|
|
|
|