From 1d7d2026370ca2b32d236213aef9f43b2b951a6b Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Wed, 17 Jan 2024 02:17:09 -0500 Subject: [PATCH] Fix formatting --- src/algorithms/levenberg_marquardt.jl | 4 ++-- src/core/approximate_jacobian.jl | 23 ++++++---------------- src/core/generalized_first_order.jl | 6 ++---- src/core/spectral_methods.jl | 3 +-- src/descent/damped_newton.jl | 12 ++++------- src/descent/dogleg.jl | 14 ++----------- src/descent/geodesic_acceleration.jl | 17 +++++----------- src/descent/steepest.jl | 7 +++---- src/globalization/trust_region.jl | 6 +----- src/internal/approximate_initialization.jl | 3 +-- 10 files changed, 27 insertions(+), 68 deletions(-) diff --git a/src/algorithms/levenberg_marquardt.jl b/src/algorithms/levenberg_marquardt.jl index 820e29b15..72dd63957 100644 --- a/src/algorithms/levenberg_marquardt.jl +++ b/src/algorithms/levenberg_marquardt.jl @@ -2,7 +2,7 @@ LevenbergMarquardt(; linsolve = nothing, precs = DEFAULT_PRECS, damping_initial::Real = 1.0, α_geodesic::Real = 0.75, damping_increase_factor::Real = 2.0, damping_decrease_factor::Real = 3.0, - finite_diff_step_geodesic::Real = 0.1, b_uphill::Real = 1.0, autodiff = nothing, + finite_diff_step_geodesic = 0.1, b_uphill::Real = 1.0, autodiff = nothing, min_damping_D::Real = 1e-8, disable_geodesic = Val(false)) An advanced Levenberg-Marquardt implementation with the improvements suggested in @@ -33,7 +33,7 @@ For the remaining arguments, see [`GeodesicAcceleration`](@ref) and function LevenbergMarquardt(; concrete_jac = missing, linsolve = nothing, precs = DEFAULT_PRECS, damping_initial::Real = 1.0, α_geodesic::Real = 0.75, damping_increase_factor::Real = 2.0, damping_decrease_factor::Real = 3.0, - finite_diff_step_geodesic::Real = 0.1, b_uphill::Real = 1.0, autodiff = nothing, + finite_diff_step_geodesic = 0.1, b_uphill::Real = 1.0, autodiff = nothing, min_damping_D::Real = 1e-8, disable_geodesic = False) if concrete_jac !== missing Base.depwarn("The `concrete_jac` keyword argument is deprecated and will be \ diff --git a/src/core/approximate_jacobian.jl b/src/core/approximate_jacobian.jl index 305574d81..54cf5b34a 100644 --- a/src/core/approximate_jacobian.jl +++ b/src/core/approximate_jacobian.jl @@ -173,8 +173,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip}, J = initialization_cache(nothing) inv_workspace, J = INV ? __safe_inv_workspace(J) : (nothing, J) descent_cache = __internal_init(prob, alg.descent, J, fu, u; abstol, reltol, - internalnorm, - linsolve_kwargs, pre_inverted = Val(INV), timer) + internalnorm, linsolve_kwargs, pre_inverted = Val(INV), timer) du = get_du(descent_cache) reinit_rule_cache = __internal_init(alg.reinit_rule, J, fu, u, du) @@ -191,8 +190,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip}, supports_trust_region(alg.descent) || error("Trust Region not supported by \ $(alg.descent).") trustregion_cache = __internal_init(prob, alg.trustregion, f, fu, u, p; - internalnorm, - kwargs...) + internalnorm, kwargs...) GB = :TrustRegion end @@ -205,12 +203,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip}, GB = :LineSearch end - update_rule_cache = __internal_init(prob, - alg.update_rule, - J, - fu, - u, - du; + update_rule_cache = __internal_init(prob, alg.update_rule, J, fu, u, du; internalnorm) trace = init_nonlinearsolve_trace(alg, u, fu, ApplyArray(__zero, J), du; @@ -256,8 +249,7 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip}; elseif recompute_jacobian === nothing # Standard Step reinit = __internal_solve!(cache.reinit_rule_cache, cache.J, cache.fu, - cache.u, - cache.du) + cache.u, cache.du) reinit && (countable_reinit = true) elseif recompute_jacobian reinit = true # Force ReInitialization: Don't count towards resetting @@ -276,9 +268,7 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip}; end if reinit - J_init = __internal_solve!(cache.initialization_cache, - cache.fu, - cache.u, + J_init = __internal_solve!(cache.initialization_cache, cache.fu, cache.u, Val(true)) cache.J = INV ? __safe_inv!!(cache.inv_workspace, J_init) : J_init J = cache.J @@ -318,8 +308,7 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip}; elseif GB === :TrustRegion @static_timeit cache.timer "trustregion" begin tr_accepted, u_new, fu_new = __internal_solve!(cache.trustregion_cache, J, - cache.fu, - cache.u, δu, descent_intermediates) + cache.fu, cache.u, δu, descent_intermediates) if tr_accepted @bb copyto!(cache.u, u_new) @bb copyto!(cache.fu, fu_new) diff --git a/src/core/generalized_first_order.jl b/src/core/generalized_first_order.jl index 23be1cc58..1ac5ae018 100644 --- a/src/core/generalized_first_order.jl +++ b/src/core/generalized_first_order.jl @@ -229,8 +229,7 @@ function __step!(cache::GeneralizedFirstOrderAlgorithmCache{iip, GB}; if GB === :LineSearch @static_timeit cache.timer "linesearch" begin linesearch_failed, α = __internal_solve!(cache.linesearch_cache, - cache.u, - δu) + cache.u, δu) end if linesearch_failed cache.retcode = ReturnCode.InternalLineSearchFailed @@ -243,8 +242,7 @@ function __step!(cache::GeneralizedFirstOrderAlgorithmCache{iip, GB}; elseif GB === :TrustRegion @static_timeit cache.timer "trustregion" begin tr_accepted, u_new, fu_new = __internal_solve!(cache.trustregion_cache, J, - cache.fu, - cache.u, δu, descent_intermediates) + cache.fu, cache.u, δu, descent_intermediates) if tr_accepted @bb copyto!(cache.u, u_new) @bb copyto!(cache.fu, fu_new) diff --git a/src/core/spectral_methods.jl b/src/core/spectral_methods.jl index 908e23bff..31ef18343 100644 --- a/src/core/spectral_methods.jl +++ b/src/core/spectral_methods.jl @@ -130,8 +130,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem, alg::GeneralizedDFSane @bb fu_cache = copy(fu) linesearch_cache = __internal_init(prob, alg.linesearch, prob.f, fu, u, prob.p; - maxiters, - internalnorm, kwargs...) + maxiters, internalnorm, kwargs...) abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fu, u_cache, termination_condition) diff --git a/src/descent/damped_newton.jl b/src/descent/damped_newton.jl index 66375fec9..5a192a586 100644 --- a/src/descent/damped_newton.jl +++ b/src/descent/damped_newton.jl @@ -94,8 +94,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::DampedNewtonDescen rhs_damp = fu end damping_fn_cache = __internal_init(prob, alg.damping_fn, alg.initial_damping, - jac_damp, - rhs_damp, u, False; kwargs...) + jac_damp, rhs_damp, u, False; kwargs...) D = damping_fn_cache(nothing) D isa Number && (D = D * I) rhs_cache = vcat(_vec(fu), _vec(u)) @@ -103,8 +102,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::DampedNewtonDescen A, b = J_cache, rhs_cache elseif mode === :simple damping_fn_cache = __internal_init(prob, alg.damping_fn, alg.initial_damping, J, fu, - u, False; - kwargs...) + u, False; kwargs...) J_cache = __maybe_unaliased(J, alias_J) D = damping_fn_cache(nothing) J_damped = __dampen_jacobian!!(J_cache, J, D) @@ -186,10 +184,8 @@ function __internal_solve!(cache::DampedNewtonDescentCache{INV, mode}, J, fu, u, INV && (J = inv(J)) @bb cache.JᵀJ_cache = transpose(J) × J @bb cache.Jᵀfu_cache = transpose(J) × vec(fu) - D = __internal_solve!(cache.damping_fn_cache, - cache.JᵀJ_cache, - cache.Jᵀfu_cache, - True) + D = __internal_solve!(cache.damping_fn_cache, cache.JᵀJ_cache, + cache.Jᵀfu_cache, True) cache.J = __dampen_jacobian!!(cache.J, cache.JᵀJ_cache, D) A = __maybe_symmetric(cache.J) elseif !recompute_A diff --git a/src/descent/dogleg.jl b/src/descent/dogleg.jl index 0d1e179a4..e1a50832f 100644 --- a/src/descent/dogleg.jl +++ b/src/descent/dogleg.jl @@ -82,12 +82,7 @@ function __internal_solve!(cache::DoglegCache{INV, NF}, J, fu, u, idx::Val{N} = want to use a Trust Region." δu = get_du(cache, idx) T = promote_type(eltype(u), eltype(fu)) - δu_newton, _, _ = __internal_solve!(cache.newton_cache, - J, - fu, - u, - idx; - skip_solve, + δu_newton, _, _ = __internal_solve!(cache.newton_cache, J, fu, u, idx; skip_solve, kwargs...) # Newton's Step within the trust region @@ -108,12 +103,7 @@ function __internal_solve!(cache::DoglegCache{INV, NF}, J, fu, u, idx::Val{N} = @bb cache.δu_cache_mul = JᵀJ × vec(δu_cauchy) δuJᵀJδu = __dot(δu_cauchy, cache.δu_cache_mul) else - δu_cauchy, _, _ = __internal_solve!(cache.cauchy_cache, - J, - fu, - u, - idx; - skip_solve, + δu_cauchy, _, _ = __internal_solve!(cache.cauchy_cache, J, fu, u, idx; skip_solve, kwargs...) J_ = INV ? inv(J) : J l_grad = cache.internalnorm(δu_cauchy) diff --git a/src/descent/geodesic_acceleration.jl b/src/descent/geodesic_acceleration.jl index cac2bfb11..fcb1ec83e 100644 --- a/src/descent/geodesic_acceleration.jl +++ b/src/descent/geodesic_acceleration.jl @@ -84,8 +84,7 @@ function set_acceleration!(cache::GeodesicAccelerationCache, δa, ::Val{N}) wher end function __internal_init(prob::AbstractNonlinearProblem, alg::GeodesicAcceleration, J, fu, - u; - shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, linsolve_kwargs = (;), + u; shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, linsolve_kwargs = (;), abstol = nothing, reltol = nothing, internalnorm::F = DEFAULT_NORM, kwargs...) where {INV, N, F} T = promote_type(eltype(u), eltype(fu)) @@ -94,8 +93,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::GeodesicAccelerati @bb δu_ = similar(u) end descent_cache = __internal_init(prob, alg.descent, J, fu, u; shared = Val(N * 2), - pre_inverted, - linsolve_kwargs, abstol, reltol, kwargs...) + pre_inverted, linsolve_kwargs, abstol, reltol, kwargs...) @bb Jv = similar(fu) @bb fu_cache = copy(fu) @bb u_cache = similar(u) @@ -107,12 +105,7 @@ function __internal_solve!(cache::GeodesicAccelerationCache, J, fu, u, idx::Val{ skip_solve::Bool = false, kwargs...) where {N} a, v, δu = get_acceleration(cache, idx), get_velocity(cache, idx), get_du(cache, idx) skip_solve && return δu, true, (; a, v) - v, _, _ = __internal_solve!(cache.descent_cache, - J, - fu, - u, - Val(2N - 1); - skip_solve, + v, _, _ = __internal_solve!(cache.descent_cache, J, fu, u, Val(2N - 1); skip_solve, kwargs...) @bb @. cache.u_cache = u + cache.h * v @@ -121,9 +114,9 @@ function __internal_solve!(cache::GeodesicAccelerationCache, J, fu, u, idx::Val{ J !== nothing && @bb(cache.Jv=J × vec(v)) Jv = _restructure(cache.fu_cache, cache.Jv) @bb @. cache.fu_cache = (2 / cache.h) * ((cache.fu_cache - fu) / cache.h - Jv) + a, _, _ = __internal_solve!(cache.descent_cache, J, cache.fu_cache, u, Val(2N); - skip_solve, - kwargs..., reuse_A_if_factorization = true) + skip_solve, kwargs..., reuse_A_if_factorization = true) norm_v = cache.internalnorm(v) norm_a = cache.internalnorm(a) diff --git a/src/descent/steepest.jl b/src/descent/steepest.jl index 331d61ac5..d19505a86 100644 --- a/src/descent/steepest.jl +++ b/src/descent/steepest.jl @@ -30,10 +30,9 @@ end @internal_caches SteepestDescentCache :lincache @inline function __internal_init(prob::AbstractNonlinearProblem, alg::SteepestDescent, J, - fu, - u; shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, linsolve_kwargs = (;), - abstol = nothing, reltol = nothing, timer = get_timer_output(), - kwargs...) where {INV, N} + fu, u; shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, + linsolve_kwargs = (;), abstol = nothing, reltol = nothing, + timer = get_timer_output(), kwargs...) where {INV, N} INV && @assert length(fu)==length(u) "Non-Square Jacobian Inverse doesn't make sense." @bb δu = similar(u) δus = N ≤ 1 ? nothing : map(2:N) do i diff --git a/src/globalization/trust_region.jl b/src/globalization/trust_region.jl index d30753c5c..4e3b2f387 100644 --- a/src/globalization/trust_region.jl +++ b/src/globalization/trust_region.jl @@ -418,11 +418,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::GenericTrustRegion u_cache, fu_cache, false, 0, 0, alg) end -function __internal_solve!(cache::GenericTrustRegionSchemeCache, - J, - fu, - u, - δu, +function __internal_solve!(cache::GenericTrustRegionSchemeCache, J, fu, u, δu, descent_stats) T = promote_type(eltype(u), eltype(fu)) @bb @. cache.u_cache = u + δu diff --git a/src/internal/approximate_initialization.jl b/src/internal/approximate_initialization.jl index 72e49fe91..bb9898009 100644 --- a/src/internal/approximate_initialization.jl +++ b/src/internal/approximate_initialization.jl @@ -91,8 +91,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::IdentityInitializa internalnorm) end function __internal_init(prob::AbstractNonlinearProblem, alg::IdentityInitialization, - solver, - f::F, fu, u, p; internalnorm::IN = DEFAULT_NORM, kwargs...) where {F, IN} + solver, f::F, fu, u, p; internalnorm::IN = DEFAULT_NORM, kwargs...) where {F, IN} α = __initial_alpha(alg.alpha, u, fu, internalnorm) if alg.structure isa DiagonalStructure @assert length(u)==length(fu) "Diagonal Jacobian Structure must be square!"