Skip to content

Commit

Permalink
Add OptimizationOptimJL.Optim.BFGS() tests for issues 747, 754
Browse files Browse the repository at this point in the history
SciML#754
SciML#747

partially addressing SciML#755

NB: unrelated problem, tests using Optimization.AutoModelingToolkit() were failing,
added code
    @test (sol = solve(prob, Optim.Newton())) isa Any  # test exception not thrown
and similar so exception is caught and other tests run
  • Loading branch information
sjdaines committed May 21, 2024
1 parent a7950ac commit 764c066
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions lib/OptimizationOptimJL/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ end

@testset "OptimizationOptimJL.jl" begin
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
function rosenbrock_grad!(dx, x, p)
dx[1] = -2*(p[1] - x[1]) -4 * p[2] * (x[2] - x[1]^2)*x[1]
dx[2]= 2*p[2]*(x[2]-x[1]^2)
return nothing
end

x0 = zeros(2)
_p = [1.0, 100.0]
l1 = rosenbrock(x0, _p)
Expand Down Expand Up @@ -89,7 +95,7 @@ end
cons = cons)

prob = OptimizationProblem(optprob, x0, _p, lcons = [-5.0], ucons = [10.0])
sol = solve(prob, IPNewton())
@test (sol = solve(prob, IPNewton())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

optprob = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();
Expand Down Expand Up @@ -156,18 +162,36 @@ end
sol = solve(prob, BFGS())
@test 10 * sol.objective < l1

# https://github.com/SciML/Optimization.jl/issues/747
optprob = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
prob = OptimizationProblem(optprob, x0, _p; lb = [-1.0, -1.0], ub = [0.8, 0.8])
@test (sol = solve(prob, Optim.BFGS())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

# https://github.com/SciML/Optimization.jl/issues/754
optprob = OptimizationFunction(rosenbrock; grad=rosenbrock_grad!)
prob = OptimizationProblem(optprob, x0, _p)
@test (sol = solve(prob, Optim.BFGS())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

# https://github.com/SciML/Optimization.jl/issues/754
optprob = OptimizationFunction(rosenbrock; grad=rosenbrock_grad!)
prob = OptimizationProblem(optprob, x0, _p; lb = [-1.0, -1.0], ub = [0.8, 0.8])
@test (sol = solve(prob, Optim.BFGS())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

optprob = OptimizationFunction(rosenbrock, Optimization.AutoModelingToolkit())
prob = OptimizationProblem(optprob, x0, _p)
sol = solve(prob, Optim.BFGS())
@test (sol = solve(prob, Optim.BFGS())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

optprob = OptimizationFunction(rosenbrock,
Optimization.AutoModelingToolkit(true, false))
@test (optprob = OptimizationFunction(rosenbrock,
Optimization.AutoModelingToolkit(true, false))) isa Any # test exception not thrown
prob = OptimizationProblem(optprob, x0, _p)
sol = solve(prob, Optim.Newton())
@test (sol = solve(prob, Optim.Newton())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

sol = solve(prob, Optim.KrylovTrustRegion())
@test (sol = solve(prob, Optim.KrylovTrustRegion())) isa Any # test exception not thrown
@test 10 * sol.objective < l1

@testset "cache" begin
Expand Down

0 comments on commit 764c066

Please sign in to comment.