Skip to content

Fix return codes for nonlinear least squares#606

Merged
ChrisRackauckas merged 5 commits into
masterfrom
nonlinearleastsquare
May 13, 2025
Merged

Fix return codes for nonlinear least squares#606
ChrisRackauckas merged 5 commits into
masterfrom
nonlinearleastsquare

Conversation

@ChrisRackauckas
Copy link
Copy Markdown
Member

Fixes #459. The crux of the issue is that f(x) = residual only applies in the NonlinearProblem and SteadyStateProblem cases. When f(x) is a nonlinear least squares problem, finding a local minima is a solution, not a failure of the algorithm. Thus this reclassifies Stalled in NLLSQ to StalledSuccess, which makes it a successful return.

Algorithms which require the NonlinearLeastSquares solution to have ||resid|| < tol thus need to be careful with the return handling, as is done in the PR that introduces this return code SciML/SciMLBase.jl#1016. However, that's a fairly odd case because it's feasibility checking, while the normal use case for NLLSQ is for optimization, and in an optimization case there's no reason to believe you should always have a solution close to zero.

Fixes #459. The crux of the issue is that `f(x) = residual` only applies in the NonlinearProblem and SteadyStateProblem cases. When `f(x)` is a nonlinear least squares problem, finding a local minima is a solution, not a failure of the algorithm. Thus this reclassifies Stalled in NLLSQ to StalledSuccess, which makes it a successful return.

Algorithms which require the NonlinearLeastSquares solution to have `||resid|| < tol` thus need to be careful with the return handling, as is done in the PR that introduces this return code SciML/SciMLBase.jl#1016. However, that's a fairly odd case because it's feasibility checking, while the normal use case for NLLSQ is for optimization, and in an optimization case there's no reason to believe you should always have a solution close to zero.
Comment on lines +165 to +166
if SciMLBase.successful_retcode($(cache_syms[i]).retcode) &&
$(cache_syms[i]).retcode != ReturnCode.StalledSuccess
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
if SciMLBase.successful_retcode($(cache_syms[i]).retcode) &&
$(cache_syms[i]).retcode != ReturnCode.StalledSuccess
if SciMLBase.successful_retcode($(cache_syms[i]).retcode) &&
$(cache_syms[i]).retcode != ReturnCode.StalledSuccess

function (cache::NonlinearTerminationModeCache)(
mode::AbstractSafeNonlinearTerminationMode, du, u, uprev, abstol, reltol, args...
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment thread test/core_tests.jl
Comment on lines +432 to +433
f(u,p) = [1.0]
nlf = NonlinearFunction(f; resid_prototype=zeros(1))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
f(u,p) = [1.0]
nlf = NonlinearFunction(f; resid_prototype=zeros(1))
f(u, p) = [1.0]
nlf = NonlinearFunction(f; resid_prototype = zeros(1))

Comment thread lib/NonlinearSolveBase/src/termination_conditions.jl Outdated
Comment thread test/core_tests.jl Outdated
Comment thread test/core_tests.jl
sol = solve(prob)
@test SciMLBase.successful_retcode(sol)
@test sol.retcode == ReturnCode.StalledSuccess
end No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

Comment thread lib/NonlinearSolveBase/Project.toml Outdated
Comment thread lib/NonlinearSolveBase/src/termination_conditions.jl Outdated
@ChrisRackauckas ChrisRackauckas merged commit 251caa5 into master May 13, 2025
72 of 88 checks passed
@ChrisRackauckas ChrisRackauckas deleted the nonlinearleastsquare branch May 13, 2025 02:37
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.

NonlinearLeastSquaresProblem returns stalled

1 participant