-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApprox_Proj.jl
331 lines (296 loc) · 9.65 KB
/
Approx_Proj.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
using LinearAlgebra, Statistics
function probsimplexproj(w, v)
###
# A function for projection onto probability simplex in the centered framework.
# Function projects the point and assigns the output value to input for performance
# enhancement.
#
# v: input point to be projected
# w: output vector, the same size as v
#
# Returns:
# nothing
###
rho = sort(v, rev = true);
d = length(v);
u = Vector{Float64}(undef, d);
sum = 0;
for i = 1:d
sum += rho[i];
u[i] = (1/i) * (sum - 1);
end
i_star = 1;
while( i_star <= d )
if rho[i_star] > u[i_star]
i_star += 1;
else
break
end
end
i_star -= 1;
for i = 1:d
w[i] = max( v[i] - u[i_star] - 1/2, -1/2 );
end
return nothing
end
function simplexhyperproj(w, v)
###
# A simplified function for projection onto probability simplex in the centered framework.
# Function projects the point onto the supporting hyperplane of the probability simplex
# and assigns the output value to input for performance enhancement.
#
# v: input point to be projected
# w: output vector, the same size as v
#
# Returns:
# nothing
###
d = length(v);
# for i = 1:d
# v[i] = max( v[i], -1/2 );
# end
v_shift = mean(v) - 1/d;
for i = 1:d
w[i] = v[i] - v_shift - 1/2;
end
return nothing
end
function EXACT(v)
###
# This function projects a point onto the parity polytope EXACTLY.
# The input vector should be given in the non-centered framework. Output is given in the non-centered framework
# as well. However, the computations inside the function takes place in the centered
# framework, as it is practically more efficient. The function updates the input vector
# as the output vector to enhance performance of the code.
#
# v: input point to be projected
#
# Returns:
# the projected point
###
d = length(v);
v -= ones(d) * (1/2); #shifting to fit in the centered projection framework
f = zeros(d);
for i = 1:d
if v[i] >= 0
f[i] = 1; #facet identifictation
end
end
if sum(f)%2 == 0
i_star = argmin( abs.(v) );
f[i_star] = 1 - f[i_star];
end
v_tilde = similar(v);
for i = 1:d
v_tilde[i] = v[i] * (-1)^f[i]; #similarity transform
end
u_tilde = similar(v);
probsimplexproj(u_tilde, v_tilde); #simplex projection
v_tildeclipped = similar(v);
for i = 1:d
v_tildeclipped[i] = min( max( v_tilde[i], -1/2 ), 1/2 );
end
w = similar(v);
if sum(v_tildeclipped) >= 1 - d/2 #membership test
for i = 1:d
w[i] = min( max( v[i], -1/2 ), 1/2 );
end
else
for i = 1:d
w[i] = u_tilde[i] * (-1)^f[i]; #similarity transform
end
end
#return nothing
return w + ones(d) * (1/2) #invert shifting to fit in the uncentered projection framework
end
function APA(v)
###
# This function is an approximation of the parity polytope projection, known as Affine Projeciton Algorithm (APA).
# It approximates the projection by projecting onto the supporing hyperplane of all d
# even-parity vertices which neighbor the closest odd-parity hypercube vertex to the point to be projected.
# The input vector should be given in the non-centered framework. Output is given in the non-centered framework
# as well. However, the computations inside the function takes place in the centered
# framework, as it is practically more efficient. The function updates the input vector
# as the output vector to enhance performance of the code.
#
# v: input point to be projected
#
# Returns:
# the projected point
###
d = length(v);
v -= ones(d) * (1/2); #shifting to fit in the centered projection framework
f = zeros(d);
for i = 1:d
if v[i] >= 0
f[i] = 1; #facet identifictation
end
end
if sum(f)%2 == 0
i_star = argmin( abs.(v) );
f[i_star] = 1 - f[i_star];
end
v_tilde = similar(v);
for i = 1:d
v_tilde[i] = v[i] * (-1)^f[i]; #similarity transform
end
u_tilde = similar(v);
simplexhyperproj(u_tilde, v_tilde); #simple simplex hyperplane projeciton (without sorting needed)
v_tildeclipped = similar(v);
for i = 1:d
v_tildeclipped[i] = min( max( v_tilde[i], -1/2 ), 1/2 );
end
w = similar(v);
if sum(v_tildeclipped) >= 1 - d/2 #membership test
for i = 1:d
w[i] = min( max( v[i], -1/2 ), 1/2 );
end
else
for i = 1:d
w[i] = u_tilde[i] * (-1)^f[i]; #similarity transform
end
end
#return nothing
return w + ones(d) * (1/2) #invert shifting to fit in the uncentered projection framework
end
function EVA(v)
###
# This function is an approximation of the parity polytope projection, namely as Even-Vertex-Algorithm (EVA).
# It approximates the projection by projecting onto the closest even-parity vertex of the polytope to the point to be projected.
# The input vector should be given in the non-centered framework. Output is given in the non-centered framework
# as well. However, the computations inside the function takes place in the centered
# framework, as it is practically more efficient.
#
# v: input point to be projected
#
# Returns:
# f: the projected point
###
d = length(v);
v -= ones(d) * (1/2);
f = zeros(d);
for i = 1:d
if v[i] >= 0;
f[i] = 1;
end
end
if sum(f)%2 == 1
i_star = argmin( abs.(v) );
f[i_star] = 1 - f[i_star];
end
return f
end
function LSA(v)
###
# This function is an approximation of the parity polytope projection, namely as Line-Segment-Algorithm (LSA).
# It approximates the projection by projecting onto the line-segment connecting the two closest even-parity vertices neighboring
# the nearest odd-parity hypercube vertex to the point to be projected.
# The input vector should be given in the non-centered framework. Output is given in the non-centered framework
# as well. However, the computations inside the function takes place in the centered
# framework, as it is practically more efficient.
#
# v: input point to be projected
#
# Returns:
# z: the projected point
###
d = length(v);
u = similar(v);
theta = -ones(d);
f = zeros(d);
for i = 1:d
u[i] = min( max(v[i], 0.0), 1.0);
if v[i] >= 0.5
f[i] = 1
theta[i] = 1;
end
end
if sum(f)%2 == 0
i_star = argmin( abs.(v.-0.5) );
theta[i_star] *= -1;
f[i_star] = 1 - f[i_star];
end
s = sum(f);
if sum(theta.*u) <= s-1
z = u;
else #Line-Segment Projection
sort_idx = sortperm(abs.(v.-0.5));
p = sort_idx[1];
q = sort_idx[2];
A = copy(f);
B = copy(f);
A[p] = 1 - f[p];
B[q] = 1 - f[q];
AB = zeros(d);
AB[p] = B[p] - A[p];
AB[q] = B[q] - A[q];
t = 0.5 * ( (B[p]-A[p])*(v[p]-A[p]) + (B[q]-A[q])*(v[q]-A[q]) );
t = min( max(t, 0.0), 1.0);
z = A .+ t*AB;
end
return z
end
# Limited-Hyperplane Projection Algorithm (χ-HYPPA)
function χ_SAPA(v, χ)
###
# This function is an approximation of the parity polytope projection, namely as χ-Sparse Affine Projection Algorithm (χ-SAPA).
# It approximates the projection by projecting onto the supporting hyperplance of the χ closest even-parity vertices neighboring
# the nearest odd-parity hypercube vertex to the point to be projected.
# The input vector should be given in the non-centered framework. Output is given in the non-centered framework
# as well. However, the computations inside the function takes place in the centered
# framework, as it is practically more efficient. The function updates the input vector
# as the output vector to enhance performance of the code.
#
# v: input point to be projected
#
# Returns:
# w_non: the projected point
###
d = length(v)
v -= ones(d) * (1/2) #shifting to fit in the centered projection framework
f = zeros(d);
for i = 1:d
if v[i] >= 0
f[i] = 1; #facet identifictation
end
end
if sum(f)%2 == 0
i_star = argmin( abs.(v) );
f[i_star] = 1 - f[i_star];
end
I_σ = sortperm( abs.( v ) )
I_χ = I_σ[1:χ] # choosing the indices of χ closest even-parity vertices
ṽ = similar(v)
for i = 1:d
ṽ[i] = v[i] * (-1)^f[i]; #similarity transform
end
ũ = similar(v)
v_par_sum = 0
for i in I_χ
v_par_sum += ṽ[i]
end
v_shift = ( v_par_sum - 1 ) / χ
for i = 1:d
if i ∈ I_χ
ũ[i] = ṽ[i] - v_shift - 1/2
else
ũ[i] = - 1/2
end
end
ṽ_clipped = similar(v);
for i = 1:d
ṽ_clipped[i] = min( max( ṽ[i], -1/2 ), 1/2 );
end
w = similar(v);
if sum(ṽ_clipped) >= 1 - d/2 #membership test
for i = 1:d
w[i] = min( max( v[i], -1/2 ), 1/2 );
end
else
for i = 1:d
w[i] = ũ[i] * (-1)^f[i]; #similarity transform
end
end
w_non = w + ones(d).*0.5
return w_non
end