Passed
Push — master ( 4bb434...c73ac4 )
by Simon
01:32
created

tests.test_parameters.test_replacement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_replacement_0() 0 9 2
A parabola_function() 0 3 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import pytest
6
import numpy as np
7
8
9
from gradient_free_optimizers import (
10
    BayesianOptimizer,
11
    TreeStructuredParzenEstimators,
12
    LipschitzOptimizer,
13
    ForestOptimizer,
14
)
15
16
17
def parabola_function(para):
18
    loss = para["x"] * para["x"] + para["y"] * para["y"]
19
    return -loss
20
21
22
search_space = {
23
    "x": np.arange(-1, 1, 1),
24
    "y": np.arange(-1, 1, 1),
25
}
26
27
optimizer_para = (
28
    "optimizer",
29
    [
30
        (BayesianOptimizer),
31
        (TreeStructuredParzenEstimators),
32
        (LipschitzOptimizer),
33
        (ForestOptimizer),
34
    ],
35
)
36
37
38
@pytest.mark.parametrize(*optimizer_para)
39
def test_replacement_0(optimizer):
40
    opt = optimizer(search_space, replacement=True)
41
    opt.search(parabola_function, n_iter=15)
42
43
    opt_false = optimizer(search_space, replacement=False)
44
    with pytest.raises((ValueError, IndexError)):
45
        opt_false.search(parabola_function, n_iter=15)
46
    assert len(opt_false.all_pos_comb) == 0
47