1 | import time |
||
2 | import pytest |
||
3 | import numpy as np |
||
4 | from sklearn.datasets import load_breast_cancer |
||
5 | from sklearn.model_selection import cross_val_score |
||
6 | from sklearn.tree import DecisionTreeClassifier |
||
7 | from gradient_free_optimizers import ( |
||
8 | RandomSearchOptimizer, |
||
9 | HillClimbingOptimizer, |
||
10 | ) |
||
11 | |||
12 | |||
13 | def objective_function(para): |
||
14 | score = -para["x1"] * para["x1"] |
||
15 | return score |
||
16 | |||
17 | |||
18 | search_space = { |
||
19 | "x1": np.arange(0, 100000, 0.1), |
||
20 | } |
||
21 | |||
22 | |||
23 | def test_early_stop_0(): |
||
24 | early_stopping = { |
||
25 | "n_iter_no_change": 5, |
||
26 | "tol_abs": 0.1, |
||
27 | "tol_rel": 0.1, |
||
28 | } |
||
29 | |||
30 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": [{"x1": 0}]}) |
||
31 | opt.search( |
||
32 | objective_function, |
||
33 | n_iter=1000, |
||
34 | early_stopping=early_stopping, |
||
35 | ) |
||
36 | |||
37 | |||
38 | def test_early_stop_1(): |
||
39 | early_stopping = { |
||
40 | "n_iter_no_change": 5, |
||
41 | "tol_abs": None, |
||
42 | "tol_rel": 5, |
||
43 | } |
||
44 | |||
45 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": [{"x1": 0}]}) |
||
46 | opt.search( |
||
47 | objective_function, |
||
48 | n_iter=1000, |
||
49 | early_stopping=early_stopping, |
||
50 | ) |
||
51 | |||
52 | |||
53 | def test_early_stop_2(): |
||
54 | early_stopping = { |
||
55 | "n_iter_no_change": 5, |
||
56 | "tol_abs": 0.1, |
||
57 | "tol_rel": None, |
||
58 | } |
||
59 | |||
60 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": [{"x1": 0}]}) |
||
61 | opt.search( |
||
62 | objective_function, |
||
63 | n_iter=1000, |
||
64 | early_stopping=early_stopping, |
||
65 | ) |
||
66 | |||
67 | |||
68 | View Code Duplication | def test_early_stop_3(): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
69 | def objective_function(para): |
||
70 | score = -para["x1"] * para["x1"] |
||
71 | return score |
||
72 | |||
73 | search_space = { |
||
74 | "x1": np.arange(0, 100, 0.1), |
||
75 | } |
||
76 | |||
77 | n_iter_no_change = 5 |
||
78 | early_stopping = { |
||
79 | "n_iter_no_change": n_iter_no_change, |
||
80 | } |
||
81 | |||
82 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": [{"x1": 0}]}) |
||
83 | opt.search( |
||
84 | objective_function, |
||
85 | n_iter=100000, |
||
86 | early_stopping=early_stopping, |
||
87 | ) |
||
88 | search_data = opt.search_data |
||
89 | n_performed_iter = len(search_data) |
||
90 | |||
91 | print("\n n_performed_iter \n", n_performed_iter) |
||
92 | print("\n n_iter_no_change \n", n_iter_no_change) |
||
93 | |||
94 | assert n_performed_iter == (n_iter_no_change + 1) |
||
95 | |||
96 | |||
97 | View Code Duplication | def test_early_stop_4(): |
|
0 ignored issues
–
show
|
|||
98 | def objective_function(para): |
||
99 | return para["x1"] |
||
100 | |||
101 | search_space = { |
||
102 | "x1": np.arange(0, 100, 0.1), |
||
103 | } |
||
104 | |||
105 | n_iter_no_change = 5 |
||
106 | early_stopping = { |
||
107 | "n_iter_no_change": 5, |
||
108 | "tol_abs": 1, |
||
109 | "tol_rel": None, |
||
110 | } |
||
111 | |||
112 | start1 = {"x1": 0} |
||
113 | start2 = {"x1": 1} |
||
114 | start3 = {"x1": 2} |
||
115 | start4 = {"x1": 3} |
||
116 | start5 = {"x1": 4} |
||
117 | |||
118 | warm_start_l = [ |
||
119 | start1, |
||
120 | start1, |
||
121 | start1, |
||
122 | start1, |
||
123 | start1, |
||
124 | start2, |
||
125 | start2, |
||
126 | start2, |
||
127 | start3, |
||
128 | start3, |
||
129 | start3, |
||
130 | start4, |
||
131 | start4, |
||
132 | start4, |
||
133 | start5, |
||
134 | start5, |
||
135 | start5, |
||
136 | ] |
||
137 | n_iter = len(warm_start_l) |
||
138 | |||
139 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": warm_start_l}) |
||
140 | opt.search( |
||
141 | objective_function, |
||
142 | n_iter=n_iter, |
||
143 | early_stopping=early_stopping, |
||
144 | ) |
||
145 | search_data = opt.search_data |
||
146 | n_performed_iter = len(search_data) |
||
147 | |||
148 | print("\n n_performed_iter \n", n_performed_iter) |
||
149 | print("\n n_iter_no_change \n", n_iter_no_change) |
||
150 | |||
151 | assert n_performed_iter == n_iter |
||
152 | |||
153 | |||
154 | View Code Duplication | def test_early_stop_5(): |
|
0 ignored issues
–
show
|
|||
155 | def objective_function(para): |
||
156 | return para["x1"] |
||
157 | |||
158 | search_space = { |
||
159 | "x1": np.arange(0, 100, 0.01), |
||
160 | } |
||
161 | |||
162 | n_iter_no_change = 5 |
||
163 | early_stopping = { |
||
164 | "n_iter_no_change": n_iter_no_change, |
||
165 | "tol_abs": 10, |
||
166 | "tol_rel": None, |
||
167 | } |
||
168 | |||
169 | start1 = {"x1": 0} |
||
170 | start2 = {"x1": 9} |
||
171 | start3 = {"x1": 20} |
||
172 | |||
173 | warm_start_l = [ |
||
174 | start1, |
||
175 | start1, |
||
176 | start1, |
||
177 | start1, |
||
178 | start1, |
||
179 | start2, |
||
180 | start2, |
||
181 | start2, |
||
182 | start3, |
||
183 | start3, |
||
184 | start3, |
||
185 | ] |
||
186 | n_iter = len(warm_start_l) |
||
187 | |||
188 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": warm_start_l}) |
||
189 | opt.search( |
||
190 | objective_function, |
||
191 | n_iter=n_iter, |
||
192 | early_stopping=early_stopping, |
||
193 | ) |
||
194 | search_data = opt.search_data |
||
195 | n_performed_iter = len(search_data) |
||
196 | |||
197 | print("\n n_performed_iter \n", n_performed_iter) |
||
198 | print("\n n_iter_no_change \n", n_iter_no_change) |
||
199 | |||
200 | assert n_performed_iter == (n_iter_no_change + 1) |
||
201 | |||
202 | |||
203 | View Code Duplication | def test_early_stop_6(): |
|
0 ignored issues
–
show
|
|||
204 | def objective_function(para): |
||
205 | return para["x1"] |
||
206 | |||
207 | search_space = { |
||
208 | "x1": np.arange(0, 100, 0.01), |
||
209 | } |
||
210 | |||
211 | n_iter_no_change = 5 |
||
212 | early_stopping = { |
||
213 | "n_iter_no_change": 5, |
||
214 | "tol_abs": None, |
||
215 | "tol_rel": 10, |
||
216 | } |
||
217 | |||
218 | start1 = {"x1": 1} |
||
219 | start2 = {"x1": 1.1} |
||
220 | start3 = {"x1": 1.22} |
||
221 | start4 = {"x1": 1.35} |
||
222 | start5 = {"x1": 1.48} |
||
223 | |||
224 | warm_start_l = [ |
||
225 | start1, |
||
226 | start1, |
||
227 | start1, |
||
228 | start1, |
||
229 | start1, |
||
230 | start2, |
||
231 | start2, |
||
232 | start2, |
||
233 | start3, |
||
234 | start3, |
||
235 | start3, |
||
236 | start4, |
||
237 | start4, |
||
238 | start4, |
||
239 | start5, |
||
240 | start5, |
||
241 | start5, |
||
242 | ] |
||
243 | n_iter = len(warm_start_l) |
||
244 | |||
245 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": warm_start_l}) |
||
246 | opt.search( |
||
247 | objective_function, |
||
248 | n_iter=n_iter, |
||
249 | early_stopping=early_stopping, |
||
250 | ) |
||
251 | search_data = opt.search_data |
||
252 | n_performed_iter = len(search_data) |
||
253 | |||
254 | print("\n n_performed_iter \n", n_performed_iter) |
||
255 | print("\n n_iter_no_change \n", n_iter_no_change) |
||
256 | |||
257 | assert n_performed_iter == n_iter |
||
258 | |||
259 | |||
260 | View Code Duplication | def test_early_stop_7(): |
|
0 ignored issues
–
show
|
|||
261 | def objective_function(para): |
||
262 | return para["x1"] |
||
263 | |||
264 | search_space = { |
||
265 | "x1": np.arange(0, 100, 0.01), |
||
266 | } |
||
267 | |||
268 | n_iter_no_change = 5 |
||
269 | early_stopping = { |
||
270 | "n_iter_no_change": n_iter_no_change, |
||
271 | "tol_abs": None, |
||
272 | "tol_rel": 10, |
||
273 | } |
||
274 | |||
275 | start1 = {"x1": 1} |
||
276 | start2 = {"x1": 1.09} |
||
277 | start3 = {"x1": 1.20} |
||
278 | |||
279 | warm_start_l = [ |
||
280 | start1, |
||
281 | start1, |
||
282 | start1, |
||
283 | start1, |
||
284 | start1, |
||
285 | start2, |
||
286 | start2, |
||
287 | start2, |
||
288 | start3, |
||
289 | start3, |
||
290 | start3, |
||
291 | ] |
||
292 | n_iter = len(warm_start_l) |
||
293 | |||
294 | opt = HillClimbingOptimizer(search_space, initialize={"warm_start": warm_start_l}) |
||
295 | opt.search( |
||
296 | objective_function, |
||
297 | n_iter=n_iter, |
||
298 | early_stopping=early_stopping, |
||
299 | ) |
||
300 | search_data = opt.search_data |
||
301 | n_performed_iter = len(search_data) |
||
302 | |||
303 | print("\n n_performed_iter \n", n_performed_iter) |
||
304 | print("\n n_iter_no_change \n", n_iter_no_change) |
||
305 | |||
306 | assert n_performed_iter == (n_iter_no_change + 1) |
||
307 |