Passed
Pull Request — master (#110)
by
unknown
01:23
created

tests.test_api.test_early_stop.test_early_stop_4()   B

Complexity

Conditions 1

Size

Total Lines 62
Code Lines 49

Duplication

Lines 62
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 49
nop 0
dl 62
loc 62
rs 8.669
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import time
2
import pytest
3
import numpy as np
4
5
from hyperactive.optimizers import RandomSearchOptimizer, HillClimbingOptimizer
6
from hyperactive.experiment import BaseExperiment
7
from hyperactive.search_config import SearchConfig
8
9
10
class Experiment(BaseExperiment):
11
    def objective_function(self, para):
12
        score = -para["x0"] * para["x0"]
13
        return score
14
15
16
experiment = Experiment()
17
search_space = {
18
    "x0": list(np.arange(0, 100000, 0.1)),
19
}
20
search_config = SearchConfig(
21
    x0=list(np.arange(0, 100000, 0.1)),
22
)
23
24
25
def test_early_stop_0():
26
    early_stopping = {
27
        "n_iter_no_change": 5,
28
        "tol_abs": 0.1,
29
        "tol_rel": 0.1,
30
    }
31
32
    hyper = RandomSearchOptimizer()
33
    hyper.add_search(
34
        experiment,
35
        search_config,
36
        n_iter=1000,
37
        initialize={"warm_start": [{"x0": 0}]},
38
        early_stopping=early_stopping,
39
    )
40
    hyper.run()
41
42
43
def test_early_stop_1():
44
    early_stopping = {
45
        "n_iter_no_change": 5,
46
        "tol_abs": None,
47
        "tol_rel": 5,
48
    }
49
50
    hyper = RandomSearchOptimizer()
51
    hyper.add_search(
52
        experiment,
53
        search_config,
54
        n_iter=1000,
55
        initialize={"warm_start": [{"x0": 0}]},
56
        early_stopping=early_stopping,
57
    )
58
    hyper.run()
59
60
    hyper = RandomSearchOptimizer()
61
    hyper.add_search(
62
        experiment,
63
        search_config,
64
        n_iter=1000,
65
        initialize={"warm_start": [{"x0": 0}]},
66
        early_stopping=early_stopping,
67
    )
68
    hyper.run()
69
70
71
def test_early_stop_3():
72
    def objective_function(para):
73
        score = -para["x0"] * para["x0"]
74
        return score
75
76
    search_space = {
77
        "x0": list(np.arange(0, 100, 0.1)),
78
    }
79
80
    n_iter_no_change = 5
81
    early_stopping = {
82
        "n_iter_no_change": n_iter_no_change,
83
    }
84
85
    hyper = RandomSearchOptimizer()
86
    hyper.add_search(
87
        experiment,
88
        search_config,
89
        n_iter=100000,
90
        initialize={"warm_start": [{"x0": 0}]},
91
        early_stopping=early_stopping,
92
    )
93
    hyper.run()
94
95
    search_data = hyper.search_data(experiment)
96
    n_performed_iter = len(search_data)
97
98
    print("\n n_performed_iter \n", n_performed_iter)
99
    print("\n n_iter_no_change \n", n_iter_no_change)
100
101
    assert n_performed_iter == (n_iter_no_change + 1)
102
103
104 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...
105
    class Experiment(BaseExperiment):
106
        def objective_function(self, para):
107
            return para["x0"]
108
109
    experiment = Experiment()
110
111
    search_config = SearchConfig(
112
        x0=list(np.arange(0, 100, 0.01)),
113
    )
114
115
    n_iter_no_change = 5
116
    early_stopping = {
117
        "n_iter_no_change": 5,
118
        "tol_abs": 1,
119
        "tol_rel": None,
120
    }
121
122
    start1 = {"x0": 0}
123
    start2 = {"x0": 1}
124
    start3 = {"x0": 2}
125
    start4 = {"x0": 3}
126
    start5 = {"x0": 4}
127
128
    warm_start_l = [
129
        start1,
130
        start1,
131
        start1,
132
        start1,
133
        start1,
134
        start2,
135
        start2,
136
        start2,
137
        start3,
138
        start3,
139
        start3,
140
        start4,
141
        start4,
142
        start4,
143
        start5,
144
        start5,
145
        start5,
146
    ]
147
    n_iter = len(warm_start_l)
148
149
    hyper = RandomSearchOptimizer()
150
    hyper.add_search(
151
        experiment,
152
        search_config,
153
        n_iter=n_iter,
154
        initialize={"warm_start": warm_start_l},
155
        early_stopping=early_stopping,
156
    )
157
    hyper.run()
158
159
    search_data = hyper.search_data(experiment)
160
    n_performed_iter = len(search_data)
161
162
    print("\n n_performed_iter \n", n_performed_iter)
163
    print("\n n_iter_no_change \n", n_iter_no_change)
164
165
    assert n_performed_iter == n_iter
166
167
168 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...
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 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...
225
    class Experiment(BaseExperiment):
226
        def objective_function(self, para):
227
            return para["x0"]
228
229
    experiment = Experiment()
230
231
    search_config = SearchConfig(
232
        x0=list(np.arange(0, 100, 0.01)),
233
    )
234
235
    n_iter_no_change = 5
236
    early_stopping = {
237
        "n_iter_no_change": 5,
238
        "tol_abs": None,
239
        "tol_rel": 10,
240
    }
241
242
    start1 = {"x0": 1}
243
    start2 = {"x0": 1.1}
244
    start3 = {"x0": 1.22}
245
    start4 = {"x0": 1.35}
246
    start5 = {"x0": 1.48}
247
248
    warm_start_l = [
249
        start1,
250
        start1,
251
        start1,
252
        start1,
253
        start1,
254
        start2,
255
        start2,
256
        start2,
257
        start3,
258
        start3,
259
        start3,
260
        start4,
261
        start4,
262
        start4,
263
        start5,
264
        start5,
265
        start5,
266
    ]
267
    n_iter = len(warm_start_l)
268
269
    hyper = RandomSearchOptimizer()
270
    hyper.add_search(
271
        experiment,
272
        search_config,
273
        n_iter=n_iter,
274
        initialize={"warm_start": warm_start_l},
275
        early_stopping=early_stopping,
276
    )
277
    hyper.run()
278
279
    search_data = hyper.search_data(experiment)
280
    n_performed_iter = len(search_data)
281
282
    print("\n n_performed_iter \n", n_performed_iter)
283
    print("\n n_iter_no_change \n", n_iter_no_change)
284
285
    assert n_performed_iter == n_iter
286
287
288 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...
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