Skip to content

Commit

Permalink
reset LM alg and change it in another PR
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedLaghdafHABIBOULLAH committed Sep 27, 2024
1 parent 3421f29 commit 1500d76
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/LM_alg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function LM(
subsolver = R2,
subsolver_options = ROSolverOptions(ϵa = options.ϵa),
selected::AbstractVector{<:Integer} = 1:(nls.meta.nvar),
nonlinear::Bool = true,
) where {H}
start_time = time()
elapsed_time = 0.0
Expand All @@ -63,7 +62,6 @@ function LM(
γ = options.γ
θ = options.θ
σmin = options.σmin
σk = options.σk

# store initial values of the subsolver_options fields that will be modified
ν_subsolver = subsolver_options.ν
Expand All @@ -87,6 +85,7 @@ function LM(
end

# initialize parameters
σk = max(1 / options.ν, σmin)
xk = copy(x0)
hk = h(xk[selected])
if hk == Inf
Expand All @@ -105,15 +104,13 @@ function LM(
k = 0
Fobj_hist = zeros(maxIter)
Hobj_hist = zeros(maxIter)
R = eltype(xk)
sqrt_ξ1_νInv = one(R)
Complex_hist = zeros(Int, maxIter)
Grad_hist = zeros(Int, maxIter)
Resid_hist = zeros(Int, maxIter)

if verbose > 0
#! format: off
@info @sprintf "%6s %8s %8s %8s %7s %7s %8s %7s %7s %7s %7s %1s" "outer" "inner" "f(x)" "h(x)" "(ξ1/ν)" "√ξ" "ρ" "σ" "‖x‖" "‖s‖" "‖Jₖ‖²" "reg"
@info @sprintf "%6s %8s %8s %8s %7s %7s %8s %7s %7s %7s %7s %1s" "outer" "inner" "f(x)" "h(x)" "ξ1" "√ξ" "ρ" "σ" "‖x‖" "‖s‖" "‖Jₖ‖²" "reg"
#! format: on
end

Expand Down Expand Up @@ -179,24 +176,22 @@ function LM(
prox!(s, ψ, ∇fk, ν)
ξ1 = fk + hk - mk1(s) + max(1, abs(fk + hk)) * 10 * eps() # TODO: isn't mk(s) returned by subsolver?
ξ1 > 0 || error("LM: first prox-gradient step should produce a decrease but ξ1 = $(ξ1)")
sqrt_ξ1_νInv = ξ1 > 0 ? sqrt(ξ1 * νInv) : 10.

if ξ1 0 && k == 1
ϵ_increment = ϵr * sqrt_ξ1_νInv
ϵ_increment = ϵr * sqrt(ξ1)
ϵ += ϵ_increment # make stopping test absolute and relative
ϵ_subsolver += ϵ_increment
end

if sqrt_ξ1_νInv < ϵ
if sqrt(ξ1) < ϵ
# the current xk is approximately first-order stationary
optimal = true
continue
end

# subsolver_options.ϵa = k == 1 ? 1.0e-1 : max(ϵ_subsolver, min(1.0e-2, ξ1 / 10))
subsolver_options.ϵa = k == 1 ? 1.0e-3 : min(sqrt_ξ1_νInv ^ (1.5) , sqrt_ξ1_νInv * 1e-3) # 1.0e-5 default
subsolver_options.ϵa = k == 1 ? 1.0e-1 : max(ϵ_subsolver, min(1.0e-2, ξ1 / 10))
subsolver_options.ν = ν
subsolver_args = subsolver == R2DH ? (SpectralGradient(νInv, nls.meta.nvar),) : ()
subsolver_args = subsolver == TRDH ? (SpectralGradient(1 / ν, nls.meta.nvar),) : ()
@debug "setting inner stopping tolerance to" subsolver_options.optTol
s, iter, _ = with_logger(subsolver_logger) do
subsolver(φ, ∇φ!, ψ, subsolver_args..., subsolver_options, s)
Expand Down Expand Up @@ -226,7 +221,7 @@ function LM(

if (verbose > 0) && (k % ptf == 0)
#! format: off
@info @sprintf "%6d %8d %8.1e %8.1e %7.1e %7.1e %8.1e %7.1e %7.1e %7.1e %7.1e %1s" k iter fk hk sqrt_ξ1_νInv sqrt(ξ) ρk σk norm(xk) norm(s) νInv σ_stat
@info @sprintf "%6d %8d %8.1e %8.1e %7.1e %7.1e %8.1e %7.1e %7.1e %7.1e %7.1e %1s" k iter fk hk sqrt(ξ1) sqrt(ξ) ρk σk norm(xk) norm(s) νInv σ_stat
#! format: off
end

Expand All @@ -248,10 +243,7 @@ function LM(
Jk = jac_op_residual(nls, xk)
jtprod_residual!(nls, xk, Fk, ∇fk)

# update opnorm if not linear least squares
if nonlinear == true
σmax = opnorm(Jk)
end
σmax = opnorm(Jk)

Complex_hist[k] += 1
end
Expand All @@ -260,7 +252,6 @@ function LM(
σk = σk * γ
end
νInv = (1 + θ) * (σmax^2 + σk) # ‖J'J + σₖ I‖ = ‖J‖² + σₖ

tired = k maxIter || elapsed_time > maxTime
end

Expand All @@ -269,9 +260,9 @@ function LM(
@info @sprintf "%6d %8s %8.1e %8.1e" k "" fk hk
elseif optimal
#! format: off
@info @sprintf "%6d %8d %8.1e %8.1e %7.1e %7.1e %8s %7.1e %7.1e %7.1e %7.1e" k 1 fk hk sqrt_ξ1_νInv sqrt(ξ1) "" σk norm(xk) norm(s) νInv
@info @sprintf "%6d %8d %8.1e %8.1e %7.1e %7.1e %8s %7.1e %7.1e %7.1e %7.1e" k 1 fk hk sqrt(ξ1) sqrt(ξ1) "" σk norm(xk) norm(s) νInv
#! format: on
@info "LM: terminating with √(ξ1/ν) = $(sqrt_ξ1_νInv)"
@info "LM: terminating with √ξ1 = $(sqrt(ξ1))"
end
end
status = if optimal
Expand All @@ -288,7 +279,7 @@ function LM(
set_status!(stats, status)
set_solution!(stats, xk)
set_objective!(stats, fk + hk)
set_residuals!(stats, zero(eltype(xk)), ξ1 0 ? sqrt_ξ1_νInv : ξ1)
set_residuals!(stats, zero(eltype(xk)), ξ1 0 ? sqrt(ξ1) : ξ1)
set_iter!(stats, k)
set_time!(stats, elapsed_time)
set_solver_specific!(stats, :Fhist, Fobj_hist[1:k])
Expand Down

0 comments on commit 1500d76

Please sign in to comment.