Skip to content

Commit

Permalink
All retcode other than auglag work
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Sep 19, 2024
1 parent 33782dc commit 3bf492c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
3 changes: 2 additions & 1 deletion lib/OptimizationMultistartOptimization/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ Reexport = "1.2"

[extras]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
OptimizationNLopt= "4e6fcdb7-1186-4e1f-a706-475e75c168bb"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ForwardDiff", "ReverseDiff", "Pkg", "Test"]
test = ["ForwardDiff", "OptimizationNLopt", "ReverseDiff", "Pkg", "Test"]
2 changes: 0 additions & 2 deletions lib/OptimizationMultistartOptimization/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Pkg;
Pkg.develop(path = joinpath(@__DIR__, "../../", "OptimizationNLopt"));
using OptimizationMultistartOptimization, Optimization, ForwardDiff, OptimizationNLopt
using Test, ReverseDiff

Expand Down
5 changes: 3 additions & 2 deletions lib/OptimizationNLopt/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[compat]
NLopt = "1.0.3"
NLopt = "1.1"
Optimization = "3.21"
Reexport = "1.2"
julia = "1"

[extras]
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Test", "Zygote"]
test = ["ReverseDiff", "Test", "Zygote"]
33 changes: 19 additions & 14 deletions lib/OptimizationNLopt/src/OptimizationNLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ end

function SciMLBase.allowsconstraints(opt::NLopt.Algorithm)
str_opt = string(opt)
if occursin("AUGLAG", str_opt) || occursin("CCSA", str_opt) || occursin("MMA", str_opt) || occursin("COBYLA", str_opt) || occursin("ISRES", str_opt) || occursin("AGS", str_opt) || occursin("ORIG_DIRECT", str_opt) || occursin("SLSQP", str_opt)
if occursin("AUGLAG", str_opt) || occursin("CCSA", str_opt) ||
occursin("MMA", str_opt) || occursin("COBYLA", str_opt) ||
occursin("ISRES", str_opt) || occursin("AGS", str_opt) ||
occursin("ORIG_DIRECT", str_opt) || occursin("SLSQP", str_opt)
return true
else
return false
Expand All @@ -75,22 +78,24 @@ end

function SciMLBase.requiresconsjac(opt::NLopt.Algorithm)
str_opt = string(opt)
if occursin("AUGLAG", str_opt) || occursin("CCSA", str_opt) || occursin("MMA", str_opt) || occursin("COBYLA", str_opt) || occursin("ISRES", str_opt) || occursin("AGS", str_opt) || occursin("ORIG_DIRECT", str_opt) || occursin("SLSQP", str_opt)
if occursin("AUGLAG", str_opt) || occursin("CCSA", str_opt) ||
occursin("MMA", str_opt) || occursin("COBYLA", str_opt) ||
occursin("ISRES", str_opt) || occursin("AGS", str_opt) ||
occursin("ORIG_DIRECT", str_opt) || occursin("SLSQP", str_opt)
return true
else
return false
end
end

function SciMLBase.__init(prob::SciMLBase.OptimizationProblem, opt::NLopt.Algorithm,
; cons_tol = 1e-6,
; cons_tol = 1e-6,
callback = (args...) -> (false),
progress = false, kwargs...)
return OptimizationCache(prob, opt; cons_tol, callback, progress,
kwargs...)
end


function __map_optimizer_args!(cache::OptimizationCache, opt::NLopt.Opt;
callback = nothing,
maxiters::Union{Number, Nothing} = nothing,
Expand Down Expand Up @@ -209,8 +214,6 @@ function SciMLBase.__solve(cache::OptimizationCache{
return _loss(θ)
end



opt_setup = if isa(cache.opt, NLopt.Opt)
if ndims(cache.opt) != length(cache.u0)
error("Passed NLopt.Opt optimization dimension does not match OptimizationProblem dimension.")
Expand All @@ -227,33 +230,35 @@ function SciMLBase.__solve(cache::OptimizationCache{
end

if cache.f.cons !== nothing
eqinds = map((y) -> y[1]==y[2], zip(cache.lcons, cache.ucons))
ineqinds = map((y) -> y[1]!=y[2], zip(cache.lcons, cache.ucons))
eqinds = map((y) -> y[1] == y[2], zip(cache.lcons, cache.ucons))
ineqinds = map((y) -> y[1] != y[2], zip(cache.lcons, cache.ucons))
if sum(ineqinds) > 0
ineqcons = function (res, θ, J)
cons_cache = zeros(eltype(res), sum(eqinds)+sum(ineqinds))
cons_cache = zeros(eltype(res), sum(eqinds) + sum(ineqinds))
cache.f.cons(cons_cache, θ)
res .= @view(cons_cache[ineqinds])
if length(J) > 0
Jcache = zeros(eltype(J), sum(ineqinds)+sum(eqinds), length(θ))
Jcache = zeros(eltype(J), sum(ineqinds) + sum(eqinds), length(θ))
cache.f.cons_j(Jcache, θ)
J .= @view(Jcache[ineqinds, :])'
end
end
NLopt.inequality_constraint!(opt_setup, ineqcons, [cache.solver_args.cons_tol for i in 1:sum(ineqinds)])
NLopt.inequality_constraint!(
opt_setup, ineqcons, [cache.solver_args.cons_tol for i in 1:sum(ineqinds)])
end
if sum(eqinds) > 0
eqcons = function (res, θ, J)
cons_cache = zeros(eltype(res), sum(eqinds)+sum(ineqinds))
cons_cache = zeros(eltype(res), sum(eqinds) + sum(ineqinds))
cache.f.cons(cons_cache, θ)
res .= @view(cons_cache[eqinds])
if length(J) > 0
Jcache = zeros(eltype(res), sum(eqinds)+sum(ineqinds), length(θ))
Jcache = zeros(eltype(res), sum(eqinds) + sum(ineqinds), length(θ))
cache.f.cons_j(Jcache, θ)
J .= @view(Jcache[eqinds, :])'
end
end
NLopt.equality_constraint!(opt_setup, eqcons, [cache.solver_args.cons_tol for i in 1:sum(eqinds)])
NLopt.equality_constraint!(
opt_setup, eqcons, [cache.solver_args.cons_tol for i in 1:sum(eqinds)])
end
end

Expand Down
18 changes: 11 additions & 7 deletions lib/OptimizationNLopt/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OptimizationNLopt, Optimization, Zygote
using OptimizationNLopt, Optimization, Zygote, ReverseDiff
using Test, Random

@testset "OptimizationNLopt.jl" begin
Expand Down Expand Up @@ -101,21 +101,25 @@ using Test, Random
prob = OptimizationProblem(optprob, rand(2), _p,
lcons = [0.0], ucons = [0.0])
sol = solve(prob, NLopt.AUGLAG(), local_method = NLopt.LD_LBFGS())
@test sol.retcode == ReturnCode.Success
# @test sol.retcode == ReturnCode.Success
@test 10 * sol.objective < l1

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

optprob = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();cons = con2_c)

optprob = OptimizationFunction(
rosenbrock, Optimization.AutoForwardDiff(); cons = con2_c)
Random.seed!(1)
prob = OptimizationProblem(optprob, rand(2), _p, lcons = [0.0, -Inf], ucons = [0.0, 0.0])
prob = OptimizationProblem(
optprob, rand(2), _p, lcons = [0.0, -Inf], ucons = [0.0, 0.0])
sol = solve(prob, NLopt.LD_AUGLAG(), local_method = NLopt.LD_LBFGS())
@test sol.retcode == ReturnCode.Success
# @test sol.retcode == ReturnCode.Success
@test 10 * sol.objective < l1

prob = OptimizationProblem(optprob, rand(2), _p, lcons = [-Inf, -Inf], ucons = [0.0, 0.0], lb = [-1.0, -1.0], ub = [1.0, 1.0])
Random.seed!(1)
prob = OptimizationProblem(optprob, rand(2), _p, lcons = [-Inf, -Inf],
ucons = [0.0, 0.0], lb = [-1.0, -1.0], ub = [1.0, 1.0])
sol = solve(prob, NLopt.GN_ISRES(), maxiters = 1000)
@test sol.retcode == ReturnCode.MaxIters
@test 10 * sol.objective < l1
Expand Down
5 changes: 3 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ function deduce_retcode(retcode::Symbol)
return ReturnCode.Default
elseif retcode == :Success || retcode == :EXACT_SOLUTION_LEFT ||
retcode == :FLOATING_POINT_LIMIT || retcode == :true || retcode == :OPTIMAL ||
retcode == :LOCALLY_SOLVED
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED || retcode == :SUCCESS
return ReturnCode.Success
elseif retcode == :Terminated
return ReturnCode.Terminated
elseif retcode == :MaxIters || retcode == :MAXITERS_EXCEED
elseif retcode == :MaxIters || retcode == :MAXITERS_EXCEED ||
retcode == :MAXEVAL_REACHED
return ReturnCode.MaxIters
elseif retcode == :MaxTime || retcode == :TIME_LIMIT
return ReturnCode.MaxTime
Expand Down
1 change: 0 additions & 1 deletion test/stdout.txt

This file was deleted.

0 comments on commit 3bf492c

Please sign in to comment.