Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constraints support for NOMAD #817

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions lib/OptimizationNOMAD/src/OptimizationNOMAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ using NOMAD, Optimization.SciMLBase
export NOMADOpt
struct NOMADOpt end

@enum ConstraintBarrierType ExtremeBarrierMethod ProgressiveBarrierMethod

SciMLBase.allowsbounds(::NOMADOpt) = true
SciMLBase.allowscallback(::NOMADOpt) = false
SciMLBase.allowsconstraints(::NOMADOpt) = true

function __map_optimizer_args!(prob::OptimizationProblem, opt::NOMAD.NomadProblem;
callback = nothing,
Expand Down Expand Up @@ -40,12 +43,14 @@ function __map_optimizer_args!(prob::OptimizationProblem, opt::NOMAD.NomadProble
return nothing
end

@inline strcnsmethod(m::ConstraintBarrierType) = m === ExtremeBarrierMethod ? "EB" : "PB"

function SciMLBase.__solve(prob::OptimizationProblem, opt::NOMADOpt;
maxiters::Union{Number, Nothing} = nothing,
maxtime::Union{Number, Nothing} = nothing,
abstol::Union{Number, Nothing} = nothing,
reltol::Union{Number, Nothing} = nothing,
progress = false,
cons_method = ExtremeBarrierMethod,
kwargs...)
local x

Expand All @@ -57,15 +62,27 @@ function SciMLBase.__solve(prob::OptimizationProblem, opt::NOMADOpt;
return first(x)
end

function bb(x)
l = _loss(x)
success = !isnan(l) && !isinf(l)
count_eval = true
return (success, count_eval, [l])
end

if !isnothing(prob.lcons) | !isnothing(prob.ucons)
@warn "Linear and nonlinear constraints defined in OptimizationProblem are currently not used by $(opt)"
if prob.f.cons === nothing
function bb(x)
l = _loss(x)
success = !isnan(l) && !isinf(l)
count_eval = true
return (success, count_eval, [l])
end
else
eqinds = findall(i -> prob.lcons[i] == prob.ucons[i], 1:length(prob.ucons))
function bbcons(x)
l = _loss(x)
c = zeros(eltype(x), length(prob.ucons))
prob.f.cons(c, x, prob.p)
c -= prob.ucons
if !isempty(eqinds)
c[eqinds] = abs.(c[eqinds])
end
success = !isnan(l) && !isinf(l)
count_eval = true
return (success, count_eval, vcat(l, c))
end
end

bounds = (;)
Expand All @@ -77,7 +94,13 @@ function SciMLBase.__solve(prob::OptimizationProblem, opt::NOMADOpt;
bounds = (; bounds..., upper_bound = prob.ub)
end

opt_setup = NOMAD.NomadProblem(length(prob.u0), 1, ["OBJ"], bb; bounds...)
if prob.f.cons === nothing
opt_setup = NOMAD.NomadProblem(length(prob.u0), 1, ["OBJ"], bb; bounds...)
else
opt_setup = NOMAD.NomadProblem(length(prob.u0), 1 + length(prob.ucons),
vcat("OBJ", fill(strcnsmethod(cons_method), length(prob.ucons))),
bbcons; bounds...)
end

__map_optimizer_args!(prob, opt_setup, maxiters = maxiters, maxtime = maxtime,
abstol = abstol, reltol = reltol; kwargs...)
Expand Down
15 changes: 15 additions & 0 deletions lib/OptimizationNOMAD/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@ using Test
prob = OptimizationProblem(f, x0, _p; lb = [-1.0, -1.0], ub = [1.5, 1.5])
sol = Optimization.solve(prob, NOMADOpt())
@test 10 * sol.objective < l1

cons = (res, x, p) -> (res[1] = x[1]^2 + x[2]^2; nothing)
f = OptimizationFunction(rosenbrock, cons = cons)
prob = OptimizationProblem(f, x0, _p; lcons = [-Inf], ucons = [1.0])
sol = Optimization.solve(prob, NOMADOpt(), maxiters = 5000)
@test 10 * sol.objective < l1

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

f = OptimizationFunction(rosenbrock, cons = con2_c)
prob = OptimizationProblem(f, x0, _p; lcons = [-Inf, -Inf], ucons = [0.5, 0.0])
sol = Optimization.solve(prob, NOMADOpt(), maxiters = 5000)
@test sol.objective < l1
end
Loading