Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update LM solver #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/LM_alg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ 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 @@ -62,6 +63,7 @@ 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 @@ -85,7 +87,6 @@ function LM(
end

# initialize parameters
σk = max(1 / options.ν, σmin)
xk = copy(x0)
hk = h(xk[selected])
if hk == Inf
Expand All @@ -104,13 +105,15 @@ 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 @@ -176,22 +179,24 @@ 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?????

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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

if sqrt(ξ1) < ϵ
if sqrt_ξ1_νInv < ϵ
# 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-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.ν = ν
subsolver_args = subsolver == TRDH ? (SpectralGradient(1 / ν, nls.meta.nvar),) : ()
subsolver_args = subsolver == R2DH ? (SpectralGradient(νInv, 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 @@ -221,7 +226,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) 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_νInv sqrt(ξ) ρk σk norm(xk) norm(s) νInv σ_stat
#! format: off
end

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

σmax = opnorm(Jk)
# update opnorm if not linear least squares
if nonlinear == true
σmax = opnorm(Jk)
end
Comment on lines +252 to +254
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if nonlinear == true
σmax = opnorm(Jk)
end
if nonlinear
σmax = opnorm(Jk)
end


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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

tired = k ≥ maxIter || elapsed_time > maxTime
end

Expand All @@ -260,9 +269,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) 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_νInv sqrt(ξ1) "" σk norm(xk) norm(s) νInv
#! format: on
@info "LM: terminating with √ξ1 = $(sqrt(ξ1))"
@info "LM: terminating with √(ξ1/ν) = $(sqrt_ξ1_νInv)"
end
end
status = if optimal
Expand All @@ -279,7 +288,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) : ξ1)
set_residuals!(stats, zero(eltype(xk)), ξ1 ≥ 0 ? sqrt_ξ1_νInv : ξ1)
set_iter!(stats, k)
set_time!(stats, elapsed_time)
set_solver_specific!(stats, :Fhist, Fobj_hist[1:k])
Expand Down