Skip to content

Fix return codes for nonlinear least squares #606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2025

Conversation

ChrisRackauckas
Copy link
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
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

@@ -146,6 +149,7 @@ end
function (cache::NonlinearTerminationModeCache)(
mode::AbstractSafeNonlinearTerminationMode, du, u, uprev, abstol, reltol, args...
)

Copy link
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 on lines +432 to +433
f(u,p) = [1.0]
nlf = NonlinearFunction(f; resid_prototype=zeros(1))
Copy link
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))

sol = solve(prob)
@test SciMLBase.successful_retcode(sol)
@test sol.retcode == ReturnCode.StalledSuccess
end
Copy link
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

@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