consistent_meta: add an absolute tolerance (zero initial points fail on rounding), and test the boolean fields

Abierto
#150 0 comentarios 0 reacciones 0 asignados Ver en GitHub

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

  1. Give consistent_meta the same absolute floor as consistent_functions, with an atol keyword that consistent_nlps passes 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 than isapprox.

  2. Test the boolean fields:

    @test fi == fj
    

    One caveat: some tests in the ecosystem may compare models that differ in these flags on purpose, for example a model without hprod against one with it. Then consistent_nlps could 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

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de JuliaSmoothOptimizers/NLPModelsTest.jl

Todos los issues de JuliaSmoothOptimizers/NLPModelsTest.jl

Issues similares

Más issues de Julia

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.