-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangulation.py
345 lines (249 loc) · 10.3 KB
/
triangulation.py
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
import numpy as np
import cvxopt
from cvxopt import matrix, spdiag, blas, lapack, solvers, sqrt
from scipy.optimize import linprog
cvxopt.solvers.options['show_progress'] = False
cvxopt.solvers.options["reltol"] = 1e-8
cvxopt.solvers.options["abstol"] = np.finfo(float).eps*2
cvxopt.solvers.options["feastol"] = 1e-8
cvxopt.solvers.options["maxiters"] = 100
def exactLP(X, p, tol=None):
"""
Solves the exact problem (E) using an interior point solver
:param X: np array of shape (n, d), with data as rows
:param p: np array of shape (1, d), the point to be represented
:param tol: The threshold to determine which values should be considered zero, if None uses the largest log gap
:return support: an array of the indices in X containing the support of weights
:return solved_weights: the weights to solve (E)
"""
# LP setup
n, d = X.shape
# costs
c = cvxopt.matrix(np.power(np.linalg.norm(X - p, axis=1), 2))
A_ = cvxopt.matrix(np.vstack([X.T, np.ones((1, n))]))
b = np.ones(d+1)
b[:d] = p[0]
b_ = cvxopt.matrix(b)
G_ = cvxopt.spmatrix(-1, range(n), range(n))
h_ = cvxopt.matrix(np.zeros(n))
initvals = {'x': cvxopt.matrix(np.ones((n, 1)) / n)}
sol = cvxopt.solvers.lp(c, G_, h_, A_, b_, initvals=initvals)
solved_weights = np.array(sol['x'])
idxes = np.array(list(range(n)))
# use weight on smaller end of the largest log-gap
if tol is None:
sorted_weights = np.sort(solved_weights.reshape(-1))
# handle situations where small entries may be slightly negative
sorted_weights[sorted_weights < 0.0] = sorted_weights[sorted_weights > 0.0].min()
gap = np.log(sorted_weights[1:]) - np.log(sorted_weights[:-1])
max_gap = gap.argmax()
# check that there is a significant gap, otherwise threshold at 0 (all values are away from 0)
median_gap = np.median(gap)
MAD = np.median(np.abs(gap - median_gap))
if gap.max() < median_gap + 3 * 1.4826* MAD:
tol = 0.0
else:
tol = sorted_weights[max_gap]
support = idxes[(solved_weights > tol).reshape(-1)]
return support, solved_weights
def cvxLP(X, p, tol=None, return_objective=False):
"""
solves the Convex Hull linear program described here: https://www.cs.mcgill.ca/~fukuda/download/paper/polyfaq.pdf (pg 23)
that finds the simplex a point p belongs to by identifying the vertices in X
:param X: np array of shape (n, d), with data as rows
:param tol: the threshold to consider what values should be considered zero, if None uses the largest log gap
:param p: np array of shape (1, d), the point to be represented
:param return_objective: whether or not to return the objective value and dual variables
:return support: the indices of X corresponding to the vertices of the d-simplex containing p
"""
p = p.reshape(-1)
n, d = X.shape
G = matrix(1.0, (n, d+1))
G[:, :d] = X
c = matrix(1.0, (d+1, 1))
c[:d] = p
fX = matrix(0.0, (n,1))
for i in range(n):
fX[i] = X[i] @ X[i].T
sol = solvers.lp(-c, G, fX)
z = np.array(sol['z']).reshape(-1)
# use weight on smaller end of the largest log-gap
if tol is None:
sorted_dual = np.sort(z)
zero_idx = sorted_dual == 0
sorted_dual[sorted_dual == 0] = sorted_dual[np.logical_not(zero_idx)].min()
gap = np.log(sorted_dual[1:]) - np.log(sorted_dual[:-1])
max_gap = gap.argmax()
# if max is within 3 MAD of median, consider all weights to be non-zero
median_gap = np.median(gap)
MAD = np.median(np.abs(gap - median_gap))
if gap.max() < median_gap + 3 * 1.4826* MAD:
tol = 0.0
else:
tol = sorted_dual[max_gap]
# tol = min(1e-10, tol)
idxs = np.arange(n)
support = idxs[z > tol]
if return_objective:
objective = sol['primal objective']
return support, objective, np.array(sol['x'])
else:
return support
def customCVX(A, p, rho, tol=None, verbose=False):
"""
Solves (R) using an interior point solver with a custom KKT solver as described in the appendix of the paper
:param X: np array of shape (n, d), with data as rows
:param p: np array of shape (1, d), the point to be represented
:param rho: the regularization parameter
:param tol: the threshold to consider what values should be considered zero, if None uses the largest log gap
:param verbose: whether or not to pass verbose to cvxopt
:return support: an array of the indices in X containing the support of weights
:return solved_weights: the weights to solve (R)
"""
# adjust rho for the standard form QP
rho /= 2
# for comparison with other method
c = np.power(np.linalg.norm(p - A, axis=1), 2).reshape(-1, 1)
q = cvxopt.matrix(rho * c - A @ p.T)
X = A
A = cvxopt.matrix(A.T)
d, n = A.size
h = cvxopt.matrix(np.zeros(n))
def P(u, v, alpha = 1.0, beta = 0.0 ):
"""
v := alpha * A * A.T * u + beta * v
"""
blas.scal(beta, v)
tmp = A*u
blas.gemv(A.T, tmp, v, alpha=alpha, beta=beta)
def G(u, v, alpha=1.0, beta=0.0, trans='N'):
"""
v := alpha*-I * u + beta * v (trans = 'N' or 'T')
"""
blas.scal(beta, v)
blas.axpy(u, v, alpha=-alpha)
# Customized solver for the KKT system
# see overleaf for details
S = matrix(0.0, (d, d))
v = matrix(0.0, (d, 1))
def Fkkt(W):
# D = - W^T * W
Dvecsqrt = W['d']
Dvec = -Dvecsqrt**2
D = spdiag(Dvec)
TrD = sum(Dvec)
TrDsqrt = sqrt(-TrD)
# Asc = A*diag(d)^-1/2
ADsqrt = A * spdiag(Dvecsqrt)
# (rank k and rank 1 update)
# S = A * D * A' - AD11'DA' - I
# rank k update
blas.syrk(ADsqrt, S, alpha=-1.0)
AD = A*D
ones = matrix(1.0, (n, 1))
AD1 = AD*ones / TrDsqrt
# rank 1 update
blas.syr(AD1, S, alpha=1.0)
# subtract identity
S[::d+1] -= 1.0
# # compute cholesky - doesn't seem to be any faster
ipiv = matrix(0, (n, 1), tc='i')
lapack.sytrf(S, ipiv)
def g(x, y, z):
# v = rhs of linear system
blas.gemv(AD, x - (y + sum(cvxopt.mul(Dvec, x) + z)) / TrD * ones, v)
blas.axpy(A*z, v)
# solve linear system for v
# lapack.sysv(S, v)
lapack.sytrs(S, ipiv, v)
# compute u_x
u_x = cvxopt.mul(Dvec, A.T*v - sum(cvxopt.mul(Dvec, (A.T*v)))*ones / TrD - (x + cvxopt.div(z, Dvec) - (y + sum(cvxopt.mul(Dvec, x) + z)) / TrD * ones))
# compute u_y
blas.axpy(cvxopt.matrix(sum(cvxopt.mul(Dvec, (A.T*(A*u_x))) - cvxopt.mul(Dvec, x) - z)), y, alpha=-1.0)
# blas.scal(1/TrD, y)
y /= TrD
# compute u_z
blas.swap(x, u_x)
blas.axpy(x, z)
z[:] = cvxopt.div(z, -Dvecsqrt)
# blas.swap(z, tmp)
return g
A_ = cvxopt.matrix(np.ones((1, n)))
b_ = cvxopt.matrix(np.ones(1))
# set params
maxiters = cvxopt.solvers.options["maxiters"]
feastol = cvxopt.solvers.options["feastol"]
reltol = cvxopt.solvers.options["reltol"]
show_progress = cvxopt.solvers.options["show_progress"]
cvxopt.solvers.options["maxiters"] = 300
cvxopt.solvers.options["feastol"] = np.finfo(float).eps * 1e6
cvxopt.solvers.options["reltol"] = np.finfo(float).eps * 1e6
cvxopt.solvers.options["show_progress"] = verbose
# solve problem
initvals = {'x': cvxopt.matrix(np.ones((n, 1))/n)}
sol = solvers.qp(P=P, q=q, G=G, h=h, A=A_, b=b_, kktsolver = Fkkt, initvals=initvals)
# reset params
cvxopt.solvers.options["feastol"] = feastol
cvxopt.solvers.options["reltol"] = reltol
cvxopt.solvers.options["show_progress"] = show_progress
cvxopt.solvers.options["maxiters"] = maxiters
# get solutions
solved_weights = np.array(sol["x"])
idxes = np.array(list(range(n)))
# special case
if d+1 == n:
if np.isclose(np.linalg.norm(solved_weights.reshape(1, -1) @ X - p), 0):
return idxes, solved_weights
# use weight on smaller end of the largest log-gap
if tol is None:
sorted_weights = np.sort(solved_weights.reshape(-1))
# handle situations where small entries may be slightly negative
sorted_weights[sorted_weights < 0.0] = sorted_weights[sorted_weights > 0.0].min()
gap = np.log(sorted_weights[1:]) - np.log(sorted_weights[:-1])
max_gap = gap.argmax()
# check that there is a significant gap, otherwise threshold at 0 (all values are away from 0)
median_gap = np.median(gap)
MAD = np.median(np.abs(gap - median_gap))
if gap.max() < median_gap + 3 * 1.4826* MAD:
tol = 0.0
else:
tol = sorted_weights[max_gap]
support = idxes[(solved_weights > tol).reshape(-1)]
return support, solved_weights
def exactLPSimplex(X, p):
"""
Solve (E) using the simplex method
:param X: np array of shape (n, d), with data as rows
:param p: np array of shape (1, d), the point to be represented
:return support: an array of the indices in X containing the support of weights
:return solved_weights: the weights to solve (E)
"""
n = X.shape[0]
c = np.power(np.linalg.norm(X - p, axis=1), 2).reshape(-1, 1)
# A_ub = -eye(n)
A_eq = np.vstack([X.T, np.ones((1, n))])
# b_ub = np.zeros((n, 1))
b_eq = np.concatenate([p.reshape(-1), np.ones(1)], axis=0)
sol = linprog(c=c, A_eq=A_eq, b_eq=b_eq, method="highs")
w = sol["x"]
support = np.arange(n)[w > 0]
weights = w[support]
return support, weights
def convexHullLPSimplex(X, p):
"""
Solves the convex hull LP using the simplex method
:param X: np array of shape (n, d), with data as rows
:param p: np array of shape (1, d), the point to be represented
:return support: an array of the indices in X containing the support of weights
"""
p = p.reshape(-1, 1)
n, d = X.shape
A_ub = np.ones((n, d + 1))
A_ub[:, :d] = X
c = np.ones((d + 1, 1))
c[:d] = p
b_ub = np.linalg.norm(X, axis=1)**2
sol = linprog(c=-c, A_ub=A_ub, b_ub=b_ub, bounds=(None, None), method="highs")
z = sol['slack']
support = np.arange(n)[z==0]
return support