JuliaSmoothOptimizers / NLPModelsModifiers.jl

Other
9 stars 9 forks source link

Fix ordering of slack variabels in `cons` #54

Closed tmigot closed 2 years ago

tmigot commented 2 years ago

I introduced a bug in #48 . The ordering of slack variables matters. Here is a minimum working example illustrating the issue.

using ADNLPModels, NLPModels, NLPModelsModifiers
nlp = ADNLPModel(x -> sum(x), ones(2), x -> [x[1]^2; x[2]*x[1]], [-Inf, 1.0], [1.0, Inf])
function NLPModels.cons_nln!(::ADNLPModel, x, c)
  c .= [x[1]^2; x[2]*x[1]]
end
function NLPModels.jac_nln_structure!(::ADNLPModel, rows, cols)
  rows .= [1, 1, 2, 2]
  cols .= [1, 2, 1, 2]
  return rows, cols
end
function NLPModels.jac_nln_coord!(::ADNLPModel, x, vals)
  vals .= [2*x[1], 0.0, x[2], x[1]]
  return vals
end
function NLPModels.jprod_nln!(::ADNLPModel, x, v, jv)
  jv .= [2*x[1] * v[1], x[2] * v[1] + x[1] * v[2]]
  return jv
end
x0 = [1., 2.]
snlp = SlackModel(nlp)
s0 = [1., 2., 0.5, 4.]
#=
The slack variables are implicitly ordered as `[s(low), s(upp), s(rng)]`, where
`low`, `upp` and `rng` represent the indices of the constraints of the form
``c_L ≤ c(x) < ∞``, ``-∞ < c(x) ≤ c_U`` and
``c_L ≤ c(x) ≤ c_U``, respectively.
=#
@show nlp.meta.jlow == [2]
@show nlp.meta.jupp == [1]
@show nlp.meta.jrng == []
# So the constraint becomes: x[1]^2 + x[4] ; x[2] * x[1] + x[3]
@show cons_nln(snlp, s0) == [s0[1]^2 - s0[4] ; s0[2] * s0[1] - s0[3]]
codecov[bot] commented 2 years ago

Codecov Report

Merging #54 (0da1efc) into main (50bbc20) will not change coverage. The diff coverage is 58.33%.

@@           Coverage Diff           @@
##             main      #54   +/-   ##
=======================================
  Coverage   95.28%   95.28%           
=======================================
  Files           6        6           
  Lines         742      742           
=======================================
  Hits          707      707           
  Misses         35       35           
Impacted Files Coverage Δ
src/slack-model.jl 91.68% <58.33%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 50bbc20...0da1efc. Read the comment docs.

github-actions[bot] commented 2 years ago
Package name latest stable
ADNLPModels.jl
AmplNLReader.jl
CUTEst.jl
CaNNOLeS.jl
DCI.jl
JSOSolvers.jl
LLSModels.jl
NLPModelsIpopt.jl
NLPModelsJuMP.jl
NLPModelsTest.jl
Percival.jl
QuadraticModels.jl
SolverBenchmark.jl
SolverTools.jl