consistent_meta: add an absolute tolerance (zero initial points fail on rounding), and test the boolean fields
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 57/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Ambito
- testing
Direzione di ricerca
Read src/nlp/consistency.jl, especially consistent_meta at lines 95–123 and the consistent_functions comparison around line 182. Check existing tests for consistent_meta and how consistent_nlps passes keyword arguments; then add coverage for near-zero vector fields and boolean fields. Done means the tolerance handles the reproducer and boolean comparisons are tested, with any intended exceptions clarified.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Written by Claude (Claude Code) at the request of @ocots.
Summary
consistent_meta compares the vector fields of the meta (x0, lvar, uvar, y0, …) with a relative tolerance only. Two NLPs whose initial points are both zero, up to rounding, are therefore reported as inconsistent. The comparisons of consistent_functions already add an absolute floor; consistent_meta could do the same.
A second, smaller point: the boolean fields (grad_available, …) are read but never tested.
Where
src/nlp/consistency.jl, v0.10.9, unchanged on main: consistent_meta, lines 95–123
function consistent_meta(nlps; rtol = 1.0e-8)
fields = [:nvar, :x0, :lvar, :uvar, :ifix, :ilow, :iupp, :irng, :ifree, :ncon, :y0]
boolean_fields = [:grad_available, :jac_available, :hess_available,
:jprod_available, :jtprod_available, :hprod_available]
N = length(nlps)
for field in fields
@testset "Field $field" begin
for i = 1:(N - 1)
fi = getfield(nlps[i].meta, field)
fj = getfield(nlps[i + 1].meta, field)
@test isapprox(fi, fj, rtol = rtol) # relative only
end
end
end
for field in boolean_fields
@testset "Field $field" begin
for i = 1:(N - 1)
fi = getfield(nlps[i].meta, field)
fj = getfield(nlps[i + 1].meta, field)
# no @test: the fields are read, not compared
end
end
end
end
For vectors, isapprox(a, b; rtol) tests ‖a − b‖ ≤ rtol · max(‖a‖, ‖b‖). When both vectors are zero up to rounding, the right-hand side is itself at rounding level, and the test fails.
The functions are compared with an absolute floor instead, for example the objective at line 182:
@test isapprox(fs[i], fs[j], atol = rtol * max(fmin, 1.0))
Reproducer
using ADNLPModels, NLPModelsTest, Test
f(x) = sum(x .^ 2)
a = ADNLPModel(f, [0.0, 0.0])
b = ADNLPModel(f, [0.0, 1e-14]) # the same point, up to rounding
@testset "repro" begin
NLPModelsTest.consistent_meta([a, b])
end
Field x0: Test Failed at .../NLPModelsTest/src/nlp/consistency.jl:111
Expression: isapprox(fi, fj, rtol = rtol)
Evaluated: isapprox([0.0, 0.0], [0.0, 1.0e-14]; rtol = 1.0e-8)
Test Summary: | Pass Fail Total
repro | 10 1 11
Field x0 | 1 1
...
Field grad_available | 0
Field jac_available | 0
The last lines show the second point: the *_available fields contribute no test.
How we use it
In OptimalControlProblems.jl, we check that the JuMP model of each optimal control problem is exactly the NLP that CTDirect builds with ADNLPModels (control-toolbox/OptimalControlProblems.jl#371, control-toolbox/OptimalControlProblems.jl#166). The two NLPs have their variables and constraints in different orders, so we wrap one of them in an AbstractNLPModel that presents it in the order of the other, then call consistent_nlps([adnlp, wrapped]).
consistent_functions evaluates at x = 10 * [-(-1)^i], which is outside the domain of several of our problems (NaN from log, sqrt, divisions). We therefore recentre both NLPs the same way around their initial point: x ↦ x0 + s·x. The initial point of the recentred NLPs is then 0 for one and about 1e-13 for the other, and Field x0 fails, although the two models are the same. We work around it by recentring so that the initial point becomes 1 instead of 0. That works, but it is a trick to avoid a tolerance problem.
Suggestion
-
Give
consistent_metathe same absolute floor asconsistent_functions, with anatolkeyword thatconsistent_nlpspasses through:function consistent_meta(nlps; rtol = 1.0e-8, atol = rtol) ... @test isapprox(fi, fj, rtol = rtol, atol = atol)This reads
‖a − b‖ ≤ max(atol, rtol · max(‖a‖, ‖b‖)). A test that passes today still passes. For the integer fields (nvar,ncon,ifix, …),==would be clearer thanisapprox. -
Test the boolean fields:
@test fi == fjOne caveat: some tests in the ecosystem may compare models that differ in these flags on purpose, for example a model without
hprodagainst one with it. Thenconsistent_nlpscould take the list of flags to compare, or report a difference in them as a warning.
We are happy to open a pull request if this direction suits you.
- Lingua principale
- Julia
- Stelle
- 7
- Fork
- 4
- Merge medio
- 17h 32m
- PR unite (30g)
- 1
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di JuliaSmoothOptimizers/NLPModelsTest.jl
-
Update the tests with the counters with both legacy and linear API are implementedForse di nuovo libera @tmigot l’ha presa 483 giorni fa e non c’è nessuna pull request aperta. Apertabug
JuliaSmoothOptimizers/NLPModelsTest.jl#124 · 1 assegnatario ·
-
Add degenerate test problemsAperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
-
enhancement good first issue
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
Tutte le issue di JuliaSmoothOptimizers/NLPModelsTest.jl
Issue simili
-
Release CorleoneInverse as 0.1.1Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
SciML/Corleone.jl#200 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
JuliaAI/MLJTransforms.jl#120 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
exercism/julia#1205 · 3 commenti · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
SciML/OrdinaryDiffEq.jl#5175 ·
I maintainer di solito rispondono entro 1 giorno