/
quasi_Newton.jl
427 lines (382 loc) · 16.2 KB
/
quasi_Newton.jl
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
@doc raw"""
quasi_Newton(M, F, ∇F, x)
Perform a quasi Newton iteration for `F` on the manifold `M` starting
in the point `x` using a retraction ``R`` and a vector transport ``T``
The ``k``th iteration consists of
1. Compute the search direction ``η_k = -\mathcal{B}_k [∇f (x_k)]`` or solve ``\mathcal{H}_k [η_k] = -∇f (x_k)]``.
2. Determine a suitable stepsize ``α_k`` along the curve ``\gamma(α) = R_{x_k}(α η_k)`` e.g. by using [`WolfePowellLineseach`](@ref).
3. Compute ``x_{k+1} = R_{x_k}(α_k η_k)``.
4. Define ``s_k = T_{x_k, α_k η_k}(α_k η_k)`` and ``y_k = ∇f(x_{k+1}) - T_{x_k, α_k η_k}(∇f(x_k))``.
5. Compute the new approximate Hessian ``H_{k+1}`` or its inverse ``B_k``.
# Input
* `M` – a manifold ``\mathcal{M}``.
* `F` – a cost function ``F \colon \mathcal{M} \to ℝ`` to minimize.
* `∇F`– the gradient ``∇F \colon \mathcal{M} \to T_x\mathcal M`` of ``F``.
* `x` – an initial value ``x \in \mathcal{M}``.
stopping_criterion::StoppingCriterion=StopWhenAny(
StopAfterIteration(max(1000, memory_size)), StopWhenGradientNormLess(10^(-6))
),
return_options=false,
# Optional
* `basis` – (`DefaultOrthonormalBasis()`) basis within the tangent space(s) to represent the Hessian (inverse).
* `cautious_update` – (`false`) – whether or not to use a [`QuasiNewtonCautiousDirectionUpdate`](@ref)
* `cautious_function` – (`(x) -> x*10^(-4)`) – a monotone increasing function that is zero at 0 and strictly increasing at 0 for the cautious update.
* `direction_update` – ([`InverseBFGS`](@ref)`()`) the update rule to use.
* `initial_operator` – (`Matrix{Float64}(I,n,n)`) initial matrix to use die the approximation, where `n=manifold_dimension(M)`, see also `scale_initial_operator`.
* `memory_size` – (`20`) limited memory, number of ``s_k, y_k`` to store. Set to a negative value to use a full memory representation
* `retraction_method` – (`ExponentialRetraction()`) a retraction method to use, by default the exponntial map.
* `scale_initial_operator` - (`true`) scale initial operator with ``\frac{⟨s_k,y_k⟩_{x_k}}{\lVert y_k\rVert_{x_k}}`` in the computation
* `step_size` – ([`WolfePowellLineseach`](@ref)`(retraction_method, vector_transport_method)`) specify a [`Stepsize`](@ref).
* `stopping_criterion` - (`StopWhenAny(StopAfterIteration(max(1000, memory_size)), StopWhenGradientNormLess(10^(-6))`) specify a [`StoppingCriterion`](@ref)
* `vector_transport_method` – (`ParallelTransport()`) a vector transport to use, by default the parallel transport.
* `return_options` – (`false`) – specify whether to return just the result `x` (default) or the complete [`Options`](@ref), e.g. to access recorded values. if activated, the extended result, i.e. the
# Output
* `x_opt` – the resulting (approximately critical) point of the quasi–Newton method
OR
* `options` – the options returned by the solver (see `return_options`)
"""
function quasi_Newton(M::Manifold, F::Function, ∇F::G, x::P; kwargs...) where {P,G}
x_res = allocate(x)
copyto!(x_res, x)
return quasi_Newton!(M, F, ∇F, x_res; kwargs...)
end
@doc raw"""
quasi_Newton!(M, F, ∇F, x; options...)
Perform a quasi Newton iteration for `F` on the manifold `M` starting
in the point `x` using a retraction ``R`` and a vector transport ``T``.
# Input
* `M` – a manifold ``\mathcal{M}``.
* `F` – a cost function ``F \colon \mathcal{M} \to ℝ`` to minimize.
* `∇F`– the gradient ``∇F \colon \mathcal{M} \to T_x\mathcal M`` of ``F``.
* `x` – an initial value ``x \in \mathcal{M}``.
For all optional parameters, see [`quasi_Newton`](@ref).
"""
function quasi_Newton!(
M::Manifold,
F::Function,
∇F::G,
x::P;
retraction_method::AbstractRetractionMethod=ExponentialRetraction(),
vector_transport_method::AbstractVectorTransportMethod=ParallelTransport(),
basis::AbstractBasis=DefaultOrthonormalBasis(),
direction_update::AbstractQuasiNewtonUpdateRule=InverseBFGS(),
cautious_update::Bool=false,
cautious_function::Function=x -> x * 10^(-4),
memory_size::Int=20,
initial_operator::AbstractMatrix=Matrix{Float64}(
I, manifold_dimension(M), manifold_dimension(M)
),
scale_initial_operator::Bool=true,
step_size::Stepsize=WolfePowellLineseach(retraction_method, vector_transport_method),
stopping_criterion::StoppingCriterion=StopWhenAny(
StopAfterIteration(max(1000, memory_size)), StopWhenGradientNormLess(10^(-6))
),
return_options=false,
kwargs...,
) where {P,G}
if memory_size >= 0
local_dir_upd = QuasiNewtonLimitedMemoryDirectionUpdate(
direction_update,
zero_tangent_vector(M, x),
memory_size;
scale=scale_initial_operator,
vector_transport_method=vector_transport_method,
)
else
local_dir_upd = QuasiNewtonMatrixDirectionUpdate(
direction_update,
basis,
initial_operator;
scale=scale_initial_operator,
vector_transport_method=vector_transport_method,
)
end
if cautious_update == true
local_dir_upd = QuasiNewtonCautiousDirectionUpdate(
local_dir_upd; θ=cautious_function
)
end
o = QuasiNewtonOptions(
x,
∇F(x),
local_dir_upd,
stopping_criterion,
step_size;
retraction_method=retraction_method,
vector_transport_method=vector_transport_method,
)
p = GradientProblem(M, F, ∇F)
o = decorate_options(o; kwargs...)
resultO = solve(p, o)
if return_options
return resultO
else
return get_solver_result(resultO)
end
end
function initialize_solver!(::GradientProblem, ::QuasiNewtonOptions) end
function step_solver!(p::GradientProblem, o::QuasiNewtonOptions, iter)
o.∇ = get_gradient(p, o.x)
η = o.direction_update(p, o)
α = o.stepsize(p, o, iter, η)
x_old = deepcopy(o.x)
retract!(p.M, o.x, o.x, α * η, o.retraction_method)
β = locking_condition_scale(
p.M, o.direction_update, x_old, α * η, o.x, o.vector_transport_method
)
vector_transport_to!(
p.M, o.sk, x_old, α * η, o.x, get_update_vector_transport(o.direction_update)
)
vector_transport_to!(
p.M, o.∇, x_old, o.∇, o.x, get_update_vector_transport(o.direction_update)
)
o.yk = get_gradient(p, o.x) / β - o.∇
update_hessian!(o.direction_update, p, o, x_old, iter)
return o
end
function locking_condition_scale(
M::Manifold, ::AbstractQuasiNewtonDirectionUpdate, x_old, v, x, vt
)
return norm(M, x_old, v) / norm(M, x, vector_transport_to(M, x_old, v, x, vt))
end
@doc raw"""
update_hessian!(d, p, o, x_old, iter)
update the hessian wihtin the [`QuasiNewtonOptions`](@ref) `o` given a [`Problem`](@ref) `p`
as well as the an [`AbstractQuasiNewtonDirectionUpdate`](@ref) `d` and the last iterate `x_old`.
Note that the current (`iter`th) iterate is already stored in `o.x`.
See also [`AbstractQuasiNewtonUpdateRule`](@ref) for the different rules that are available
within `d`.
"""
update_hessian!(d::AbstractQuasiNewtonDirectionUpdate, ::Any, ::Any, ::Any, ::Any)
function update_hessian!(
d::QuasiNewtonMatrixDirectionUpdate{InverseBFGS}, p, o, x_old, iter
)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
skyk_c = inner(p.M, o.x, o.sk, o.yk)
if iter == 1 && d.scale == true
d.matrix = skyk_c / inner(p.M, o.x, o.yk, o.yk) * d.matrix
end
d.matrix =
(I - sk_c * yk_c' / skyk_c) * d.matrix * (I - yk_c * sk_c' / skyk_c) +
sk_c * sk_c' / skyk_c
return d
end
# BFGS update
function update_hessian!(d::QuasiNewtonMatrixDirectionUpdate{BFGS}, p, o, x_old, iter)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
skyk_c = inner(p.M, o.x, o.sk, o.yk)
if iter == 1 && d.scale == true
d.matrix = inner(p.M, o.x, o.yk, o.yk) / skyk_c * d.matrix
end
d.matrix =
d.matrix + yk_c * yk_c' / skyk_c -
d.matrix * sk_c * sk_c' * d.matrix / dot(sk_c, d.matrix * sk_c)
return d
end
# Inverese DFP update
function update_hessian!(d::QuasiNewtonMatrixDirectionUpdate{InverseDFP}, p, o, x_old, iter)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
skyk_c = inner(p.M, o.x, o.sk, o.yk)
if iter == 1 && d.scale == true
d.matrix = inner(p.M, o.x, o.sk, o.sk) / skyk_c * d.matrix
end
d.matrix =
d.matrix + sk_c * sk_c' / skyk_c -
d.matrix * yk_c * yk_c' * d.matrix / dot(yk_c, d.matrix * yk_c)
return d
end
# DFP update
function update_hessian!(d::QuasiNewtonMatrixDirectionUpdate{DFP}, p, o, x_old, iter)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
skyk_c = inner(p.M, o.x, o.sk, o.yk)
if iter == 1 && d.scale == true
d.matrix = skyk_c / inner(p.M, o.x, o.sk, o.sk) * d.matrix
end
d.matrix =
(I - yk_c * sk_c' / skyk_c) * d.matrix * (I - sk_c * yk_c' / skyk_c) +
yk_c * yk_c' / skyk_c
return d
end
# Inverse SR-1 update
function update_hessian!(
d::QuasiNewtonMatrixDirectionUpdate{InverseSR1}, p, o, x_old, ::Int
)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
# computing the new matrix which represents the approximating operator in the next iteration
srvec = sk_c - d.matrix * yk_c
if d.update.r < 0 || abs(dot(srvec, yk_c)) >= d.update.r * norm(srvec) * norm(yk_c)
d.matrix = d.matrix + srvec * srvec' / (srvec' * yk_c)
end
return d
end
# SR-1 update
function update_hessian!(d::QuasiNewtonMatrixDirectionUpdate{SR1}, p, o, x_old, ::Int)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
# computing the new matrix which represents the approximating operator in the next iteration
srvec = yk_c - d.matrix * sk_c
if d.update.r < 0 || abs(dot(srvec, sk_c)) >= d.update.r * norm(srvec) * norm(sk_c)
d.matrix = d.matrix + srvec * srvec' / (srvec' * sk_c)
end
return d
end
# Inverse Broyden update
function update_hessian!(
d::QuasiNewtonMatrixDirectionUpdate{InverseBroyden}, p, o, x_old, ::Int
)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
skyk_c = inner(p.M, o.x, o.sk, o.yk)
ykBkyk_c = yk_c' * d.matrix * yk_c
φ = update_broyden_factor!(d, sk_c, yk_c, skyk_c, ykBkyk_c, d.update.update_rule)
# computing the new matrix which represents the approximating operator in the next iteration
d.matrix =
d.matrix - (d.matrix * yk_c * yk_c' * d.matrix) / ykBkyk_c +
(sk_c * sk_c') / skyk_c +
φ *
ykBkyk_c *
(sk_c / skyk_c - (d.matrix * yk_c) / ykBkyk_c) *
(sk_c / skyk_c - (d.matrix * yk_c) / ykBkyk_c)'
return d
end
# Broyden update
function update_hessian!(d::QuasiNewtonMatrixDirectionUpdate{Broyden}, p, o, x_old, ::Int)
update_basis!(d.basis, p.M, x_old, o.x, d.vector_transport_method)
yk_c = get_coordinates(p.M, o.x, o.yk, d.basis)
sk_c = get_coordinates(p.M, o.x, o.sk, d.basis)
skyk_c = inner(p.M, o.x, o.sk, o.yk)
skHksk_c = sk_c' * d.matrix * sk_c
φ = update_broyden_factor!(d, sk_c, yk_c, skyk_c, skHksk_c, d.update.update_rule)
# computing the new matrix which represents the approximating operator in the next iteration
d.matrix =
d.matrix - (d.matrix * sk_c * sk_c' * d.matrix) / skHksk_c +
(yk_c * yk_c') / skyk_c +
φ *
skHksk_c *
(yk_c / skyk_c - (d.matrix * sk_c) / skHksk_c) *
(yk_c / skyk_c - (d.matrix * sk_c) / skHksk_c)'
return d
end
function update_broyden_factor!(d, sk_c, yk_c, skyk_c, skHksk_c, s::Symbol)
return update_broyden_factor!(d, sk_c, yk_c, skyk_c, skHksk_c, Val(s))
end
function update_broyden_factor!(d, ::Any, ::Any, ::Any, ::Any, ::Val{:constant})
return d.update.φ
end
function update_broyden_factor!(d, ::Any, yk_c, skyk_c, skHksk_c, ::Val{:Davidon})
yk_c_c = d.matrix \ yk_c
ykyk_c_c = yk_c' * yk_c_c
if skyk_c <= 2 * (skHksk_c * ykyk_c_c) / (skHksk_c + ykyk_c_c)
return d.update.φ =
(skyk_c * (ykyk_c_c - skyk_c)) / (skHksk_c * ykyk_c_c - skyk_c^2)
else
return d.update.φ = skyk_c / (skyk_c - skHksk_c)
end
end
function update_broyden_factor!(d, sk_c, ::Any, skyk_c, ykBkyk_c, ::Val{:InverseDavidon})
sk_c_c = d.matrix \ sk_c
sksk_c_c = sk_c' * sk_c_c
if skyk_c <= 2 * (ykBkyk_c * sksk_c_c) / (ykBkyk_c + sksk_c_c)
return d.update.φ =
(skyk_c * (sksk_c_c - skyk_c)) / (ykBkyk_c * sksk_c_c - skyk_c^2)
else
return d.update.φ = skyk_c / (skyk_c - ykBkyk_c)
end
end
function update_basis!(
b::AbstractBasis, ::Manifold, ::P, ::P, ::AbstractVectorTransportMethod
) where {P}
return b
end
function update_basis!(
b::CachedBasis, M::Manifold, x::P, y::P, m::AbstractVectorTransportMethod
) where {P}
# transport all basis tangent vectors in the tangent space of the next iterate
for v in b.data
vector_transport_to!(M, v, x, v, y, m)
end
return b
end
# Cautious update
function update_hessian!(
d::QuasiNewtonCautiousDirectionUpdate{U}, p, o, x_old, iter
) where {U<:AbstractQuasiNewtonDirectionUpdate}
# computing the bound used in the decission rule
bound = d.θ(norm(p.M, o.x, o.∇))
sk_normsq = norm(p.M, o.x, o.sk)^2
if sk_normsq != 0 && (inner(p.M, o.x, o.sk, o.yk) / sk_normsq) >= bound
update_hessian!(d.update, p, o, x_old, iter)
end
return d
end
# Limited-memory update
function update_hessian!(
d::QuasiNewtonLimitedMemoryDirectionUpdate{U}, p, o, x_old, ::Int
) where {U<:InverseBFGS}
(capacity(d.memory_s) == 0) && return d
# only transport the first if it does not get overwritten at the end
start = length(d.memory_s) == capacity(d.memory_s) ? 2 : 1
for i in start:length(d.memory_s)
# transport all stored tangent vectors in the tangent space of the next iterate
vector_transport_to!(
p.M, d.memory_s[i], x_old, d.memory_s[i], o.x, d.vector_transport_method
)
vector_transport_to!(
p.M, d.memory_y[i], x_old, d.memory_y[i], o.x, d.vector_transport_method
)
end
# add newest
push!(d.memory_s, o.sk)
push!(d.memory_y, o.yk)
return d
end
# all Cautious Limited Memory
function update_hessian!(
d::QuasiNewtonCautiousDirectionUpdate{QuasiNewtonLimitedMemoryDirectionUpdate{NT,T,VT}},
p,
o,
x_old,
iter,
) where {NT<:AbstractQuasiNewtonUpdateRule,T,VT<:AbstractVectorTransportMethod}
# computing the bound used in the decission rule
bound = d.θ(norm(p.M, x_old, get_gradient(p, x_old)))
sk_normsq = norm(p.M, o.x, o.sk)^2
# if the decission rule is fulfilled, the new sk and yk are added
if sk_normsq != 0 && (inner(p.M, o.x, o.sk, o.yk) / sk_normsq) >= bound
update_hessian!(d.update, p, o, x_old, iter)
else
# the stored vectores are just transported to the new tangent space, sk and yk are not added
for i in 1:length(d.update.memory_s)
vector_transport_to!(
p.M,
d.update.memory_s[i],
x_old,
d.update.memory_s[i],
o.x,
d.update.vector_transport_method,
)
vector_transport_to!(
p.M,
d.update.memory_y[i],
x_old,
d.update.memory_y[i],
o.x,
d.update.vector_transport_method,
)
end
end
return d
end
get_solver_result(o::QuasiNewtonOptions) = o.x