|
9 | 9 | d3 = TwiceDifferentiable(f, g!, h!, initial_x) |
10 | 10 |
|
11 | 11 | for method in (NelderMead(), SimulatedAnnealing()) |
12 | | - ot_run = false |
| 12 | + ot_count = 0 |
13 | 13 | cb = tr -> begin |
14 | | - @test tr[end].iteration % 3 == 0 |
15 | | - ot_run = true |
| 14 | + ot_count += 1 |
16 | 15 | false |
17 | 16 | end |
18 | 17 | options = Optim.Options(callback = cb, show_every = 3, store_trace = true) |
19 | | - optimize(f, initial_x, method, options) |
20 | | - @test ot_run |
| 18 | + res1 = optimize(f, initial_x, method, options) |
| 19 | + @test ot_count == 1+res1.iterations |
21 | 20 |
|
22 | | - os_run = false |
| 21 | + os_count_2 = 0 |
23 | 22 | cb = os -> begin |
24 | | - @test os.iteration % 3 == 0 |
25 | | - os_run = true |
| 23 | + os_count_2 += 1 |
26 | 24 | false |
27 | 25 | end |
28 | 26 | options = Optim.Options(callback = cb, show_every = 3) |
29 | | - optimize(f, initial_x, method, options) |
30 | | - @test os_run |
| 27 | + res2 = optimize(f, initial_x, method, options) |
| 28 | + @test os_count_2 == 1+res2.iterations |
31 | 29 |
|
32 | 30 | # Test early stopping by callbacks |
33 | 31 | options = Optim.Options(callback = x -> x.iteration == 5 ? true : false) |
34 | | - optimize(f, zeros(2), NelderMead(), options) |
| 32 | + res3 = optimize(f, zeros(2), NelderMead(), options) |
| 33 | + @test res3.iterations == 5 |
35 | 34 | end |
36 | 35 |
|
37 | | - for method in |
38 | | - (BFGS(), ConjugateGradient(), GradientDescent(), MomentumGradientDescent()) |
39 | | - ot_run = false |
| 36 | + for method in (BFGS(), ConjugateGradient(), GradientDescent(), MomentumGradientDescent()) |
| 37 | + ot_count = 0 |
40 | 38 | cb = tr -> begin |
41 | | - @test tr[end].iteration % 3 == 0 |
42 | | - ot_run = true |
| 39 | + ot_count += 1 |
43 | 40 | false |
44 | 41 | end |
45 | 42 | options = Optim.Options(callback = cb, show_every = 3, store_trace = true) |
| 43 | + res1 = optimize(d2, initial_x, method, options) |
| 44 | + @test ot_count == 1+res1.iterations |
46 | 45 |
|
47 | | - optimize(d2, initial_x, method, options) |
48 | | - @test ot_run |
49 | | - |
50 | | - os_run = false |
| 46 | + os_count = 0 |
51 | 47 | cb = os -> begin |
52 | | - @test os.iteration % 3 == 0 |
53 | | - os_run = true |
| 48 | + os_count += 1 |
54 | 49 | false |
55 | 50 | end |
56 | 51 | options = Optim.Options(callback = cb, show_every = 3) |
57 | | - optimize(d2, initial_x, method, options) |
58 | | - @test os_run |
| 52 | + res2 = optimize(d2, initial_x, method, options) |
| 53 | + @test os_count == 1+res2.iterations |
59 | 54 | end |
60 | 55 |
|
61 | 56 | for method in (Newton(),) |
62 | | - ot_run = false |
| 57 | + ot_count = 0 |
63 | 58 | cb = tr -> begin |
64 | | - @test tr[end].iteration % 3 == 0 |
65 | | - ot_run = true |
| 59 | + ot_count += 1 |
66 | 60 | false |
67 | 61 | end |
68 | 62 | options = Optim.Options(callback = cb, show_every = 3, store_trace = true) |
69 | | - optimize(d3, initial_x, method, options) |
70 | | - @test ot_run |
| 63 | + res1 = optimize(d3, initial_x, method, options) |
| 64 | + @test ot_count == 1+res1.iterations |
71 | 65 |
|
72 | | - os_run = false |
| 66 | + os_count = 0 |
73 | 67 | cb = os -> begin |
74 | | - @test os.iteration % 3 == 0 |
75 | | - os_run = true |
| 68 | + os_count += 1 |
76 | 69 | false |
77 | 70 | end |
78 | 71 | options = Optim.Options(callback = cb, show_every = 3) |
79 | | - optimize(d3, initial_x, method, options) |
80 | | - @test os_run |
| 72 | + res2 = optimize(d3, initial_x, method, options) |
| 73 | + @test os_count == 1+res2.iterations |
81 | 74 | end |
| 75 | + |
82 | 76 | res = optimize(x -> x^2, -5, 5, callback = _ -> true) |
83 | 77 | @test res.iterations == 0 |
84 | 78 | end |
0 commit comments