Skip to content

Commit

Permalink
test: newton raphson
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Sep 30, 2024
1 parent 8ef02ef commit b4b012c
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 6 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ LineSearchLineSearchesExt = "LineSearches"
ADTypes = "1.9"
CommonSolve = "0.2.4"
ConcreteStructs = "0.2.3"
DifferentiationInterface = "0.6.2"
Enzyme = "0.13.3"
FastClosures = "0.3"
FiniteDiff = "2.24.0"
Expand All @@ -36,13 +37,14 @@ NonlinearProblemLibrary = "0.1.2"
ReTestItems = "1.28.0"
ReverseDiff = "1.15.3"
SciMLBase = "2.53.1"
SciMLJacobianOperators = "0.1.0"
SciMLJacobianOperators = "0.1"
Test = "1.10"
Tracker = "0.2.35"
Zygote = "0.6.71"
julia = "1.10"

[extras]
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand All @@ -57,4 +59,4 @@ Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Enzyme", "FiniteDiff", "ForwardDiff", "Hwloc", "InteractiveUtils", "LineSearches", "NonlinearProblemLibrary", "ReTestItems", "ReverseDiff", "Test", "Tracker", "Zygote"]
test = ["DifferentiationInterface", "Enzyme", "FiniteDiff", "ForwardDiff", "Hwloc", "InteractiveUtils", "LineSearches", "NonlinearProblemLibrary", "ReTestItems", "ReverseDiff", "Test", "Tracker", "Zygote"]
12 changes: 8 additions & 4 deletions test/custom_optimizer_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ end
nlf(x, p) = [p[1] - x[1], 10.0 * (x[2] - x[1]^2)]
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [1.0])

@testset for autodiff in (AutoTracker(), AutoForwardDiff(), AutoZygote(),
@testset for autodiff in (
AutoTracker(), AutoForwardDiff(), AutoZygote(),
AutoEnzyme(), AutoReverseDiff(), AutoFiniteDiff()
)
@testset "method: $(nameof(typeof(method)))" for method in (
BackTracking(; order = 3), StrongWolfe(),
HagerZhang(), MoreThuente()
BackTracking(; order = 3),
StrongWolfe(),
HagerZhang(),
MoreThuente()
)
linesearch = LineSearchesJL(; method, autodiff)
fu, u, iter, alphas = gradient_descent(nlp, linesearch; autodiff)
Expand Down Expand Up @@ -103,7 +106,8 @@ end
nlf(x, p) = [p[1] - x[1], 10.0 * (x[2] - x[1]^2)]
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [1.0])

@testset for autodiff in (AutoTracker(), AutoForwardDiff(), AutoZygote(),
@testset for autodiff in (
AutoTracker(), AutoForwardDiff(), AutoZygote(),
AutoEnzyme(), AutoReverseDiff(), AutoFiniteDiff()
)
@testset "method: $(nameof(typeof(method)))" for method in (
Expand Down
157 changes: 157 additions & 0 deletions test/root_finding_tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,161 @@
# Here we write out Newton Raphson and test integration with LineSearch.jl. Main tests are
# over at NonlinearSolve.jl and SimpleNonlinearSolve.jl
@testsetup module RootFinding

using SciMLBase, DifferentiationInterface, ForwardDiff
using SciMLBase: AbstractNonlinearProblem
const DI = DifferentiationInterface

function newton_raphson(prob::AbstractNonlinearProblem, ls)
if SciMLBase.isinplace(prob)
return newton_raphson_iip(prob, ls)
else
return newton_raphson_oop(prob, ls)
end
end

function newton_raphson_oop(prob::AbstractNonlinearProblem, ls)
u = copy(prob.u0)
fu = prob.f(u, prob.p)

ls_cache = init(prob, ls, fu, u)

alphas = Float64[]
iter = 0
for _ in 1:100
iter += 1

maximum(abs, fu) < 1e-8 && return true, fu, u, iter, alphas

J = DI.jacobian(prob.f, AutoForwardDiff(), u, Constant(prob.p))
δu = -J \ fu

ls_sol = solve!(ls_cache, u, δu)

push!(alphas, ls_sol.step_size)
@. u = u + ls_sol.step_size * δu

fu = prob.f(u, prob.p)
end

return false, fu, u, iter, alphas
end

function newton_raphson_iip(prob::AbstractNonlinearProblem, ls)
u = copy(prob.u0)
fu = similar(u)
fu2 = similar(u)
prob.f(fu, u, prob.p)

ls_cache = init(prob, ls, fu, u)

alphas = Float64[]
iter = 0
for _ in 1:100
iter += 1

maximum(abs, fu) < 1e-8 && return true, fu, u, iter, alphas

J = DI.jacobian(prob.f, fu2, AutoForwardDiff(), u, Constant(prob.p))
δu = -J \ fu

ls_sol = solve!(ls_cache, u, δu)

push!(alphas, ls_sol.step_size)
@. u = u + ls_sol.step_size * δu

prob.f(fu, u, prob.p)
end

return false, fu, u, iter, alphas
end

export newton_raphson

end

@testitem "LineSearches.jl: Newton Raphson" setup=[RootFinding] begin
using LineSearches, SciMLBase
using ADTypes, Tracker, ForwardDiff, Zygote, Enzyme, ReverseDiff, FiniteDiff

@testset "OOP Problem" begin
nlf(x, p) = x .^ 2 .- p
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0])

@testset for autodiff in (
AutoTracker(), AutoForwardDiff(), AutoZygote(),
AutoEnzyme(), AutoReverseDiff(), AutoFiniteDiff()
)
@testset "method: $(nameof(typeof(method)))" for method in (
BackTracking(; order = 3),
StrongWolfe(),
HagerZhang(),
MoreThuente(),
Static()
)
linesearch = LineSearchesJL(; method, autodiff)
converged, fu, u, iter, alphas = newton_raphson(nlp, linesearch)

@test fu[0.0, 0.0] atol=1e-3
@test abs.(u)sqrt.([3.0, 3.0]) atol=1e-3
end
end
end

@testset "In-Place Problem" begin
nlf(dx, x, p) = (dx .= x .^ 2 .- p)
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0])

@testset for autodiff in (
AutoForwardDiff(), AutoEnzyme(), AutoReverseDiff(), AutoFiniteDiff()
)
@testset "method: $(nameof(typeof(method)))" for method in (
BackTracking(; order = 3),
StrongWolfe(),
HagerZhang(),
MoreThuente(),
Static()
)
linesearch = LineSearchesJL(; method, autodiff)
converged, fu, u, iter, alphas = newton_raphson(nlp, linesearch)

@test fu[0.0, 0.0] atol=1e-3
@test abs.(u)sqrt.([3.0, 3.0]) atol=1e-3
end
end
end
end

@testitem "Native Line Search: Newton Raphson" setup=[RootFinding] begin
using LineSearches, SciMLBase

@testset "OOP Problem" begin
nlf(x, p) = x .^ 2 .- p
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0])

@testset "method: $(nameof(typeof(method)))" for method in (
LiFukushimaLineSearch(),
NoLineSearch(0.5)
)
converged, fu, u, iter, alphas = newton_raphson(nlp, method)

@test fu[0.0, 0.0] atol=1e-1
@test abs.(u)sqrt.([3.0, 3.0]) atol=1e-1
end
end

@testset "In-Place Problem" begin
nlf(dx, x, p) = (dx .= x .^ 2 .- p)
nlp = NonlinearProblem(nlf, [-1.0, 1.0], [3.0])

@testset "method: $(nameof(typeof(method)))" for method in (
LiFukushimaLineSearch(),
NoLineSearch(0.5)
)
converged, fu, u, iter, alphas = newton_raphson(nlp, method)

@test fu[0.0, 0.0] atol=1e-1
@test abs.(u)sqrt.([3.0, 3.0]) atol=1e-1
end
end
end

2 comments on commit b4b012c

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/116359

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" b4b012cf4fc8322bd4a2b35f968c50c8a2519ede
git push origin v0.1.0

Please sign in to comment.