1
|
|
|
import copy |
2
|
|
|
import pytest |
3
|
|
|
import numpy as np |
4
|
|
|
import pandas as pd |
5
|
|
|
|
6
|
|
|
from hyperactive import Hyperactive |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
search_space = { |
10
|
|
|
"x1": list(np.arange(0, 100, 1)), |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def _test_func(): |
15
|
|
|
pass |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def _test_func_1(): |
19
|
|
|
pass |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def objective_function_0(opt): |
23
|
|
|
if opt.pass_through["stuff"] != 1: |
24
|
|
|
print("\n pass_through:", opt.pass_through["stuff"]) |
25
|
|
|
assert False |
26
|
|
|
|
27
|
|
|
score = -opt["x1"] * opt["x1"] |
28
|
|
|
return score |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def objective_function_1(opt): |
32
|
|
|
if opt.pass_through["stuff"] != 0.001: |
33
|
|
|
print("\n pass_through:", opt.pass_through["stuff"]) |
34
|
|
|
assert False |
35
|
|
|
|
36
|
|
|
score = -opt["x1"] * opt["x1"] |
37
|
|
|
return score |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def objective_function_2(opt): |
41
|
|
|
if opt.pass_through["stuff"] != [1, 2, 3]: |
42
|
|
|
print("\n pass_through:", opt.pass_through["stuff"]) |
43
|
|
|
assert False |
44
|
|
|
|
45
|
|
|
score = -opt["x1"] * opt["x1"] |
46
|
|
|
return score |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def objective_function_3(opt): |
50
|
|
|
if opt.pass_through["stuff"] != _test_func: |
51
|
|
|
print("\n pass_through:", opt.pass_through["stuff"]) |
52
|
|
|
assert False |
53
|
|
|
|
54
|
|
|
score = -opt["x1"] * opt["x1"] |
55
|
|
|
return score |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
pass_through_0 = {"stuff": 1} |
59
|
|
|
pass_through_1 = {"stuff": 0.001} |
60
|
|
|
pass_through_2 = {"stuff": [1, 2, 3]} |
61
|
|
|
pass_through_3 = {"stuff": _test_func} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
pass_through_setup_0 = (objective_function_0, pass_through_0) |
65
|
|
|
pass_through_setup_1 = (objective_function_1, pass_through_1) |
66
|
|
|
pass_through_setup_2 = (objective_function_2, pass_through_2) |
67
|
|
|
pass_through_setup_3 = (objective_function_3, pass_through_3) |
68
|
|
|
|
69
|
|
|
pass_through_setups = ( |
70
|
|
|
"pass_through_setup", |
71
|
|
|
[ |
72
|
|
|
(pass_through_setup_0), |
73
|
|
|
(pass_through_setup_1), |
74
|
|
|
(pass_through_setup_2), |
75
|
|
|
(pass_through_setup_3), |
76
|
|
|
], |
77
|
|
|
) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
@pytest.mark.parametrize(*pass_through_setups) |
81
|
|
|
def test_pass_through_0(pass_through_setup): |
82
|
|
|
objective_function = pass_through_setup[0] |
83
|
|
|
pass_through = pass_through_setup[1] |
84
|
|
|
|
85
|
|
|
hyper = Hyperactive() |
86
|
|
|
hyper.add_search( |
87
|
|
|
objective_function, |
88
|
|
|
search_space, |
89
|
|
|
n_iter=100, |
90
|
|
|
pass_through=pass_through, |
91
|
|
|
) |
92
|
|
|
hyper.run() |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def objective_function_0(opt): |
96
|
|
|
if opt.nth_iter > 1: |
97
|
|
|
assert opt.pass_through["stuff"] == 2 |
98
|
|
|
opt.pass_through["stuff"] = 2 |
99
|
|
|
|
100
|
|
|
score = -opt["x1"] * opt["x1"] |
101
|
|
|
return score |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def objective_function_1(opt): |
105
|
|
|
if opt.nth_iter > 1: |
106
|
|
|
assert opt.pass_through["stuff"] == 0.002 |
107
|
|
|
opt.pass_through["stuff"] = 0.002 |
108
|
|
|
|
109
|
|
|
score = -opt["x1"] * opt["x1"] |
110
|
|
|
return score |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def objective_function_2(opt): |
114
|
|
|
if opt.nth_iter > 1: |
115
|
|
|
assert 4 in opt.pass_through["stuff"] |
116
|
|
|
opt.pass_through["stuff"].append(4) |
117
|
|
|
|
118
|
|
|
score = -opt["x1"] * opt["x1"] |
119
|
|
|
return score |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def objective_function_3(opt): |
123
|
|
|
if opt.nth_iter > 1: |
124
|
|
|
assert opt.pass_through["stuff"] == _test_func_1 |
125
|
|
|
opt.pass_through["stuff"] = _test_func_1 |
126
|
|
|
|
127
|
|
|
score = -opt["x1"] * opt["x1"] |
128
|
|
|
return score |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
pass_through_setup_0 = (objective_function_0, pass_through_0) |
132
|
|
|
pass_through_setup_1 = (objective_function_1, pass_through_1) |
133
|
|
|
pass_through_setup_2 = (objective_function_2, pass_through_2) |
134
|
|
|
pass_through_setup_3 = (objective_function_3, pass_through_3) |
135
|
|
|
|
136
|
|
|
pass_through_setups = ( |
137
|
|
|
"pass_through_setup", |
138
|
|
|
[ |
139
|
|
|
(pass_through_setup_0), |
140
|
|
|
(pass_through_setup_1), |
141
|
|
|
(pass_through_setup_2), |
142
|
|
|
(pass_through_setup_3), |
143
|
|
|
], |
144
|
|
|
) |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
@pytest.mark.parametrize(*pass_through_setups) |
148
|
|
|
def test_pass_through_1(pass_through_setup): |
149
|
|
|
objective_function = pass_through_setup[0] |
150
|
|
|
pass_through = pass_through_setup[1] |
151
|
|
|
|
152
|
|
|
hyper = Hyperactive() |
153
|
|
|
hyper.add_search( |
154
|
|
|
objective_function, |
155
|
|
|
search_space, |
156
|
|
|
n_iter=100, |
157
|
|
|
pass_through=pass_through, |
158
|
|
|
) |
159
|
|
|
pass_through = copy.deepcopy(pass_through) |
160
|
|
|
|
161
|
|
|
hyper.run() |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
@pytest.mark.parametrize(*pass_through_setups) |
165
|
|
|
def test_pass_through_2(pass_through_setup): |
166
|
|
|
objective_function = pass_through_setup[0] |
167
|
|
|
pass_through = pass_through_setup[1] |
168
|
|
|
|
169
|
|
|
hyper = Hyperactive() |
170
|
|
|
hyper.add_search( |
171
|
|
|
objective_function, |
172
|
|
|
search_space, |
173
|
|
|
n_iter=100, |
174
|
|
|
n_jobs=2, |
175
|
|
|
pass_through=pass_through, |
176
|
|
|
) |
177
|
|
|
pass_through = copy.deepcopy(pass_through) |
178
|
|
|
|
179
|
|
|
hyper.run() |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
@pytest.mark.parametrize(*pass_through_setups) |
183
|
|
|
def test_pass_through_3(pass_through_setup): |
184
|
|
|
objective_function = pass_through_setup[0] |
185
|
|
|
pass_through = pass_through_setup[1] |
186
|
|
|
|
187
|
|
|
hyper = Hyperactive() |
188
|
|
|
hyper.add_search( |
189
|
|
|
objective_function, |
190
|
|
|
search_space, |
191
|
|
|
n_iter=100, |
192
|
|
|
n_jobs=4, |
193
|
|
|
pass_through=pass_through, |
194
|
|
|
) |
195
|
|
|
pass_through = copy.deepcopy(pass_through) |
196
|
|
|
|
197
|
|
|
hyper.run() |
198
|
|
|
|