Skip to content

Commit 5c8c889

Browse files
Add tests for AutoSparseReverseDiff
1 parent 3505679 commit 5c8c889

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/ADtests.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,45 @@ sol = solve(prob, Optim.KrylovTrustRegion())
490490

491491
sol = solve(prob, Optimisers.ADAM(0.1), maxiters = 1000)
492492
@test 10 * sol.objective < l1
493+
494+
optf = OptimizationFunction(rosenbrock, Optimization.AutoSparseReverseDiff(), cons = con2_c)
495+
optprob = Optimization.instantiate_function(optf, x0, Optimization.AutoSparseReverseDiff(),
496+
nothing, 2)
497+
G2 = Array{Float64}(undef, 2)
498+
optprob.grad(G2, x0)
499+
@test G1G2 rtol=1e-4
500+
H2 = Array{Float64}(undef, 2, 2)
501+
optprob.hess(H2, x0)
502+
@test H1H2 rtol=1e-4
503+
res = Array{Float64}(undef, 2)
504+
optprob.cons(res, x0)
505+
@test res[0.0, 0.0] atol=1e-4
506+
optprob.cons(res, [1.0, 2.0])
507+
@test res [5.0, 0.682941969615793]
508+
J = Array{Float64}(undef, 2, 2)
509+
optprob.cons_j(J, [5.0, 3.0])
510+
@test all(isapprox(J, [10.0 6.0; -0.149013 -0.958924]; rtol = 1e-3))
511+
H3 = [Array{Float64}(undef, 2, 2), Array{Float64}(undef, 2, 2)]
512+
optprob.cons_h(H3, x0)
513+
@test H3 [[2.0 0.0; 0.0 2.0], [-0.0 1.0; 1.0 0.0]]
514+
515+
optf = OptimizationFunction(rosenbrock, Optimization.AutoSparseReverseDiff())
516+
optprob = Optimization.instantiate_function(optf, x0, Optimization.AutoSparseReverseDiff(),
517+
nothing)
518+
optprob.grad(G2, x0)
519+
@test G1G2 rtol=1e-6
520+
optprob.hess(H2, x0)
521+
@test H1H2 rtol=1e-6
522+
523+
prob = OptimizationProblem(optf, x0)
524+
sol = solve(prob, Optim.BFGS())
525+
@test 10 * sol.objective < l1
526+
527+
sol = solve(prob, Optim.Newton())
528+
@test 10 * sol.objective < l1
529+
530+
sol = solve(prob, Optim.KrylovTrustRegion())
531+
@test sol.objective < l1
532+
533+
sol = solve(prob, Optimisers.ADAM(0.1), maxiters = 1000)
534+
@test 10 * sol.objective < l1

0 commit comments

Comments
 (0)