@@ -3,6 +3,32 @@ using OptimizationOptimJL,
3
3
Random, ModelingToolkit
4
4
using Test
5
5
6
+ struct CallbackTester
7
+ dim:: Int
8
+ has_grad:: Bool
9
+ has_hess:: Bool
10
+ end
11
+ function CallbackTester (dim:: Int ; has_grad = false , has_hess = false )
12
+ CallbackTester (dim, has_grad, has_hess)
13
+ end
14
+
15
+ function (cb:: CallbackTester )(state, loss_val)
16
+ @test length (state. u) == cb. dim
17
+ if cb. has_grad
18
+ @test state. grad isa AbstractVector
19
+ @test length (state. grad) == cb. dim
20
+ else
21
+ @test state. grad === nothing
22
+ end
23
+ if cb. has_hess
24
+ @test state. hess isa AbstractMatrix
25
+ @test size (state. hess) == (cb. dim, cb. dim)
26
+ else
27
+ @test state. hess === nothing
28
+ end
29
+ return false
30
+ end
31
+
6
32
@testset " OptimizationOptimJL.jl" begin
7
33
rosenbrock (x, p) = (p[1 ] - x[1 ])^ 2 + p[2 ] * (x[2 ] - x[1 ]^ 2 )^ 2
8
34
x0 = zeros (2 )
@@ -13,34 +39,43 @@ using Test
13
39
sol = solve (prob,
14
40
Optim. NelderMead (;
15
41
initial_simplex = Optim. AffineSimplexer (; a = 0.025 ,
16
- b = 0.5 )))
42
+ b = 0.5 )); callback = CallbackTester ( length (x0)) )
17
43
@test 10 * sol. objective < l1
18
44
19
45
f = OptimizationFunction (rosenbrock, Optimization. AutoForwardDiff ())
20
46
21
47
Random. seed! (1234 )
22
48
prob = OptimizationProblem (f, x0, _p, lb = [- 1.0 , - 1.0 ], ub = [0.8 , 0.8 ])
23
- sol = solve (prob, SAMIN ())
49
+ sol = solve (prob, SAMIN (); callback = CallbackTester ( length (x0)) )
24
50
@test 10 * sol. objective < l1
25
51
26
- sol = solve (prob, Optim. IPNewton ())
52
+ sol = solve (
53
+ prob, Optim. IPNewton ();
54
+ callback = CallbackTester (length (x0); has_grad = true , has_hess = true )
55
+ )
27
56
@test 10 * sol. objective < l1
28
57
29
58
prob = OptimizationProblem (f, x0, _p)
30
59
Random. seed! (1234 )
31
- sol = solve (prob, SimulatedAnnealing ())
60
+ sol = solve (prob, SimulatedAnnealing (); callback = CallbackTester ( length (x0)) )
32
61
@test 10 * sol. objective < l1
33
62
34
- sol = solve (prob, Optim. BFGS ())
63
+ sol = solve (prob, Optim. BFGS (); callback = CallbackTester ( length (x0); has_grad = true ) )
35
64
@test 10 * sol. objective < l1
36
65
37
- sol = solve (prob, Optim. Newton ())
66
+ sol = solve (
67
+ prob, Optim. Newton ();
68
+ callback = CallbackTester (length (x0); has_grad = true , has_hess = true )
69
+ )
38
70
@test 10 * sol. objective < l1
39
71
40
72
sol = solve (prob, Optim. KrylovTrustRegion ())
41
73
@test 10 * sol. objective < l1
42
74
43
- sol = solve (prob, Optim. BFGS (), maxiters = 1 )
75
+ sol = solve (
76
+ prob, Optim. BFGS ();
77
+ maxiters = 1 , callback = CallbackTester (length (x0); has_grad = true )
78
+ )
44
79
@test sol. original. iterations == 1
45
80
46
81
sol = solve (prob, Optim. BFGS (), maxiters = 1 , local_maxiters = 2 )
@@ -92,7 +127,8 @@ using Test
92
127
optprob = OptimizationFunction (rosenbrock, Optimization. AutoZygote ())
93
128
94
129
prob = OptimizationProblem (optprob, x0, _p, lb = [- 1.0 , - 1.0 ], ub = [0.8 , 0.8 ])
95
- sol = solve (prob, Optim. Fminbox ())
130
+ sol = solve (
131
+ prob, Optim. Fminbox (); callback = CallbackTester (length (x0); has_grad = true ))
96
132
@test 10 * sol. objective < l1
97
133
98
134
Random. seed! (1234 )
0 commit comments