Skip to content

Commit 3fa39ac

Browse files
add rayliegh quotient
1 parent 79c772f commit 3fa39ac

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/R2N.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mutable struct R2NSolver{
1717
xkn::V
1818
s::V
1919
s1::V
20+
v0::V
2021
has_bnds::Bool
2122
l_bound::V
2223
u_bound::V
@@ -44,6 +45,10 @@ function R2NSolver(
4445
xkn = similar(x0)
4546
s = similar(x0)
4647
s1 = similar(x0)
48+
49+
v0 = randn(T, length(x0))
50+
v0 /= norm(v0)
51+
4752
has_bnds = any(l_bound .!= T(-Inf)) || any(u_bound .!= T(Inf))
4853
if has_bnds
4954
l_bound_m_x = similar(xk)
@@ -75,6 +80,7 @@ function R2NSolver(
7580
xkn,
7681
s,
7782
s1,
83+
v0,
7884
has_bnds,
7985
l_bound,
8086
u_bound,
@@ -130,6 +136,7 @@ For advanced usage, first define a solver "R2NSolver" to preallocate the memory
130136
- `η2::T = T(0.9)`: very successful iteration threshold;
131137
- `γ::T = T(3)`: regularization parameter multiplier, σ := σ/γ when the iteration is very successful and σ := σγ when the iteration is unsuccessful;
132138
- `θ::T = 1/(1 + eps(T)^(1 / 5))`: is the model decrease fraction with respect to the decrease of the Cauchy model;
139+
- `compute_opnorm::Bool = false`: whether the operator norm of Bₖ should be computed at each iteration. If false, a Rayleigh quotient is computed instead. The first option causes the solver to converge in fewer iterations but the computational cost per iteration is larger;
133140
- `m_monotone::Int = 1`: monotonicity parameter. By default, R2N is monotone but the non-monotone variant will be used if `m_monotone > 1`;
134141
- `sub_kwargs::Dict{Symbol}`: a dictionary containing the keyword arguments to be sent to the subsolver. The solver will fail if invalid keyword arguments are provided to the subsolver.
135142
@@ -215,6 +222,7 @@ function SolverCore.solve!(
215222
γ::T = T(3),
216223
β::T = 1 / eps(T),
217224
θ::T = 1/(1 + eps(T)^(1 / 5)),
225+
compute_opnorm::Bool = false,
218226
sub_kwargs::Dict{Symbol} = Dict(),
219227
) where {T, V, G}
220228
reset!(stats)
@@ -287,9 +295,15 @@ function SolverCore.solve!(
287295

288296
quasiNewtTest = isa(nlp, QuasiNewtonModel)
289297
λmax::T = T(1)
298+
found_λ = true
290299
solver.subpb.model.B = hess_op(nlp, xk)
291300

292-
λmax, found_λ = opnorm(solver.subpb.model.B)
301+
if !compute_opnorm
302+
mul!(solver.subpb.model.v, solver.subpb.model.B, solver.v0)
303+
λmax = dot(solver.v0, solver.subpb.model.v)
304+
else
305+
λmax, found_λ = opnorm(solver.subpb.model.B)
306+
end
293307
found_λ || error("operator norm computation failed")
294308

295309
ν₁ = θ / (λmax + σk)
@@ -419,7 +433,13 @@ function SolverCore.solve!(
419433
end
420434
solver.subpb.model.B = hess_op(nlp, xk)
421435

422-
λmax, found_λ = opnorm(solver.subpb.model.B)
436+
if !compute_opnorm
437+
mul!(solver.subpb.model.v, solver.subpb.model.B, solver.v0)
438+
λmax = dot(solver.v0, solver.subpb.model.v)
439+
else
440+
λmax, found_λ = opnorm(solver.subpb.model.B)
441+
end
442+
423443
found_λ || error("operator norm computation failed")
424444

425445
∇fk⁻ .= ∇fk

test/test_allocs.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ end
4444
for (h, h_name) ((NormL0(λ), "l0"),)
4545
for (solver, solver_name) ((:R2Solver, "R2"), (:R2DHSolver, "R2DH"), (:R2NSolver, "R2N"))
4646
@testset "$(solver_name)" begin
47-
solver_name == "R2N" && continue #FIXME
4847
reg_nlp = RegularizedNLPModel(LBFGSModel(bpdn), h)
4948
solver = eval(solver)(reg_nlp)
5049
stats = RegularizedExecutionStats(reg_nlp)
5150
solver_name == "R2" &&
5251
@test @wrappedallocs(solve!(solver, reg_nlp, stats, ν = 1.0, atol = 1e-6, rtol = 1e-6)) ==
5352
0
54-
solver_name == "R2DH" && @test @wrappedallocs(
53+
(solver_name == "R2DH" || solver_name == "R2N") && @test @wrappedallocs(
5554
solve!(solver, reg_nlp, stats, σk = 1.0, atol = 1e-6, rtol = 1e-6)
5655
) == 0
5756
@test stats.status == :first_order

0 commit comments

Comments
 (0)