Passed
Push — master ( 44bd28...268666 )
by Simon
03:57
created

tests.test_results.test_attributes_results_0()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import numpy as np
2
import pandas as pd
3
from gradient_free_optimizers import RandomSearchOptimizer
4
5
6
def objective_function(para):
7
    score = -para["x1"] * para["x1"]
8
    return score
9
10
11
search_space = {
12
    "x1": np.arange(0, 100000, 0.1),
13
}
14
15
16
def test_attributes_results_0():
17
    opt = RandomSearchOptimizer(search_space)
18
    opt.search(objective_function, n_iter=100)
19
20
    assert isinstance(opt.results, pd.DataFrame)
21
22
23
def test_attributes_results_1():
24
    opt = RandomSearchOptimizer(search_space)
25
    opt.search(objective_function, n_iter=100)
26
27
    assert set(search_space.keys()) < set(opt.results.columns)
28
29
30
def test_attributes_results_2():
31
    opt = RandomSearchOptimizer(search_space)
32
    opt.search(objective_function, n_iter=100)
33
34
    assert "x1" in list(opt.results.columns)
35
36
37
def test_attributes_results_3():
38
    opt = RandomSearchOptimizer(search_space)
39
    opt.search(objective_function, n_iter=100)
40
41
    assert "score" in list(opt.results.columns)
42
43
44
def test_attributes_results_4():
45
    opt = RandomSearchOptimizer(search_space)
46
    opt.search(
47
        objective_function, n_iter=1, initialize={}, warm_start=[{"x1": 0}]
48
    )
49
50
    assert 0 in list(opt.results["x1"].values)
51
52
53
def test_attributes_results_5():
54
    opt = RandomSearchOptimizer(search_space)
55
    opt.search(
56
        objective_function, n_iter=1, initialize={}, warm_start=[{"x1": 10}]
57
    )
58
59
    assert 10 in list(opt.results["x1"].values)
60
61
62 View Code Duplication
def test_attributes_results_6():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
    def objective_function(para):
64
        score = -para["x1"] * para["x1"]
65
        return score
66
67
    search_space = {
68
        "x1": np.arange(0, 10, 1),
69
    }
70
71
    opt = RandomSearchOptimizer(search_space)
72
    opt.search(
73
        objective_function, n_iter=20, initialize={"random": 1}, memory=False
74
    )
75
76
    x1_results = list(opt.results["x1"].values)
77
78
    print("\n x1_results \n", x1_results)
79
80
    assert len(set(x1_results)) < len(x1_results)
81
82
83 View Code Duplication
def test_attributes_results_7():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
    def objective_function(para):
85
        score = -para["x1"] * para["x1"]
86
        return score
87
88
    search_space = {
89
        "x1": np.arange(0, 10, 1),
90
    }
91
92
    opt = RandomSearchOptimizer(search_space)
93
    opt.search(
94
        objective_function, n_iter=20, initialize={"random": 1}, memory=True
95
    )
96
97
    x1_results = list(opt.results["x1"].values)
98
99
    print("\n x1_results \n", x1_results)
100
101
    assert len(set(x1_results)) == len(x1_results)
102
103
104
def test_attributes_results_8():
105
    def objective_function(para):
106
        score = -para["x1"] * para["x1"]
107
        return score
108
109
    search_space = {
110
        "x1": np.arange(-10, 11, 1),
111
    }
112
113
    results = pd.DataFrame(np.arange(-10, 10, 1), columns=["x1"])
114
    results["score"] = 0
115
116
    opt = RandomSearchOptimizer(search_space)
117
    opt.search(
118
        objective_function,
119
        n_iter=100,
120
        initialize={},
121
        memory=True,
122
        memory_warm_start=results,
123
    )
124
125
    print("\n opt.results \n", opt.results)
126
127
    x1_results = list(opt.results["x1"].values)
128
129
    assert 10 == x1_results[0]
130
131