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

Aperta
#150 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

  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.

Lingua principale
Julia
Stelle
7
Fork
4
Merge medio
17h 32m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaSmoothOptimizers/NLPModelsTest.jl

Tutte le issue di JuliaSmoothOptimizers/NLPModelsTest.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.