|
| 1 | +# This is a copy of the version in NonlinearSolve.jl. Temporarily kept here till we move |
| 2 | +# line searches into a dedicated package. |
| 3 | +@kwdef @concrete struct LiFukushimaLineSearch |
| 4 | + lambda_0 = 1 |
| 5 | + beta = 0.5 |
| 6 | + sigma_1 = 0.001 |
| 7 | + sigma_2 = 0.001 |
| 8 | + eta = 0.1 |
| 9 | + rho = 0.1 |
| 10 | + nan_maxiters = missing |
| 11 | + maxiters::Int = 100 |
| 12 | +end |
| 13 | + |
| 14 | +@concrete mutable struct LiFukushimaLineSearchCache{T <: Union{Nothing, Int}} |
| 15 | + ϕ |
| 16 | + λ₀ |
| 17 | + β |
| 18 | + σ₁ |
| 19 | + σ₂ |
| 20 | + η |
| 21 | + ρ |
| 22 | + α |
| 23 | + nan_maxiters::T |
| 24 | + maxiters::Int |
| 25 | +end |
| 26 | + |
| 27 | +@concrete struct StaticLiFukushimaLineSearchCache |
| 28 | + f |
| 29 | + p |
| 30 | + λ₀ |
| 31 | + β |
| 32 | + σ₁ |
| 33 | + σ₂ |
| 34 | + η |
| 35 | + ρ |
| 36 | + maxiters::Int |
| 37 | +end |
| 38 | + |
| 39 | +(alg::LiFukushimaLineSearch)(prob, fu, u) = __generic_init(alg, prob, fu, u) |
| 40 | +function (alg::LiFukushimaLineSearch)(prob, fu::Union{Number, SArray}, |
| 41 | + u::Union{Number, SArray}) |
| 42 | + (alg.nan_maxiters === missing || alg.nan_maxiters === nothing) && |
| 43 | + return __static_init(alg, prob, fu, u) |
| 44 | + @warn "`LiFukushimaLineSearch` with NaN checking is not non-allocating" maxlog=1 |
| 45 | + return __generic_init(alg, prob, fu, u) |
| 46 | +end |
| 47 | + |
| 48 | +function __generic_init(alg::LiFukushimaLineSearch, prob, fu, u) |
| 49 | + @bb u_cache = similar(u) |
| 50 | + @bb fu_cache = similar(fu) |
| 51 | + T = promote_type(eltype(fu), eltype(u)) |
| 52 | + |
| 53 | + ϕ = @closure (u, δu, α) -> begin |
| 54 | + @bb @. u_cache = u + α * δu |
| 55 | + return NONLINEARSOLVE_DEFAULT_NORM(__eval_f(prob, fu_cache, u_cache)) |
| 56 | + end |
| 57 | + |
| 58 | + nan_maxiters = ifelse(alg.nan_maxiters === missing, 5, alg.nan_maxiters) |
| 59 | + |
| 60 | + return LiFukushimaLineSearchCache(ϕ, T(alg.lambda_0), T(alg.beta), T(alg.sigma_1), |
| 61 | + T(alg.sigma_2), T(alg.eta), T(alg.rho), T(true), nan_maxiters, alg.maxiters) |
| 62 | +end |
| 63 | + |
| 64 | +function __static_init(alg::LiFukushimaLineSearch, prob, fu, u) |
| 65 | + T = promote_type(eltype(fu), eltype(u)) |
| 66 | + return StaticLiFukushimaLineSearchCache(prob.f, prob.p, T(alg.lambda_0), T(alg.beta), |
| 67 | + T(alg.sigma_1), T(alg.sigma_2), T(alg.eta), T(alg.rho), alg.maxiters) |
| 68 | +end |
| 69 | + |
| 70 | +function (cache::LiFukushimaLineSearchCache)(u, δu) |
| 71 | + T = promote_type(eltype(u), eltype(δu)) |
| 72 | + ϕ = @closure α -> cache.ϕ(u, δu, α) |
| 73 | + fx_norm = ϕ(T(0)) |
| 74 | + |
| 75 | + # Non-Blocking exit if the norm is NaN or Inf |
| 76 | + DiffEqBase.NAN_CHECK(fx_norm) && return cache.α |
| 77 | + |
| 78 | + # Early Terminate based on Eq. 2.7 |
| 79 | + du_norm = NONLINEARSOLVE_DEFAULT_NORM(δu) |
| 80 | + fxλ_norm = ϕ(cache.α) |
| 81 | + fxλ_norm ≤ cache.ρ * fx_norm - cache.σ₂ * du_norm^2 && return cache.α |
| 82 | + |
| 83 | + λ₂, λ₁ = cache.λ₀, cache.λ₀ |
| 84 | + fxλp_norm = ϕ(λ₂) |
| 85 | + |
| 86 | + if cache.nan_maxiters !== nothing |
| 87 | + if DiffEqBase.NAN_CHECK(fxλp_norm) |
| 88 | + nan_converged = false |
| 89 | + for _ in 1:(cache.nan_maxiters) |
| 90 | + λ₁, λ₂ = λ₂, cache.β * λ₂ |
| 91 | + fxλp_norm = ϕ(λ₂) |
| 92 | + nan_converged = DiffEqBase.NAN_CHECK(fxλp_norm)::Bool |
| 93 | + nan_converged && break |
| 94 | + end |
| 95 | + nan_converged || return cache.α |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + for i in 1:(cache.maxiters) |
| 100 | + fxλp_norm = ϕ(λ₂) |
| 101 | + converged = fxλp_norm ≤ (1 + cache.η) * fx_norm - cache.σ₁ * λ₂^2 * du_norm^2 |
| 102 | + converged && return λ₂ |
| 103 | + λ₁, λ₂ = λ₂, cache.β * λ₂ |
| 104 | + end |
| 105 | + |
| 106 | + return cache.α |
| 107 | +end |
| 108 | + |
| 109 | +function (cache::StaticLiFukushimaLineSearchCache)(u, δu) |
| 110 | + T = promote_type(eltype(u), eltype(δu)) |
| 111 | + |
| 112 | + # Early Terminate based on Eq. 2.7 |
| 113 | + fx_norm = NONLINEARSOLVE_DEFAULT_NORM(cache.f(u, cache.p)) |
| 114 | + du_norm = NONLINEARSOLVE_DEFAULT_NORM(δu) |
| 115 | + fxλ_norm = NONLINEARSOLVE_DEFAULT_NORM(cache.f(u .+ δu, cache.p)) |
| 116 | + fxλ_norm ≤ cache.ρ * fx_norm - cache.σ₂ * du_norm^2 && return T(true) |
| 117 | + |
| 118 | + λ₂, λ₁ = cache.λ₀, cache.λ₀ |
| 119 | + |
| 120 | + for i in 1:(cache.maxiters) |
| 121 | + fxλp_norm = NONLINEARSOLVE_DEFAULT_NORM(cache.f(u .+ λ₂ .* δu, cache.p)) |
| 122 | + converged = fxλp_norm ≤ (1 + cache.η) * fx_norm - cache.σ₁ * λ₂^2 * du_norm^2 |
| 123 | + converged && return λ₂ |
| 124 | + λ₁, λ₂ = λ₂, cache.β * λ₂ |
| 125 | + end |
| 126 | + |
| 127 | + return T(true) |
| 128 | +end |
0 commit comments