tests.test_results.test_attributes_results_1()   A
last analyzed

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.search_data, 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.search_data.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.search_data.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.search_data.columns)
42
43
44
def test_attributes_results_4():
45
    opt = RandomSearchOptimizer(search_space, initialize={"warm_start": [{"x1": 0}]})
46
    opt.search(objective_function, n_iter=1)
47
48
    assert 0 in list(opt.search_data["x1"].values)
49
50
51
def test_attributes_results_5():
52
    opt = RandomSearchOptimizer(search_space, initialize={"warm_start": [{"x1": 10}]})
53
    opt.search(objective_function, n_iter=1)
54
55
    assert 10 in list(opt.search_data["x1"].values)
56
57
58
def test_attributes_results_6():
59
    def objective_function(para):
60
        score = -para["x1"] * para["x1"]
61
        return score
62
63
    search_space = {
64
        "x1": np.arange(0, 10, 1),
65
    }
66
67
    opt = RandomSearchOptimizer(search_space, initialize={"random": 1})
68
    opt.search(objective_function, n_iter=20, memory=False)
69
70
    x1_results = list(opt.search_data["x1"].values)
71
72
    print("\n x1_results \n", x1_results)
73
74
    assert len(set(x1_results)) < len(x1_results)
75
76
77
"""
78
def test_attributes_results_7():
79
    def objective_function(para):
80
        score = -para["x1"] * para["x1"]
81
        return score
82
83
    search_space = {
84
        "x1": np.arange(0, 10, 1),
85
    }
86
87
    opt = RandomSearchOptimizer(search_space)
88
    opt.search(
89
        objective_function, n_iter=20, initialize={"random": 1}, memory=True
90
    )
91
92
    x1_results = list(opt.search_data["x1"].values)
93
94
    print("\n x1_results \n", x1_results)
95
96
    assert len(set(x1_results)) == len(x1_results)
97
98
99
def test_attributes_results_8():
100
    def objective_function(para):
101
        score = -para["x1"] * para["x1"]
102
        return score
103
104
    search_space = {
105
        "x1": np.arange(-10, 11, 1),
106
    }
107
108
    results = pd.DataFrame(np.arange(-10, 10, 1), columns=["x1"])
109
    results["score"] = 0
110
111
    opt = RandomSearchOptimizer(search_space)
112
    opt.search(
113
        objective_function,
114
        n_iter=100,
115
        initialize={},
116
        memory=True,
117
        memory_warm_start=results,
118
    )
119
120
    print("\n opt.search_data \n", opt.search_data)
121
122
    x1_results = list(opt.search_data["x1"].values)
123
124
    assert 10 == x1_results[0]
125
"""
126