Passed
Push — master ( 7dc8c7...0f59c2 )
by Simon
03:25
created

tests.test_initializers.test_initialize_grid_1()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 0
dl 11
loc 11
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(para):
6
    score = -para["x1"] * para["x1"]
7
    return score
8
9
10
search_space = {
11
    "x1": np.arange(-100, 101, 1),
12
}
13
14
15
def test_initialize_warm_start_0():
16
    init = {
17
        "x1": 0,
18
    }
19
20
    initialize = {"warm_start": [init]}
21
22
    opt = RandomSearchOptimizer(search_space, initialize=initialize)
23
    opt.search(objective_function, n_iter=1)
24
25
    # print("\nself.results \n", opt.results)
26
27
    assert abs(opt.best_score) < 0.001
28
29
30 View Code Duplication
def test_initialize_warm_start_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
    search_space = {
32
        "x1": np.arange(-10, 10, 1),
33
    }
34
    init = {
35
        "x1": -10,
36
    }
37
38
    initialize = {"warm_start": [init]}
39
40
    opt = RandomSearchOptimizer(search_space, initialize=initialize)
41
    opt.search(objective_function, n_iter=1)
42
43
    assert opt.best_para == init
44
45
46 View Code Duplication
def test_initialize_warm_start_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
47
    search_space = {
48
        "x1": np.arange(-10, 10, 1),
49
    }
50
    init = {
51
        "x1": -10,
52
    }
53
54
    initialize = {"warm_start": [init], "random": 0, "vertices": 0, "grid": 0}
55
56
    opt = RandomSearchOptimizer(search_space, initialize=initialize)
57
    opt.search(objective_function, n_iter=1)
58
59
    assert opt.best_para == init
60
61
62
def test_initialize_vertices():
63
    initialize = {"vertices": 2}
64
65
    opt = RandomSearchOptimizer(search_space, initialize=initialize)
66
    opt.search(objective_function, n_iter=2)
67
68
    assert abs(opt.best_score) - 10000 < 0.001
69
70
71 View Code Duplication
def test_initialize_grid_0():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
72
    search_space = {
73
        "x1": np.arange(-1, 2, 1),
74
    }
75
    initialize = {"grid": 1}
76
77
    opt = RandomSearchOptimizer(search_space, initialize=initialize)
78
    opt.search(objective_function, n_iter=1)
79
80
    assert abs(opt.best_score) < 0.001
81
82
83 View Code Duplication
def test_initialize_grid_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
    search_space = {
85
        "x1": np.arange(-2, 3, 1),
86
    }
87
88
    initialize = {"grid": 1}
89
90
    opt = RandomSearchOptimizer(search_space, initialize=initialize)
91
    opt.search(objective_function, n_iter=1)
92
93
    assert abs(opt.best_score) - 1 < 0.001
94