Passed
Push — master ( 199200...2b2f7b )
by Simon
04:29
created

objective_function()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import pytest
2
import numpy as np
3
4
from tqdm import tqdm
5
from ._parametrize import optimizers
6
from hyperactive.search_space import SearchSpace
7
8
9
def objective_function(opt):
10
    score = -opt["x1"] * opt["x1"]
11
    return score
12
13
14
def objective_function_m5(opt):
15
    score = -(opt["x1"] - 5) * (opt["x1"] - 5)
16
    return score
17
18
19
def objective_function_p5(opt):
20
    score = -(opt["x1"] + 5) * (opt["x1"] + 5)
21
    return score
22
23
24
search_space_0 = {"x1": list(np.arange(-100, 101, 1))}
25
search_space_1 = {"x1": list(np.arange(0, 101, 1))}
26
search_space_2 = {"x1": list(np.arange(-100, 1, 1))}
27
28
search_space_3 = {"x1": list(np.arange(-10, 11, 0.1))}
29
search_space_4 = {"x1": list(np.arange(0, 11, 0.1))}
30
search_space_5 = {"x1": list(np.arange(-10, 1, 0.1))}
31
32
search_space_6 = {"x1": list(np.arange(-0.0000000003, 0.0000000003, 0.0000000001))}
33
search_space_7 = {"x1": list(np.arange(0, 0.0000000003, 0.0000000001))}
34
search_space_8 = {"x1": list(np.arange(-0.0000000003, 0, 0.0000000001))}
35
36
objective_para = (
37
    "objective",
38
    [
39
        (objective_function),
40
        (objective_function_m5),
41
        (objective_function_p5),
42
    ],
43
)
44
45
search_space_para = (
46
    "search_space",
47
    [
48
        (search_space_0),
49
        (search_space_1),
50
        (search_space_2),
51
        (search_space_3),
52
        (search_space_4),
53
        (search_space_5),
54
        (search_space_6),
55
        (search_space_7),
56
        (search_space_8),
57
    ],
58
)
59
60
61
@pytest.mark.parametrize(*objective_para)
62
@pytest.mark.parametrize(*search_space_para)
63
@pytest.mark.parametrize(*optimizers)
64
def test_gfo_opt_wrapper_0(Optimizer, search_space, objective):
65
    search_space = search_space
66
    objective_function = objective
67
68
    n_iter = 10
69
    s_space = SearchSpace(search_space)
70
71
    initialize = {"vertices": 2}
72
    pass_through = {}
73
    callbacks = None
74
    catch = None
75
    max_score = None
76
    early_stopping = None
77
    random_state = None
78
    memory = None
79
    memory_warm_start = None
80
    verbosity = ["progress_bar", "print_results", "print_times"]
81
82
    opt = Optimizer()
83
84
    opt.setup_search(
85
        objective_function=objective_function,
86
        s_space=s_space,
87
        n_iter=n_iter,
88
        initialize=initialize,
89
        pass_through=pass_through,
90
        callbacks=callbacks,
91
        catch=catch,
92
        max_score=max_score,
93
        early_stopping=early_stopping,
94
        random_state=random_state,
95
        memory=memory,
96
        memory_warm_start=memory_warm_start,
97
        verbosity=verbosity,
98
    )
99
    opt.max_time = None
100
    opt.search(nth_process=0, p_bar=tqdm(total=n_iter))
101
102
    assert opt.best_score == objective_function(opt.best_para)
103