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