Code Duplication    Length = 53-54 lines in 2 locations

tests/test_api/test_early_stop.py 2 locations

@@ 168-221 (lines=54) @@
165
    assert n_performed_iter == n_iter
166
167
168
def test_early_stop_5():
169
    class Experiment(BaseExperiment):
170
        def objective_function(self, para):
171
            return para["x0"]
172
173
    experiment = Experiment()
174
175
    search_config = SearchConfig(
176
        x0=list(np.arange(0, 100, 0.01)),
177
    )
178
179
    n_iter_no_change = 5
180
    early_stopping = {
181
        "n_iter_no_change": n_iter_no_change,
182
        "tol_abs": 10,
183
        "tol_rel": None,
184
    }
185
186
    start1 = {"x0": 0}
187
    start2 = {"x0": 9}
188
    start3 = {"x0": 20}
189
190
    warm_start_l = [
191
        start1,
192
        start1,
193
        start1,
194
        start1,
195
        start1,
196
        start2,
197
        start2,
198
        start2,
199
        start3,
200
        start3,
201
        start3,
202
    ]
203
    n_iter = len(warm_start_l)
204
205
    hyper = RandomSearchOptimizer()
206
    hyper.add_search(
207
        experiment,
208
        search_config,
209
        n_iter=n_iter,
210
        initialize={"warm_start": warm_start_l},
211
        early_stopping=early_stopping,
212
    )
213
    hyper.run()
214
215
    search_data = hyper.search_data(experiment)
216
    n_performed_iter = len(search_data)
217
218
    print("\n n_performed_iter \n", n_performed_iter)
219
    print("\n n_iter_no_change \n", n_iter_no_change)
220
221
    assert n_performed_iter == (n_iter_no_change + 1)
222
223
224
def test_early_stop_6():
@@ 288-340 (lines=53) @@
285
    assert n_performed_iter == n_iter
286
287
288
def test_early_stop_7():
289
    def objective_function(para):
290
        return para["x0"]
291
292
    experiment = Experiment()
293
294
    search_config = SearchConfig(
295
        x0=list(np.arange(0, 100, 0.01)),
296
    )
297
298
    n_iter_no_change = 5
299
    early_stopping = {
300
        "n_iter_no_change": n_iter_no_change,
301
        "tol_abs": None,
302
        "tol_rel": 10,
303
    }
304
305
    start1 = {"x0": 1}
306
    start2 = {"x0": 1.09}
307
    start3 = {"x0": 1.20}
308
309
    warm_start_l = [
310
        start1,
311
        start1,
312
        start1,
313
        start1,
314
        start1,
315
        start2,
316
        start2,
317
        start2,
318
        start3,
319
        start3,
320
        start3,
321
    ]
322
    n_iter = len(warm_start_l)
323
324
    hyper = RandomSearchOptimizer()
325
    hyper.add_search(
326
        experiment,
327
        search_config,
328
        n_iter=n_iter,
329
        initialize={"warm_start": warm_start_l},
330
        early_stopping=early_stopping,
331
    )
332
    hyper.run()
333
334
    search_data = hyper.search_data(experiment)
335
    n_performed_iter = len(search_data)
336
337
    print("\n n_performed_iter \n", n_performed_iter)
338
    print("\n n_iter_no_change \n", n_iter_no_change)
339
340
    assert n_performed_iter == (n_iter_no_change + 1)
341