-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Changes from 1 commit
87c3ddc
90e74fa
1a24b24
cec2b7b
88bb9ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -160,7 +160,10 @@ end | |||||||||
InternalAPI.step!($(cache_syms[i]), args...; kwargs...) | ||||||||||
$(cache_syms[i]).nsteps += 1 | ||||||||||
if !NonlinearSolveBase.not_terminated($(cache_syms[i])) | ||||||||||
if SciMLBase.successful_retcode($(cache_syms[i]).retcode) | ||||||||||
# If a NonlinearLeastSquaresProblem StalledSuccess, try the next | ||||||||||
# solver to see if you get a lower residual | ||||||||||
if SciMLBase.successful_retcode($(cache_syms[i]).retcode) && | ||||||||||
$(cache_syms[i]).retcode != ReturnCode.StalledSuccess | ||||||||||
Comment on lines
+165
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||
cache.best = $(i) | ||||||||||
cache.force_stop = true | ||||||||||
cache.retcode = $(cache_syms[i]).retcode | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -21,6 +21,7 @@ const AbsNormModes = Union{ | |||
step_norm_trace | ||||
max_stalled_steps | ||||
u_diff_cache::uType | ||||
leastsq::Bool | ||||
end | ||||
|
||||
get_abstol(cache::NonlinearTerminationModeCache) = cache.abstol | ||||
|
@@ -36,7 +37,7 @@ function update_u!!(cache::NonlinearTerminationModeCache, u) | |||
end | ||||
|
||||
function CommonSolve.init( | ||||
::AbstractNonlinearProblem, mode::AbstractNonlinearTerminationMode, du, u, | ||||
prob::AbstractNonlinearProblem, mode::AbstractNonlinearTerminationMode, du, u, | ||||
saved_value_prototype...; abstol = nothing, reltol = nothing, kwargs... | ||||
) | ||||
T = promote_type(eltype(du), eltype(u)) | ||||
|
@@ -80,10 +81,12 @@ function CommonSolve.init( | |||
|
||||
length(saved_value_prototype) == 0 && (saved_value_prototype = nothing) | ||||
|
||||
leastsq = typeof(prob) <: NonlinearLeastSquaresProblem | ||||
|
||||
return NonlinearTerminationModeCache( | ||||
u_unaliased, ReturnCode.Default, abstol, reltol, best_value, mode, | ||||
initial_objective, objectives_trace, 0, saved_value_prototype, | ||||
u0_norm, step_norm_trace, max_stalled_steps, u_diff_cache | ||||
u0_norm, step_norm_trace, max_stalled_steps, u_diff_cache, leastsq | ||||
) | ||||
end | ||||
|
||||
|
@@ -146,6 +149,7 @@ end | |||
function (cache::NonlinearTerminationModeCache)( | ||||
mode::AbstractSafeNonlinearTerminationMode, du, u, uprev, abstol, reltol, args... | ||||
) | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||
if mode isa AbsNormSafeTerminationMode || mode isa AbsNormSafeBestTerminationMode | ||||
objective = Utils.apply_norm(mode.internalnorm, du) | ||||
criteria = abstol | ||||
|
@@ -177,7 +181,7 @@ function (cache::NonlinearTerminationModeCache)( | |||
end | ||||
|
||||
# Main Termination Criteria | ||||
if objective ≤ criteria | ||||
if !cache.leastsq && objective ≤ criteria | ||||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
cache.retcode = ReturnCode.Success | ||||
return true | ||||
end | ||||
|
@@ -195,7 +199,13 @@ function (cache::NonlinearTerminationModeCache)( | |||
min_obj, max_obj = extrema(cache.objectives_trace) | ||||
end | ||||
if min_obj < mode.min_max_factor * max_obj | ||||
cache.retcode = ReturnCode.Stalled | ||||
if cache.leastsq | ||||
# If least squares, found a local minima thus success | ||||
cache.retcode = ReturnCode.StalledSuccess | ||||
else | ||||
# Not a success if f(x)>0 and residual too high | ||||
cache.retcode = ReturnCode.Stalled | ||||
end | ||||
return true | ||||
end | ||||
end | ||||
|
@@ -209,7 +219,7 @@ function (cache::NonlinearTerminationModeCache)( | |||
end | ||||
du_norm = L2_NORM(cache.u_diff_cache) | ||||
cache.step_norm_trace[mod1(cache.nsteps, length(cache.step_norm_trace))] = du_norm | ||||
if cache.nsteps > mode.max_stalled_steps | ||||
if cache.nsteps > mode.max_stalled_steps || iszero(du_norm) | ||||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
max_step_norm = maximum(cache.step_norm_trace) | ||||
if mode isa AbsNormSafeTerminationMode || | ||||
mode isa AbsNormSafeBestTerminationMode | ||||
|
@@ -218,7 +228,11 @@ function (cache::NonlinearTerminationModeCache)( | |||
stalled_step = max_step_norm ≤ reltol * (max_step_norm + cache.u0_norm) | ||||
end | ||||
if stalled_step | ||||
cache.retcode = ReturnCode.Stalled | ||||
if cache.leastsq | ||||
cache.retcode = ReturnCode.StalledSuccess | ||||
else | ||||
cache.retcode = ReturnCode.Stalled | ||||
end | ||||
return true | ||||
end | ||||
end | ||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -427,3 +427,13 @@ end | |||||||||
|
||||||||||
@test !(solve(nlprob, NewtonRaphson()).alg.autodiff isa AutoPolyesterForwardDiff) | ||||||||||
end | ||||||||||
|
||||||||||
@testitem "NonlinearLeastSquares ReturnCode" tags=[:core] begin | ||||||||||
f(u,p) = [1.0] | ||||||||||
nlf = NonlinearFunction(f; resid_prototype=zeros(1)) | ||||||||||
Comment on lines
+432
to
+433
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||
prob = NonlinearLeastSquaresProblem(nlf, [1.0]) | ||||||||||
sol = solve(prob) | ||||||||||
@test SciMLBase.successful_retcode(sol) | ||||||||||
@test sol.retcode == ReturnCode.StalledSuccess | ||||||||||
@test sol.stats.nf == 3 | ||||||||||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
end | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
Uh oh!
There was an error while loading. Please reload this page.