Passed
Push — master ( afb360...c5e615 )
by Simon
01:17
created

tests.test_initializers.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 numpy as np
2
from gradient_free_optimizers import RandomSearchOptimizer
3
4
5
def objective_function(pos_new):
6
    score = -pos_new[0] * pos_new[0]
7
    return score
8
9
10
search_space = [np.arange(-100, 101, 1)]
11
12
13
def test_initialize_warm_start():
14
    initialize = {"warm_start": [np.array([0])]}
15
16
    opt = RandomSearchOptimizer(search_space)
17
    opt.search(
18
        objective_function, n_iter=0, initialize=initialize,
19
    )
20
21
    assert abs(opt.best_score) < 0.001
22
23
24
def test_initialize_vertices():
25
    initialize = {"vertices": 2}
26
27
    opt = RandomSearchOptimizer(search_space)
28
    opt.search(
29
        objective_function, n_iter=0, initialize=initialize,
30
    )
31
32
    assert abs(opt.best_score) - 10000 < 0.001
33
34
35
def test_initialize_grid_0():
36
    search_space = [np.arange(-1, 2, 1)]
37
    initialize = {"grid": 1}
38
39
    opt = RandomSearchOptimizer(search_space)
40
    opt.search(
41
        objective_function, n_iter=0, initialize=initialize,
42
    )
43
44
    assert abs(opt.best_score) < 0.001
45
46
47
def test_initialize_grid_1():
48
    search_space = [np.arange(-2, 3, 1)]
49
    initialize = {"grid": 1}
50
51
    opt = RandomSearchOptimizer(search_space)
52
    opt.search(
53
        objective_function, n_iter=0, initialize=initialize,
54
    )
55
56
    assert abs(opt.best_score) - 1 < 0.001
57
58