consistent_meta: add an absolute tolerance (zero initial points fail on rounding), and test the boolean fields
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 57/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Área
- testing
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Julia
- Estrellas
- 7
- Forks
- 4
- Merge medio
- 17 h 32 min
- PR fusionados (30 d)
- 1
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de JuliaSmoothOptimizers/NLPModelsTest.jl
-
Update the tests with the counters with both legacy and linear API are implementedQuizá libre de nuevo @tmigot la tomó hace 483 días y no hay ningún pull request abierto. Abiertobug
JuliaSmoothOptimizers/NLPModelsTest.jl#124 · 1 asignado ·
-
Add degenerate test problemsAbierto
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
-
enhancement good first issue
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 25/100
Todos los issues de JuliaSmoothOptimizers/NLPModelsTest.jl
Issues similares
-
Release CorleoneInverse as 0.1.1Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
SciML/Corleone.jl#200 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
JuliaAI/MLJTransforms.jl#120 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
exercism/julia#1205 · 3 comentarios · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
SciML/OrdinaryDiffEq.jl#5175 ·
Los mantenedores suelen responder en 1 día