Passed
Pull Request — master (#101)
by Simon
01:35
created

tests.test_api.test_early_stop.test_early_stop_4()   B

Complexity

Conditions 1

Size

Total Lines 59
Code Lines 47

Duplication

Lines 59
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 47
nop 0
dl 59
loc 59
rs 8.7345
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 View Code Duplication
def test_early_stop_3():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
    def objective_function(para):
106
        return para["x0"]
107
108
    search_space = {
109
        "x0": list(np.arange(0, 100, 0.1)),
110
    }
111
112
    n_iter_no_change = 5
113
    early_stopping = {
114
        "n_iter_no_change": 5,
115
        "tol_abs": 0.1,
116
        "tol_rel": None,
117
    }
118
119
    start1 = {"x0": 0}
120
    start2 = {"x0": 0.1}
121
    start3 = {"x0": 0.2}
122
    start4 = {"x0": 0.3}
123
    start5 = {"x0": 0.4}
124
125
    warm_start_l = [
126
        start1,
127
        start1,
128
        start1,
129
        start1,
130
        start1,
131
        start2,
132
        start2,
133
        start2,
134
        start3,
135
        start3,
136
        start3,
137
        start4,
138
        start4,
139
        start4,
140
        start5,
141
        start5,
142
        start5,
143
    ]
144
    n_iter = len(warm_start_l)
145
146
    hyper = RandomSearchOptimizer()
147
    hyper.add_search(
148
        experiment,
149
        search_config,
150
        n_iter=n_iter,
151
        initialize={"warm_start": warm_start_l},
152
        early_stopping=early_stopping,
153
    )
154
    hyper.run()
155
156
    search_data = hyper.search_data(experiment)
157
    n_performed_iter = len(search_data)
158
159
    print("\n n_performed_iter \n", n_performed_iter)
160
    print("\n n_iter_no_change \n", n_iter_no_change)
161
162
    assert n_performed_iter == n_iter
163
164
165 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...
166
    def objective_function(para):
167
        return para["x0"]
168
169
    search_space = {
170
        "x0": list(np.arange(0, 100, 0.01)),
171
    }
172
173
    n_iter_no_change = 5
174
    early_stopping = {
175
        "n_iter_no_change": n_iter_no_change,
176
        "tol_abs": 0.1,
177
        "tol_rel": None,
178
    }
179
180
    start1 = {"x0": 0}
181
    start2 = {"x0": 0.09}
182
    start3 = {"x0": 0.20}
183
184
    warm_start_l = [
185
        start1,
186
        start1,
187
        start1,
188
        start1,
189
        start1,
190
        start2,
191
        start2,
192
        start2,
193
        start3,
194
        start3,
195
        start3,
196
    ]
197
    n_iter = len(warm_start_l)
198
199
    hyper = RandomSearchOptimizer()
200
    hyper.add_search(
201
        experiment,
202
        search_config,
203
        n_iter=n_iter,
204
        initialize={"warm_start": warm_start_l},
205
        early_stopping=early_stopping,
206
    )
207
    hyper.run()
208
209
    search_data = hyper.search_data(experiment)
210
    n_performed_iter = len(search_data)
211
212
    print("\n n_performed_iter \n", n_performed_iter)
213
    print("\n n_iter_no_change \n", n_iter_no_change)
214
215
    assert n_performed_iter == (n_iter_no_change + 1)
216
217
218 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...
219
    def objective_function(para):
220
        return para["x0"]
221
222
    search_space = {
223
        "x0": list(np.arange(0, 100, 0.01)),
224
    }
225
226
    n_iter_no_change = 5
227
    early_stopping = {
228
        "n_iter_no_change": 5,
229
        "tol_abs": None,
230
        "tol_rel": 10,
231
    }
232
233
    start1 = {"x0": 1}
234
    start2 = {"x0": 1.1}
235
    start3 = {"x0": 1.22}
236
    start4 = {"x0": 1.35}
237
    start5 = {"x0": 1.48}
238
239
    warm_start_l = [
240
        start1,
241
        start1,
242
        start1,
243
        start1,
244
        start1,
245
        start2,
246
        start2,
247
        start2,
248
        start3,
249
        start3,
250
        start3,
251
        start4,
252
        start4,
253
        start4,
254
        start5,
255
        start5,
256
        start5,
257
    ]
258
    n_iter = len(warm_start_l)
259
260
    hyper = RandomSearchOptimizer()
261
    hyper.add_search(
262
        experiment,
263
        search_config,
264
        n_iter=n_iter,
265
        initialize={"warm_start": warm_start_l},
266
        early_stopping=early_stopping,
267
    )
268
    hyper.run()
269
270
    search_data = hyper.search_data(experiment)
271
    n_performed_iter = len(search_data)
272
273
    print("\n n_performed_iter \n", n_performed_iter)
274
    print("\n n_iter_no_change \n", n_iter_no_change)
275
276
    assert n_performed_iter == n_iter
277
278
279 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...
280
    def objective_function(para):
281
        return para["x0"]
282
283
    search_space = {
284
        "x0": list(np.arange(0, 100, 0.01)),
285
    }
286
287
    n_iter_no_change = 5
288
    early_stopping = {
289
        "n_iter_no_change": n_iter_no_change,
290
        "tol_abs": None,
291
        "tol_rel": 10,
292
    }
293
294
    start1 = {"x0": 1}
295
    start2 = {"x0": 1.09}
296
    start3 = {"x0": 1.20}
297
298
    warm_start_l = [
299
        start1,
300
        start1,
301
        start1,
302
        start1,
303
        start1,
304
        start2,
305
        start2,
306
        start2,
307
        start3,
308
        start3,
309
        start3,
310
    ]
311
    n_iter = len(warm_start_l)
312
313
    hyper = RandomSearchOptimizer()
314
    hyper.add_search(
315
        experiment,
316
        search_config,
317
        n_iter=n_iter,
318
        initialize={"warm_start": warm_start_l},
319
        early_stopping=early_stopping,
320
    )
321
    hyper.run()
322
323
    search_data = hyper.search_data(experiment)
324
    n_performed_iter = len(search_data)
325
326
    print("\n n_performed_iter \n", n_performed_iter)
327
    print("\n n_iter_no_change \n", n_iter_no_change)
328
329
    assert n_performed_iter == (n_iter_no_change + 1)
330