Skip to content

Commit

Permalink
Add Lagrange multipliers for hess_coord! and hprod! without y in Math…
Browse files Browse the repository at this point in the history
…OptNLPModel and MathOptNLSModel
  • Loading branch information
amontoison committed Jun 20, 2024
1 parent 547401a commit 9952897
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/moi_nlp_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mutable struct MathOptNLPModel <: AbstractNLPModel{Float64, Vector{Float64}}
lincon::LinearConstraints
quadcon::QuadraticConstraints
nlcon::NonLinearStructure
λ::Vector{Float64}
obj::Objective
counters::Counters
end
Expand All @@ -32,6 +33,7 @@ function nlp_model(moimodel::MOI.ModelLike; hessian::Bool = true, name::String =

nlp_data = _nlp_block(moimodel)
nnln, nlcon, nl_lcon, nl_ucon = parser_NL(nlp_data, hessian = hessian)
λ = zeros(nnln - quadcon.nquad) # Lagrange multipliers for hess_coord! and hprod! without y

if nlp_data.has_objective
obj = Objective("NONLINEAR", 0.0, spzeros(Float64, nvar), COO(), 0)
Expand Down Expand Up @@ -64,7 +66,7 @@ function nlp_model(moimodel::MOI.ModelLike; hessian::Bool = true, name::String =
name = name,
)

return MathOptNLPModel(meta, nlp_data.evaluator, lincon, quadcon, nlcon, obj, Counters()), index_map
return MathOptNLPModel(meta, nlp_data.evaluator, lincon, quadcon, nlcon, λ, obj, Counters()), index_map
end

function NLPModels.obj(nlp::MathOptNLPModel, x::AbstractVector)
Expand Down Expand Up @@ -337,8 +339,7 @@ function NLPModels.hess_coord!(
view(vals, (nlp.obj.nnzh + 1):(nlp.meta.nnzh)) .= 0.0
end
if nlp.obj.type == "NONLINEAR"
λ = zeros(nlp.meta.nnln - nlp.quadcon.nquad) # Should be stored in the structure MathOptNLPModel
MOI.eval_hessian_lagrangian(nlp.eval, vals, x, obj_weight, λ)
MOI.eval_hessian_lagrangian(nlp.eval, vals, x, obj_weight, nlp.λ)
end
return vals
end
Expand Down Expand Up @@ -389,8 +390,7 @@ function NLPModels.hprod!(
coo_sym_add_mul!(nlp.obj.hessian.rows, nlp.obj.hessian.cols, nlp.obj.hessian.vals, v, hv, obj_weight)
end
if nlp.obj.type == "NONLINEAR"
λ = zeros(nlp.meta.nnln - nlp.quadcon.nquad) # Should be stored in the structure MathOptNLPModel
MOI.eval_hessian_lagrangian_product(nlp.eval, hv, x, v, obj_weight, λ)
MOI.eval_hessian_lagrangian_product(nlp.eval, hv, x, v, obj_weight, nlp.λ)
end
return hv
end
9 changes: 5 additions & 4 deletions src/moi_nls_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mutable struct MathOptNLSModel <: AbstractNLSModel{Float64, Vector{Float64}}
lincon::LinearConstraints
quadcon::QuadraticConstraints
nlcon::NonLinearStructure
λ::Vector{Float64}
counters::NLSCounters
end

Expand All @@ -35,6 +36,7 @@ function MathOptNLSModel(cmodel::JuMP.Model, F; hessian::Bool = true, name::Stri

nlp_data = _nlp_block(moimodel)
nnln, nlcon, nl_lcon, nl_ucon = parser_NL(nlp_data, hessian = hessian)
λ = zeros(nnln - quadcon.nquad) # Lagrange multipliers for hess_coord! and hprod! without y

nequ = nlinequ + nnlnequ
Fnnzj = linequ.nnzj + nlequ.nnzj
Expand Down Expand Up @@ -76,6 +78,7 @@ function MathOptNLSModel(cmodel::JuMP.Model, F; hessian::Bool = true, name::Stri
lincon,
quadcon,
nlcon,
λ,
NLSCounters(),
)
end
Expand Down Expand Up @@ -483,13 +486,12 @@ function NLPModels.hess_coord!(
end
view(vals, (nls.lls.nnzh + 1):(nls.lls.nnzh + nls.quadcon.nnzh)) .= 0.0
if nls.nls_meta.nnln > 0
λ = zeros(nls.meta.nnln - nls.quadcon.nquad) # Should be stored in the structure MathOptNLSModel
MOI.eval_hessian_lagrangian(
nls.ceval,
view(vals, (nls.lls.nnzh + nls.quadcon.nnzh + 1):(nls.meta.nnzh)),
x,
obj_weight,
λ,
nls.λ,
)
else
view(vals, (nls.lls.nnzh + nls.quadcon.nnzh + 1):(nls.meta.nnzh)) .= 0.0
Expand Down Expand Up @@ -539,8 +541,7 @@ function NLPModels.hprod!(
)
increment!(nls, :neval_hprod)
if nls.nls_meta.nnln > 0
λ = zeros(nls.meta.nnln - nls.quadcon.nquad) # Should be stored in the structure MathOptNLSModel
MOI.eval_hessian_lagrangian_product(nls.ceval, hv, x, v, obj_weight, λ)
MOI.eval_hessian_lagrangian_product(nls.ceval, hv, x, v, obj_weight, nls.λ)
end
if nls.nls_meta.nlin > 0
(nls.nls_meta.nnln == 0) && (hv .= 0.0)
Expand Down

0 comments on commit 9952897

Please sign in to comment.