Skip to content

Commit

Permalink
Add constrained test:
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Apr 25, 2024
1 parent e70bd46 commit 6d38326
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
29 changes: 5 additions & 24 deletions test/lbfgsb.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Optimization
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff, Tracker
using ModelingToolkit, Enzyme, Random
using ForwardDiff, Zygote, ReverseDiff, FiniteDiff
using Test

x0 = zeros(2)
Expand All @@ -11,30 +10,12 @@ optf = OptimizationFunction(rosenbrock, AutoEnzyme())
prob = OptimizationProblem(optf, x0)
@time res = solve(prob, Optimization.LBFGS(), maxiters = 100)

@test res.u[1.0, 1.0] atol=1e-3

optf = OptimizationFunction(rosenbrock, AutoZygote())
prob = OptimizationProblem(optf, x0, lb = [0.0, 0.0], ub = [0.3, 0.3])
res = solve(prob, Optimization.LBFGS(), maxiters = 100)

@test res.u[0.3, 0.09] atol=1e-3

function con2_c(res, x, p)
res .= [x[1]^2 + x[2]^2, (x[2] * sin(x[1]) + x[1])-5]
end

optf = OptimizationFunction(rosenbrock, AutoForwardDiff(), cons = con2_c)
prob = OptimizationProblem(optf, x0, lcons = [1.0, -Inf], ucons = [1.0, 0.0], lb = [-1.0, -1.0], ub = [1.0, 1.0])
res = solve(prob, Optimization.LBFGS(), maxiters = 100)

@test res.objective < l1
cons_cache = [0.,0.]
con2_c(cons_cache, res.u, res.cache.p)
@test cons_cache[1] 1.0 atol=1e-3
@test cons_cache[2] < 0.0

optf = OptimizationFunction(rosenbrock, AutoZygote(), cons = con2_c)
prob = OptimizationProblem(optf, x0, lcons = [1.0, -Inf], ucons = [1.0, 0.0], lb = [-1.0, -1.0], ub = [1.0, 1.0])
res = solve(prob, Optimization.LBFGS(), maxiters = 100)

@test res.objective < l1
prob = OptimizationProblem(optf, x0, lcons = [1.0, -Inf],
ucons = [1.0, 0.0], lb = [-1.0, -1.0],
ub = [1.0, 1.0])
@time res = solve(prob, Optimization.LBFGS(), maxiters = 100)
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ end
VERSION >= v"1.9" && @safetestset "AD Performance Regression Tests" begin
include("AD_performance_regression.jl")
end
@safetestset "Optimization" begin
include("lbfgsb.jl")
end
@safetestset "Mini batching" begin
include("minibatch.jl")
end
Expand Down

0 comments on commit 6d38326

Please sign in to comment.