|
1
|
|
|
import os |
|
2
|
|
|
import inspect |
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
import pandas as pd |
|
7
|
|
|
|
|
8
|
|
|
from hyperactive import Hyperactive, LongTermMemory |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def func1(): |
|
12
|
|
|
pass |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def func2(): |
|
16
|
|
|
pass |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def func3(): |
|
20
|
|
|
pass |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def objective_function(opt): |
|
24
|
|
|
score = -opt["x1"] * opt["x1"] |
|
25
|
|
|
return score |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class class1: |
|
29
|
|
|
pass |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
class class2: |
|
33
|
|
|
pass |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class class3: |
|
37
|
|
|
pass |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class class1_: |
|
41
|
|
|
def __init__(self): |
|
42
|
|
|
pass |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
class class2_: |
|
46
|
|
|
def __init__(self): |
|
47
|
|
|
pass |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
class class3_: |
|
51
|
|
|
def __init__(self): |
|
52
|
|
|
pass |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
search_space_int = { |
|
56
|
|
|
"x1": list(range(0, 3, 1)), |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
search_space_float = { |
|
60
|
|
|
"x1": list(np.arange(0, 0.003, 0.001)), |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
search_space_str = { |
|
64
|
|
|
"x1": list(range(0, 100, 1)), |
|
65
|
|
|
"str1": ["0", "1", "2"], |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
search_space_func = { |
|
69
|
|
|
"x1": list(range(0, 100, 1)), |
|
70
|
|
|
"func1": [func1, func2, func3], |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
search_space_class = { |
|
75
|
|
|
"x1": list(range(0, 100, 1)), |
|
76
|
|
|
"class1": [class1, class2, class3], |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
search_space_obj = { |
|
81
|
|
|
"x1": list(range(0, 100, 1)), |
|
82
|
|
|
"class1": [class1_(), class2_(), class3_()], |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
search_space_lists = { |
|
86
|
|
|
"x1": list(range(0, 100, 1)), |
|
87
|
|
|
"list1": [[1, 1, 1], [1, 2, 1], [1, 1, 2]], |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
search_space_para = ( |
|
91
|
|
|
"search_space", |
|
92
|
|
|
[ |
|
93
|
|
|
(search_space_int), |
|
94
|
|
|
(search_space_float), |
|
95
|
|
|
(search_space_str), |
|
96
|
|
|
(search_space_func), |
|
97
|
|
|
(search_space_class), |
|
98
|
|
|
(search_space_obj), |
|
99
|
|
|
(search_space_lists), |
|
100
|
|
|
], |
|
101
|
|
|
) |
|
102
|
|
|
|
|
103
|
|
|
path_para = ( |
|
104
|
|
|
"path", |
|
105
|
|
|
[("./"), (None),], |
|
106
|
|
|
) |
|
107
|
|
|
|
|
108
|
|
|
""" |
|
109
|
|
|
@pytest.mark.parametrize(*path_para) |
|
110
|
|
|
@pytest.mark.parametrize(*search_space_para) |
|
111
|
|
|
def test_ltm_0(search_space, path): |
|
112
|
|
|
model_name = "test_ltm_0" |
|
113
|
|
|
|
|
114
|
|
|
def objective_function(opt): |
|
115
|
|
|
score = -opt["x1"] * opt["x1"] |
|
116
|
|
|
return score |
|
117
|
|
|
|
|
118
|
|
|
hyper = Hyperactive() |
|
119
|
|
|
hyper.add_search(objective_function, search_space, n_iter=25) |
|
120
|
|
|
hyper.run() |
|
121
|
|
|
|
|
122
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
123
|
|
|
results1 = hyper.results(objective_function) |
|
124
|
|
|
memory.save(results1, objective_function) |
|
125
|
|
|
|
|
126
|
|
|
results2 = memory.load() |
|
127
|
|
|
remove_file(path) |
|
128
|
|
|
|
|
129
|
|
|
assert results1.equals(results2) |
|
130
|
|
|
""" |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
def test_ltm_10(): |
|
134
|
|
|
path = None |
|
135
|
|
|
model_name = "test_ltm_0" |
|
136
|
|
|
|
|
137
|
|
|
def objective_function(opt): |
|
138
|
|
|
score = -opt["x1"] * opt["x1"] |
|
139
|
|
|
return score |
|
140
|
|
|
|
|
141
|
|
|
search_space = { |
|
142
|
|
|
"x1": list(range(0, 3, 1)), |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
146
|
|
|
|
|
147
|
|
|
hyper = Hyperactive() |
|
148
|
|
|
hyper.add_search( |
|
149
|
|
|
objective_function, search_space, n_iter=25, long_term_memory=memory |
|
150
|
|
|
) |
|
151
|
|
|
hyper.run() |
|
152
|
|
|
|
|
153
|
|
|
hyper = Hyperactive() |
|
154
|
|
|
hyper.add_search( |
|
155
|
|
|
objective_function, search_space, n_iter=25, long_term_memory=memory |
|
156
|
|
|
) |
|
157
|
|
|
hyper.run() |
|
158
|
|
|
|
|
159
|
|
|
memory.remove_model_data() |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
""" |
|
163
|
|
|
@pytest.mark.parametrize(*path_para) |
|
164
|
|
|
@pytest.mark.parametrize(*search_space_para) |
|
165
|
|
|
def test_ltm_1(search_space, path): |
|
166
|
|
|
model_name = "test_ltm_1" |
|
167
|
|
|
|
|
168
|
|
|
def objective_function(opt): |
|
169
|
|
|
score = -opt["x1"] * opt["x1"] |
|
170
|
|
|
return score |
|
171
|
|
|
|
|
172
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
173
|
|
|
|
|
174
|
|
|
hyper0 = Hyperactive() |
|
175
|
|
|
hyper0.add_search( |
|
176
|
|
|
objective_function, search_space, n_iter=25, long_term_memory=memory |
|
177
|
|
|
) |
|
178
|
|
|
hyper0.run() |
|
179
|
|
|
|
|
180
|
|
|
hyper1 = Hyperactive() |
|
181
|
|
|
hyper1.add_search( |
|
182
|
|
|
objective_function, search_space, n_iter=25, long_term_memory=memory |
|
183
|
|
|
) |
|
184
|
|
|
hyper1.run() |
|
185
|
|
|
|
|
186
|
|
|
remove_file() |
|
187
|
|
|
""" |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
View Code Duplication |
def test_ltm_int(): |
|
|
|
|
|
|
191
|
|
|
path = "./" |
|
192
|
|
|
model_name = "test_ltm_int" |
|
193
|
|
|
array = np.arange(3 * 10).reshape(10, 3) |
|
194
|
|
|
results1 = pd.DataFrame(array, columns=["x1", "x2", "x3"]) |
|
195
|
|
|
|
|
196
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
197
|
|
|
memory.save(results1, objective_function) |
|
198
|
|
|
|
|
199
|
|
|
results2 = memory.load() |
|
200
|
|
|
|
|
201
|
|
|
memory.remove_model_data() |
|
202
|
|
|
|
|
203
|
|
|
assert results1.equals(results2) |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
View Code Duplication |
def test_ltm_float(): |
|
|
|
|
|
|
207
|
|
|
path = "./" |
|
208
|
|
|
model_name = "test_ltm_float" |
|
209
|
|
|
array = np.arange(3 * 10).reshape(10, 3) |
|
210
|
|
|
array = array / 1000 |
|
211
|
|
|
results1 = pd.DataFrame(array, columns=["x1", "x2", "x3"]) |
|
212
|
|
|
|
|
213
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
214
|
|
|
memory.save(results1, objective_function) |
|
215
|
|
|
|
|
216
|
|
|
results2 = memory.load() |
|
217
|
|
|
|
|
218
|
|
|
memory.remove_model_data() |
|
219
|
|
|
|
|
220
|
|
|
assert results1.equals(results2) |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
def test_ltm_str(): |
|
224
|
|
|
path = "./" |
|
225
|
|
|
model_name = "test_ltm_str" |
|
226
|
|
|
array = ["str1", "str2", "str3"] |
|
227
|
|
|
results1 = pd.DataFrame( |
|
228
|
|
|
[array, array, array, array, array], columns=["x1", "x2", "x3"] |
|
229
|
|
|
) |
|
230
|
|
|
|
|
231
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
232
|
|
|
memory.save(results1, objective_function) |
|
233
|
|
|
|
|
234
|
|
|
results2 = memory.load() |
|
235
|
|
|
|
|
236
|
|
|
memory.remove_model_data() |
|
237
|
|
|
|
|
238
|
|
|
assert results1.equals(results2) |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
def func1(): |
|
242
|
|
|
pass |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
def func2(): |
|
246
|
|
|
pass |
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
def func3(): |
|
250
|
|
|
pass |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
def test_ltm_func(): |
|
254
|
|
|
path = "./" |
|
255
|
|
|
model_name = "test_ltm_func" |
|
256
|
|
|
|
|
257
|
|
|
array = [func1, func2, func3] |
|
258
|
|
|
results1 = pd.DataFrame( |
|
259
|
|
|
[array, array, array, array, array], columns=["x1", "x2", "x3"] |
|
260
|
|
|
) |
|
261
|
|
|
|
|
262
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
263
|
|
|
memory.save(results1, objective_function) |
|
264
|
|
|
|
|
265
|
|
|
results2 = memory.load() |
|
266
|
|
|
|
|
267
|
|
|
memory.remove_model_data() |
|
268
|
|
|
|
|
269
|
|
|
func_str_list = [] |
|
270
|
|
|
for func_ in array: |
|
271
|
|
|
func_str_list.append(inspect.getsource(func_)) |
|
272
|
|
|
|
|
273
|
|
|
for func_ in list(results2.values.flatten()): |
|
274
|
|
|
func_str_ = inspect.getsource(func_) |
|
275
|
|
|
if func_str_ not in func_str_list: |
|
276
|
|
|
assert False |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
class class1: |
|
280
|
|
|
name = "class1" |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
class class2: |
|
284
|
|
|
name = "class2" |
|
285
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
class class3: |
|
288
|
|
|
name = "class3" |
|
289
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
def test_ltm_class(): |
|
292
|
|
|
path = "./" |
|
293
|
|
|
model_name = "test_ltm_class" |
|
294
|
|
|
|
|
295
|
|
|
array = [class1, class2, class3] |
|
296
|
|
|
results1 = pd.DataFrame( |
|
297
|
|
|
[array, array, array, array, array], columns=["x1", "x2", "x3"] |
|
298
|
|
|
) |
|
299
|
|
|
|
|
300
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
301
|
|
|
memory.save(results1, objective_function) |
|
302
|
|
|
|
|
303
|
|
|
results2 = memory.load() |
|
304
|
|
|
|
|
305
|
|
|
memory.remove_model_data() |
|
306
|
|
|
|
|
307
|
|
|
for class_1 in list(results2.values.flatten()): |
|
308
|
|
|
assert_ = False |
|
309
|
|
|
for class_2 in array: |
|
310
|
|
|
if class_1.name == class_2.name: |
|
311
|
|
|
assert_ = True |
|
312
|
|
|
break |
|
313
|
|
|
|
|
314
|
|
|
assert assert_ |
|
315
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
class class1_: |
|
318
|
|
|
def __init__(self): |
|
319
|
|
|
self.name = "class1_" |
|
320
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
class class2_: |
|
323
|
|
|
def __init__(self): |
|
324
|
|
|
self.name = "class2_" |
|
325
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
class class3_: |
|
328
|
|
|
def __init__(self): |
|
329
|
|
|
self.name = "class3_" |
|
330
|
|
|
|
|
331
|
|
|
|
|
332
|
|
|
def test_ltm_obj(): |
|
333
|
|
|
path = "./" |
|
334
|
|
|
model_name = "test_ltm_obj" |
|
335
|
|
|
|
|
336
|
|
|
array = [class1_(), class2_(), class3_()] |
|
337
|
|
|
results1 = pd.DataFrame( |
|
338
|
|
|
[array, array, array, array, array], columns=["x1", "x2", "x3"] |
|
339
|
|
|
) |
|
340
|
|
|
|
|
341
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
342
|
|
|
memory.save(results1, objective_function) |
|
343
|
|
|
|
|
344
|
|
|
results2 = memory.load() |
|
345
|
|
|
|
|
346
|
|
|
memory.remove_model_data() |
|
347
|
|
|
|
|
348
|
|
|
for obj_1 in list(results2.values.flatten()): |
|
349
|
|
|
assert_ = False |
|
350
|
|
|
for obj_2 in array: |
|
351
|
|
|
if obj_1.name == obj_2.name: |
|
352
|
|
|
assert_ = True |
|
353
|
|
|
break |
|
354
|
|
|
|
|
355
|
|
|
assert assert_ |
|
356
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
def test_ltm_list(): |
|
359
|
|
|
path = "./" |
|
360
|
|
|
model_name = "test_ltm_list" |
|
361
|
|
|
|
|
362
|
|
|
dict_ = {"x1": [[1, 1, 1], [1, 2, 1], [1, 1, 2]]} |
|
363
|
|
|
results1 = pd.DataFrame(dict_) |
|
364
|
|
|
print("\n results1 \n", results1) |
|
365
|
|
|
|
|
366
|
|
|
memory = LongTermMemory(model_name, path=path) |
|
367
|
|
|
memory.save(results1, objective_function) |
|
368
|
|
|
|
|
369
|
|
|
results2 = memory.load() |
|
370
|
|
|
|
|
371
|
|
|
memory.remove_model_data() |
|
372
|
|
|
print("\n results2 \n", results2) |
|
373
|
|
|
|
|
374
|
|
|
for list_1 in list(results2.values.flatten()): |
|
375
|
|
|
assert_ = False |
|
376
|
|
|
for list_2 in list(dict_.values())[0]: |
|
377
|
|
|
print("\n list_1 ", list_1) |
|
378
|
|
|
print("list_2 ", list_2) |
|
379
|
|
|
print(list_1 == list_2) |
|
380
|
|
|
|
|
381
|
|
|
if list_1 == list_2: |
|
382
|
|
|
assert_ = True |
|
383
|
|
|
break |
|
384
|
|
|
|
|
385
|
|
|
assert assert_ |
|
386
|
|
|
|
|
387
|
|
|
|