Completed
Push — master ( f67568...5e5af8 )
by Simon
01:28
created

hyperactive.optimizers.sequence_model.sbom   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A SBOM.__init__() 0 3 1
A SBOM._all_possible_pos() 0 7 2
A SBOM.get_random_sample() 0 9 2
A SBOM._sample_size() 0 3 1
A SBOM._init_iteration() 0 17 2
A SbomPositioner.__init__() 0 2 1
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