Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed82008 committed Nov 15, 2023
1 parent 7b8caf1 commit 41e6258
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 60 deletions.
102 changes: 55 additions & 47 deletions src/NonconvexPercival.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ const AugLag = PercivalAlg
nt::NamedTuple
end
function PercivalOptions(; first_order = true, memory = 5, kwargs...)
return PercivalOptions(
(;first_order = first_order,
memory = memory, inity = ones,
kwargs...),
)
return PercivalOptions((;
first_order = first_order,
memory = memory,
inity = ones,
kwargs...,
),)
end
const AugLagOptions = PercivalOptions

Expand All @@ -32,18 +33,20 @@ const AugLagOptions = PercivalOptions
counter::Base.RefValue{Int}
end
function PercivalWorkspace(
model::VecModel, x0::AbstractVector = getinit(model);
options = PercivalOptions(), kwargs...,
model::VecModel,
x0::AbstractVector = getinit(model);
options = PercivalOptions(),
kwargs...,
)
problem, counter = get_percival_problem(model, copy(x0))
return PercivalWorkspace(model, problem, copy(x0), options, counter)
end
@params struct PercivalResult <: AbstractResult
minimizer
minimum
problem
result
fcalls
minimizer::Any
minimum::Any
problem::Any
result::Any
fcalls::Any
end
const AugLagWorkspace = PercivalWorkspace

Expand All @@ -52,52 +55,61 @@ function optimize!(workspace::PercivalWorkspace)
counter[] = 0
m = getnconstraints(workspace.model)
problem.meta.x0 .= x0
result = _percival(problem; options.nt...,
inity = options.nt.inity(eltype(x0), m))
result = _percival(problem; options.nt..., inity = options.nt.inity(eltype(x0), m))
result.solution = result.solution[1:length(x0)]
return PercivalResult(
copy(result.solution), result.objective, problem, result, counter[],
copy(result.solution),
result.objective,
problem,
result,
counter[],
)
end

function _percival(nlp;
function _percival(
nlp;
T = eltype(nlp.meta.x0),
μ::Real = convert(T, 10),
max_iter::Int = 1000, max_time::Real = Inf,
max_eval::Int = 100000, atol::Real = convert(T, 1e-6),
rtol::Real = convert(T, 1e-6), ctol::Real = convert(T, 1e-6),
first_order = true, memory = 5,
max_iter::Int = 1000,
max_time::Real = Inf,
max_eval::Int = 100000,
atol::Real = convert(T, 1e-6),
rtol::Real = convert(T, 1e-6),
ctol::Real = convert(T, 1e-6),
first_order = true,
memory = 5,
subsolver_logger::Percival.AbstractLogger = Percival.NullLogger(),
inity = nothing, max_cgiter = 100, subsolver_max_eval = 200, kwargs...,
inity = nothing,
max_cgiter = 100,
subsolver_max_eval = 200,
kwargs...,
)
modifier = m -> begin
op = NLPModelsModifiers.LinearOperators.LBFGSOperator(T, m.meta.nvar; mem = memory)
return NLPModelsModifiers.LBFGSModel(m.meta, m, op)
end
modifier =
m -> begin
op = NLPModelsModifiers.LinearOperators.LBFGSOperator(T, m.meta.nvar; mem = memory)
return NLPModelsModifiers.LBFGSModel(m.meta, m, op)
end
_kwargs = (
max_iter = max_iter, max_time = max_time,
max_eval = max_eval, atol = atol, rtol = rtol,
max_iter = max_iter,
max_time = max_time,
max_eval = max_eval,
atol = atol,
rtol = rtol,
subsolver_logger = subsolver_logger,
subproblem_modifier = first_order ? modifier : identity,
subsolver_max_eval = subsolver_max_eval,
subsolver_kwargs = Dict(:max_cgiter => max_cgiter),
)
if Percival.unconstrained(nlp) || Percival.bound_constrained(nlp)
return Percival.percival(
Val(:tron), nlp; _kwargs...,
)
return Percival.percival(Val(:tron), nlp; _kwargs...)
elseif Percival.equality_constrained(nlp)
return Percival.percival(
Val(:equ), nlp; inity = inity, _kwargs...,
)
return Percival.percival(Val(:equ), nlp; inity = inity, _kwargs...)
else # has inequalities
return Percival.percival(
Val(:ineq), nlp; inity = inity, _kwargs...,
)
return Percival.percival(Val(:ineq), nlp; inity = inity, _kwargs...)
end
end

function Workspace(model::VecModel, optimizer::PercivalAlg, args...; kwargs...,)
function Workspace(model::VecModel, optimizer::PercivalAlg, args...; kwargs...)
return PercivalWorkspace(model, args...; kwargs...)
end

Expand All @@ -121,14 +133,8 @@ function get_percival_problem(model::VecModel, x0::AbstractVector)
model.ineq_constraints
end
obj = CountingFunction(getobjective(model))
return get_percival_problem(
obj,
ineq,
eq,
x0,
getmin(model),
getmax(model),
), obj.counter
return get_percival_problem(obj, ineq, eq, x0, getmin(model), getmax(model)),
obj.counter
end
function get_percival_problem(obj, ineq_constr, eq_constr, x0, xlb, xub)
T = eltype(x0)
Expand Down Expand Up @@ -175,7 +181,9 @@ function zygote_nlp_model(f, x0::S, lvar::S, uvar::S, c, lcon::S, ucon::S) where
nvar = length(x0)
ncon = length(lcon)
adbackend = ADNLPModels.ADModelBackend(
nvar, f, ncon;
nvar,
f,
ncon;
gradient_backend = ADNLPModels.ZygoteADGradient,
hprod_backend = ADNLPModels.ForwardDiffADHvprod,
jprod_backend = ADNLPModels.ZygoteADJprod,
Expand All @@ -186,7 +194,7 @@ function zygote_nlp_model(f, x0::S, lvar::S, uvar::S, c, lcon::S, ucon::S) where
)
nnzh = nvar * (nvar + 1) / 2
nnzj = nvar * ncon
meta = ADNLPModels.NLPModelMeta{T, S}(
meta = ADNLPModels.NLPModelMeta{T,S}(
nvar,
x0 = x0,
lvar = lvar,
Expand Down
26 changes: 13 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NonconvexPercival, LinearAlgebra, Test

f(x::AbstractVector) = sqrt(x[2])
g(x::AbstractVector, a, b) = (a*x[1] + b)^3 - x[2]
g(x::AbstractVector, a, b) = (a * x[1] + b)^3 - x[2]

alg = AugLag()

Expand All @@ -13,8 +13,8 @@ alg = AugLag()
for first_order in (true, false)
options = AugLagOptions(first_order = first_order)
r = optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r.minimum - sqrt(8/27)) < 1e-6
@test norm(r.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r.minimizer - [1 / 3, 8 / 27]) < 1e-6
end
end

Expand All @@ -23,12 +23,12 @@ end
addvar!(m, [1e-4, 1e-4], [10.0, 10.0])
add_ineq_constraint!(m, x -> g(x, 2, 0))
add_ineq_constraint!(m, x -> g(x, -1, 1))
add_eq_constraint!(m, x -> sum(x) - 1/3 - 8/27)
add_eq_constraint!(m, x -> sum(x) - 1 / 3 - 8 / 27)
for first_order in (true, false)
options = AugLagOptions(first_order = first_order)
r = optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r.minimum - sqrt(8/27)) < 1e-6
@test norm(r.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r.minimizer - [1 / 3, 8 / 27]) < 1e-6
end
end

Expand All @@ -38,12 +38,12 @@ end
addvar!(m, T.([1e-4, 1e-4]), T.([10.0, 10.0]))
add_ineq_constraint!(m, x -> g(x, T(2), T(0)))
add_ineq_constraint!(m, x -> g(x, T(-1), T(1)))
add_eq_constraint!(m, x -> sum(x) - T(1/3) - T(8/27))
add_eq_constraint!(m, x -> sum(x) - T(1 / 3) - T(8 / 27))
for first_order in (true, false)
options = AugLagOptions(first_order = first_order)
r = optimize(m, alg, T.([1.234, 2.345]), options = options)
@test abs(r.minimum - sqrt(T(8/27))) < T(1e-6)
@test norm(r.minimizer - T.([1/3, 8/27])) < T(1e-6)
@test abs(r.minimum - sqrt(T(8 / 27))) < T(1e-6)
@test norm(r.minimizer - T.([1 / 3, 8 / 27])) < T(1e-6)
@test r.minimum isa T
@test eltype(r.minimizer) <: T
end
Expand All @@ -57,8 +57,8 @@ end
for first_order in (true, false)
options = AugLagOptions(first_order = first_order)
r = optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r.minimum - sqrt(8/27)) < 1e-6
@test norm(r.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r.minimizer - [1 / 3, 8 / 27]) < 1e-6
end
end

Expand All @@ -72,8 +72,8 @@ end
for first_order in (true, false)
options = AugLagOptions(first_order = first_order)
r = optimize(m, alg, [1.234, 2.345], options = options)
@test abs(r.minimum - sqrt(8/27)) < 1e-6
@test norm(r.minimizer - [1/3, 8/27]) < 1e-6
@test abs(r.minimum - sqrt(8 / 27)) < 1e-6
@test norm(r.minimizer - [1 / 3, 8 / 27]) < 1e-6
end
end
#=
Expand Down

0 comments on commit 41e6258

Please sign in to comment.