Passed
Push — master ( e3d174...7ebf7e )
by Simon
01:32
created

test_sbom_parameter.test_smbo_para()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import pytest
6
import numpy as np
7
8
from gradient_free_optimizers import (
9
    BayesianOptimizer,
10
    TreeStructuredParzenEstimators,
11
    DecisionTreeOptimizer,
12
    EnsembleOptimizer,
13
)
14
15
16
def objective_function(para):
17
    score = -para["x1"] * para["x1"]
18
    return score
19
20
21
search_space = {"x1": np.arange(-10, 11, 1)}
22
23
24
sbom_para = [
25
    ({"warm_start_smbo": None}),
26
    ({"warm_start_smbo": None}),
27
    ({"rand_rest_p": 0}),
28
    ({"rand_rest_p": 0.5}),
29
    ({"rand_rest_p": 1}),
30
    ({"rand_rest_p": 10}),
31
]
32
33
34
pytest_wrapper = ("para", sbom_para)
35
36
optimizers_sbom = (
37
    "Optimizer",
38
    [
39
        (BayesianOptimizer),
40
        (TreeStructuredParzenEstimators),
41
        (DecisionTreeOptimizer),
42
        (EnsembleOptimizer),
43
    ],
44
)
45
46
47
@pytest.mark.parametrize(*optimizers_sbom)
48
@pytest.mark.parametrize(*pytest_wrapper)
49
def test_smbo_para(Optimizer, para):
50
    opt = Optimizer(search_space, **para)
51
    opt.search(
52
        objective_function,
53
        n_iter=10,
54
        memory=False,
55
        verbosity=False,
56
        initialize={"vertices": 2},
57
    )
58
59
    for optimizer in opt.optimizers:
60
        para_key = list(para.keys())[0]
61
        para_value = getattr(optimizer, para_key)
62
63
        assert para_value == para[para_key]
64