tests.test_early_stop.test_early_stop_0()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 17
rs 9.75
c 0
b 0
f 0
cc 1
nop 0
1
"""Test module for early stopping functionality."""
2
3
import numpy as np
4
5
from hyperactive import Hyperactive
6
7
8
def objective_function(para):
9
    """Return simple quadratic objective function for testing."""
10
    score = -para["x1"] * para["x1"]
11
    return score
12
13
14
search_space = {
15
    "x1": list(np.arange(0, 100000, 0.1)),
16
}
17
18
19
def test_early_stop_0():
20
    """Test early stopping with both absolute and relative tolerance."""
21
    early_stopping = {
22
        "n_iter_no_change": 5,
23
        "tol_abs": 0.1,
24
        "tol_rel": 0.1,
25
    }
26
27
    hyper = Hyperactive()
28
    hyper.add_search(
29
        objective_function,
30
        search_space,
31
        n_iter=1000,
32
        initialize={"warm_start": [{"x1": 0}]},
33
        early_stopping=early_stopping,
34
    )
35
    hyper.run()
36
37
38
def test_early_stop_1():
39
    """Test early stopping with relative tolerance only."""
40
    early_stopping = {
41
        "n_iter_no_change": 5,
42
        "tol_abs": None,
43
        "tol_rel": 5,
44
    }
45
46
    hyper = Hyperactive()
47
    hyper.add_search(
48
        objective_function,
49
        search_space,
50
        n_iter=1000,
51
        initialize={"warm_start": [{"x1": 0}]},
52
        early_stopping=early_stopping,
53
    )
54
    hyper.run()
55
56
57
def test_early_stop_2():
58
    """Test early stopping with absolute tolerance only."""
59
    early_stopping = {
60
        "n_iter_no_change": 5,
61
        "tol_abs": 0.1,
62
        "tol_rel": None,
63
    }
64
65
    hyper = Hyperactive()
66
    hyper.add_search(
67
        objective_function,
68
        search_space,
69
        n_iter=1000,
70
        initialize={"warm_start": [{"x1": 0}]},
71
        early_stopping=early_stopping,
72
    )
73
    hyper.run()
74
75
76
def test_early_stop_3():
77
    """Test early stopping without tolerance and verify iteration count."""
78
79
    def objective_function(para):
80
        score = -para["x1"] * para["x1"]
81
        return score
82
83
    search_space = {
84
        "x1": list(np.arange(0, 100, 0.1)),
85
    }
86
87
    n_iter_no_change = 5
88
    early_stopping = {
89
        "n_iter_no_change": n_iter_no_change,
90
    }
91
92
    hyper = Hyperactive()
93
    hyper.add_search(
94
        objective_function,
95
        search_space,
96
        n_iter=100000,
97
        initialize={"warm_start": [{"x1": 0}]},
98
        early_stopping=early_stopping,
99
    )
100
    hyper.run()
101
102
    search_data = hyper.search_data(objective_function)
103
    n_performed_iter = len(search_data)
104
105
    print("\n n_performed_iter \n", n_performed_iter)
106
    print("\n n_iter_no_change \n", n_iter_no_change)
107
108
    assert n_performed_iter == (n_iter_no_change + 1)
109
110
111 View Code Duplication
def test_early_stop_4():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
112
    """Test early stopping with absolute tolerance where no early stop occurs."""
113
114
    def objective_function(para):
115
        return para["x1"]
116
117
    search_space = {
118
        "x1": list(np.arange(0, 100, 0.1)),
119
    }
120
121
    n_iter_no_change = 5
122
    early_stopping = {
123
        "n_iter_no_change": 5,
124
        "tol_abs": 0.1,
125
        "tol_rel": None,
126
    }
127
128
    start1 = {"x1": 0}
129
    start2 = {"x1": 0.1}
130
    start3 = {"x1": 0.2}
131
    start4 = {"x1": 0.3}
132
    start5 = {"x1": 0.4}
133
134
    warm_start_l = [
135
        start1,
136
        start1,
137
        start1,
138
        start1,
139
        start1,
140
        start2,
141
        start2,
142
        start2,
143
        start3,
144
        start3,
145
        start3,
146
        start4,
147
        start4,
148
        start4,
149
        start5,
150
        start5,
151
        start5,
152
    ]
153
    n_iter = len(warm_start_l)
154
155
    hyper = Hyperactive()
156
    hyper.add_search(
157
        objective_function,
158
        search_space,
159
        n_iter=n_iter,
160
        initialize={"warm_start": warm_start_l},
161
        early_stopping=early_stopping,
162
    )
163
    hyper.run()
164
165
    search_data = hyper.search_data(objective_function)
166
    n_performed_iter = len(search_data)
167
168
    print("\n n_performed_iter \n", n_performed_iter)
169
    print("\n n_iter_no_change \n", n_iter_no_change)
170
171
    assert n_performed_iter == n_iter
172
173
174 View Code Duplication
def test_early_stop_5():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
175
    """Test early stopping with absolute tolerance where early stop occurs."""
176
177
    def objective_function(para):
178
        return para["x1"]
179
180
    search_space = {
181
        "x1": list(np.arange(0, 100, 0.01)),
182
    }
183
184
    n_iter_no_change = 5
185
    early_stopping = {
186
        "n_iter_no_change": n_iter_no_change,
187
        "tol_abs": 0.1,
188
        "tol_rel": None,
189
    }
190
191
    start1 = {"x1": 0}
192
    start2 = {"x1": 0.09}
193
    start3 = {"x1": 0.20}
194
195
    warm_start_l = [
196
        start1,
197
        start1,
198
        start1,
199
        start1,
200
        start1,
201
        start2,
202
        start2,
203
        start2,
204
        start3,
205
        start3,
206
        start3,
207
    ]
208
    n_iter = len(warm_start_l)
209
210
    hyper = Hyperactive()
211
    hyper.add_search(
212
        objective_function,
213
        search_space,
214
        n_iter=n_iter,
215
        initialize={"warm_start": warm_start_l},
216
        early_stopping=early_stopping,
217
    )
218
    hyper.run()
219
220
    search_data = hyper.search_data(objective_function)
221
    n_performed_iter = len(search_data)
222
223
    print("\n n_performed_iter \n", n_performed_iter)
224
    print("\n n_iter_no_change \n", n_iter_no_change)
225
226
    assert n_performed_iter == (n_iter_no_change + 1)
227
228
229 View Code Duplication
def test_early_stop_6():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
230
    """Test early stopping with relative tolerance where no early stop occurs."""
231
232
    def objective_function(para):
233
        return para["x1"]
234
235
    search_space = {
236
        "x1": list(np.arange(0, 100, 0.01)),
237
    }
238
239
    n_iter_no_change = 5
240
    early_stopping = {
241
        "n_iter_no_change": 5,
242
        "tol_abs": None,
243
        "tol_rel": 10,
244
    }
245
246
    start1 = {"x1": 1}
247
    start2 = {"x1": 1.1}
248
    start3 = {"x1": 1.22}
249
    start4 = {"x1": 1.35}
250
    start5 = {"x1": 1.48}
251
252
    warm_start_l = [
253
        start1,
254
        start1,
255
        start1,
256
        start1,
257
        start1,
258
        start2,
259
        start2,
260
        start2,
261
        start3,
262
        start3,
263
        start3,
264
        start4,
265
        start4,
266
        start4,
267
        start5,
268
        start5,
269
        start5,
270
    ]
271
    n_iter = len(warm_start_l)
272
273
    hyper = Hyperactive()
274
    hyper.add_search(
275
        objective_function,
276
        search_space,
277
        n_iter=n_iter,
278
        initialize={"warm_start": warm_start_l},
279
        early_stopping=early_stopping,
280
    )
281
    hyper.run()
282
283
    search_data = hyper.search_data(objective_function)
284
    n_performed_iter = len(search_data)
285
286
    print("\n n_performed_iter \n", n_performed_iter)
287
    print("\n n_iter_no_change \n", n_iter_no_change)
288
289
    assert n_performed_iter == n_iter
290
291
292 View Code Duplication
def test_early_stop_7():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
293
    """Test early stopping with relative tolerance where early stop occurs."""
294
295
    def objective_function(para):
296
        return para["x1"]
297
298
    search_space = {
299
        "x1": list(np.arange(0, 100, 0.01)),
300
    }
301
302
    n_iter_no_change = 5
303
    early_stopping = {
304
        "n_iter_no_change": n_iter_no_change,
305
        "tol_abs": None,
306
        "tol_rel": 10,
307
    }
308
309
    start1 = {"x1": 1}
310
    start2 = {"x1": 1.09}
311
    start3 = {"x1": 1.20}
312
313
    warm_start_l = [
314
        start1,
315
        start1,
316
        start1,
317
        start1,
318
        start1,
319
        start2,
320
        start2,
321
        start2,
322
        start3,
323
        start3,
324
        start3,
325
    ]
326
    n_iter = len(warm_start_l)
327
328
    hyper = Hyperactive()
329
    hyper.add_search(
330
        objective_function,
331
        search_space,
332
        n_iter=n_iter,
333
        initialize={"warm_start": warm_start_l},
334
        early_stopping=early_stopping,
335
    )
336
    hyper.run()
337
338
    search_data = hyper.search_data(objective_function)
339
    n_performed_iter = len(search_data)
340
341
    print("\n n_performed_iter \n", n_performed_iter)
342
    print("\n n_iter_no_change \n", n_iter_no_change)
343
344
    assert n_performed_iter == (n_iter_no_change + 1)
345