forked from JuliaFirstOrder/ProximalAlgorithms.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimaldual.jl
325 lines (288 loc) · 12.2 KB
/
primaldual.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
# Latafat, Patrinos, "Asymmetric forward–backward–adjoint splitting for
# solving monotone inclusions involving three operators", Computational
# Optimization and Applications, vol. 68, no. 1, pp. 57-93 (2017).
#
# Latafat, Patrinos, "Primal-dual proximal algorithms for structured convex
# optimization: a unifying framework", In Large-Scale and Distributed
# Optimization, Giselsson and Rantzer, Eds. Springer International Publishing,
# pp. 97–120 (2018).
#
# Chambolle, Pock, "A First-Order Primal-Dual Algorithm for Convex Problems
# with Applications to Imaging", Journal of Mathematical Imaging and Vision,
# vol. 40, no. 1, pp. 120-145 (2011).
#
# Condat, "A primal–dual splitting method for convex optimization
# involving Lipschitzian, proximable and linear composite terms",
# Journal of Optimization Theory and Applications, vol. 158, no. 2,
# pp 460-479 (2013).
#
# Vũ, "A splitting algorithm for dual monotone inclusions involving
# cocoercive operators", Advances in Computational Mathematics, vol. 38, no. 3,
# pp. 667-681 (2013).
using Base.Iterators
using ProximalAlgorithms.IterationTools
using ProximalOperators: Zero
using LinearAlgebra
using Printf
"""
AFBAIteration(; <keyword-arguments>)
Instantiate the asymmetric forward-backward-adjoint algorithm (AFBA, see [1])
for solving convex optimization problems of the form
minimize f(x) + g(x) + (h □ l)(L x),
where `f` is smooth, `g` and `h` are possibly nonsmooth and `l` is strongly
convex. Symbol `□` denotes the infimal convolution, and `L` is a linear mapping.
Points `x0` and `y0` are the initial primal and dual iterates, respectively.
If unspecified, functions `f`, `g`, and `h` default to the identically zero
function, `l` defaults to the indicator of the set `{0}`, and `L` defaults to
the identity. Important keyword arguments, in case `f` and `l` are set, are:
* `beta_f`: Lipschitz constant of gradient of `f` (default: zero)
* `beta_l`: Lipschitz constant of gradient of the conjugate of `l` (default: zero)
These are used to determine the algorithm default stepsizes, `gamma1` and `gamma2`,
in case they are not directly specified.
Optional keyword arguments are:
* `gamma1`: stepsize corresponding to the primal updates (default: see [1] for each case)
* `gamma2`: stepsize corresponding to the dual updates (default: see [1] for each case)
* `theta`: nonnegative algorithm parameter (default: `1.0`)
* `mu`: algorithm parameter in the range [0,1] (default: `1.0`)
* `tol`: primal-dual termination tolerance (default: `1e-5`)
* `maxit`: maximum number of iterations (default: `10000`)
* `verbose`, verbosity level (default: `1`)
* `verbose_freq`, verbosity frequency for `verbose = 1` (default: `100`)
The iterator implements Algorithm 3 of [1] with constant stepsize (α_n=λ)
for several prominant special cases:
1) θ = 2 ==> Corresponds to the Vu-Condat Algorithm [2,3].
2) θ = 1, μ=1
3) θ = 0, μ=1
4) θ ∈ [0,∞), μ=0
See [2, Section 5.2] and [1, Figure 1] for stepsize conditions, special cases,
and relation to other algorithms.
# Arguments
- `x0`: initial primal point.
- `y0`: initial dual point.
- `f=Zero()`: smooth objective term.
- `g=Zero()`: proximable objective term.
- `h=Zero()`: proximable objective term.
- `l=IndZero()`: strongly convex function.
- `L=I`: linear operator (e.g. a matrix).
- `beta_f=0`: Lipschitz constant of the gradient of `f`.
- `beta_l=0`: Lipschitz constant of the gradient of `l` conjugate.
- `theta=1`: nonnegative algorithm parameter.
- `mu=1`: algorithm parameter in the range [0,1].
- `gamma1`: primal stepsize (see [1] for the default choice).
- `gamma2`: dual stepsize (see [1] for the default choice).
# References
- [1] Latafat, Patrinos, "Asymmetric forward–backward–adjoint splitting for
solving monotone inclusions involving three operators", Computational
Optimization and Applications, vol. 68, no. 1, pp. 57-93 (2017).
- [2] Latafat, Patrinos, "Primal-dual proximal algorithms for structured convex
optimization : a unifying framework", In Large-Scale and Distributed
Optimization, Giselsson and Rantzer, Eds. Springer International Publishing,
pp. 97–120 ( 2018).
- [3] Condat, "A primal–dual splitting method for convex optimization
involving Lipschitzian, proximable and linear composite terms",
Journal of Optimization Theory and Applications, vol. 158, no. 2,
pp 460-479 (2013).
- [4] Vũ, "A splitting algorithm for dual monotone inclusions involving
cocoercive operators", Advances in Computational Mathematics, vol. 38, no. 3,
pp. 667-681 (2013).
"""
Base.@kwdef struct AFBAIteration{R,Tx,Ty,Tf,Tg,Th,Tl,TL}
f::Tf = Zero()
g::Tg = Zero()
h::Th = Zero()
l::Tl = IndZero()
L::TL = if isa(h, Zero) L = 0 * I else I end
x0::Tx
y0::Ty
beta_f::R = real(eltype(x0))(0)
beta_l::R = real(eltype(x0))(0)
theta = 1
mu = 1
lambda::R = real(eltype(x0))(1)
gamma::Tuple{R, R} = begin
if lambda != 1
@warn "default stepsizes are not supported with this choice of lamdba, reverted to default lambda"
lambda = real(eltype(x0))(1)
end
AFBA_default_stepsizes(L, h, theta, mu, beta_f, beta_l)
end
end
"""
VuCondatIteration(; <keyword-arguments>)
Instantiate the Vũ-Condat primal-dual algorithm (see [1, 2])
for solving convex optimization problems of the form
minimize f(x) + g(x) + (h □ l)(L x),
where `f` is smooth, `g` and `h` are possibly nonsmooth and `l` is strongly
convex. Symbol `□` denotes the infimal convolution, and `L` is a linear mapping.
For the arguments see [`AFBAIteration](@ref).
# References
- [1] Condat, "A primal–dual splitting method for convex optimization
involving Lipschitzian, proximable and linear composite terms",
Journal of Optimization Theory and Applications, vol. 158, no. 2,
pp 460-479 (2013).
- [2] Vũ, "A splitting algorithm for dual monotone inclusions involving
cocoercive operators", Advances in Computational Mathematics, vol. 38, no. 3,
pp. 667-681 (2013).
"""
VuCondatIteration(kwargs...) = AFBAIteration(kwargs..., theta=2)
Base.IteratorSize(::Type{<:AFBAIteration}) = Base.IsInfinite()
Base.@kwdef struct AFBAState{Tx,Ty}
x::Tx
y::Ty
xbar::Tx = zero(x)
ybar::Ty = zero(y)
gradf::Tx = zero(x)
gradl::Ty = zero(y)
FPR_x::Tx = zero(x)
FPR_y::Ty = zero(y)
temp_x::Tx = zero(x)
temp_y::Ty = zero(y)
end
function Base.iterate(iter::AFBAIteration, state::AFBAState = AFBAState(x=copy(iter.x0), y=copy(iter.y0)))
# perform xbar-update step
gradient!(state.gradf, iter.f, state.x)
mul!(state.temp_x, iter.L', state.y)
state.temp_x .+= state.gradf
state.temp_x .*= -iter.gamma[1]
state.temp_x .+= state.x
prox!(state.xbar, iter.g, state.temp_x, iter.gamma[1])
# perform ybar-update step
gradient!(state.gradl, Conjugate(iter.l), state.y)
state.temp_x .= iter.theta .* state.xbar .+ (1 - iter.theta) .* state.x
mul!(state.temp_y, iter.L, state.temp_x)
state.temp_y .-= state.gradl
state.temp_y .*= iter.gamma[2]
state.temp_y .+= state.y
prox!(state.ybar, Conjugate(iter.h), state.temp_y, iter.gamma[2])
# the residues
state.FPR_x .= state.xbar .- state.x
state.FPR_y .= state.ybar .- state.y
# perform x-update step
state.temp_y .= (iter.mu * (2 - iter.theta) * iter.gamma[1]) .* state.FPR_y
mul!(state.temp_x, iter.L', state.temp_y)
state.x .+= iter.lambda .* (state.FPR_x .- state.temp_x)
# perform y-update step
state.temp_x .= ((1 - iter.mu) * (2 - iter.theta) * iter.gamma[2]) .* state.FPR_x
mul!(state.temp_y, iter.L, state.temp_x)
state.y .+= iter.lambda .* (state.FPR_y .+ state.temp_y)
return state, state
end
# Solver
struct AFBA{R, K}
maxit::Int
tol::R
verbose::Bool
freq::Int
kwargs::K
end
function (solver::AFBA)(x0, y0; kwargs...)
stop(state::AFBAState) = norm(state.FPR_x, Inf) + norm(state.FPR_y, Inf) <= solver.tol
disp((it, state)) =
@printf("%6d | %7.4e\n", it, norm(state.FPR_x, Inf) + norm(state.FPR_y, Inf))
iter = AFBAIteration(; x0=x0, y0=y0, solver.kwargs..., kwargs...)
iter = take(halt(iter, stop), solver.maxit)
iter = enumerate(iter)
if solver.verbose
iter = tee(sample(iter, solver.freq), disp)
end
num_iters, state_final = loop(iter)
return state_final.xbar, state_final.ybar, num_iters
end
AFBA(; maxit=10_000, tol=1e-5, verbose=false, freq=100, kwargs...) =
AFBA(maxit, tol, verbose, freq, kwargs)
VuCondat(; maxit=10_000, tol=1e-5, verbose=false, freq=100, kwargs...) =
AFBA(maxit=maxit, tol=tol, verbose=verbose, freq=freq, kwargs..., theta=2)
function AFBA_default_stepsizes(L, h, theta, mu, beta_f::R, beta_l::R) where {R<:Real}
# lambda = 1
if isa(h, Zero)
gamma1 = R(1.99) / beta_f
gamma2 = R(1) # does not matter
else
par = R(5) # scaling parameter for comparing Lipschitz constants and \|L\|
par2 = R(100) # scaling parameter for α
alpha = R(1)
nmL = R(opnorm(L))
if theta == 2 # default stepsize for Vu-Condat
if nmL > par * max(beta_l, beta_f)
alpha = R(1)
elseif beta_f > par * beta_l
alpha = par2 * nmL / beta_f
elseif beta_l > par * beta_f
alpha = beta_l / (par2 * nmL)
end
gamma1 = R(1) / (beta_f / 2 + nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + nmL * alpha)
elseif theta == 1 && mu == 1 # SPCA
if nmL > par2 * beta_l # for the case beta_f = 0
alpha = R(1)
elseif beta_l > par * beta_f
alpha = beta_l / (par2 * nmL)
end
gamma1 = beta_f > 0 ? R(1.99) / beta_f : R(1) / (nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + gamma1 * nmL^2)
elseif theta == 0 && mu == 1 # PPCA
temp = R(3)
if beta_f == 0
nmL *= sqrt(temp)
if nmL > par * beta_l
alpha = R(1)
else
alpha = beta_l / (par2 * nmL)
end
gamma1 = R(1) / (beta_f / 2 + nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + nmL * alpha)
else
if nmL > par * max(beta_l, beta_f)
alpha = R(1)
elseif beta_f > par * beta_l
alpha = par2 * nmL / beta_f
elseif beta_l > par * beta_f
alpha = beta_l / (par2 * nmL)
end
xi = 1 + 2 * nmL / (nmL + alpha * beta_f / 2)
gamma1 = R(1) / (beta_f / 2 + nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + xi * nmL * alpha)
end
elseif mu == 0 # SDCA & PDCA
temp = theta^2 - 3 * theta + 3
if beta_l == R(0)
nmL *= sqrt(temp)
if nmL > par * beta_f
alpha = R(1)
else
alpha = par2 * nmL / beta_f
end
gamma1 = R(1) / (beta_f / 2 + nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + nmL * alpha)
else
if nmL > par * max(beta_l, beta_f)
alpha = R(1)
elseif beta_f > par * beta_l
alpha = par2 * nmL / beta_f
elseif beta_l > par * beta_f
alpha = beta_l / (par2 * nmL)
end
eta = 1 + (temp - 1) * alpha * nmL / (alpha * nmL + beta_l / 2)
gamma1 = R(1) / (beta_f / 2 + eta * nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + nmL * alpha)
end
elseif theta == 0 && mu == 0.5 # PPDCA
if beta_l == 0 || beta_f == 0
if nmL > par * max(beta_l, beta_f)
alpha = R(1)
elseif beta_f > par * beta_l
alpha = par2 * nmL / beta_f
elseif beta_l > par * beta_f
alpha = beta_l / (par2 * nmL)
end
else
alpha = sqrt(beta_l / beta_f) / 2
end
gamma1 = R(1) / (beta_f / 2 + nmL / alpha)
gamma2 = R(0.99) / (beta_l / 2 + nmL * alpha)
else
error("this choice of theta and mu is not supported!")
end
end
return gamma1, gamma2
end