1
|
|
|
import pytest |
2
|
|
|
import numbers |
3
|
|
|
import numpy as np |
4
|
|
|
import pandas as pd |
5
|
|
|
|
6
|
|
|
from hyperactive.optimizers import HillClimbingOptimizer |
7
|
|
|
from hyperactive.experiment import BaseExperiment |
8
|
|
|
from hyperactive.search_config import SearchConfig |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Experiment(BaseExperiment): |
12
|
|
|
def objective_function(self, opt): |
13
|
|
|
score = -opt["x1"] * opt["x1"] |
14
|
|
|
return score |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class Experiment1(BaseExperiment): |
18
|
|
|
def objective_function(self, opt): |
19
|
|
|
score = -opt["x1"] * opt["x1"] |
20
|
|
|
return score |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
experiment = Experiment() |
24
|
|
|
experiment1 = Experiment1() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
search_config = SearchConfig( |
28
|
|
|
x1=list(np.arange(0, 100, 1)), |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def test_attributes_best_score_objective_function_0(): |
33
|
|
|
hyper = HillClimbingOptimizer() |
34
|
|
|
hyper.add_search( |
35
|
|
|
experiment, |
36
|
|
|
search_config, |
37
|
|
|
n_iter=15, |
38
|
|
|
) |
39
|
|
|
hyper.run() |
40
|
|
|
|
41
|
|
|
assert isinstance(hyper.best_score(experiment), numbers.Number) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_attributes_best_score_objective_function_1(): |
45
|
|
|
hyper = HillClimbingOptimizer() |
46
|
|
|
hyper.add_search( |
47
|
|
|
experiment, |
48
|
|
|
search_config, |
49
|
|
|
n_iter=15, |
50
|
|
|
) |
51
|
|
|
hyper.add_search( |
52
|
|
|
experiment1, |
53
|
|
|
search_config, |
54
|
|
|
n_iter=15, |
55
|
|
|
) |
56
|
|
|
hyper.run() |
57
|
|
|
|
58
|
|
|
assert isinstance(hyper.best_score(experiment), numbers.Number) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
""" |
62
|
|
|
def test_attributes_best_score_search_id_0(): |
63
|
|
|
hyper = HillClimbingOptimizer() |
64
|
|
|
hyper.add_search( |
65
|
|
|
experiment, |
66
|
|
|
search_config, |
67
|
|
|
search_id="1", |
68
|
|
|
n_iter=15, |
69
|
|
|
) |
70
|
|
|
hyper.run() |
71
|
|
|
|
72
|
|
|
assert isinstance(hyper.best_score(experiment), numbers.Number) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_attributes_best_score_search_id_1(): |
76
|
|
|
hyper = HillClimbingOptimizer() |
77
|
|
|
hyper.add_search( |
78
|
|
|
experiment, |
79
|
|
|
search_config, |
80
|
|
|
search_id="1", |
81
|
|
|
n_iter=15, |
82
|
|
|
) |
83
|
|
|
hyper.add_search( |
84
|
|
|
experiment1, |
85
|
|
|
search_config, |
86
|
|
|
search_id="2", |
87
|
|
|
n_iter=15, |
88
|
|
|
) |
89
|
|
|
hyper.run() |
90
|
|
|
|
91
|
|
|
assert isinstance(hyper.best_score(experiment), numbers.Number) |
92
|
|
|
""" |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_attributes_best_para_objective_function_0(): |
96
|
|
|
hyper = HillClimbingOptimizer() |
97
|
|
|
hyper.add_search( |
98
|
|
|
experiment, |
99
|
|
|
search_config, |
100
|
|
|
n_iter=15, |
101
|
|
|
) |
102
|
|
|
hyper.run() |
103
|
|
|
|
104
|
|
|
assert isinstance(hyper.best_para(experiment), dict) |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def test_attributes_best_para_objective_function_1(): |
108
|
|
|
hyper = HillClimbingOptimizer() |
109
|
|
|
hyper.add_search( |
110
|
|
|
experiment, |
111
|
|
|
search_config, |
112
|
|
|
n_iter=15, |
113
|
|
|
) |
114
|
|
|
hyper.add_search( |
115
|
|
|
experiment1, |
116
|
|
|
search_config, |
117
|
|
|
n_iter=15, |
118
|
|
|
) |
119
|
|
|
hyper.run() |
120
|
|
|
|
121
|
|
|
assert isinstance(hyper.best_para(experiment), dict) |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
""" |
125
|
|
|
def test_attributes_best_para_search_id_0(): |
126
|
|
|
hyper = HillClimbingOptimizer() |
127
|
|
|
hyper.add_search( |
128
|
|
|
experiment, |
129
|
|
|
search_config, |
130
|
|
|
search_id="1", |
131
|
|
|
n_iter=15, |
132
|
|
|
) |
133
|
|
|
hyper.run() |
134
|
|
|
|
135
|
|
|
assert isinstance(hyper.best_para("1"), dict) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def test_attributes_best_para_search_id_1(): |
139
|
|
|
hyper = HillClimbingOptimizer() |
140
|
|
|
hyper.add_search( |
141
|
|
|
experiment, |
142
|
|
|
search_config, |
143
|
|
|
search_id="1", |
144
|
|
|
n_iter=15, |
145
|
|
|
) |
146
|
|
|
hyper.add_search( |
147
|
|
|
experiment1, |
148
|
|
|
search_config, |
149
|
|
|
search_id="2", |
150
|
|
|
n_iter=15, |
151
|
|
|
) |
152
|
|
|
hyper.run() |
153
|
|
|
|
154
|
|
|
assert isinstance(hyper.best_para("1"), dict) |
155
|
|
|
""" |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
def test_attributes_results_objective_function_0(): |
159
|
|
|
hyper = HillClimbingOptimizer() |
160
|
|
|
hyper.add_search( |
161
|
|
|
experiment, |
162
|
|
|
search_config, |
163
|
|
|
n_iter=15, |
164
|
|
|
) |
165
|
|
|
hyper.run() |
166
|
|
|
|
167
|
|
|
assert isinstance(hyper.search_data(experiment), pd.DataFrame) |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
def test_attributes_results_objective_function_1(): |
171
|
|
|
hyper = HillClimbingOptimizer() |
172
|
|
|
hyper.add_search( |
173
|
|
|
experiment, |
174
|
|
|
search_config, |
175
|
|
|
n_iter=15, |
176
|
|
|
) |
177
|
|
|
hyper.add_search( |
178
|
|
|
experiment1, |
179
|
|
|
search_config, |
180
|
|
|
n_iter=15, |
181
|
|
|
) |
182
|
|
|
hyper.run() |
183
|
|
|
|
184
|
|
|
assert isinstance(hyper.search_data(experiment), pd.DataFrame) |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
""" |
188
|
|
|
def test_attributes_results_search_id_0(): |
189
|
|
|
hyper = HillClimbingOptimizer() |
190
|
|
|
hyper.add_search( |
191
|
|
|
experiment, |
192
|
|
|
search_config, |
193
|
|
|
search_id="1", |
194
|
|
|
n_iter=15, |
195
|
|
|
) |
196
|
|
|
hyper.run() |
197
|
|
|
|
198
|
|
|
assert isinstance(hyper.search_data("1"), pd.DataFrame) |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
def test_attributes_results_search_id_1(): |
202
|
|
|
hyper = HillClimbingOptimizer() |
203
|
|
|
hyper.add_search( |
204
|
|
|
experiment, |
205
|
|
|
search_config, |
206
|
|
|
search_id="1", |
207
|
|
|
n_iter=15, |
208
|
|
|
) |
209
|
|
|
hyper.add_search( |
210
|
|
|
experiment1, |
211
|
|
|
search_config, |
212
|
|
|
search_id="2", |
213
|
|
|
n_iter=15, |
214
|
|
|
) |
215
|
|
|
hyper.run() |
216
|
|
|
|
217
|
|
|
assert isinstance(hyper.search_data("1"), pd.DataFrame) |
218
|
|
|
""" |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
def test_attributes_result_errors_0(): |
222
|
|
|
with pytest.raises(ValueError): |
223
|
|
|
hyper = HillClimbingOptimizer() |
224
|
|
|
hyper.add_search(experiment, search_config, n_iter=15) |
225
|
|
|
hyper.run() |
226
|
|
|
|
227
|
|
|
hyper.best_para(experiment1) |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
def test_attributes_result_errors_1(): |
231
|
|
|
with pytest.raises(ValueError): |
232
|
|
|
hyper = HillClimbingOptimizer() |
233
|
|
|
hyper.add_search(experiment, search_config, n_iter=15) |
234
|
|
|
hyper.run() |
235
|
|
|
|
236
|
|
|
hyper.best_score(experiment1) |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
def test_attributes_result_errors_2(): |
240
|
|
|
with pytest.raises(ValueError): |
241
|
|
|
hyper = HillClimbingOptimizer() |
242
|
|
|
hyper.add_search(experiment, search_config, n_iter=15) |
243
|
|
|
hyper.run() |
244
|
|
|
|
245
|
|
|
hyper.search_data(experiment1) |
246
|
|
|
|