1
|
|
|
import sys, pytest |
2
|
|
|
import numpy as np |
3
|
|
|
import pandas as pd |
4
|
|
|
|
5
|
|
|
from hyperactive import Hyperactive |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
if sys.platform.startswith("win"): |
9
|
|
|
pytest.skip("skip these tests for windows", allow_module_level=True) |
10
|
|
|
|
11
|
|
|
|
12
|
|
View Code Duplication |
def test_search_space_0(): |
|
|
|
|
13
|
|
|
def objective_function(opt): |
14
|
|
|
score = -opt["x1"] * opt["x1"] |
15
|
|
|
return score |
16
|
|
|
|
17
|
|
|
search_space = { |
18
|
|
|
"x1": list(range(0, 3, 1)), |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
hyper = Hyperactive() |
22
|
|
|
hyper.add_search( |
23
|
|
|
objective_function, |
24
|
|
|
search_space, |
25
|
|
|
n_iter=15, |
26
|
|
|
) |
27
|
|
|
hyper.run() |
28
|
|
|
|
29
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
30
|
|
|
assert hyper.best_para(objective_function)["x1"] in search_space["x1"] |
31
|
|
|
|
32
|
|
|
|
33
|
|
View Code Duplication |
def test_search_space_1(): |
|
|
|
|
34
|
|
|
def objective_function(opt): |
35
|
|
|
score = -opt["x1"] * opt["x1"] |
36
|
|
|
return score |
37
|
|
|
|
38
|
|
|
search_space = { |
39
|
|
|
"x1": list(np.arange(0, 0.003, 0.001)), |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
hyper = Hyperactive() |
43
|
|
|
hyper.add_search( |
44
|
|
|
objective_function, |
45
|
|
|
search_space, |
46
|
|
|
n_iter=15, |
47
|
|
|
) |
48
|
|
|
hyper.run() |
49
|
|
|
|
50
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
51
|
|
|
assert hyper.best_para(objective_function)["x1"] in search_space["x1"] |
52
|
|
|
|
53
|
|
|
|
54
|
|
View Code Duplication |
def test_search_space_2(): |
|
|
|
|
55
|
|
|
def objective_function(opt): |
56
|
|
|
score = -opt["x1"] * opt["x1"] |
57
|
|
|
return score |
58
|
|
|
|
59
|
|
|
search_space = { |
60
|
|
|
"x1": list(range(0, 100, 1)), |
61
|
|
|
"str1": ["0", "1", "2"], |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
hyper = Hyperactive() |
65
|
|
|
hyper.add_search( |
66
|
|
|
objective_function, |
67
|
|
|
search_space, |
68
|
|
|
n_iter=15, |
69
|
|
|
) |
70
|
|
|
hyper.run() |
71
|
|
|
|
72
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
73
|
|
|
assert hyper.best_para(objective_function)["str1"] in search_space["str1"] |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_search_space_3(): |
77
|
|
|
def func1(): |
78
|
|
|
pass |
79
|
|
|
|
80
|
|
|
def func2(): |
81
|
|
|
pass |
82
|
|
|
|
83
|
|
|
def func3(): |
84
|
|
|
pass |
85
|
|
|
|
86
|
|
|
def objective_function(opt): |
87
|
|
|
score = -opt["x1"] * opt["x1"] |
88
|
|
|
return score |
89
|
|
|
|
90
|
|
|
search_space = { |
91
|
|
|
"x1": list(range(0, 100, 1)), |
92
|
|
|
"func1": [func1, func2, func3], |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
hyper = Hyperactive() |
96
|
|
|
hyper.add_search( |
97
|
|
|
objective_function, |
98
|
|
|
search_space, |
99
|
|
|
n_iter=15, |
100
|
|
|
) |
101
|
|
|
hyper.run() |
102
|
|
|
|
103
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
104
|
|
|
assert hyper.best_para(objective_function)["func1"] in search_space["func1"] |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def test_search_space_4(): |
108
|
|
|
class class1: |
109
|
|
|
pass |
110
|
|
|
|
111
|
|
|
class class2: |
112
|
|
|
pass |
113
|
|
|
|
114
|
|
|
class class3: |
115
|
|
|
pass |
116
|
|
|
|
117
|
|
|
def objective_function(opt): |
118
|
|
|
score = -opt["x1"] * opt["x1"] |
119
|
|
|
return score |
120
|
|
|
|
121
|
|
|
search_space = { |
122
|
|
|
"x1": list(range(0, 100, 1)), |
123
|
|
|
"class1": [class1, class2, class3], |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
hyper = Hyperactive() |
127
|
|
|
hyper.add_search( |
128
|
|
|
objective_function, |
129
|
|
|
search_space, |
130
|
|
|
n_iter=15, |
131
|
|
|
) |
132
|
|
|
hyper.run() |
133
|
|
|
|
134
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
135
|
|
|
assert hyper.best_para(objective_function)["class1"] in search_space["class1"] |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def test_search_space_5(): |
139
|
|
|
class class1: |
140
|
|
|
def __init__(self): |
141
|
|
|
pass |
142
|
|
|
|
143
|
|
|
class class2: |
144
|
|
|
def __init__(self): |
145
|
|
|
pass |
146
|
|
|
|
147
|
|
|
class class3: |
148
|
|
|
def __init__(self): |
149
|
|
|
pass |
150
|
|
|
|
151
|
|
|
def class_f1(): |
152
|
|
|
return class1 |
153
|
|
|
|
154
|
|
|
def class_f2(): |
155
|
|
|
return class2 |
156
|
|
|
|
157
|
|
|
def class_f3(): |
158
|
|
|
return class3 |
159
|
|
|
|
160
|
|
|
def objective_function(opt): |
161
|
|
|
score = -opt["x1"] * opt["x1"] |
162
|
|
|
return score |
163
|
|
|
|
164
|
|
|
search_space = { |
165
|
|
|
"x1": list(range(0, 100, 1)), |
166
|
|
|
"class1": [class_f1, class_f2, class_f3], |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
hyper = Hyperactive() |
170
|
|
|
hyper.add_search( |
171
|
|
|
objective_function, |
172
|
|
|
search_space, |
173
|
|
|
n_iter=15, |
174
|
|
|
) |
175
|
|
|
hyper.run() |
176
|
|
|
|
177
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
178
|
|
|
assert hyper.best_para(objective_function)["class1"] in search_space["class1"] |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
def test_search_space_6(): |
182
|
|
|
def objective_function(opt): |
183
|
|
|
score = -opt["x1"] * opt["x1"] |
184
|
|
|
return score |
185
|
|
|
|
186
|
|
|
def list_f1(): |
187
|
|
|
return [0, 1] |
188
|
|
|
|
189
|
|
|
def list_f2(): |
190
|
|
|
return [1, 0] |
191
|
|
|
|
192
|
|
|
search_space = { |
193
|
|
|
"x1": list(range(0, 100, 1)), |
194
|
|
|
"list1": [list_f1, list_f2], |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
hyper = Hyperactive() |
198
|
|
|
hyper.add_search( |
199
|
|
|
objective_function, |
200
|
|
|
search_space, |
201
|
|
|
n_iter=15, |
202
|
|
|
) |
203
|
|
|
hyper.run() |
204
|
|
|
|
205
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
206
|
|
|
assert hyper.best_para(objective_function)["list1"] in search_space["list1"] |
207
|
|
|
|