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

Use Jacobian Based Algorithms if user supplies custom Jacobian #279

Merged
merged 2 commits into from
Nov 7, 2023
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NonlinearSolve"
uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
authors = ["SciML"]
version = "2.8.0"
version = "2.8.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
4 changes: 2 additions & 2 deletions src/broyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ An implementation of `Broyden` with reseting and line search.

- `max_resets`: the maximum number of resets to perform. Defaults to `3`.
- `reset_tolerance`: the tolerance for the reset check. Defaults to
`sqrt(eps(eltype(u)))`.
`sqrt(eps(real(eltype(u))))`.
- `linesearch`: the line search algorithm to use. Defaults to [`LineSearch()`](@ref),
which means that no line search is performed. Algorithms from `LineSearches.jl` can be
used here directly, and they will be converted to the correct `LineSearch`. It is
Expand Down Expand Up @@ -67,7 +67,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::GeneralBroyde
u = alias_u0 ? u0 : deepcopy(u0)
fu = evaluate_f(prob, u)
J⁻¹ = __init_identity_jacobian(u, fu)
reset_tolerance = alg.reset_tolerance === nothing ? sqrt(eps(eltype(u))) :
reset_tolerance = alg.reset_tolerance === nothing ? sqrt(eps(real(eltype(u)))) :
alg.reset_tolerance
reset_check = x -> abs(x) ≤ reset_tolerance

Expand Down
46 changes: 33 additions & 13 deletions src/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

"""
RobustMultiNewton(; concrete_jac = nothing, linsolve = nothing, precs = DEFAULT_PRECS,
adkwargs...)
adkwargs...)

A polyalgorithm focused on robustness. It uses a mixture of Newton methods with different
globalizing techniques (trust region updates, line searches, etc.) in order to find a
Expand Down Expand Up @@ -212,7 +212,7 @@

"""
FastShortcutNonlinearPolyalg(; concrete_jac = nothing, linsolve = nothing,
precs = DEFAULT_PRECS, adkwargs...)
precs = DEFAULT_PRECS, must_use_jacobian::Val = Val(false), adkwargs...)

A polyalgorithm focused on balancing speed and robustness. It first tries less robust methods
for more performance and then tries more robust techniques if the faster ones fail.
Expand All @@ -238,15 +238,25 @@
[LinearSolve.jl documentation](https://docs.sciml.ai/LinearSolve/stable/).
"""
function FastShortcutNonlinearPolyalg(; concrete_jac = nothing, linsolve = nothing,
precs = DEFAULT_PRECS, adkwargs...)
algs = (GeneralKlement(; linsolve, precs),
GeneralBroyden(),
NewtonRaphson(; concrete_jac, linsolve, precs, adkwargs...),
NewtonRaphson(; concrete_jac, linsolve, precs, linesearch = BackTracking(),
adkwargs...),
TrustRegion(; concrete_jac, linsolve, precs, adkwargs...),
TrustRegion(; concrete_jac, linsolve, precs,
radius_update_scheme = RadiusUpdateSchemes.Bastin, adkwargs...))
precs = DEFAULT_PRECS, must_use_jacobian::Val{JAC} = Val(false),
adkwargs...) where {JAC}
if JAC
algs = (NewtonRaphson(; concrete_jac, linsolve, precs, adkwargs...),

Check warning on line 244 in src/default.jl

View check run for this annotation

Codecov / codecov/patch

src/default.jl#L244

Added line #L244 was not covered by tests
NewtonRaphson(; concrete_jac, linsolve, precs, linesearch = BackTracking(),
adkwargs...),
TrustRegion(; concrete_jac, linsolve, precs, adkwargs...),
TrustRegion(; concrete_jac, linsolve, precs,
radius_update_scheme = RadiusUpdateSchemes.Bastin, adkwargs...))
else
algs = (GeneralKlement(; linsolve, precs),
GeneralBroyden(),
NewtonRaphson(; concrete_jac, linsolve, precs, adkwargs...),
NewtonRaphson(; concrete_jac, linsolve, precs, linesearch = BackTracking(),
adkwargs...),
TrustRegion(; concrete_jac, linsolve, precs, adkwargs...),
TrustRegion(; concrete_jac, linsolve, precs,
radius_update_scheme = RadiusUpdateSchemes.Bastin, adkwargs...))
end
return NonlinearSolvePolyAlgorithm(algs, Val(:NLS))
end

Expand Down Expand Up @@ -288,12 +298,22 @@

## Defaults

## TODO: In the long run we want to use an `Assumptions` API like LinearSolve to specify
## the conditioning of the Jacobian and such
## Defaults to a fast and robust poly algorithm in most cases. If the user went through
## the trouble of specifying a custom jacobian function, we should use algorithms that
## can use that!

function SciMLBase.__init(prob::NonlinearProblem, ::Nothing, args...; kwargs...)
return SciMLBase.__init(prob, FastShortcutNonlinearPolyalg(), args...; kwargs...)
must_use_jacobian = Val(prob.f.jac !== nothing)
return SciMLBase.__init(prob, FastShortcutNonlinearPolyalg(; must_use_jacobian),
args...; kwargs...)
end

function SciMLBase.__solve(prob::NonlinearProblem, ::Nothing, args...; kwargs...)
return SciMLBase.__solve(prob, FastShortcutNonlinearPolyalg(), args...; kwargs...)
must_use_jacobian = Val(prob.f.jac !== nothing)
return SciMLBase.__solve(prob, FastShortcutNonlinearPolyalg(; must_use_jacobian),
args...; kwargs...)
end

function SciMLBase.__init(prob::NonlinearLeastSquaresProblem, ::Nothing, args...; kwargs...)
Expand Down
4 changes: 2 additions & 2 deletions src/klement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function perform_step!(cache::GeneralKlementCache{true})
mul!(cache.Jdu, J, _vec(du))
cache.fu .= cache.fu2 .- cache.fu
cache.fu .= _restructure(cache.fu,
(_vec(cache.fu) .- cache.Jdu) ./ max.(cache.Jᵀ²du, eps(T)))
(_vec(cache.fu) .- cache.Jdu) ./ max.(cache.Jᵀ²du, eps(real(T))))
mul!(cache.J_cache, _vec(cache.fu), _vec(du)')
cache.J_cache .*= J
mul!(cache.J_cache2, cache.J_cache, J)
Expand Down Expand Up @@ -202,7 +202,7 @@ function perform_step!(cache::GeneralKlementCache{false})
cache.Jdu = J * _vec(cache.du)
cache.fu = cache.fu2 .- cache.fu
cache.fu = _restructure(cache.fu,
(_vec(cache.fu) .- cache.Jdu) ./ max.(cache.Jᵀ²du, eps(T)))
(_vec(cache.fu) .- cache.Jdu) ./ max.(cache.Jᵀ²du, eps(real(T))))
cache.J_cache = ((_vec(cache.fu) * _vec(cache.du)') .* J) * J
cache.J = J .+ cache.J_cache

Expand Down
4 changes: 2 additions & 2 deletions src/lbroyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ An implementation of `LimitedMemoryBroyden` with reseting and line search.
- `max_resets`: the maximum number of resets to perform. Defaults to `3`.
- `reset_tolerance`: the tolerance for the reset check. Defaults to
`sqrt(eps(eltype(u)))`.
`sqrt(eps(real(eltype(u))))`.
- `threshold`: the number of vectors to store in the low rank approximation. Defaults
to `10`.
- `linesearch`: the line search algorithm to use. Defaults to [`LineSearch()`](@ref),
Expand Down Expand Up @@ -82,7 +82,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::LimitedMemory
threshold = min(alg.threshold, maxiters)
U, Vᵀ = __init_low_rank_jacobian(u, fu, threshold)
du = copy(fu)
reset_tolerance = alg.reset_tolerance === nothing ? sqrt(eps(eltype(u))) :
reset_tolerance = alg.reset_tolerance === nothing ? sqrt(eps(real(eltype(u)))) :
alg.reset_tolerance
reset_check = x -> abs(x) reset_tolerance

Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
hasmethod(issingular, Tuple{T}) && return :(issingular(x))
return :(__issingular(x))
end
__issingular(x::AbstractMatrix{T}) where {T} = cond(x) > inv(sqrt(eps(T)))
__issingular(x::AbstractMatrix{T}) where {T} = cond(x) > inv(sqrt(eps(real(T))))

Check warning on line 305 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L305

Added line #L305 was not covered by tests
__issingular(x) = false ## If SciMLOperator and such

# If factorization is LU then perform that and update the linsolve cache
Expand Down
13 changes: 11 additions & 2 deletions test/23_test_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ using NonlinearSolve, LinearAlgebra, LinearSolve, NonlinearProblemLibrary, Test
problems = NonlinearProblemLibrary.problems
dicts = NonlinearProblemLibrary.dicts

function test_on_library(problems, dicts, alg_ops, broken_tests, ϵ = 1e-4)
function test_on_library(problems, dicts, alg_ops, broken_tests, ϵ = 1e-4;
skip_tests = nothing)
for (idx, (problem, dict)) in enumerate(zip(problems, dicts))
x = dict["start"]
res = similar(x)
Expand All @@ -15,6 +16,11 @@ function test_on_library(problems, dicts, alg_ops, broken_tests, ϵ = 1e-4)
termination_condition = AbsNormTerminationMode())
problem(res, sol.u, nothing)

skip = skip_tests !== nothing && idx in skip_tests[alg]
if skip
@test_skip norm(res) ≤ ϵ
continue
end
broken = idx in broken_tests[alg] ? true : false
@test norm(res)≤ϵ broken=broken
catch
Expand Down Expand Up @@ -90,7 +96,10 @@ end
broken_tests = Dict(alg => Int[] for alg in alg_ops)
broken_tests[alg_ops[1]] = [1, 2, 4, 5, 6, 11, 12, 13, 14]

test_on_library(problems, dicts, alg_ops, broken_tests)
skip_tests = Dict(alg => Int[] for alg in alg_ops)
skip_tests[alg_ops[1]] = [22]

test_on_library(problems, dicts, alg_ops, broken_tests; skip_tests)
end

@testset "GeneralKlement 23 Test Problems" begin
Expand Down
Loading