-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDMM.jl
441 lines (368 loc) · 13.3 KB
/
RDMM.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
428
429
430
431
432
433
434
435
436
437
438
439
440
using LinearAlgebra
using Random
using Statistics
using FFTW
using Convex
using SCS
using Random
"""
Algorithm 1 from (Cand`es, Jiang, Pilanci, 2021). Utilizes RDMM to solve a
least squares problem of the form ||Ax-b||_2^2
Inputs:
A - Data matrix
b - Data vector
N - Number of agents
maxiter - number of RDMM iterations to perform
abserr - desired absolute difference in norm of mean(x) between iterations.
relerr - desired relative difference in norm of mean(x) between iterations. If the
difference in norms is less than the sum of absolute and relative error, the
function stops iterating and returns the current point.
mu - step size (best to just leave as is unless you really know to use something else)
rflag - determines whether to use randomized preprocessing. Without rflag=true,
this function utilizes standard distributed ADMM instead of RDMM
ret_lambda - boolean to determine if lambda is returned
Outputs:
mean(x) - optimal primal variable for the least squares problem
lambda - final dual coefficients for distributed problem
"""
function rdmm_ls(A, b, N; maxiter=100, abserr=0.001, relerr=0.01, mu=1, rflag=true, ret_lambda = false)
n = size(A,1)
d = size(A,2)
SA, Sb = preprocess_ls(A, b, N; rflag=rflag)
AtStSA = [zeros(d,d) for i=1:N]
invAtStSA = [zeros(d,d) for i=1:N]
AtStSb = [zeros(d,1) for i=1:N]
Threads.@threads for i=1:N
AtStSA[i] = SA[i]'*SA[i]
invAtStSA[i] = inv(AtStSA[i])
AtStSb[i] = SA[i]'*Sb[i]
end
error = abserr + relerr * norm(b)
x = [zeros(d,1) for i=1:N]
xold = zeros(d,1)
lambda = [zeros(d,1) for i=1:N]
pieces = [zeros(d,1) for i=1:N,j=1:N]
for k=1:maxiter
xold = mean(x)
Threads.@threads for i=1:N
x[i] = invAtStSA[i]*(AtStSb[i]-lambda[i])
end
if norm(mean(x)-xold) <= error
break
end
meanx = mean(x)
Threads.@threads for i=1:N
for j=1:N
pieces[i,j] = AtStSA[j]*(x[i] - meanx)
end
lambda[i] += (mu/sqrt(k))*sum([pieces[i,j] for j=1:N])
end
end
if ret_lambda
return mean(x), lambda
end
return mean(x)
end
"""
Algorithm 2 from (Cand`es, Jiang, Pilanci, 2021). Utilizes RDMM to solve a
regularized least squares problem of the form 1/2*||Ax-b||_2^2 + eta/2*||x||_2^2
Inputs:
A - Data matrix
b - Data vector
eta - regularization constant
N - Number of agents
maxiter - number of RDMM iterations to perform
abserr - desired absolute difference in norm of mean(x) between iterations.
relerr - desired relative difference in norm of mean(x) between iterations. If the
difference in norms is less than the sum of absolute and relative error, the
function stops iterating and returns the current point.
mu - step size (best to just leave as is unless you really know to use something else)
rflag - determines whether to use randomized preprocessing. Without rflag=true,
this function utilizes standard distributed ADMM instead of RDMM
ret_lambda - boolean to determine if lambda is returned
Outputs:
A'*mean(y)/eta - optimal primal variable for the least squares problem
lambda - final dual coefficients for distributed problem
"""
function rdmm_ridge(A, b, eta, N; maxiter=100, abserr=0.001, relerr=0.01, mu=1, rflag=true, ret_lambda = false)
n = size(A,1)
d = size(A,2)
SAt = preprocess_ridge(A, N; rflag=rflag)
ASStAt = [zeros(n,n) for i=1:N]
invASStAt = [zeros(n,n) for i=1:N]
Threads.@threads for i=1:N
trueASStAt = SAt[i]'*SAt[i]
ASStAt[i] = trueASStAt + I(n)/(N^2)
invASStAt[i] = inv(trueASStAt/eta + I(n)/N)
end
error = abserr + relerr * norm(b)
y = [zeros(n,1) for i=1:N]
yold = zeros(n,1)
lambda = [zeros(n,1) for i=1:N]
pieces = [zeros(n,1) for i=1:N,j=1:N]
for k=1:maxiter
yold = mean(y)
Threads.@threads for i=1:N
y[i] = invASStAt[i]*(b/N-lambda[i])
end
if norm(mean(y)-yold) <= error
break
end
meany = mean(y)
Threads.@threads for i=1:N
for j=1:N
pieces[i,j] = ASStAt[j]*(y[i] - meany)
end
lambda[i] += (mu/sqrt(k))*sum([pieces[i,j] for j=1:N])
end
end
if ret_lambda
return A'*mean(y)/eta,lambda
end
return A'*mean(y)/eta
end
"""
Algorithm 3 from (Cand`es, Jiang, Pilanci, 2021). Utilizes RDMM to solve a
quadratic problem with an L-smooth regularizer of the form 1/2*||Ax-b||_2^2+g(x)
Note: This problem only supports two workers as it isn't guaranteed to converge with more
Inputs:
A - Data matrix
b - Data vector
g - L-smooth regularizer. Must be continuously differentiable, twice differentiable,
and the derivative must be Lipschitz continuous with Lipschitz constant L.
L - the Lipschitz constant for the derivative of g. Does not have to be tight.
maxiter - number of RDMM iterations to perform
abserr - desired absolute difference in norm of mean(x) between iterations.
relerr - desired relative difference in norm of mean(x) between iterations. If the
difference in norms is less than the sum of absolute and relative error, the
function stops iterating and returns the current point.
mu - step size (best to just leave as is unless you really know to use something else)
rflag - determines whether to use randomized preprocessing. Without rflag=true,
this function utilizes standard distributed ADMM instead of RDMM
ret_lambda - boolean to determine if lambda is returned
Outputs:
(x+y)/2 - optimal primal variable for the least squares problem
lambda - final dual coefficients for distributed problem
"""
function rdmm_qr(A, b, g, L; maxiter=100, abserr=0.001, relerr=0.01, mu=1, rflag=true, ret_lambda = false)
n = size(A,1)
d = size(A,2)
SA, Sb = preprocess_qr(A, b; rflag=true)
# Need to add this stuff after parallelization. This is needed for the "pieces" approach
#AtStSA = [zeros(d,d) for i=1:2]
#Threads.@threads for i=1:2
# AtStSA[i] = SA[i]'*SA[i]
#end
error = abserr + relerr * norm(b)
x = zeros(d,1)
y = zeros(d,1)
lambda = zeros(d,1)
for k=1:maxiter
xold = x
yold = y
# temporarily implemented in convex while we search for a better solution
xvar = Variable(d)
yvar = Variable(d)
probx = minimize(0.5*square(norm(SA[1]*xvar-Sb[1],2))+0.5*g(xvar)-lambda'*xvar)
proby = minimize(0.5*square(norm(SA[2]*yvar-Sb[2],2))+0.5*g(yvar)+lambda'*yvar)
solve!(probx, SCS.Optimizer(verbose=false))
solve!(proby, SCS.Optimizer(verbose=false))
x = evaluate(xvar)
y = evaluate(yvar)
if norm((x+y-xold-yold)/2) <= error
break
end
lambda += (mu/sqrt(k))*(A'*A+L*I(d))*(y-x)
end
if ret_lambda
return (x+y)/2, lambda
end
return (x+y)/2
end
"""
Algorithm 4 from (Cand`es, Jiang, Pilanci, 2021). Utilizes RDMM to minimize equation (15).
This was implemented mainly for testing and also completeness. It solves a sub-problem
required in iterations of a splitting cone solver for general cone problems. The
problem is of the form 1/2*||Az-(A^T)^(-1)wx-wy)||_2^2 + 1/2*||z||_2^2 where (A^T)^(-1)
is the pseudo inverse of A transpose.
Inputs:
A - Data matrix
wy - Data vector
wx - Data vector
N - Number of agents
maxiter - number of RDMM iterations to perform
abserr - desired absolute difference in norm of mean(x) between iterations.
relerr - desired relative difference in norm of mean(x) between iterations. If the
difference in norms is less than the sum of absolute and relative error, the
function stops iterating and returns the current point.
mu - step size (best to just leave as is unless you really know to use something else)
rflag - determines whether to use randomized preprocessing. Without rflag=true,
this function utilizes standard distributed ADMM instead of RDMM
ret_lambda - boolean to determine if lambda is returned
Outputs:
mean(z) - optimal primal variable for the least squares problem
lambda - final dual coefficients for distributed problem
"""
function rdmm_socp(A, wy, wx, N; maxiter=100, abserr=0.001, relerr=0.01, mu=1, rflag=true, ret_lambda = false)
n = size(A,1)
d = size(A,2)
Ahat = vcat(A, I(d))
SAhat = preprocess_socp(Ahat,rflag=rflag)
AhattStSAhat = [zeros(d,d) for i=1:N]
invAhattStSAhat = [zeros(d,d) for i=1:N]
Threads.@threads for i=1:N
AhattStSAhat[i] = SAhat[i]'*SAhat[i]
invAhattStSAhat[i] = inv(AhattStSAhat[i])
end
error = abserr + relerr * (norm(wx) + norm(wy))
z = [zeros(n,1) for i=1:N]
lambdavector = (A'*wy-wx)/N
lambda = [lambdavector for i=1:N]
pieces = [zeros(d,1) for i=1:N,j=1:N]
for k=1:maxiter
zold = z
Threads.@threads for i=1:N
z[i] = -invAhattStSAhat*lambda[i]
end
if norm(z-zold) <= error
break
end
meanz = mean(z)
Threads.@threads for i=1:N
for j=1:N
pieces[i,j] = AhattStSAhat*(z[i] - meanz)
end
lambda[i] += (mu/sqrt(k))*sum([pieces[i,j] for j=1:N])
end
end
if ret_lambda
return mean(z),lambda
end
return mean(z)
end
"""
Utility function to generate the random components needed for the delta-stable
decomposition of identity.
Inputs:
n - Number of dimensions
N - Number of agents
Outputs:
D - Matrix with iid random Rademacher entries, i.e., +/- 1 entries.
dividedindices - Partition of 1:n into N pieces roughly equal-sized pieces
"""
function generatePD(n, N; rflag=true)
dividedindices = rflag ? Iterators.partition(randperm(n), Int(ceil(n/N))) :
Iterators.partition(collect(1:n), Int(ceil(n/N)))
D = rflag ? Diagonal(sign.(rand(n) .- 0.5)) : I(n)
return dividedindices, D
end
"""
Utility function to preprocess A and b for RDMM LS
Inputs:
A - A matrix of size n by d
b - A column vector of size n
N - The number of agents. N should also satisfy N <= n/d.
Outputs:
SA - List of all S_i*A
Sb - List of all S_i*b.
"""
function preprocess_ls(A, b, N; rflag=true)
# TO DO: Check correctness
n = size(A, 1)
d = size(A, 2)
if N > n/d
throw(DimensionMismatch("N can be at most n/d"))
end
dividedindices, D = generatePD(n, N; rflag=rflag)
HDA = A
HDb = b
if rflag
HDA = FFTW.r2r(D*A, FFTW.DHT, 1)
HDb = FFTW.r2r(D*b, FFTW.DHT)
end
SA = []
Sb = []
normfactor = rflag ? sqrt(n) : 1
for indexcollection in dividedindices
push!(SA, HDA[indexcollection, :] / normfactor)
push!(Sb, HDb[indexcollection, :] / normfactor)
end
return SA, Sb
end
"""
Utility function to preprocess A for RDMM Ridge
Inputs:
A - A matrix of size n by d.
N - The number of agents. N should divide d (the number of columns in A).
Outputs:
SAt - List of all S_i*At.
"""
function preprocess_ridge(A, N; rflag=true)
# TO DO: Check correctness
n = size(A, 1)
d = size(A, 2)
dividedindices, D = generatePD(d, N; rflag=rflag)
HDAt = A'
if rflag
HDAt = FFTW.r2r(D*A', FFTW.DHT, 1)
end
SAt = []
normfactor = rflag ? sqrt(d) : 1
for indexcollection in dividedindices
push!(SAt, HDAt[indexcollection, :] / normfactor)
end
return SAt
end
"""
Utility function to preprocess A and b for RDMM Quadratic w/ Regularizer
Inputs:
A - A matrix of size n by d
b - A column vector of size n
Outputs:
SA - List of all S_i*A
Sb - List of all S_i*b.
"""
function preprocess_qr(A, b; rflag=true)
# TO DO: Check to make sure we don't need n >= 2*d
n = size(A, 1)
d = size(A, 2)
dividedindices, D = generatePD(n, 2; rflag=rflag)
HDA = A
HDb = b
if rflag
HDA = FFTW.r2r(D*A, FFTW.DHT, 1)
HDb = FFTW.r2r(D*b, FFTW.DHT)
end
SA = []
Sb = []
normfactor = rflag ? sqrt(n) : 1
for indexcollection in dividedindices
push!(SA, HDA[indexcollection, :] / normfactor)
push!(Sb, HDb[indexcollection, :] / normfactor)
end
return SA, Sb
end
"""
Utility function to preprocess A and b for RDMM Regularized LS
Inputs:
Ahat - A matrix of size n by (d+n), where the right-most n by n block is the
identity matrix.
Outputs:
SAhat - List of all S_i*Ahat.
"""
function preprocess_socp(Ahat; rflag=true)
# TO DO: Check to make sure we don't need n >= 2*d
nplusd = size(Ahat, 1)
d = size(Ahat, 2)
dividedindices, D = generatePD(nplusd, N; rflag=rflag)
HDAhat = Ahat
if rflag
HDAhat = FFTW.r2r(D*Ahat, FFTW.DHT, 1)
end
SAhat = []
normfactor = rflag ? sqrt(nplusd) : 1
for indexcollection in dividedindices
push!(SAhat, HDAhat[indexcollection, :] / normfactor)
end
return SAhat
end