Skip to content

Commit f7f6332

Browse files
tansongchenavik-pal
authored andcommitted
Add Halley's method via descent API
1 parent 810eeb3 commit f7f6332

File tree

8 files changed

+154
-6
lines changed

8 files changed

+154
-6
lines changed

Project.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
2525
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
2626
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2727
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
28+
SciMLJacobianOperators = "19f34311-ddf3-4b8b-af20-060888a46c0e"
2829
SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7"
2930
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3031
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"
@@ -113,6 +114,7 @@ StaticArrays = "1.9"
113114
StaticArraysCore = "1.4"
114115
Sundials = "4.23.1"
115116
SymbolicIndexingInterface = "0.3.31"
117+
TaylorDiff = "0.3"
116118
Test = "1.10"
117119
Zygote = "0.6.69"
118120
julia = "1.10"
@@ -146,8 +148,9 @@ SpeedMapping = "f1835b91-879b-4a3f-a438-e4baacf14412"
146148
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
147149
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
148150
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
151+
TaylorDiff = "b36ab563-344f-407b-a36a-4f200bebf99c"
149152
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
150153
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
151154

152155
[targets]
153-
test = ["Aqua", "BandedMatrices", "BenchmarkTools", "CUDA", "Enzyme", "ExplicitImports", "FastLevenbergMarquardt", "FixedPointAcceleration", "Hwloc", "InteractiveUtils", "LeastSquaresOptim", "LineSearches", "MINPACK", "NLSolvers", "NLsolve", "NaNMath", "NonlinearProblemLibrary", "OrdinaryDiffEqTsit5", "PETSc", "Pkg", "Random", "ReTestItems", "SIAMFANLEquations", "SparseConnectivityTracer", "SpeedMapping", "StableRNGs", "StaticArrays", "Sundials", "Test", "Zygote"]
156+
test = ["Aqua", "BandedMatrices", "BenchmarkTools", "CUDA", "Enzyme", "ExplicitImports", "FastLevenbergMarquardt", "FixedPointAcceleration", "Hwloc", "InteractiveUtils", "LeastSquaresOptim", "LineSearches", "MINPACK", "NLSolvers", "NLsolve", "NaNMath", "NonlinearProblemLibrary", "OrdinaryDiffEqTsit5", "PETSc", "Pkg", "Random", "ReTestItems", "SIAMFANLEquations", "SparseConnectivityTracer", "SpeedMapping", "StableRNGs", "StaticArrays", "Sundials", "TaylorDiff", "Test", "Zygote"]

lib/NonlinearSolveBase/Project.toml

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ LineSearch = "87fe0de2-c867-4266-b59a-2f0a94fc965b"
3535
LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae"
3636
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3737
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"
38+
TaylorDiff = "b36ab563-344f-407b-a36a-4f200bebf99c"
3839

3940
[extensions]
4041
NonlinearSolveBaseBandedMatricesExt = "BandedMatrices"
@@ -44,6 +45,7 @@ NonlinearSolveBaseLineSearchExt = "LineSearch"
4445
NonlinearSolveBaseLinearSolveExt = "LinearSolve"
4546
NonlinearSolveBaseSparseArraysExt = "SparseArrays"
4647
NonlinearSolveBaseSparseMatrixColoringsExt = "SparseMatrixColorings"
48+
NonlinearSolveBaseTaylorDiffExt = "TaylorDiff"
4749

4850
[compat]
4951
ADTypes = "1.9"
@@ -77,6 +79,7 @@ SparseArrays = "1.10"
7779
SparseMatrixColorings = "0.4.5"
7880
StaticArraysCore = "1.4"
7981
SymbolicIndexingInterface = "0.3.31"
82+
TaylorDiff = "0.3"
8083
Test = "1.10"
8184
TimerOutputs = "0.5.23"
8285
julia = "1.10"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module NonlinearSolveBaseTaylorDiffExt
2+
using SciMLBase: NonlinearFunction
3+
using NonlinearSolveBase: HalleyDescentCache
4+
import NonlinearSolveBase: evaluate_hvvp
5+
using TaylorDiff: derivative, derivative!
6+
using FastClosures: @closure
7+
8+
function evaluate_hvvp(
9+
hvvp, cache::HalleyDescentCache, f::NonlinearFunction{iip}, p, u, δu) where {iip}
10+
if iip
11+
binary_f = @closure (y, x) -> f(y, x, p)
12+
derivative!(hvvp, binary_f, cache.fu, u, δu, Val(2))
13+
else
14+
unary_f = Base.Fix2(f, p)
15+
hvvp = derivative(unary_f, u, δu, Val(2))
16+
end
17+
hvvp
18+
end
19+
20+
end

lib/NonlinearSolveBase/src/NonlinearSolveBase.jl

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ include("polyalg.jl")
5151

5252
include("descent/common.jl")
5353
include("descent/newton.jl")
54+
include("descent/halley.jl")
5455
include("descent/steepest.jl")
5556
include("descent/damped_newton.jl")
5657
include("descent/dogleg.jl")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
HalleyDescent(; linsolve = nothing)
3+
4+
Improve the NewtonDescent with higher-order terms. First compute the descent direction as ``J a = -fu``.
5+
Then compute the hessian-vector-vector product and solve for the second-order correction term as ``J b = H a a``.
6+
Finally, compute the descent direction as ``δu = a * a / (b / 2 - a)``.
7+
8+
Note that `import TaylorDiff` is required to use this descent algorithm.
9+
10+
See also [`NewtonDescent`](@ref).
11+
"""
12+
@kwdef @concrete struct HalleyDescent <: AbstractDescentDirection
13+
linsolve = nothing
14+
end
15+
16+
supports_line_search(::HalleyDescent) = true
17+
18+
@concrete mutable struct HalleyDescentCache <: AbstractDescentCache
19+
f
20+
p
21+
δu
22+
δus
23+
b
24+
fu
25+
hvvp
26+
lincache
27+
timer
28+
preinverted_jacobian <: Union{Val{false}, Val{true}}
29+
end
30+
31+
@internal_caches HalleyDescentCache :lincache
32+
33+
function InternalAPI.init(
34+
prob::NonlinearProblem, alg::HalleyDescent, J, fu, u; stats,
35+
shared = Val(1), pre_inverted::Val = Val(false),
36+
linsolve_kwargs = (;), abstol = nothing, reltol = nothing,
37+
timer = get_timer_output(), kwargs...)
38+
@bb δu = similar(u)
39+
@bb b = similar(u)
40+
@bb fu = similar(fu)
41+
@bb hvvp = similar(fu)
42+
δus = Utils.unwrap_val(shared) 1 ? nothing : map(2:Utils.unwrap_val(shared)) do i
43+
@bb δu_ = similar(u)
44+
end
45+
lincache = Utils.unwrap_val(pre_inverted) ? nothing :
46+
construct_linear_solver(
47+
alg, alg.linsolve, J, Utils.safe_vec(fu), Utils.safe_vec(u);
48+
stats, abstol, reltol, linsolve_kwargs...
49+
)
50+
return HalleyDescentCache(
51+
prob.f, prob.p, δu, δus, b, fu, hvvp, lincache, timer, pre_inverted)
52+
end
53+
54+
function InternalAPI.solve!(
55+
cache::HalleyDescentCache, J, fu, u, idx::Val = Val(1);
56+
skip_solve::Bool = false, new_jacobian::Bool = true, kwargs...)
57+
δu = SciMLBase.get_du(cache, idx)
58+
skip_solve && return DescentResult(; δu)
59+
if preinverted_jacobian(cache)
60+
@assert J!==nothing "`J` must be provided when `pre_inverted = Val(true)`."
61+
@bb δu = J × vec(fu)
62+
else
63+
@static_timeit cache.timer "linear solve 1" begin
64+
linres = cache.lincache(;
65+
A = J, b = Utils.safe_vec(fu),
66+
kwargs..., linu = Utils.safe_vec(δu),
67+
reuse_A_if_factorization = !new_jacobian || (idx !== Val(1)))
68+
δu = Utils.restructure(SciMLBase.get_du(cache, idx), linres.u)
69+
if !linres.success
70+
set_du!(cache, δu, idx)
71+
return DescentResult(; δu, success = false, linsolve_success = false)
72+
end
73+
end
74+
end
75+
b = cache.b
76+
# compute the hessian-vector-vector product
77+
hvvp = evaluate_hvvp(cache.hvvp, cache, cache.f, cache.p, u, δu)
78+
# second linear solve, reuse factorization if possible
79+
if preinverted_jacobian(cache)
80+
@bb b = J × vec(hvvp)
81+
else
82+
@static_timeit cache.timer "linear solve 2" begin
83+
linres = cache.lincache(;
84+
A = J, b = Utils.safe_vec(hvvp),
85+
kwargs..., linu = Utils.safe_vec(b),
86+
reuse_A_if_factorization = true)
87+
b = Utils.restructure(cache.b, linres.u)
88+
if !linres.success
89+
set_du!(cache, δu, idx)
90+
return DescentResult(; δu, success = false, linsolve_success = false)
91+
end
92+
end
93+
end
94+
@bb @. δu = δu * δu / (b / 2 - δu)
95+
set_du!(cache, δu, idx)
96+
cache.b = b
97+
return DescentResult(; δu)
98+
end
99+
100+
evaluate_hvvp(hvvp, cache, f, p, u, δu) = error("not implemented. please import TaylorDiff")

lib/NonlinearSolveFirstOrder/src/NonlinearSolveFirstOrder.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ using NonlinearSolveBase: NonlinearSolveBase, AbstractNonlinearSolveAlgorithm,
2020
AbstractTrustRegionMethodCache,
2121
Utils, InternalAPI, get_timer_output, @static_timeit,
2222
update_trace!, L2_NORM,
23-
NewtonDescent, DampedNewtonDescent, GeodesicAcceleration,
24-
Dogleg
23+
NewtonDescent, DampedNewtonDescent, HalleyDescent,
24+
GeodesicAcceleration, Dogleg
2525
using SciMLBase: SciMLBase, AbstractNonlinearProblem, NLStats, ReturnCode,
2626
NonlinearFunction,
2727
NonlinearLeastSquaresProblem, NonlinearProblem, NoSpecialize
@@ -31,6 +31,7 @@ using FiniteDiff: FiniteDiff # Default Finite Difference Method
3131
using ForwardDiff: ForwardDiff # Default Forward Mode AD
3232

3333
include("raphson.jl")
34+
include("halley.jl")
3435
include("gauss_newton.jl")
3536
include("levenberg_marquardt.jl")
3637
include("trust_region.jl")
@@ -93,7 +94,7 @@ end
9394

9495
@reexport using SciMLBase, NonlinearSolveBase
9596

96-
export NewtonRaphson, PseudoTransient
97+
export NewtonRaphson, Halley, PseudoTransient
9798
export GaussNewton, LevenbergMarquardt, TrustRegion
9899

99100
export RadiusUpdateSchemes
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Halley(; concrete_jac = nothing, linsolve = nothing, linesearch = missing,
3+
autodiff = nothing)
4+
5+
An experimental Halley's method implementation. Improves the convergence rate of Newton's method by using second-order derivative information to correct the descent direction.
6+
7+
Currently depends on TaylorDiff.jl to handle the correction terms,
8+
might have more general implementation in the future.
9+
"""
10+
function Halley(; concrete_jac = nothing, linsolve = nothing,
11+
linesearch = missing, autodiff = nothing)
12+
return GeneralizedFirstOrderAlgorithm(;
13+
concrete_jac, name = :Halley, linesearch,
14+
descent = HalleyDescent(; linsolve), autodiff)
15+
end

test/23_test_problems_tests.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@testsetup module RobustnessTesting
22
using NonlinearSolve, LinearAlgebra, LinearSolve, NonlinearProblemLibrary, Test
3+
import TaylorDiff
34

45
problems = NonlinearProblemLibrary.problems
56
dicts = NonlinearProblemLibrary.dicts
@@ -61,10 +62,14 @@ end
6162
end
6263

6364
@testitem "23 Test Problems: Halley" setup=[RobustnessTesting] tags=[:core] begin
64-
alg_ops = (SimpleHalley(; autodiff = AutoForwardDiff()),)
65+
alg_ops = (
66+
Halley(),
67+
SimpleHalley(; autodiff = AutoForwardDiff())
68+
)
6569

6670
broken_tests = Dict(alg => Int[] for alg in alg_ops)
67-
broken_tests[alg_ops[1]] = [1, 5, 15, 16, 18]
71+
broken_tests[alg_ops[1]] = [1, 5, 15, 16]
72+
broken_tests[alg_ops[2]] = [1, 5, 15, 16, 18]
6873

6974
test_on_library(problems, dicts, alg_ops, broken_tests)
7075
end

0 commit comments

Comments
 (0)