Fix return codes for nonlinear least squares#606
Merged
Conversation
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 |
Contributor
There was a problem hiding this comment.
[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... | ||
| ) | ||
|
|
Contributor
There was a problem hiding this comment.
[JuliaFormatter] reported by reviewdog 🐶
Suggested change
Comment on lines
+432
to
+433
| f(u,p) = [1.0] | ||
| nlf = NonlinearFunction(f; resid_prototype=zeros(1)) |
Contributor
There was a problem hiding this comment.
[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)) |
ChrisRackauckas
commented
May 12, 2025
ChrisRackauckas
commented
May 12, 2025
| sol = solve(prob) | ||
| @test SciMLBase.successful_retcode(sol) | ||
| @test sol.retcode == ReturnCode.StalledSuccess | ||
| end No newline at end of file |
Contributor
There was a problem hiding this comment.
[JuliaFormatter] reported by reviewdog 🐶
Suggested change
| end | |
| end |
ChrisRackauckas
commented
May 12, 2025
ChrisRackauckas
commented
May 13, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #459. The crux of the issue is that
f(x) = residualonly applies in the NonlinearProblem and SteadyStateProblem cases. Whenf(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|| < tolthus 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.