Skip to content

Commit e43dbc0

Browse files
reset LM alg and change it in another PR
1 parent 3421f29 commit e43dbc0

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

src/LM_alg.jl

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function LM(
4747
subsolver = R2,
4848
subsolver_options = ROSolverOptions(ϵa = options.ϵa),
4949
selected::AbstractVector{<:Integer} = 1:(nls.meta.nvar),
50-
nonlinear::Bool = true,
5150
) where {H}
5251
start_time = time()
5352
elapsed_time = 0.0
@@ -63,7 +62,6 @@ function LM(
6362
γ = options.γ
6463
θ = options.θ
6564
σmin = options.σmin
66-
σk = options.σk
6765

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

8987
# initialize parameters
88+
σk = max(1 / options.ν, σmin)
9089
xk = copy(x0)
9190
hk = h(xk[selected])
9291
if hk == Inf
@@ -105,15 +104,13 @@ function LM(
105104
k = 0
106105
Fobj_hist = zeros(maxIter)
107106
Hobj_hist = zeros(maxIter)
108-
R = eltype(xk)
109-
sqrt_ξ1_νInv = one(R)
110107
Complex_hist = zeros(Int, maxIter)
111108
Grad_hist = zeros(Int, maxIter)
112109
Resid_hist = zeros(Int, maxIter)
113110

114111
if verbose > 0
115112
#! format: off
116-
@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"
113+
@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"
117114
#! format: on
118115
end
119116

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

184180
if ξ1 0 && k == 1
185-
ϵ_increment = ϵr * sqrt_ξ1_νInv
181+
ϵ_increment = ϵr * sqrt(ξ1)
186182
ϵ += ϵ_increment # make stopping test absolute and relative
187183
ϵ_subsolver += ϵ_increment
188184
end
189185

190-
if sqrt_ξ1_νInv < ϵ
186+
if sqrt(ξ1) < ϵ
191187
# the current xk is approximately first-order stationary
192188
optimal = true
193189
continue
194190
end
195191

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

227222
if (verbose > 0) && (k % ptf == 0)
228223
#! format: off
229-
@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
224+
@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
230225
#! format: off
231226
end
232227

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

251-
# update opnorm if not linear least squares
252-
if nonlinear == true
253-
σmax = opnorm(Jk)
254-
end
246+
σmax = opnorm(Jk)
255247

256248
Complex_hist[k] += 1
257249
end
@@ -260,7 +252,6 @@ function LM(
260252
σk = σk * γ
261253
end
262254
νInv = (1 + θ) * (σmax^2 + σk) # ‖J'J + σₖ I‖ = ‖J‖² + σₖ
263-
264255
tired = k maxIter || elapsed_time > maxTime
265256
end
266257

@@ -269,9 +260,9 @@ function LM(
269260
@info @sprintf "%6d %8s %8.1e %8.1e" k "" fk hk
270261
elseif optimal
271262
#! format: off
272-
@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
263+
@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
273264
#! format: on
274-
@info "LM: terminating with √(ξ1/ν) = $(sqrt_ξ1_νInv)"
265+
@info "LM: terminating with √ξ1 = $(sqrt(ξ1))"
275266
end
276267
end
277268
status = if optimal
@@ -288,7 +279,7 @@ function LM(
288279
set_status!(stats, status)
289280
set_solution!(stats, xk)
290281
set_objective!(stats, fk + hk)
291-
set_residuals!(stats, zero(eltype(xk)), ξ1 0 ? sqrt_ξ1_νInv : ξ1)
282+
set_residuals!(stats, zero(eltype(xk)), ξ1 0 ? sqrt(ξ1) : ξ1)
292283
set_iter!(stats, k)
293284
set_time!(stats, elapsed_time)
294285
set_solver_specific!(stats, :Fhist, Fobj_hist[1:k])
@@ -298,4 +289,4 @@ function LM(
298289
set_solver_specific!(stats, :NLSGradHist, Grad_hist[1:k])
299290
set_solver_specific!(stats, :ResidHist, Resid_hist[1:k])
300291
return stats
301-
end
292+
end

0 commit comments

Comments
 (0)