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

Line Search for Newton-Raphson #194

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
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
47 changes: 43 additions & 4 deletions src/raphson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mutable struct NewtonRaphsonCache{iip, fType, algType, uType, duType, resType, p
u::uType
fu::resType
p::pType
α::Real
uf::ufType
linsolve::L
J::jType
Expand All @@ -99,7 +100,7 @@ mutable struct NewtonRaphsonCache{iip, fType, algType, uType, duType, resType, p
tolType,
probType, ufType, L, jType, JC}
new{iip, fType, algType, uType, duType, resType, pType, INType, tolType,
probType, ufType, L, jType, JC}(f, alg, u, fu, p,
probType, ufType, L, jType, JC}(f, alg, u, fu, p, α,
uf, linsolve, J, du1, jac_config,
force_stop, maxiters, internalnorm,
retcode, abstol, prob, stats)
Expand Down Expand Up @@ -145,6 +146,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::NewtonRaphson
end
f = prob.f
p = prob.p
α = 1.0 #line search coefficient
if iip
fu = zero(u)
f(fu, u, p)
Expand All @@ -153,7 +155,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::NewtonRaphson
end
uf, linsolve, J, du1, jac_config = jacobian_caches(alg, f, u, p, Val(iip))

return NewtonRaphsonCache{iip}(f, alg, u, fu, p, uf, linsolve, J, du1, jac_config,
return NewtonRaphsonCache{iip}(f, alg, u, fu, p, α, uf, linsolve, J, du1, jac_config,
false, maxiters, internalnorm,
ReturnCode.Default, abstol, prob, NLStats(1, 0, 0, 0, 0))
end
Expand All @@ -167,8 +169,10 @@ function perform_step!(cache::NewtonRaphsonCache{true})
linres = dolinsolve(alg.precs, linsolve, A = J, b = _vec(fu), linu = _vec(du1),
p = p, reltol = cache.abstol)
cache.linsolve = linres.cache
@. u = u - du1
f(fu, u, p)
# @. u = u - du1
# f(fu, u, p)
## Line Search ##
perform_linesearch!(cache)

if cache.internalnorm(cache.fu) < cache.abstol
cache.force_stop = true
Expand All @@ -195,6 +199,41 @@ function perform_step!(cache::NewtonRaphsonCache{false})
return nothing
end

function objective_linesearch!(cache::NewtonRaphsonCache) ## returns the objective functions required in LS
@unpack f = cache

function fo(x)
return dot(value_f(cache, x), value_f(cache, x))
end

function g!(grad, x)
grad = simple_jacobian(f, x)' * f(x)
return grad
end

function fg!(grad, x)
g!(grad, x)
fo(x)
end
return fo, g!, fg!
end
yash2798 marked this conversation as resolved.
Show resolved Hide resolved

function perform_linesearch(cache::NewtonRaphsonCache)
@unpack u, fu, du1, alg, α = cache
fo, g!, fg! = objective_linesearch!(cache)
ϕ(α) = fo(u .- α .* du1)
###

# other functions to be defined


if alg.linesearch isa Static

else
α, fu = alg.linesearch(ϕ, dϕ, ϕdϕ)
end
end

function SciMLBase.solve!(cache::NewtonRaphsonCache)
while !cache.force_stop && cache.stats.nsteps < cache.maxiters
perform_step!(cache)
Expand Down