From 6b6135dd4926ba5d47730398e772f6db02dbd9c3 Mon Sep 17 00:00:00 2001 From: Yash Raj Singh Date: Thu, 20 Apr 2023 01:09:04 +0530 Subject: [PATCH 1/3] iteration array --- src/trustRegion.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index 2433a93dd..f8fe95054 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -171,6 +171,7 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, p3::floatType p4::floatType ϵ::floatType + iter_arr::SVector{5, Float64} #can be modified to take variable length input function TrustRegionCache{iip}(f::fType, alg::algType, u::uType, fu::resType, p::pType, uf::ufType, linsolve::L, J::jType, @@ -185,7 +186,7 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, g::resType, shrink_counter::Int, step_size::su2Type, u_tmp::tmpType, fu_new::resType, make_new_J::Bool, r::floatType, p1::floatType, p2::floatType, p3::floatType, - p4::floatType, ϵ::floatType) where {iip, fType, algType, uType, + p4::floatType, ϵ::floatType, iter_arr::SVector{5, Float64}) where {iip, fType, algType, uType, resType, pType, INType, tolType, probType, ufType, L, jType, JC, floatType, trustType, @@ -201,7 +202,7 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, expand_factor, loss, loss_new, H, g, shrink_counter, step_size, u_tmp, fu_new, - make_new_J, r, p1, p2, p3, p4, ϵ) + make_new_J, r, p1, p2, p3, p4, ϵ, iter_arr) end end @@ -323,6 +324,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion, p4 = convert(eltype(u), 1.0e18) # M initial_trust_radius = convert(eltype(u), p1 * (norm(fu)^0.99)) end + iter_arr = zeros(SVector{5}) return TrustRegionCache{iip}(f, alg, u, fu, p, uf, linsolve, J, jac_config, 1, false, maxiters, internalnorm, @@ -330,7 +332,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion, max_trust_radius, step_threshold, shrink_threshold, expand_threshold, shrink_factor, expand_factor, loss, loss_new, H, g, shrink_counter, step_size, u_tmp, fu_new, - make_new_J, r, p1, p2, p3, p4, ϵ) + make_new_J, r, p1, p2, p3, p4, ϵ, iter_arr) end function perform_step!(cache::TrustRegionCache{true}) @@ -539,6 +541,9 @@ function SciMLBase.solve!(cache::TrustRegionCache) while !cache.force_stop && cache.iter < cache.maxiters && cache.shrink_counter < cache.alg.max_shrink_times perform_step!(cache) + if cache.iter <= 5 + cache.iter_arr[cache.iter] = cache.u[end] + end cache.iter += 1 end From 3cee23b944d243742ba31cf763f97fd22a195cf9 Mon Sep 17 00:00:00 2001 From: Yash Raj Singh Date: Sun, 23 Apr 2023 12:44:29 +0530 Subject: [PATCH 2/3] revert changes --- src/trustRegion.jl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index f8fe95054..df59bea2f 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -171,7 +171,6 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, p3::floatType p4::floatType ϵ::floatType - iter_arr::SVector{5, Float64} #can be modified to take variable length input function TrustRegionCache{iip}(f::fType, alg::algType, u::uType, fu::resType, p::pType, uf::ufType, linsolve::L, J::jType, @@ -186,7 +185,7 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, g::resType, shrink_counter::Int, step_size::su2Type, u_tmp::tmpType, fu_new::resType, make_new_J::Bool, r::floatType, p1::floatType, p2::floatType, p3::floatType, - p4::floatType, ϵ::floatType, iter_arr::SVector{5, Float64}) where {iip, fType, algType, uType, + p4::floatType, ϵ::floatType) where {iip, fType, algType, uType, resType, pType, INType, tolType, probType, ufType, L, jType, JC, floatType, trustType, @@ -202,7 +201,7 @@ mutable struct TrustRegionCache{iip, fType, algType, uType, resType, pType, expand_factor, loss, loss_new, H, g, shrink_counter, step_size, u_tmp, fu_new, - make_new_J, r, p1, p2, p3, p4, ϵ, iter_arr) + make_new_J, r, p1, p2, p3, p4, ϵ) end end @@ -324,7 +323,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion, p4 = convert(eltype(u), 1.0e18) # M initial_trust_radius = convert(eltype(u), p1 * (norm(fu)^0.99)) end - iter_arr = zeros(SVector{5}) + return TrustRegionCache{iip}(f, alg, u, fu, p, uf, linsolve, J, jac_config, 1, false, maxiters, internalnorm, @@ -541,9 +540,6 @@ function SciMLBase.solve!(cache::TrustRegionCache) while !cache.force_stop && cache.iter < cache.maxiters && cache.shrink_counter < cache.alg.max_shrink_times perform_step!(cache) - if cache.iter <= 5 - cache.iter_arr[cache.iter] = cache.u[end] - end cache.iter += 1 end From 99fc63fa61ec47667fdc5127fbf101e77136ab16 Mon Sep 17 00:00:00 2001 From: Yash Raj Singh Date: Sun, 23 Apr 2023 14:38:27 +0530 Subject: [PATCH 3/3] added test --- src/trustRegion.jl | 2 +- test/basictests.jl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index df59bea2f..b3af32fbe 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -331,7 +331,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion, max_trust_radius, step_threshold, shrink_threshold, expand_threshold, shrink_factor, expand_factor, loss, loss_new, H, g, shrink_counter, step_size, u_tmp, fu_new, - make_new_J, r, p1, p2, p3, p4, ϵ, iter_arr) + make_new_J, r, p1, p2, p3, p4, ϵ) end function perform_step!(cache::TrustRegionCache{true}) diff --git a/test/basictests.jl b/test/basictests.jl index 2d338ec3b..4dc696162 100644 --- a/test/basictests.jl +++ b/test/basictests.jl @@ -486,6 +486,42 @@ for options in list_of_options @test all(abs.(f(u, p)) .< 1e-10) end +# Testing consistency of iip vs oop iterations + +maxiterations = [2, 3, 4, 5] +u0 = [1.0, 1.0] +function iip_oop(f, fip, u0, radius_update_scheme, maxiters) + prob_iip = NonlinearProblem{true}(fip, u0) + solver = init(prob_iip, TrustRegion(radius_update_scheme = radius_update_scheme), abstol = 1e-9, maxiters = maxiters) + sol_iip = solve!(solver) + + prob_oop = NonlinearProblem{false}(f, u0) + solver = init(prob_oop, TrustRegion(radius_update_scheme = radius_update_scheme), abstol = 1e-9, maxiters = maxiters) + sol_oop = solve!(solver) + + return sol_iip.u[end], sol_oop.u[end] +end + +for maxiters in maxiterations + iip, oop = iip_oop(ff, ffiip, u0, RadiusUpdateSchemes.Simple, maxiters) + @test iip == oop +end + +for maxiters in maxiterations + iip, oop = iip_oop(ff, ffiip, u0, RadiusUpdateSchemes.Hei, maxiters) + @test iip == oop +end + +for maxiters in maxiterations + iip, oop = iip_oop(ff, ffiip, u0, RadiusUpdateSchemes.Yuan, maxiters) + @test iip == oop +end + +for maxiters in maxiterations + iip, oop = iip_oop(ff, ffiip, u0, RadiusUpdateSchemes.Fan, maxiters) + @test iip == oop +end + # --- LevenbergMarquardt tests --- function benchmark_immutable(f, u0)