forked from JuliaSmoothOptimizers/LinearOperators.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalHessianApproximation.jl
More file actions
255 lines (223 loc) · 6.4 KB
/
DiagonalHessianApproximation.jl
File metadata and controls
255 lines (223 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
export DiagonalPSB, DiagonalAndrei, SpectralGradient, DiagonalBFGS
"""
DiagonalPSB(d)
Construct a linear operator that represents a diagonal PSB quasi-Newton approximation
as described in
M. Zhu, J. L. Nazareth and H. Wolkowicz
The Quasi-Cauchy Relation and Diagonal Updating.
SIAM Journal on Optimization, vol. 9, number 4, pp. 1192-1204, 1999.
https://doi.org/10.1137/S1052623498331793.
The approximation satisfies the weak secant equation and is not guaranteed to be
positive definite.
# Arguments
- `d::AbstractVector`: initial diagonal approximation.
"""
mutable struct DiagonalPSB{T <: Real, I <: Integer, V <: AbstractVector{T}, F} <:
AbstractDiagonalQuasiNewtonOperator{T}
const d::V # Diagonal of the operator
const nrow::I
const ncol::I
const symmetric::Bool
const hermitian::Bool
const prod!::F
const tprod!::F
const ctprod!::F
nprod::I
ntprod::I
nctprod::I
end
@doc (@doc DiagonalPSB) function DiagonalPSB(d::AbstractVector{T}) where {T <: Real}
prod = (res, v, α, β) -> mulSquareOpDiagonal!(res, d, v, α, β)
n = length(d)
DiagonalPSB(d, n, n, true, true, prod, prod, prod, 0, 0, 0)
end
# update function
# s = x_{k+1} - x_k
# y = ∇f(x_{k+1}) - ∇f(x_k)
function push!(
B::DiagonalPSB{T, I, V, F},
s::V,
y::V,
) where {T <: Real, I <: Integer, V <: AbstractVector{T}, F}
sNorm = norm(s, 2)
if sNorm == 0
error("Cannot update DiagonalQN operator with s=0")
end
# sᵀBs = sᵀy can be scaled by ||s||² without changing the update
s2 = (si^2 for si ∈ s)
sNorm2 = sNorm^2
trA2 = dot(s2, s2) / sNorm2^2
sT_y = dot(s, y) / sNorm2
sT_B_s = dot(s2, B.d) / sNorm2
q = sT_y - sT_B_s
q /= trA2
B.d .+= q / sNorm2 .* s .^ 2
return B
end
"""
reset!(op::AbstractDiagonalQuasiNewtonOperator)
Reset the diagonal data of the given operator.
"""
function reset!(op::AbstractDiagonalQuasiNewtonOperator{T}) where {T <: Real}
op.d .= one(T)
op.nprod = 0
op.ntprod = 0
op.nctprod = 0
return op
end
"""
DiagonalAndrei(d)
Construct a linear operator that represents a diagonal quasi-Newton approximation
as described in
Andrei, N.
A diagonal quasi-Newton updating method for unconstrained optimization.
https://doi.org/10.1007/s11075-018-0562-7
The approximation satisfies the weak secant equation and is not guaranteed to be
positive definite.
# Arguments
- `d::AbstractVector`: initial diagonal approximation.
"""
mutable struct DiagonalAndrei{T <: Real, I <: Integer, V <: AbstractVector{T}, F} <:
AbstractDiagonalQuasiNewtonOperator{T}
const d::V # Diagonal of the operator
const nrow::I
const ncol::I
const symmetric::Bool
const hermitian::Bool
const prod!::F
const tprod!::F
const ctprod!::F
nprod::I
ntprod::I
nctprod::I
end
@doc (@doc DiagonalAndrei) function DiagonalAndrei(d::AbstractVector{T}) where {T <: Real}
prod = (res, v, α, β) -> mulSquareOpDiagonal!(res, d, v, α, β)
n = length(d)
DiagonalAndrei(d, n, n, true, true, prod, prod, prod, 0, 0, 0)
end
# update function
# s = x_{k+1} - x_k
# y = ∇f(x_{k+1}) - ∇f(x_k)
function push!(
B::DiagonalAndrei{T, I, V, F},
s::V,
y::V,
) where {T <: Real, I <: Integer, V <: AbstractVector{T}, F}
sNorm = norm(s, 2)
if sNorm == 0
error("Cannot update DiagonalQN operator with s=0")
end
# sᵀBs = sᵀy can be scaled by ||s||² without changing the update
s2 = (si^2 for si ∈ s)
sNorm2 = sNorm^2
trA2 = dot(s2, s2) / sNorm2^2
sT_y = dot(s, y) / sNorm2
sT_B_s = dot(s2, B.d) / sNorm2
q = sT_y - sT_B_s
sT_s = dot(s, s) / sNorm2
q += sT_s
q /= trA2
B.d .+= q / sNorm2 .* s .^ 2 .- 1
return B
end
"""
Implementation of a spectral gradient quasi-Newton approximation described in
Birgin, E. G., Martínez, J. M., & Raydan, M.
Spectral Projected Gradient Methods: Review and Perspectives.
https://doi.org/10.18637/jss.v060.i03
"""
mutable struct SpectralGradient{T <: Real, I <: Integer, F} <:
AbstractDiagonalQuasiNewtonOperator{T}
const d::Vector{T} # Diagonal coefficient of the operator (multiple of the identity)
const nrow::I
const ncol::I
const symmetric::Bool
const hermitian::Bool
const prod!::F
const tprod!::F
const ctprod!::F
nprod::I
ntprod::I
nctprod::I
end
"""
SpectralGradient(σ, n)
Construct a spectral gradient Hessian approximation.
The approximation is defined as σI.
# Arguments
- `σ::Real`: initial positive multiple of the identity;
- `n::Int`: operator size.
"""
function SpectralGradient(σ::T, n::I) where {T <: Real, I <: Integer}
@assert σ > 0
d = [σ]
prod = (res, v, α, β) -> mulSquareOpDiagonal!(res, d, v, α, β)
SpectralGradient(d, n, n, true, true, prod, prod, prod, 0, 0, 0)
end
# update function
# s = x_{k+1} - x_k
# y = ∇f(x_{k+1}) - ∇f(x_k)
function push!(
B::SpectralGradient{T, I, F},
s::V,
y::V,
) where {T <: Real, I <: Integer, F, V <: AbstractVector{T}}
if all(x -> x == 0, s)
error("Cannot divide by zero and s .= 0")
end
B.d[1] = dot(s, y) / dot(s, s)
return B
end
"""
DiagonalBFGS(d)
A diagonal approximation of the BFGS update inspired by
Marnissi, Y., Chouzenoux, E., Benazza-Benyahia, A., & Pesquet, J. C. (2020).
Majorize–minimize adapted Metropolis–Hastings algorithm.
https://ieeexplore.ieee.org/abstract/document/9050537.
# Arguments
- `d::AbstractVector`: initial diagonal approximation.
"""
mutable struct DiagonalBFGS{T <: Real, I <: Integer, V <: AbstractVector{T}, F} <:
AbstractDiagonalQuasiNewtonOperator{T}
const d::V # Diagonal of the operator
const nrow::I
const ncol::I
const symmetric::Bool
const hermitian::Bool
const prod!::F
const tprod!::F
const ctprod!::F
nprod::I
ntprod::I
nctprod::I
end
@doc (@doc DiagonalBFGS) function DiagonalBFGS(d::AbstractVector{T}) where {T <: Real}
prod = (res, v, α, β) -> mulSquareOpDiagonal!(res, d, v, α, β)
n = length(d)
DiagonalBFGS(d, n, n, true, true, prod, prod, prod, 0, 0, 0)
end
# update function
# s = x_{k+1} - x_k
# y = ∇f(x_{k+1}) - ∇f(x_k)
function push!(
B::DiagonalBFGS{T, I, V, F},
s::V,
y::V,
) where {T <: Real, I <: Integer, V <: AbstractVector{T}, F}
sNorm = norm(s, 2)
if sNorm == 0
error("Cannot update DiagonalQN operator with s=0")
end
sNorm2 = sNorm^2
sT_y = dot(s, y) / sNorm2
B.d .= abs.(y)
B.d .*= sum(B.d) / sT_y
return B
end
for op in (DiagonalPSB, DiagonalAndrei, SpectralGradient, DiagonalBFGS)
@eval begin
isallocated5(::$op) = true
has_args5(::$op) = true
end
end