Skip to content

Fix bounds transform for problems with initialization data and quasi-Newton algorithms - #955

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:claude/bounds-init-and-quasinewton
Jun 5, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:claude/bounds-init-and-quasinewton

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Note

Draft — please ignore until reviewed by @ChrisRackauckas.

Two bugs in the box-constraint bounds transform (lib/NonlinearSolveBase/src/bounds_transform.jl), both surfaced while wiring ModelingToolkit's bounds metadata through to nonlinear solvers (SciML/ModelingToolkit.jl#4308).

1. alg.autodiff FieldError on quasi-Newton algorithms

transform_bounded_problem unconditionally read alg.autodiff to choose the cache type. QuasiNewtonAlgorithm (Broyden/Klement, reached as part of the default FastShortcutNonlinearPolyalg) has no autodiff field, so solving a bounded problem with the default polyalgorithm threw:

FieldError: type QuasiNewtonAlgorithm has no field `autodiff`

Fixed by guarding the access with hasproperty.

2. Initialization data corrupts the transformed solution

The wrapped problem function carried over the original initialization_data, which is defined in the bounded coordinates. The transform reparametrizes the problem into an unbounded variable t; running the bounded-space initialization on the transformed problem interprets the iterate t as a bounded state and corrupts the result.

Concretely, every ModelingToolkit NonlinearProblem attaches initialization data, so a bounded MTK problem converged to from_unbounded(root) instead of root:

# x^2 - 4x + 3, root in [0,2] is x=1
solve(prob).u   # => [1.4621...]  ==  2*logistic(1.0)  ==  from_unbounded(1.0)   (wrong)

Initialization is now run once in the original bounded space (run_bounded_initialization) before the transform, and stripped from the transformed function (build_initializeprob = Val{false}) so it isn't re-run in t-space. Raw problems without initialization data are unaffected.

Tests

Dependencies

Builds on SciML/SciMLBase.jl#1372 (so remake preserves lb/ub, which the transform's solution-reconstruction relies on).

🤖 Generated with Claude Code

…Newton algs

Two bugs in the box-constraint bounds transform:

1. `transform_bounded_problem` unconditionally read `alg.autodiff`, which threw a
   `FieldError` for algorithms without that field (e.g. `QuasiNewtonAlgorithm`,
   reached as part of the default polyalgorithm). Guard the access.

2. The wrapped function carried over the original `initialization_data`, which is
   defined in the *bounded* coordinates. Running it on the transformed problem
   interpreted the unbounded iterate `t` as a bounded state and corrupted the
   solution (e.g. ModelingToolkit `NonlinearProblem`s, which always attach
   initialization data, converged to `from_unbounded(root)` instead of `root`).
   Initialization is now run once in the original bounded space before
   transforming, and stripped from the transformed function so it isn't re-run
   in `t`-space.

Co-Authored-By: Chris Rackauckas <[email protected]>
The umbrella `test/bounds_tests.jl` integration test exercised the fix through a
full solve, but on Julia < 1.11 the umbrella resolves the *registered*
NonlinearSolveBase (the `[sources]` path redirect to the in-repo sublibrary is
ignored there), so the test ran against unpatched code and failed.

Add a `transform_bounded_problem` unit test to NonlinearSolveBase's own test
suite, which always runs against the in-repo code (incl. on lts), covering the
quasi-Newton `autodiff`-access guard with a dummy algorithm that has no
`autodiff` field. Gate the umbrella integration test on the loaded
NonlinearSolveBase version so it only runs where the fix is present.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Pushed a follow-up addressing the Tests (core, lts) failure.

Cause: the umbrella test/bounds_tests.jl integration test exercised the fix through a full solve, but on Julia 1.10 (lts) the [sources] path redirect to the in-repo lib/NonlinearSolveBase is ignored, so the umbrella resolved the registered (unpatched) NonlinearSolveBase and hit the very alg.autodiff FieldError this PR fixes.

Fix: moved the regression coverage into NonlinearSolveBase's own test suite (lib/NonlinearSolveBase/test/bounds_transform.jl), which always runs against the in-repo code (Sublibrary CI, incl. lts) — it calls transform_bounded_problem with a dummy algorithm lacking an autodiff field. The umbrella full-solve test is now gated on pkgversion(NonlinearSolveBase) >= v"2.30.3" so it only runs where the fix is actually loaded.

Unrelated pre-existing failure: Tests (nopre, lts) fails on the "Out-of-place/Inplace Matrix Resizing" testitems (test/core_tests.jl), which assert vec(solve(matrix_prob).u) == solve(vec_prob).u with exact ==. The two solves differ by ~1 ULP on Julia 1.10 (e.g. 1.414213562373095 vs …51). This is a fragile exact-equality comparison across the matrix/vector reshape path, independent of this PR (it touches neither core_tests.jl nor the solver internals, and on lts this PR's code isn't even loaded).

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review June 5, 2026 16:35
@ChrisRackauckas
ChrisRackauckas merged commit 7324957 into SciML:master Jun 5, 2026
34 of 56 checks passed
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/ModelingToolkit.jl that referenced this pull request Jun 5, 2026
The bounds forwarded to `NonlinearProblem`/`NonlinearLeastSquaresProblem` are
only retained through the `remake` that runs during problem construction as of
SciMLBase 3.18 (SciML/SciMLBase.jl#1372); on older versions they are silently
dropped. The solve-time correctness for bounded problems carrying initialization
data is provided by NonlinearSolveBase 2.30.3 (SciML/NonlinearSolve.jl#955),
which the test environment resolves to.

Co-Authored-By: Chris Rackauckas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants