1
|
|
|
"""Test module for results functionality.""" |
2
|
|
|
|
3
|
|
|
import numpy as np |
4
|
|
|
import pandas as pd |
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from hyperactive import Hyperactive |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def objective_function(opt): |
11
|
|
|
"""Return simple quadratic objective function for results testing.""" |
12
|
|
|
score = -opt["x1"] * opt["x1"] |
13
|
|
|
return score |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
search_space = { |
17
|
|
|
"x1": list(np.arange(0, 100, 1)), |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def test_attributes_results_0(): |
22
|
|
|
"""Test search data returns pandas DataFrame.""" |
23
|
|
|
hyper = Hyperactive() |
24
|
|
|
hyper.add_search(objective_function, search_space, n_iter=100) |
25
|
|
|
hyper.run() |
26
|
|
|
|
27
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_attributes_results_1(): |
31
|
|
|
"""Test search data contains search space columns.""" |
32
|
|
|
hyper = Hyperactive() |
33
|
|
|
hyper.add_search(objective_function, search_space, n_iter=100) |
34
|
|
|
hyper.run() |
35
|
|
|
|
36
|
|
|
assert set(search_space.keys()) < set(hyper.search_data(objective_function).columns) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def test_attributes_results_2(): |
40
|
|
|
"""Test search data contains x1 column.""" |
41
|
|
|
hyper = Hyperactive() |
42
|
|
|
hyper.add_search(objective_function, search_space, n_iter=100) |
43
|
|
|
hyper.run() |
44
|
|
|
|
45
|
|
|
assert "x1" in list(hyper.search_data(objective_function).columns) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def test_attributes_results_3(): |
49
|
|
|
"""Test search data contains score column.""" |
50
|
|
|
hyper = Hyperactive() |
51
|
|
|
hyper.add_search(objective_function, search_space, n_iter=100) |
52
|
|
|
hyper.run() |
53
|
|
|
|
54
|
|
|
assert "score" in list(hyper.search_data(objective_function).columns) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_attributes_results_4(): |
58
|
|
|
"""Test warm start initialization with specific value.""" |
59
|
|
|
hyper = Hyperactive() |
60
|
|
|
hyper.add_search( |
61
|
|
|
objective_function, |
62
|
|
|
search_space, |
63
|
|
|
n_iter=1, |
64
|
|
|
initialize={"warm_start": [{"x1": 0}]}, |
65
|
|
|
) |
66
|
|
|
hyper.run() |
67
|
|
|
|
68
|
|
|
assert 0 in list(hyper.search_data(objective_function)["x1"].values) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_attributes_results_5(): |
72
|
|
|
"""Test warm start initialization with different value.""" |
73
|
|
|
hyper = Hyperactive() |
74
|
|
|
hyper.add_search( |
75
|
|
|
objective_function, |
76
|
|
|
search_space, |
77
|
|
|
n_iter=1, |
78
|
|
|
initialize={"warm_start": [{"x1": 10}]}, |
79
|
|
|
) |
80
|
|
|
hyper.run() |
81
|
|
|
|
82
|
|
|
print( |
83
|
|
|
"\n x1_results \n", |
84
|
|
|
list(hyper.search_data(objective_function)["x1"].values), |
85
|
|
|
) |
86
|
|
|
|
87
|
|
|
assert 10 in list(hyper.search_data(objective_function)["x1"].values) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def test_attributes_results_6(): |
91
|
|
|
"""Test memory disabled allows duplicate search space points.""" |
92
|
|
|
|
93
|
|
|
def objective_function(opt): |
94
|
|
|
score = -opt["x1"] * opt["x1"] |
95
|
|
|
return score |
96
|
|
|
|
97
|
|
|
search_space = { |
98
|
|
|
"x1": list(np.arange(0, 10, 1)), |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
hyper = Hyperactive() |
102
|
|
|
hyper.add_search( |
103
|
|
|
objective_function, |
104
|
|
|
search_space, |
105
|
|
|
n_iter=20, |
106
|
|
|
initialize={"random": 1}, |
107
|
|
|
memory=False, |
108
|
|
|
) |
109
|
|
|
hyper.run() |
110
|
|
|
|
111
|
|
|
x1_results = list(hyper.search_data(objective_function)["x1"].values) |
112
|
|
|
|
113
|
|
|
print("\n x1_results \n", x1_results) |
114
|
|
|
|
115
|
|
|
assert len(set(x1_results)) < len(x1_results) |
116
|
|
|
|
117
|
|
|
|
118
|
|
View Code Duplication |
def test_attributes_results_7(): |
|
|
|
|
119
|
|
|
"""Test search data without times parameter excludes timing columns.""" |
120
|
|
|
|
121
|
|
|
def objective_function(opt): |
122
|
|
|
score = -opt["x1"] * opt["x1"] |
123
|
|
|
return score |
124
|
|
|
|
125
|
|
|
search_space = { |
126
|
|
|
"x1": list(np.arange(0, 10, 1)), |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
hyper = Hyperactive() |
130
|
|
|
hyper.add_search( |
131
|
|
|
objective_function, |
132
|
|
|
search_space, |
133
|
|
|
n_iter=20, |
134
|
|
|
) |
135
|
|
|
hyper.run() |
136
|
|
|
|
137
|
|
|
search_data = hyper.search_data(objective_function) |
138
|
|
|
with pytest.raises(Exception): |
139
|
|
|
search_data["eval_times"] |
140
|
|
|
|
141
|
|
|
|
142
|
|
View Code Duplication |
def test_attributes_results_8(): |
|
|
|
|
143
|
|
|
"""Test search data without times parameter excludes iteration timing.""" |
144
|
|
|
|
145
|
|
|
def objective_function(opt): |
146
|
|
|
score = -opt["x1"] * opt["x1"] |
147
|
|
|
return score |
148
|
|
|
|
149
|
|
|
search_space = { |
150
|
|
|
"x1": list(np.arange(0, 10, 1)), |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
hyper = Hyperactive() |
154
|
|
|
hyper.add_search( |
155
|
|
|
objective_function, |
156
|
|
|
search_space, |
157
|
|
|
n_iter=20, |
158
|
|
|
) |
159
|
|
|
hyper.run() |
160
|
|
|
|
161
|
|
|
search_data = hyper.search_data(objective_function) |
162
|
|
|
with pytest.raises(Exception): |
163
|
|
|
search_data["iter_times"] |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
def test_attributes_results_9(): |
167
|
|
|
"""Test search data with times parameter includes timing columns.""" |
168
|
|
|
|
169
|
|
|
def objective_function(opt): |
170
|
|
|
score = -opt["x1"] * opt["x1"] |
171
|
|
|
return score |
172
|
|
|
|
173
|
|
|
search_space = { |
174
|
|
|
"x1": list(np.arange(0, 10, 1)), |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
hyper = Hyperactive() |
178
|
|
|
hyper.add_search( |
179
|
|
|
objective_function, |
180
|
|
|
search_space, |
181
|
|
|
n_iter=20, |
182
|
|
|
) |
183
|
|
|
hyper.run() |
184
|
|
|
|
185
|
|
|
search_data = hyper.search_data(objective_function, times=True) |
186
|
|
|
search_data["iter_times"] |
187
|
|
|
search_data["eval_times"] |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
""" |
191
|
|
|
def test_attributes_results_7(): |
192
|
|
|
def objective_function(para): |
193
|
|
|
score = -para["x1"] * para["x1"] |
194
|
|
|
return score |
195
|
|
|
|
196
|
|
|
search_space = { |
197
|
|
|
"x1": np.arange(0, 10, 1), |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
opt = RandomSearchOptimizer(search_space) |
201
|
|
|
opt.search( |
202
|
|
|
objective_function, n_iter=20, initialize={"random": 1}, memory=True |
203
|
|
|
) |
204
|
|
|
|
205
|
|
|
x1_results = list(opt.results["x1"].values) |
206
|
|
|
|
207
|
|
|
print("\n x1_results \n", x1_results) |
208
|
|
|
|
209
|
|
|
assert len(set(x1_results)) == len(x1_results) |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
def test_attributes_results_8(): |
213
|
|
|
def objective_function(para): |
214
|
|
|
score = -para["x1"] * para["x1"] |
215
|
|
|
return score |
216
|
|
|
|
217
|
|
|
search_space = { |
218
|
|
|
"x1": np.arange(-10, 11, 1), |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
results = pd.DataFrame(np.arange(-10, 10, 1), columns=["x1"]) |
222
|
|
|
results["score"] = 0 |
223
|
|
|
|
224
|
|
|
opt = RandomSearchOptimizer(search_space) |
225
|
|
|
opt.search( |
226
|
|
|
objective_function, |
227
|
|
|
n_iter=100, |
228
|
|
|
initialize={}, |
229
|
|
|
memory=True, |
230
|
|
|
memory_warm_start=results, |
231
|
|
|
) |
232
|
|
|
|
233
|
|
|
print("\n opt.results \n", opt.results) |
234
|
|
|
|
235
|
|
|
x1_results = list(opt.results["x1"].values) |
236
|
|
|
|
237
|
|
|
assert 10 == x1_results[0] |
238
|
|
|
""" |
239
|
|
|
|