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

Enable iterator interface to be used with TrustRegion #174

Merged
merged 3 commits into from
Mar 29, 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 src/raphson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function SciMLBase.solve!(cache::NewtonRaphsonCache)
retcode = cache.retcode)
end

function SciMLBase.reinit!(cache::NewtonRaphsonCache{iip}, u0 = cache.u0; p = cache.p,
function SciMLBase.reinit!(cache::NewtonRaphsonCache{iip}, u0 = cache.u; p = cache.p,
abstol = cache.abstol, maxiters = cache.maxiters) where {iip}
cache.p = p
if iip
Expand Down
26 changes: 26 additions & 0 deletions src/trustRegion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,29 @@ function SciMLBase.solve!(cache::TrustRegionCache)
SciMLBase.build_solution(cache.prob, cache.alg, cache.u, cache.fu;
retcode = cache.retcode)
end

function SciMLBase.reinit!(cache::TrustRegionCache{iip}, u0 = cache.u; p = cache.p,
abstol = cache.abstol, maxiters = cache.maxiters) where {iip}
cache.p = p
if iip
recursivecopy!(cache.u, u0)
cache.f(cache.fu, cache.u, p)
else
# don't have alias_u0 but cache.u is never mutated for OOP problems so it doesn't matter
cache.u = u0
cache.fu = cache.f(cache.u, p)
end
cache.abstol = abstol
cache.maxiters = maxiters
cache.iter = 1
cache.force_stop = false
cache.retcode = ReturnCode.Default
cache.make_new_J = true
cache.loss = get_loss(cache.fu)
cache.shrink_counter = 0
cache.trust_r = convert(eltype(cache.u), cache.alg.initial_trust_radius)
if iszero(cache.trust_r)
cache.trust_r = convert(eltype(cache.u), cache.max_trust_r / 11)
end
return cache
end
31 changes: 31 additions & 0 deletions test/basictests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,37 @@ end
@test gnewton(p) ≈ [sqrt(p[2] / p[1])]
@test ForwardDiff.jacobian(gnewton, p) ≈ ForwardDiff.jacobian(t, p)

# Iterator interface
f = (u, p) -> u * u - p
g = function (p_range)
probN = NonlinearProblem{false}(f, 0.5, p_range[begin])
cache = init(probN, TrustRegion(); maxiters = 100, abstol=1e-10)
sols = zeros(length(p_range))
for (i, p) in enumerate(p_range)
reinit!(cache, cache.u; p = p)
sol = solve!(cache)
sols[i] = sol.u
end
return sols
end
p = range(0.01, 2, length = 200)
@test g(p) ≈ sqrt.(p)

f = (res, u, p) -> (res[begin] = u[1] * u[1] - p)
g = function (p_range)
probN = NonlinearProblem{true}(f, [0.5], p_range[begin])
cache = init(probN, TrustRegion(); maxiters = 100, abstol=1e-10)
sols = zeros(length(p_range))
for (i, p) in enumerate(p_range)
reinit!(cache, [cache.u[1]]; p = p)
sol = solve!(cache)
sols[i] = sol.u[1]
end
return sols
end
p = range(0.01, 2, length = 200)
@test g(p) ≈ sqrt.(p)

# Error Checks
f, u0 = (u, p) -> u .* u .- 2.0, @SVector[1.0, 1.0]
probN = NonlinearProblem(f, u0)
Expand Down