From 679fdabec04600657bc5a77824193606c7805015 Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 08:58:56 +0100 Subject: [PATCH 1/8] Bug fix for TrustRegion when iip=true. --- src/trustRegion.jl | 6 +++--- test/basictests.jl | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index 8c0c8982e..4bd316685 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -309,8 +309,8 @@ function trust_region_step!(cache::TrustRegionCache) if r > alg.step_threshold # Take the step. - cache.u = u_tmp - cache.fu = fu_new + cache.u = copy(u_tmp) + cache.fu = copy(fu_new) cache.loss = cache.loss_new # Update the trust region radius. @@ -324,7 +324,7 @@ function trust_region_step!(cache::TrustRegionCache) cache.make_new_J = false end - if iszero(cache.fu) || cache.internalnorm(cache.step_size) < cache.abstol + if iszero(cache.fu) || cache.internalnorm(cache.fu) < cache.abstol cache.force_stop = true end end diff --git a/test/basictests.jl b/test/basictests.jl index f1c249d13..ed2907fdb 100644 --- a/test/basictests.jl +++ b/test/basictests.jl @@ -34,13 +34,13 @@ u0 = [1.0, 1.0] sol = benchmark_immutable(ff, cu0) @test sol.retcode === ReturnCode.Success -@test all(sol.u .* sol.u .- 2 .< 1e-9) +@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9) sol = benchmark_mutable(ff, u0) @test sol.retcode === ReturnCode.Success -@test all(sol.u .* sol.u .- 2 .< 1e-9) +@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9) sol = benchmark_scalar(sf, csu0) @test sol.retcode === ReturnCode.Success -@test sol.u * sol.u - 2 < 1e-9 +@test abs(sol.u * sol.u - 2) < 1e-9 # @test (@ballocated benchmark_immutable(ff, cu0)) < 200 # @test (@ballocated benchmark_mutable(ff, cu0)) < 200 @@ -59,7 +59,7 @@ u0 = [1.0, 1.0] sol = benchmark_inplace(ffiip, u0) @test sol.retcode === ReturnCode.Success -@test all(sol.u .* sol.u .- 2 .< 1e-9) +@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9) u0 = [1.0, 1.0] probN = NonlinearProblem{true}(ffiip, u0) @@ -160,13 +160,13 @@ u0 = [1.0, 1.0] sol = benchmark_immutable(ff, cu0) @test sol.retcode === ReturnCode.Success -@test all(sol.u .* sol.u .- 2 .< 1e-9) +@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9) sol = benchmark_mutable(ff, u0) @test sol.retcode === ReturnCode.Success -@test all(sol.u .* sol.u .- 2 .< 1e-9) +@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9) sol = benchmark_scalar(sf, csu0) @test sol.retcode === ReturnCode.Success -@test sol.u * sol.u - 2 < 1e-9 +@test abs(sol.u * sol.u - 2) < 1e-9 function benchmark_inplace(f, u0) probN = NonlinearProblem{true}(f, u0) @@ -181,7 +181,7 @@ u0 = [1.0, 1.0] sol = benchmark_inplace(ffiip, u0) @test sol.retcode === ReturnCode.Success -@test all(sol.u .* sol.u .- 2 .< 1e-9) +@test all(abs.(sol.u .* sol.u .- 2) .< 1e-9) u0 = [1.0, 1.0] probN = NonlinearProblem{true}(ffiip, u0) @@ -263,7 +263,7 @@ f = (u, p) -> 0.010000000000000002 .+ 0.0011552453009332421u .- p g = function (p) probN = NonlinearProblem{false}(f, u0, p) - sol = solve(probN, TrustRegion()) + sol = solve(probN, TrustRegion(), abstol = 1e-10) return sol.u end p = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] @@ -295,7 +295,7 @@ for options in list_of_options expand_factor = options[7], max_shrink_times = options[8]) - probN = NonlinearProblem(f, u0, p) - sol = solve(probN, alg) - @test all(f(u, p) .< 1e-10) + probN = NonlinearProblem{false}(f, u0, p) + sol = solve(probN, alg, abstol = 1e-10) + @test all(abs.(f(u, p)) .< 1e-10) end From c978794a206b772a0d50c2132f84581e23d3ca63 Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 09:54:46 +0100 Subject: [PATCH 2/8] changing so only iip=true copy --- .gitignore | 7 +++++++ src/trustRegion.jl | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 525e271af..2736487d6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,10 @@ docs/site/ # environment. Manifest.toml docs/src/assets/Project.toml + +.vscode +TEST1.jl +TEST2.jl +TEST3.jl +trustRegion copy.jl +trustRegionHOLDER.jl diff --git a/src/trustRegion.jl b/src/trustRegion.jl index 4bd316685..bb067baaf 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -309,8 +309,8 @@ function trust_region_step!(cache::TrustRegionCache) if r > alg.step_threshold # Take the step. - cache.u = copy(u_tmp) - cache.fu = copy(fu_new) + take_step!(cache) + cache.loss = cache.loss_new # Update the trust region radius. @@ -356,6 +356,16 @@ function dogleg!(cache::TrustRegionCache) cache.step_size = δsd + τ * N_sd end +function take_step!(cache::TrustRegionCache{true}) + cache.u = copy(cache.u_tmp) + cache.fu = copy(cache.fu_new) +end + +function take_step!(cache::TrustRegionCache{false}) + cache.u = cache.u_tmp + cache.fu = cache.fu_new +end + function SciMLBase.solve!(cache::TrustRegionCache) while !cache.force_stop && cache.iter < cache.maxiters && cache.shrink_counter < cache.alg.max_shrink_times From 52db2147aadf4bb60ebe5eb1340209a9c3d7cdeb Mon Sep 17 00:00:00 2001 From: Simon Carlson <87299998+CCsimon123@users.noreply.github.com> Date: Thu, 19 Jan 2023 09:57:27 +0100 Subject: [PATCH 3/8] Update .gitignore --- .gitignore | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2736487d6..525e271af 100644 --- a/.gitignore +++ b/.gitignore @@ -23,10 +23,3 @@ docs/site/ # environment. Manifest.toml docs/src/assets/Project.toml - -.vscode -TEST1.jl -TEST2.jl -TEST3.jl -trustRegion copy.jl -trustRegionHOLDER.jl From a30eeed002d510c20b9df641ace283317b4cb830 Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 10:06:33 +0100 Subject: [PATCH 4/8] remove unnecessary comment --- src/trustRegion.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index bb067baaf..9f42b414c 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -308,9 +308,7 @@ function trust_region_step!(cache::TrustRegionCache) end if r > alg.step_threshold - # Take the step. take_step!(cache) - cache.loss = cache.loss_new # Update the trust region radius. From 6f7261a2390d799791dd1f58a9076fb10160654d Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 10:09:27 +0100 Subject: [PATCH 5/8] format fix --- src/trustRegion.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index 9f42b414c..eb2546bab 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -307,7 +307,6 @@ function trust_region_step!(cache::TrustRegionCache) cache.shrink_counter = 0 end if r > alg.step_threshold - take_step!(cache) cache.loss = cache.loss_new From 46da9a5c83354937cac0e9608f514d08919dc441 Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 11:25:22 +0100 Subject: [PATCH 6/8] initializing as zero --- src/trustRegion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index eb2546bab..9c76c89b0 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -242,7 +242,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion, return TrustRegionCache{iip}(f, alg, u, fu, p, uf, linsolve, J, jac_config, 1, false, maxiters, internalnorm, ReturnCode.Default, abstol, prob, initial_trust_radius, - max_trust_radius, loss, loss, H, fu, 0, u, u_tmp, fu, true, + max_trust_radius, loss, loss, H, zero(fu), 0, zero(u), u_tmp, zero(fu), true, loss) end From 5744563cf4fa33e102454ab8214b15d97898b373 Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 11:26:01 +0100 Subject: [PATCH 7/8] format --- src/trustRegion.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index 9c76c89b0..cc5c6558e 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -242,7 +242,8 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::TrustRegion, return TrustRegionCache{iip}(f, alg, u, fu, p, uf, linsolve, J, jac_config, 1, false, maxiters, internalnorm, ReturnCode.Default, abstol, prob, initial_trust_radius, - max_trust_radius, loss, loss, H, zero(fu), 0, zero(u), u_tmp, zero(fu), true, + max_trust_radius, loss, loss, H, zero(fu), 0, zero(u), + u_tmp, zero(fu), true, loss) end From 69350aa40919a11a16fa245c8f304ee6a5c1497d Mon Sep 17 00:00:00 2001 From: Simon Carlson Date: Thu, 19 Jan 2023 11:38:24 +0100 Subject: [PATCH 8/8] changed from copy to .= --- src/trustRegion.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index cc5c6558e..1eeddc1d7 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -355,8 +355,8 @@ function dogleg!(cache::TrustRegionCache) end function take_step!(cache::TrustRegionCache{true}) - cache.u = copy(cache.u_tmp) - cache.fu = copy(cache.fu_new) + cache.u .= cache.u_tmp + cache.fu .= cache.fu_new end function take_step!(cache::TrustRegionCache{false})