Skip to content

Commit a9616e2

Browse files
committed
Add Lagrange multipliers for hess_coord! and hprod! without y in MathOptNLPModel and MathOptNLSModel
1 parent fe4e54f commit a9616e2

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/moi_nlp_model.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mutable struct MathOptNLPModel <: AbstractNLPModel{Float64, Vector{Float64}}
66
lincon::LinearConstraints
77
quadcon::QuadraticConstraints
88
nlcon::NonLinearStructure
9+
λ::Vector{Float64}
910
obj::Objective
1011
counters::Counters
1112
end
@@ -32,6 +33,7 @@ function nlp_model(moimodel::MOI.ModelLike; hessian::Bool = true, name::String =
3233

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

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

67-
return MathOptNLPModel(meta, nlp_data.evaluator, lincon, quadcon, nlcon, obj, Counters()), index_map
69+
return MathOptNLPModel(meta, nlp_data.evaluator, lincon, quadcon, nlcon, λ, obj, Counters()), index_map
6870
end
6971

7072
function NLPModels.obj(nlp::MathOptNLPModel, x::AbstractVector)
@@ -337,8 +339,7 @@ function NLPModels.hess_coord!(
337339
view(vals, (nlp.obj.nnzh + 1):(nlp.meta.nnzh)) .= 0.0
338340
end
339341
if nlp.obj.type == "NONLINEAR"
340-
λ = zeros(nlp.meta.nnln - nlp.quadcon.nquad) # Should be stored in the structure MathOptNLPModel
341-
MOI.eval_hessian_lagrangian(nlp.eval, vals, x, obj_weight, λ)
342+
MOI.eval_hessian_lagrangian(nlp.eval, vals, x, obj_weight, nlp.λ)
342343
end
343344
return vals
344345
end
@@ -389,8 +390,7 @@ function NLPModels.hprod!(
389390
coo_sym_add_mul!(nlp.obj.hessian.rows, nlp.obj.hessian.cols, nlp.obj.hessian.vals, v, hv, obj_weight)
390391
end
391392
if nlp.obj.type == "NONLINEAR"
392-
λ = zeros(nlp.meta.nnln - nlp.quadcon.nquad) # Should be stored in the structure MathOptNLPModel
393-
MOI.eval_hessian_lagrangian_product(nlp.eval, hv, x, v, obj_weight, λ)
393+
MOI.eval_hessian_lagrangian_product(nlp.eval, hv, x, v, obj_weight, nlp.λ)
394394
end
395395
return hv
396396
end

src/moi_nls_model.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mutable struct MathOptNLSModel <: AbstractNLSModel{Float64, Vector{Float64}}
1111
lincon::LinearConstraints
1212
quadcon::QuadraticConstraints
1313
nlcon::NonLinearStructure
14+
λ::Vector{Float64}
1415
counters::NLSCounters
1516
end
1617

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

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

3941
nequ = nlinequ + nnlnequ
4042
Fnnzj = linequ.nnzj + nlequ.nnzj
@@ -76,6 +78,7 @@ function MathOptNLSModel(cmodel::JuMP.Model, F; hessian::Bool = true, name::Stri
7678
lincon,
7779
quadcon,
7880
nlcon,
81+
λ,
7982
NLSCounters(),
8083
)
8184
end
@@ -483,13 +486,12 @@ function NLPModels.hess_coord!(
483486
end
484487
view(vals, (nls.lls.nnzh + 1):(nls.lls.nnzh + nls.quadcon.nnzh)) .= 0.0
485488
if nls.nls_meta.nnln > 0
486-
λ = zeros(nls.meta.nnln - nls.quadcon.nquad) # Should be stored in the structure MathOptNLSModel
487489
MOI.eval_hessian_lagrangian(
488490
nls.ceval,
489491
view(vals, (nls.lls.nnzh + nls.quadcon.nnzh + 1):(nls.meta.nnzh)),
490492
x,
491493
obj_weight,
492-
λ,
494+
nls.λ,
493495
)
494496
else
495497
view(vals, (nls.lls.nnzh + nls.quadcon.nnzh + 1):(nls.meta.nnzh)) .= 0.0
@@ -539,8 +541,7 @@ function NLPModels.hprod!(
539541
)
540542
increment!(nls, :neval_hprod)
541543
if nls.nls_meta.nnln > 0
542-
λ = zeros(nls.meta.nnln - nls.quadcon.nquad) # Should be stored in the structure MathOptNLSModel
543-
MOI.eval_hessian_lagrangian_product(nls.ceval, hv, x, v, obj_weight, λ)
544+
MOI.eval_hessian_lagrangian_product(nls.ceval, hv, x, v, obj_weight, nls.λ)
544545
end
545546
if nls.nls_meta.nlin > 0
546547
(nls.nls_meta.nnln == 0) && (hv .= 0.0)

0 commit comments

Comments
 (0)