-
Notifications
You must be signed in to change notification settings - Fork 4
/
projections.jl
597 lines (504 loc) · 19.1 KB
/
projections.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# find expression of projections on cones and their derivatives here:
# https://stanford.edu/~boyd/papers/pdf/cone_prog_refine.pdf
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Zeros) where {T}
projection of vector `v` on zero cone i.e. K = {0}
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Zeros) where {T}
return FillArrays.Zeros{T}(size(v))
end
"""
projection_on_set(::AbstractDistance, ::MOI.Reals, v::Array{T}) where {T}
projection of vector `v` on real cone i.e. K = R
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Reals) where {T}
return v
end
function projection_on_set(::DefaultDistance, v::T, set::MOI.EqualTo) where {T}
return zero(T) + set.value
end
function projection_on_set(::DefaultDistance, v::T, set::MOI.LessThan) where {T}
return min(v, MOI.constant(set))
end
function projection_on_set(::DefaultDistance, v::T, set::MOI.GreaterThan) where {T}
return max(v, MOI.constant(set))
end
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonnegatives) where {T}
projection of vector `v` on Nonnegative cone i.e. K = R^n+
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonnegatives) where {T}
return max.(v, zero(T))
end
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonpositives) where {T}
projection of vector `v` on Nonpositive cone i.e. K = R^n-
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonpositives) where {T}
return min.(v, zero(T))
end
"""
projection_on_set(::NormedEpigraphDistance{p}, v::AbstractVector{T}, ::MOI.SecondOrderCone) where {T}
projection of vector `v` on second order cone i.e. K = {(t, x) ∈ R+ × Rn | ||x|| ≤ t }
"""
function projection_on_set(::NormedEpigraphDistance{p}, v::AbstractVector{T}, ::MOI.SecondOrderCone) where {p, T}
t = v[1]
x = v[2:length(v)]
norm_x = LinearAlgebra.norm(x, p)
if norm_x <= t
return copy(v)
elseif norm_x <= -t
return zeros(T, size(v))
end
result = zeros(T, size(v))
result[1] = one(T)
result[2:length(v)] = x / norm_x
result *= (norm_x + t) / 2
return result
end
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, cone::MOI.SecondOrderCone) where {T}
return projection_on_set(NormedEpigraphDistance{2}(), v, cone)
end
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.PositiveSemidefiniteConeTriangle) where {T}
projection of vector `v` on positive semidefinite cone i.e. K = S^n⨥
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.PositiveSemidefiniteConeTriangle) where {T}
dim = isqrt(2*length(v))
X = unvec_symm(v, dim)
λ, U = LinearAlgebra.eigen(X)
D = LinearAlgebra.Diagonal(max.(λ, 0))
return vec_symm(U * D * U')
end
"""
unvec_symm(x, dim)
Returns a dim-by-dim symmetric matrix corresponding to `x`.
`x` is a vector of length dim*(dim + 1)/2, corresponding to a symmetric matrix
```
X = [ X11 X12 ... X1k
X21 X22 ... X2k
...
Xk1 Xk2 ... Xkk ],
```
where `vec(X) = (X11, X12, X22, X13, X23, X33, ..., Xkk)`.
### Note on inner products
Note that the scalar product for the symmetric matrix in its vectorized form is
the sum of the pairwise product of the diagonal entries plus twice the sum of
the pairwise product of the upper diagonal entries; see [p. 634, 1].
Therefore, this transformation breaks inner products:
```
dot(unvec_symm(x, dim), unvec_symm(y, dim)) != dot(x, y).
```
### References
[1] Boyd, S. and Vandenberghe, L.. *Convex optimization*. Cambridge university press, 2004.
"""
function unvec_symm(x, dim=isqrt(2length(x)))
X = zeros(eltype(x), dim, dim)
idx = 1
for i in 1:dim
for j in 1:i
X[j,i] = X[i,j] = x[idx]
idx += 1
end
end
return X
end
"""
vec_symm(X)
Returns a vectorized representation of a symmetric matrix `X`.
`vec(X) = (X11, X12, X22, X13, X23, X33, ..., Xkk)`
### Note on inner products
Note that the scalar product for the symmetric matrix in its vectorized form is
the sum of the pairwise product of the diagonal entries plus twice the sum of
the pairwise product of the upper diagonal entries; see [p. 634, 1].
Therefore, this transformation breaks inner products:
```
dot(vec_symm(X), vec_symm(Y)) != dot(X, Y).
```
### References
[1] Boyd, S. and Vandenberghe, L.. *Convex optimization*. Cambridge university press, 2004.
"""
function vec_symm(X)
return X[LinearAlgebra.triu(trues(size(X)))]
end
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.ExponentialCone) where {T}
projection of vector `v` on closure of the exponential cone
i.e. `cl(Kexp) = {(x,y,z) | y e^(x/y) <= z, y>0 } U {(x,y,z)| x <= 0, y = 0, z >= 0}`.
References:
* [Proximal Algorithms, 6.3.4](https://web.stanford.edu/~boyd/papers/pdf/prox_algs.pdf)
by Neal Parikh and Stephen Boyd.
* [Projection, presolve in MOSEK: exponential, and power cones]
(https://docs.mosek.com/slides/2018/ismp2018/ismp-friberg.pdf) by Henrik Friberg
* [Projection onto the exponential cone: a univariate root-finding problem]
(https://docs.mosek.com/whitepapers/expcone-proj.pdf) by Henrik Friberg
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, s::MOI.ExponentialCone; tol=1e-8) where {T}
_check_dimension(v, s)
if _in_exp_cone(v; dual=false)
return SVector{3}(v)
end
if _in_exp_cone(-v; dual=true)
# if in polar cone Ko = -K*
return zeros(SVector{3,T})
end
if v[1] <= 0 && v[2] <= 0
return @SVector([v[1], 0, max(v[3],0)])
end
return _exp_cone_proj_case_4(v; tol=tol)
end
function _in_exp_cone(v::AbstractVector{T}; dual=false, tol=1e-8) where {T}
if dual
return (
(isapprox(v[1], 0, atol=tol) && v[2] >= 0 && v[3] >= 0) ||
(v[1] < 0 && v[1]*exp(v[2]/v[1]) + ℯ * v[3] >= tol)
)
else
return (
(v[1] <= 0 && isapprox(v[2], 0, atol=tol) && v[3] >= 0) ||
(v[2] > 0 && v[2] * exp(v[1] / v[2]) - v[3] <= tol)
)
end
end
function _exp_cone_proj_case_4(v::AbstractVector{T}; tol=1e-8) where {T}
# Try Heuristic solutions [Friberg 2021, Lemma 5.1]
# vp = proj onto primal cone, vd = proj onto polar cone
vp = SVector{3,T}(min(v[1], 0), zero(T), max(v[3], 0))
vd = SVector{3,T}(zero(T), min(v[2], 0), min(v[3], 0))
if v[2] > 0
zp = max(v[3], v[2]*exp(v[1]/v[2]))
if zp - v[3] < norm(vp - v)
vp = SVector{3,T}(v[1], v[2], zp)
end
end
if v[1] > 0
zd = min(v[3], -v[1]*exp(v[2]/v[1] - 1))
if v[3] - zd < norm(vd - v)
vd = SVector{3,T}(v[1], v[2], zd)
end
end
# Check if heuristics above approximately satisfy the optimality conditions
opt_norm = norm(vp + vd - v)
opt_ortho = abs(dot(vp, vd))
if norm(v - vp) < tol || norm(v - vd) < tol || (opt_norm < tol && opt_ortho < tol)
return vp
end
# Failure of heuristics -> non heuristic solution
# Ref: https://docs.mosek.com/slides/2018/ismp2018/ismp-friberg.pdf, p47-48
# Thm: h(x) is smooth, strictly increasing, and changes sign on domain
r, s, t = v[1], v[2], v[3]
h(x) = (((x-1)*r + s) * exp(x) - (r - x*s)*exp(-x))/(x^2 - x + 1) - t
# Note: won't both be Inf by case 3 of projection
lb = r > 0 ? 1 - s/r : -Inf
ub = s > 0 ? r/s : Inf
# Deal with ±Inf bounds
if isinf(lb)
lb = min(ub-0.125, -0.125)
for _ in 1:10
h(lb) < 0 && break
ub = lb
lb *= 2
end
end
if isinf(ub)
ub = max(lb+0.125, 0.125)
for _ in 1:10
h(ub) > 0 && break
lb = ub
ub *= 2
end
end
# Check bounds
if !(h(lb) < 0 && h(ub) > 0)
error("Failure to find bracketing interval for exp cone projection.")
end
x = _bisection(h, lb, ub)
if x === nothing
error("Failure in root-finding for exp cone projection with boundaries ($lb, $ub).")
end
return ((x - 1) * r + s)/(x^2 - x + 1) * SVector{3,T}(x, 1, exp(x))
end
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.DualExponentialCone) where {T}
projection of vector `v` on the dual exponential cone
i.e. `Kexp^* = {(u,v,w) | u < 0, -u*exp(v/u) <= ew } U {(u,v,w)| u == 0, v >= 0, w >= 0}`.
References:
* [Proximal Algorithms, 6.3.4](https://web.stanford.edu/~boyd/papers/pdf/prox_algs.pdf)
by Neal Parikh and Stephen Boyd.
* [Projection, presolve in MOSEK: exponential, and power cones](https://docs.mosek.com/slides/2018/ismp2018/ismp-friberg.pdf)
by Henrik Friberg
"""
function projection_on_set(d::DefaultDistance, v::AbstractVector{T}, ::MOI.DualExponentialCone) where {T}
p = projection_on_set(d, -v, MOI.ExponentialCone())
return SVector{3,T}(v[1] + p[1], v[2] + p[2], v[3] + p[3])
end
"""
projection_on_set(::DefaultDistance, v::AbstractVector{T}, sets::Array{<:MOI.AbstractSet})
Projection onto `sets`, a product of sets
"""
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, sets::Array{<:MOI.AbstractSet}) where {T}
length(v) == length(sets) || throw(DimensionMismatch("Mismatch between value and set"))
return reduce(vcat, (projection_on_set(DefaultDistance(), v[i], sets[i]) for i in eachindex(sets)))
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Zeros) where {T}
derivative of projection of vector `v` on zero cone i.e. K = {0}^n
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Zeros) where {T}
return FillArrays.Zeros(length(v), length(v))
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Reals) where {T}
derivative of projection of vector `v` on real cone i.e. K = R^n
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Reals) where {T}
return FillArrays.Eye(length(v))
end
"""
projection_gradient_on_set(::DefaultDistance, v::T, ::MOI.EqualTo)
"""
function projection_gradient_on_set(::DefaultDistance, ::T, ::MOI.EqualTo) where {T}
return zero(T)
end
function projection_gradient_on_set(::DefaultDistance, v::T, s::MOI.LessThan) where {T}
return oneunit(T) * (v <= MOI.constant(s))
end
function projection_gradient_on_set(::DefaultDistance, v::T, s::MOI.GreaterThan) where {T}
return oneunit(T) * (v >= MOI.constant(s))
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonnegatives) where {T}
derivative of projection of vector `v` on Nonnegative cone i.e. K = R^n+
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonnegatives) where {T}
y = (sign.(v) .+ one(T))/2
return LinearAlgebra.Diagonal(y)
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonpositives) where {T}
derivative of projection of vector `v` on Nonpositives cone i.e. K = R^n-
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.Nonpositives) where {T}
y = @. (-sign(v) + one(T))/2
return LinearAlgebra.Diagonal(y)
end
"""
projection_gradient_on_set(::NormedEpigraphDistance{p}, v::AbstractVector{T}, ::MOI.SecondOrderCone) where {T}
derivative of projection of vector `v` on second order cone i.e. K = {(t, x) ∈ R+ × Rn | ||x|| ≤ t }
"""
function projection_gradient_on_set(::NormedEpigraphDistance{p}, v::AbstractVector{T}, ::MOI.SecondOrderCone) where {p,T}
n = length(v)
t = v[1]
x = v[2:n]
norm_x = LinearAlgebra.norm(x, p)
if norm_x <= t
return Matrix{T}(LinearAlgebra.I,n,n)
elseif norm_x <= -t
return zeros(T, n, n)
end
result = [
norm_x x';
x (norm_x + t)*Matrix{T}(LinearAlgebra.I,n-1,n-1) - (t/(norm_x^2))*(x*x')
]
result ./= (2 * norm_x)
return result
end
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, cone::MOI.SecondOrderCone) where {T}
return projection_gradient_on_set(NormedEpigraphDistance{2}(), v, cone)
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, cone::MOI.PositiveSemidefiniteConeTriangle) where {T}
derivative of projection of vector `v` on positive semidefinite cone i.e. K = S^n⨥
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.PositiveSemidefiniteConeTriangle) where {T}
n = length(v)
dim = isqrt(2n)
X = unvec_symm(v, dim)
λ, U = LinearAlgebra.eigen(X)
Tp = promote_type(T, Float64)
# if all the eigenvalues are >= 0
if all(λi ≥ zero(λi) for λi in λ)
return Matrix{Tp}(LinearAlgebra.I, n, n)
end
# k is the number of negative eigenvalues in X minus ONE
k = count(λi < 1e-4 for λi in λ)
y = zeros(Tp, n)
D = zeros(Tp, n, n)
for idx in 1:n
# set eigenvector
y[idx] = 1
# defining matrix B
X̃ = unvec_symm(y, dim)
B = U' * X̃ * U
for i in 1:size(B)[1] # do the hadamard product
for j in 1:size(B)[2]
if (i <= k && j <= k)
@inbounds B[i, j] = 0
elseif (i > k && j <= k)
λpi = max(λ[i], zero(Tp))
λmj = -min(λ[j], zero(Tp))
@inbounds B[i, j] *= λpi / (λmj + λpi)
elseif (i <= k && j > k)
λmi = -min(λ[i], zero(Tp))
λpj = max(λ[j], zero(Tp))
@inbounds B[i, j] *= λpj / (λmi + λpj)
end
end
end
@inbounds D[idx, :] = vec_symm(U * B * U')
# reset eigenvector
@inbounds y[idx] = 0
end
return D
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.ExponentialCone) where {T}
derivative of projection of vector `v` on closure of the exponential cone,
i.e. `cl(Kexp) = {(x,y,z) | y e^(x/y) <= z, y>0 } U {(x,y,z)| x <= 0, y = 0, z >= 0}`.
References:
* [Solution Refinement at Regular Points of Conic Problems](https://stanford.edu/~boyd/papers/cone_prog_refine.html)
by Enzo Busseti, Walaa M. Moursi, and Stephen Boyd
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, s::MOI.ExponentialCone) where {T}
_check_dimension(v, s)
if _in_exp_cone(v; dual=false)
return SMatrix{3,3,T}(I)
end
if _in_exp_cone(-v; dual=true)
# if in polar cone Ko = -K*
return zeros(SMatrix{3,3,T})
end
if v[1] <= 0 && v[2] <= 0
return @SMatrix(T[
1 0 0
0 0 0
0 0 (v[3] >= 0)
])
end
z1, z2, z3 = _exp_cone_proj_case_4(v)
nu = z3 - v[3]
rs = z1/z2
exp_rs = exp(rs)
mat = inv(@SMatrix([
1+nu*exp_rs/z2 -nu*exp_rs*rs/z2 0 exp_rs;
-nu*exp_rs*rs/z2 1+nu*exp_rs*rs^2/z2 0 (1-rs)*exp_rs;
0 0 1 -1
exp_rs (1-rs)*exp_rs -1 0
]))
return SMatrix{3,3}(@view(mat[1:3,1:3]))
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.DualExponentialCone) where {T}
derivative of projection of vector `v` on the dual exponential cone,
i.e. `Kexp^* = {(u,v,w) | u < 0, -u*exp(v/u) <= ew } U {(u,v,w)| u == 0, v >= 0, w >= 0}`.
References:
* [Solution Refinement at Regular Points of Conic Problems]
(https://stanford.edu/~boyd/papers/cone_prog_refine.html)
by Enzo Busseti, Walaa M. Moursi, and Stephen Boyd
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, ::MOI.DualExponentialCone) where {T}
# from Moreau decomposition: x = P_K(x) + P_-K*(x)
return I - projection_gradient_on_set(DefaultDistance(), -v, MOI.ExponentialCone())
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, sets::AbstractVector{<:MOI.AbstractSet})
Derivative of the projection of vector `v` on product of `sets`
projection_gradient_on_set[i,j] = ∂projection_on_set[i] / ∂v[j] where `projection_on_set` denotes projection of `v` on `cone`
Find expression of projections on cones and their derivatives here: https://stanford.edu/~boyd/papers/pdf/cone_prog_refine.pdf
"""
function projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, sets::AbstractVector{<:MOI.AbstractSet}) where {T}
length(v) == length(sets) || throw(DimensionMismatch("Mismatch between value and set"))
return BlockDiagonal([projection_gradient_on_set(DefaultDistance(), v[i], sets[i]) for i in eachindex(sets)])
end
"""
projection_on_set(::DefaultDistance, V::AbstractVector{T}, s::NormBallNuclear{T}) where {T}
projection of matrix `V` onto nuclear norm ball
"""
function projection_on_set(d::DefaultDistance, V::AbstractMatrix{T}, s::NormNuclearBall{T}) where {T}
U, sing_val, Vt = LinearAlgebra.svd(V)
if (sum(sing_val) <= s.radius)
return V
end
sing_val_proj = projection_on_set(d, sing_val, ProbabilitySimplex(length(sing_val), s.radius))
return U * Diagonal(sing_val_proj) * Vt'
end
# initial implementation in FrankWolfe.jl
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, s::ProbabilitySimplex{T}) where {T}
_check_dimension(v, s)
# TODO: allocating a ton, should implement the recent non-sorting alg
n = length(v)
if sum(v) ≈ s.radius && all(>=(0), v)
return v
end
rev = v .- maximum(v)
u = sort(rev, rev=true)
cssv = cumsum(u)
rho = sum(eachindex(u)) do idx
u[idx] * idx > (cssv[idx] - s.radius)
end - 1
theta = (cssv[rho+1] - s.radius) / (rho + 1)
w = clamp.(rev .- theta, 0.0, Inf)
return w
end
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, s::StandardSimplex{T}) where {T}
_check_dimension(v, s)
n = length(v)
if sum(v) ≤ s.radius && all(>=(0), v)
return v
end
x = copy(v)
sum_pos = zero(T)
for idx in eachindex(x)
if x[idx] < 0
x[idx] = 0
else
sum_pos += x[idx]
end
end
# at least one positive element
if sum_pos > 0
@. x = x / sum_pos * s.radius
end
return x
end
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, s::NormInfinityBall{T}) where {T}
if norm(v, Inf) <= s.radius
return v
end
return clamp.(v, -s.radius, s.radius)
end
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, s::NormTwoBall{T}) where {T}
nv = norm(v)
if nv <= s.radius
return v
end
return v .* s.radius ./ nv
end
# inspired by https://github.com/MPF-Optimization-Laboratory/ProjSplx.jl
function projection_on_set(::DefaultDistance, v::AbstractVector{T}, s::NormOneBall{T}) where {T}
n = length(v)
if norm(v, 1) ≤ τ
return v
end
u = abs.(v)
# simplex projection
bget = false
s_indices = sortperm(u, rev=true)
tsum = zero(τ)
@inbounds for i in 1:n-1
tsum += u[s_indices[i]]
tmax = (tsum - τ) / i
if tmax ≥ u[s_indices[i+1]]
bget = true
break
end
end
if !bget
tmax = (tsum + u[s_indices[n]] - τ) / n
end
@inbounds for i in 1:n
u[i] = max(u[i] - tmax, 0)
u[i] *= sign(v[i])
end
return u
end