Skip to content

Commit 70601cd

Browse files
add an option to store the hessian in sparse format for R2N
1 parent 350cfd6 commit 70601cd

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ ProximalOperators = "a725b495-10eb-56fe-b38b-717eba820537"
1717
RegularizedProblems = "ea076b23-609f-44d2-bb12-a4ae45328278"
1818
ShiftedProximalOperators = "d4fd37fa-580c-4e43-9b30-361c21aae263"
1919
SolverCore = "ff4d7338-4cf1-434d-91df-b86cb86fb843"
20+
SparseMatricesCOO = "fa32481b-f100-4b48-8dc8-c62f61b13870"
2021

2122
[compat]
22-
ADNLPModels = "0.8.13"
2323
Arpack = "0.5"
2424
LinearOperators = "2.10.0"
25-
ManualNLPModels = "0.2.0"
2625
NLPModels = "0.19, 0.20, 0.21"
2726
NLPModelsModifiers = "0.7"
2827
OptimizationProblems = "0.9.2"
@@ -31,6 +30,7 @@ ProximalOperators = "0.15"
3130
RegularizedProblems = "0.1.1"
3231
ShiftedProximalOperators = "0.2"
3332
SolverCore = "0.3.0"
33+
SparseMatricesCOO = "0.2.4"
3434
julia = "^1.6.0"
3535

3636
[extras]

src/R2N.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mutable struct R2NSolver{
2020
s1::V
2121
v0::V
2222
has_bnds::Bool
23+
store_h::Bool
2324
l_bound::V
2425
u_bound::V
2526
l_bound_m_x::V
@@ -34,6 +35,7 @@ function R2NSolver(
3435
reg_nlp::AbstractRegularizedNLPModel{T, V};
3536
subsolver = R2Solver,
3637
m_monotone::Int = 1,
38+
store_h = false
3739
) where {T, V}
3840
x0 = reg_nlp.model.meta.x0
3941
l_bound = reg_nlp.model.meta.lvar
@@ -67,7 +69,15 @@ function R2NSolver(
6769
has_bnds ? shifted(reg_nlp.h, xk, l_bound_m_x, u_bound_m_x, reg_nlp.selected) :
6870
shifted(reg_nlp.h, xk)
6971

70-
Bk = hess_op(reg_nlp.model, x0)
72+
store_h = isa(reg_nlp.model, QuasiNewtonModel) ? false : store_h
73+
74+
if !store_h
75+
Bk = hess_op(reg_nlp.model, x0)
76+
else
77+
rows, cols = hess_structure(reg_nlp.model)
78+
vals = hess_coord(reg_nlp.model, x0)
79+
Bk = SparseMatrixCOO(reg_nlp.model.meta.nvar, reg_nlp.model.meta.nvar, rows, cols, vals)
80+
end
7181
sub_nlp = R2NModel(Bk, ∇fk, T(1), x0)
7282
subpb = RegularizedNLPModel(sub_nlp, ψ)
7383
substats = RegularizedExecutionStats(subpb)
@@ -85,6 +95,7 @@ function R2NSolver(
8595
s1,
8696
v0,
8797
has_bnds,
98+
store_h,
8899
l_bound,
89100
u_bound,
90101
l_bound_m_x,
@@ -217,8 +228,7 @@ function SolverCore.solve!(
217228
γ::T = T(3),
218229
β::T = 1 / eps(T),
219230
θ::T = 1/(1 + eps(T)^(1 / 5)),
220-
opnorm_maxiter::Int = 5,
221-
sub_kwargs::NamedTuple = NamedTuple(),
231+
sub_kwargs::Dict{Symbol, T} = Dict{Symbol, T}(),
222232
) where {T, V, G}
223233
reset!(stats)
224234

@@ -291,8 +301,11 @@ function SolverCore.solve!(
291301

292302
quasiNewtTest = isa(nlp, QuasiNewtonModel)
293303
λmax::T = T(1)
294-
found_λ = true
295-
solver.subpb.model.B = hess_op(nlp, xk)
304+
if !solver.store_h
305+
solver.subpb.model.B = hess_op(nlp, xk)
306+
else
307+
hess_coord!(nlp, xk, solver.subpb.model.B.vals)
308+
end
296309

297310
if opnorm_maxiter 0
298311
λmax, found_λ = opnorm(solver.subpb.model.B)
@@ -445,7 +458,11 @@ function SolverCore.solve!(
445458
push!(nlp, s, solver.y)
446459
qn_copy!(nlp, solver, stats)
447460
end
448-
solver.subpb.model.B = hess_op(nlp, xk)
461+
if !solver.store_h
462+
solver.subpb.model.B = hess_op(nlp, xk)
463+
else
464+
hess_coord!(nlp, xk, solver.subpb.model.B.vals)
465+
end
449466

450467
if opnorm_maxiter 0
451468
λmax, found_λ = opnorm(solver.subpb.model.B)

src/R2NModel.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ this model represents the smooth R2N subproblem:
1414
where `B` is either an approximation of the Hessian of `f` or the Hessian itself and `∇f` represents the gradient of `f` at `x0`.
1515
`σ > 0` is a regularization parameter and `v` is a vector of the same size as `x0` used for intermediary computations.
1616
"""
17-
mutable struct R2NModel{T <: Real, V <: AbstractVector{T}, G <: AbstractLinearOperator{T}} <:
17+
mutable struct R2NModel{T <: Real, V <: AbstractVector{T}, G <: Union{AbstractLinearOperator{T}, AbstractMatrix{T}}} <:
1818
AbstractNLPModel{T, V}
1919
B::G
2020
∇f::V
@@ -34,15 +34,22 @@ function R2NModel(B::G, ∇f::V, σ::T, x0::V) where {T, V, G}
3434
return R2NModel(B::G, ∇f::V, v::V, σ::T, meta, Counters())
3535
end
3636

37-
function NLPModels.obj(nlp::R2NModel, x::AbstractVector; skip_sigma = false)
37+
function NLPModels.obj(nlp::R2NModel{T, V, M}, x::V) where{T, V, M <: AbstractLinearOperator{T}}
3838
@lencheck nlp.meta.nvar x
3939
increment!(nlp, :neval_obj)
4040
mul!(nlp.v, nlp.B, x)
4141
skip_sigma && return dot(nlp.v, x)/2 + dot(nlp.∇f, x)
4242
return dot(nlp.v, x)/2 + dot(nlp.∇f, x) + nlp.σ * dot(x, x) / 2
4343
end
4444

45-
function NLPModels.grad!(nlp::R2NModel, x::AbstractVector, g::AbstractVector)
45+
function NLPModels.obj(nlp::R2NModel{T, V, M}, x::V) where{T, V, M <: AbstractMatrix{T}}
46+
@lencheck nlp.meta.nvar x
47+
increment!(nlp, :neval_obj)
48+
mul!(nlp.v, Symmetric(nlp.B,:L), x)
49+
return dot(nlp.v, x)/2 + dot(nlp.∇f, x) + nlp.σ * dot(x, x) / 2
50+
end
51+
52+
function NLPModels.grad!(nlp::R2NModel{T, V, M}, x::V, g::V) where{T, V, M <: AbstractLinearOperator{T}}
4653
@lencheck nlp.meta.nvar x
4754
@lencheck nlp.meta.nvar g
4855
increment!(nlp, :neval_grad)
@@ -51,3 +58,13 @@ function NLPModels.grad!(nlp::R2NModel, x::AbstractVector, g::AbstractVector)
5158
g .+= nlp.σ .* x
5259
return g
5360
end
61+
62+
function NLPModels.grad!(nlp::R2NModel{T, V, M}, x::V, g::V) where{T, V, M <: AbstractMatrix{T}}
63+
@lencheck nlp.meta.nvar x
64+
@lencheck nlp.meta.nvar g
65+
increment!(nlp, :neval_grad)
66+
mul!(g, Symmetric(nlp.B,:L), x)
67+
g .+= nlp.∇f
68+
g .+= nlp.σ .* x
69+
return g
70+
end

src/RegularizedOptimization.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ using Arpack, ProximalOperators
88

99
# dependencies from us
1010
using LinearOperators,
11-
ManualNLPModels,
12-
NLPModels,
13-
NLPModelsModifiers,
14-
RegularizedProblems,
15-
ShiftedProximalOperators,
16-
SolverCore
11+
NLPModels, NLPModelsModifiers, RegularizedProblems, ShiftedProximalOperators, SolverCore, SparseMatricesCOO
1712
using Percival: AugLagModel, update_y!, update_μ!
1813

1914
const callback_docstring = "

0 commit comments

Comments
 (0)