-
Notifications
You must be signed in to change notification settings - Fork 214
/
callbacks.jl
86 lines (77 loc) · 2.57 KB
/
callbacks.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@testset "Callbacks" begin
problem = MultivariateProblems.UnconstrainedProblems.examples["Rosenbrock"]
f = MVP.objective(problem)
g! = MVP.gradient(problem)
h! = MVP.hessian(problem)
initial_x = problem.initial_x
d2 = OnceDifferentiable(f, g!, initial_x)
d3 = TwiceDifferentiable(f, g!, h!, initial_x)
for method in (NelderMead(), SimulatedAnnealing())
ot_run = false
cb = tr -> begin
@test tr[end].iteration % 3 == 0
ot_run = true
false
end
options = Optim.Options(callback = cb, show_every=3, store_trace=true)
optimize(f, initial_x, method, options)
@test ot_run == true
os_run = false
cb = os -> begin
@test os.iteration % 3 == 0
os_run = true
false
end
options = Optim.Options(callback = cb, show_every=3)
optimize(f, initial_x, method, options)
@test os_run == true
# Test early stopping by callbacks
options = Optim.Options(callback = x -> x.iteration == 5 ? true : false)
optimize(f, zeros(2), NelderMead(), options)
end
for method in (BFGS(),
ConjugateGradient(),
GradientDescent(),
MomentumGradientDescent())
ot_run = false
cb = tr -> begin
@test tr[end].iteration % 3 == 0
ot_run = true
false
end
options = Optim.Options(callback = cb, show_every=3, store_trace=true)
optimize(d2, initial_x, method, options)
@test ot_run == true
os_run = false
cb = os -> begin
@test os.iteration % 3 == 0
os_run = true
false
end
options = Optim.Options(callback = cb, show_every=3)
optimize(d2, initial_x, method, options)
@test os_run == true
end
for method in (Newton(),)
ot_run = false
cb = tr -> begin
@test tr[end].iteration % 3 == 0
ot_run = true
false
end
options = Optim.Options(callback = cb, show_every=3, store_trace=true)
optimize(d3, initial_x, method, options)
@test ot_run == true
os_run = false
cb = os -> begin
@test os.iteration % 3 == 0
os_run = true
false
end
options = Optim.Options(callback = cb, show_every=3)
optimize(d3, initial_x, method, options)
@test os_run == true
end
res = optimize(x->x^2, -5, 5, callback=_->true)
@test res.iterations == 0
end