|
1
|
|
|
# ruff: noqa: D100, D103 |
|
2
|
|
|
import sys |
|
3
|
|
|
|
|
4
|
|
|
import numpy as np |
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
from hyperactive import ( |
|
8
|
|
|
Hyperactive, |
|
9
|
|
|
) |
|
10
|
|
|
from hyperactive.optimizers import ( |
|
11
|
|
|
BayesianOptimizer, |
|
12
|
|
|
ForestOptimizer, |
|
13
|
|
|
TreeStructuredParzenEstimators, |
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
if sys.platform.startswith("win"): |
|
17
|
|
|
pytest.skip("skip these tests for windows", allow_module_level=True) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def _func1(): |
|
21
|
|
|
pass |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def _func2(): |
|
25
|
|
|
pass |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class _class1: |
|
29
|
|
|
def __init__(self): |
|
30
|
|
|
pass |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class _class2: |
|
34
|
|
|
def __init__(self): |
|
35
|
|
|
pass |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def _class_f1(): |
|
39
|
|
|
return _class1 |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def _class_f2(): |
|
43
|
|
|
return _class2 |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def _numpy_f1(): |
|
47
|
|
|
return np.array([0, 1]) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def _numpy_f2(): |
|
51
|
|
|
return np.array([1, 0]) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
search_space = { |
|
55
|
|
|
"x0": list(range(-3, 3)), |
|
56
|
|
|
"x1": list(np.arange(-1, 1, 0.001)), |
|
57
|
|
|
"string0": ["str0", "str1"], |
|
58
|
|
|
"function0": [_func1, _func2], |
|
59
|
|
|
"class0": [_class_f1, _class_f2], |
|
60
|
|
|
"numpy0": [_numpy_f1, _numpy_f2], |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def _objective_function(opt): |
|
65
|
|
|
score = -opt["x1"] * opt["x1"] |
|
66
|
|
|
return score |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
smbo_opts = [ |
|
70
|
|
|
BayesianOptimizer, |
|
71
|
|
|
TreeStructuredParzenEstimators, |
|
72
|
|
|
ForestOptimizer, |
|
73
|
|
|
] |
|
74
|
|
|
|
|
75
|
|
|
initialize = {"random": 1} |
|
76
|
|
|
n_iter = 3 |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
@pytest.mark.parametrize("smbo_opt", smbo_opts) |
|
80
|
|
|
def test_warm_start_smbo_0(smbo_opt): |
|
81
|
|
|
hyper0 = Hyperactive() |
|
82
|
|
|
hyper0.add_search(_objective_function, search_space, n_iter=n_iter) |
|
83
|
|
|
hyper0.run() |
|
84
|
|
|
|
|
85
|
|
|
search_data0 = hyper0.search_data(_objective_function) |
|
86
|
|
|
smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) |
|
87
|
|
|
|
|
88
|
|
|
hyper1 = Hyperactive() |
|
89
|
|
|
hyper1.add_search( |
|
90
|
|
|
_objective_function, |
|
91
|
|
|
search_space, |
|
92
|
|
|
n_iter=n_iter, |
|
93
|
|
|
optimizer=smbo_opt_, |
|
94
|
|
|
initialize=initialize, |
|
95
|
|
|
) |
|
96
|
|
|
hyper1.run() |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
@pytest.mark.parametrize("smbo_opt", smbo_opts) |
|
|
|
|
|
|
100
|
|
|
def test_warm_start_smbo_1(smbo_opt): |
|
101
|
|
|
hyper0 = Hyperactive(distribution="pathos") |
|
102
|
|
|
hyper0.add_search( |
|
103
|
|
|
_objective_function, |
|
104
|
|
|
search_space, |
|
105
|
|
|
n_iter=n_iter, |
|
106
|
|
|
n_jobs=2, |
|
107
|
|
|
initialize=initialize, |
|
108
|
|
|
) |
|
109
|
|
|
hyper0.run() |
|
110
|
|
|
|
|
111
|
|
|
search_data0 = hyper0.search_data(_objective_function) |
|
112
|
|
|
smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) |
|
113
|
|
|
|
|
114
|
|
|
hyper1 = Hyperactive() |
|
115
|
|
|
hyper1.add_search( |
|
116
|
|
|
_objective_function, search_space, n_iter=n_iter, optimizer=smbo_opt_ |
|
117
|
|
|
) |
|
118
|
|
|
hyper1.run() |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
View Code Duplication |
@pytest.mark.parametrize("smbo_opt", smbo_opts) |
|
|
|
|
|
|
122
|
|
|
def test_warm_start_smbo_2(smbo_opt): |
|
123
|
|
|
hyper0 = Hyperactive() |
|
124
|
|
|
hyper0.add_search(_objective_function, search_space, n_iter=n_iter) |
|
125
|
|
|
hyper0.run() |
|
126
|
|
|
|
|
127
|
|
|
search_data0 = hyper0.search_data(_objective_function) |
|
128
|
|
|
smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) |
|
129
|
|
|
|
|
130
|
|
|
hyper1 = Hyperactive(distribution="joblib") |
|
131
|
|
|
hyper1.add_search( |
|
132
|
|
|
_objective_function, |
|
133
|
|
|
search_space, |
|
134
|
|
|
n_iter=n_iter, |
|
135
|
|
|
n_jobs=2, |
|
136
|
|
|
optimizer=smbo_opt_, |
|
137
|
|
|
initialize=initialize, |
|
138
|
|
|
) |
|
139
|
|
|
hyper1.run() |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
View Code Duplication |
@pytest.mark.parametrize("smbo_opt", smbo_opts) |
|
|
|
|
|
|
143
|
|
|
def test_warm_start_smbo_3(smbo_opt): |
|
144
|
|
|
hyper0 = Hyperactive(distribution="pathos") |
|
145
|
|
|
hyper0.add_search(_objective_function, search_space, n_iter=n_iter, n_jobs=2) |
|
146
|
|
|
hyper0.run() |
|
147
|
|
|
|
|
148
|
|
|
search_data0 = hyper0.search_data(_objective_function) |
|
149
|
|
|
smbo_opt_ = smbo_opt(warm_start_smbo=search_data0) |
|
150
|
|
|
|
|
151
|
|
|
hyper1 = Hyperactive(distribution="joblib") |
|
152
|
|
|
hyper1.add_search( |
|
153
|
|
|
_objective_function, |
|
154
|
|
|
search_space, |
|
155
|
|
|
n_iter=n_iter, |
|
156
|
|
|
n_jobs=2, |
|
157
|
|
|
optimizer=smbo_opt_, |
|
158
|
|
|
initialize=initialize, |
|
159
|
|
|
) |
|
160
|
|
|
hyper1.run() |
|
161
|
|
|
|