Passed
Push — master ( 230f26...ac9e53 )
by Simon
01:17
created

tests.test_results.test_attributes_results_7()   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nop 0
dl 19
loc 19
rs 9.8
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
def test_attributes_results_6():
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
"""
84
def test_attributes_results_7():
85
    def objective_function(para):
86
        score = -para["x1"] * para["x1"]
87
        return score
88
89
    search_space = {
90
        "x1": np.arange(0, 10, 1),
91
    }
92
93
    opt = RandomSearchOptimizer(search_space)
94
    opt.search(
95
        objective_function, n_iter=20, initialize={"random": 1}, memory=True
96
    )
97
98
    x1_results = list(opt.results["x1"].values)
99
100
    print("\n x1_results \n", x1_results)
101
102
    assert len(set(x1_results)) == len(x1_results)
103
104
105
def test_attributes_results_8():
106
    def objective_function(para):
107
        score = -para["x1"] * para["x1"]
108
        return score
109
110
    search_space = {
111
        "x1": np.arange(-10, 11, 1),
112
    }
113
114
    results = pd.DataFrame(np.arange(-10, 10, 1), columns=["x1"])
115
    results["score"] = 0
116
117
    opt = RandomSearchOptimizer(search_space)
118
    opt.search(
119
        objective_function,
120
        n_iter=100,
121
        initialize={},
122
        memory=True,
123
        memory_warm_start=results,
124
    )
125
126
    print("\n opt.results \n", opt.results)
127
128
    x1_results = list(opt.results["x1"].values)
129
130
    assert 10 == x1_results[0]
131
"""
132