Skip to content
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

Better convergence assessment. #244

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/nlsolve/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ function assess_convergence(x,
xtol,
ftol)
x_converged, f_converged = false, false
if !any(isnan, x_previous) && norm(x-x_previous) <= xtol
if norm(x-x_previous) <= xtol
x_converged = true
end

if maximum(abs, f) <= ftol
f_converged = true
end

converged = x_converged || f_converged

return x_converged, f_converged, converged
return x_converged, f_converged
end

function check_isfinite(x::AbstractArray)
Expand Down
6 changes: 5 additions & 1 deletion src/solvers/anderson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ end
end

# check convergence
x_converged, f_converged, converged = assess_convergence(cache.g, cache.x, fx, xtol, ftol)
x_converged, f_converged = assess_convergence(cache.g, cache.x, fx, xtol, ftol)
if any(isnan, cache.x) || any(isnan, value(df))
break
end
converged = x_converged || f_converged
converged && break

# update current iterate
Expand Down
14 changes: 8 additions & 6 deletions src/solvers/broyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ function broyden_(df::Union{NonDifferentiable, OnceDifferentiable},
Jinv = Matrix{T}(I, n, n)
check_isfinite(value(df))
it = 0
x_converged, f_converged, converged = assess_convergence(value(df), ftol)
x_converged, f_converged = assess_convergence(x, xold, value(df), NaN, ftol)
stopped = any(isnan, x) || any(isnan, value(df)) ? true : false

# FIXME: How should this flag be set?
mayterminate = false
converged = x_converged || f_converged

# Maintain a cache for line search results
# lsr = LineSearches.LineSearchResults(T)
# lsr = LineSearches.LineSearchResults(T)

tr = SolverTrace()
tracing = store_trace || show_trace || extended_trace
Expand All @@ -61,7 +61,7 @@ function broyden_(df::Union{NonDifferentiable, OnceDifferentiable},
maybe_stuck = false
max_resets = 3
resets = 0
while !converged && it < iterations
while !stopped && !converged && it < iterations

it += 1

Expand Down Expand Up @@ -99,8 +99,10 @@ function broyden_(df::Union{NonDifferentiable, OnceDifferentiable},
end

if !maybe_stuck
x_converged, f_converged, converged = assess_convergence(x, xold, value(df), xtol, ftol)
x_converged, f_converged = assess_convergence(x, xold, value(df), xtol, ftol)
converged = x_converged || f_converged
end
stopped = any(isnan, x) || any(isnan, value(df)) ? true : false

maybe_stuck = false
@broydentrace sqeuclidean(x, xold)
Expand Down
11 changes: 8 additions & 3 deletions src/solvers/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ function newton_(df::OnceDifferentiable,
check_isfinite(value(df))
vecvalue = vec(value(df))
it = 0
x_converged, f_converged, converged = assess_convergence(value(df), ftol)
x_converged, f_converged = assess_convergence(initial_x, cache.xold, value(df), NaN, ftol)
stopped = any(isnan, cache.x) || any(isnan, value(df)) ? true : false

converged = x_converged || f_converged
x_ls = copy(cache.x)
tr = SolverTrace()
tracing = store_trace || show_trace || extended_trace
Expand Down Expand Up @@ -79,7 +82,7 @@ function newton_(df::OnceDifferentiable,
end
dfo = OnceDifferentiable(fo, go!, fgo!, cache.x, zero(real(T)))

while !converged && it < iterations
while !stopped && !converged && it < iterations

it += 1

Expand Down Expand Up @@ -117,8 +120,10 @@ function newton_(df::OnceDifferentiable,
end
# fvec is here also updated in the linesearch so no need to call f again.
copyto!(cache.x, x_ls)
x_converged, f_converged, converged = assess_convergence(cache.x, cache.xold, value(df), xtol, ftol)
x_converged, f_converged = assess_convergence(cache.x, cache.xold, value(df), xtol, ftol)
stopped = any(isnan, cache.x) || any(isnan, value(df)) ? true : false

converged = x_converged || f_converged
newtontrace(sqeuclidean(cache.x, cache.xold), tracing, extended_trace, cache, df, it, tr, store_trace, show_trace)
end
return SolverResults("Newton with line-search",
Expand Down
12 changes: 9 additions & 3 deletions src/solvers/trust_region.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ function trust_region_(df::OnceDifferentiable,
check_isfinite(cache.r)

it = 0
x_converged, f_converged, converged = assess_convergence(value(df), ftol)
x_converged, f_converged = assess_convergence(initial_x, cache.xold, value(df), NaN, ftol)
stopped = any(isnan, cache.x) || any(isnan, value(df)) ? true : false

converged = x_converged || f_converged
delta = convert(real(T), NaN)
rho = convert(real(T), NaN)
if converged
Expand Down Expand Up @@ -160,7 +163,7 @@ function trust_region_(df::OnceDifferentiable,

eta = convert(real(T), 1e-4)

while !converged && it < iterations
while !stopped && !converged && it < iterations
it += 1

# Compute proposed iteration step
Expand All @@ -187,10 +190,12 @@ function trust_region_(df::OnceDifferentiable,
end
end

x_converged, f_converged, converged = assess_convergence(cache.x, cache.xold, cache.r, xtol, ftol)
x_converged, f_converged = assess_convergence(cache.x, cache.xold, cache.r, xtol, ftol)
converged = x_converged || f_converged
else
cache.x .-= cache.p
x_converged, converged = false, false

end

@trustregiontrace euclidean(cache.x, cache.xold)
Expand All @@ -203,6 +208,7 @@ function trust_region_(df::OnceDifferentiable,
elseif rho >= 0.5
delta = max(delta, 2 * wnorm(cache.d, cache.p))
end
stopped = any(isnan, cache.x) || any(isnan, value(df)) ? true : false
end

name = "Trust-region with dogleg"
Expand Down