-
Notifications
You must be signed in to change notification settings - Fork 4
/
projections.jl
266 lines (222 loc) · 8.76 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
# 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
"""
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, X21, ..., Xk1, X22, X32, ..., Xkk)
"""
function unvec_symm(x, dim)
X = zeros(eltype(x), dim, dim)
idx = 1
for i in 1:dim
for j in 1:i
# @inbounds X[j,i] = X[i,j] = x[(i-1)*dim-div((i-1)*i, 2)+j]
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, X21, ..., Xk1, X22, X32, ..., Xkk)`
"""
function vec_symm(X)
return X[LinearAlgebra.tril(trues(size(X)))']
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 LinearAlgebra.Diagonal(ones(length(v)))
end
"""
projection_gradient_on_set(::DefaultDistance, v::T, ::MOI.EqualTo)
"""
function projection_gradient_on_set(::DefaultDistance, ::T, ::MOI.EqualTo) where {T}
y = zeros(T, 1)
return reshape(y, length(y), 1)
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)
else
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
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] = one(Tp)
# 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] = zero(Tp)
end
return D
end
"""
projection_gradient_on_set(::DefaultDistance, v::AbstractVector{T}, sets::Array{<: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::Array{<: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