-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBirkhoffPolytope.hpp
225 lines (188 loc) · 6.42 KB
/
BirkhoffPolytope.hpp
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
#pragma once
/*
Riemannian structure of the doubly stochastic matrices:
@Techreport{Douik2018Manifold,
Title = {Manifold Optimization Over the Set of Doubly Stochastic
Matrices: {A} Second-Order Geometry},
Author = {Douik, A. and Hassibi, B.},
Journal = {Arxiv preprint ArXiv:1802.02628},
Year = {2018}
}
*/
#include "TCommons.hpp"
#include "ceres/ceres.h"
// default values for Sinkhorn are taken from:
// THE SINKHORN-KNOPP ALGORITHM: CONVERGENCE AND APPLICATIONS, PHILIP A. KNIGHT
// Fig. 6.1 at 1e-4 #iterations~=40
// this effects the speed a lot, so I will not go for a lot of iterations
const double SINKHORN_EPSILON = 0.0001;
const double SINKHORN_MAXITER = 40;
// projects onto the Birkhoff Polytope
template<typename T>
static void sinkhorn(T& A, int maxIterations = SINKHORN_MAXITER, double tol = SINKHORN_EPSILON)
{
// first iteration - no test
int iter = 1;
MatrixXd c = A.colwise().sum().cwiseInverse();
VectorXd r = (A * c.transpose()).cwiseInverse();
// subsequent iterations include test
while (iter < maxIterations) {
iter++;
MatrixXd cinv = r.transpose() * A;
// test whether the tolerance was achieved on the last iteration
if (cinv.cwiseProduct(c).cwiseAbs().maxCoeff() < tol)
break;
c = cinv.cwiseInverse();
r = (A * c.transpose()).cwiseInverse();
}
A = A.cwiseProduct((r * c));
}
// project eta onto the tangent space of X
static MatrixXd DSn_project_TxM(const MatrixXd &X, const MatrixXd &eta)
{
size_t N = X.rows();
MatrixXd I = MatrixXd::Identity(N, N);
MatrixXd A(2 * N, 2 * N); // , B(2 * N, 2 * N - 1);
A << I, X,
X.transpose(), I;
MatrixXd B = A.block(0, 1, 2 * N, 2 * N - 1);
VectorXd b(2 * N);
b << eta.rowwise().sum(),
eta.colwise().sum().transpose();
VectorXd zeta = B.colPivHouseholderQr().solve((b - A.col(0)));
VectorXd alpha(N);
alpha << 1,
zeta.block(0, 0, N - 1, 1);
// beta = zeta(n:2 * n - 1);
VectorXd beta = zeta.block(N - 1, 0, N, 1);
VectorXd e = VectorXd::Ones(N);
MatrixXd etaproj = eta - (alpha*e.transpose() + e * beta.transpose()).cwiseProduct(X);
return etaproj;
}
static void DSn_project_TxM(const MatrixXd &X, const MatrixXd &eta, Map<MatrixXd>& etaproj)
{
size_t N = X.rows();
MatrixXd I = MatrixXd::Identity(N, N);
MatrixXd A(2 * N, 2 * N); // , B(2 * N, 2 * N - 1);
A << I, X,
X.transpose(), I;
MatrixXd B = A.block(0, 1, 2 * N, 2 * N - 1);
VectorXd b(2 * N);
b << eta.rowwise().sum(),
eta.colwise().sum().transpose();
VectorXd zeta = B.colPivHouseholderQr().solve((b - A.col(0)));
VectorXd alpha(N);
alpha << 1,
zeta.block(0, 0, N - 1, 1);
// beta = zeta(n:2 * n - 1);
VectorXd beta = zeta.block(N - 1, 0, N, 1);
VectorXd e = VectorXd::Ones(N);
etaproj = eta - (alpha*e.transpose() + e * beta.transpose()).cwiseProduct(X);
}
static void DSn_project_TxM(const vector<MatrixXd> &X, const vector<MatrixXd> &eta, vector<Map<MatrixXd>>& etaproj)
{
vector<MatrixXd> Ps;
Ps.resize(X.size());
for (size_t i = 0; i < X.size(); i++)
DSn_project_TxM(X[i], eta[i], etaproj[i]);
}
static double Exp(double x) // the functor we want to apply
{
if (_isnan(x) || isinf(x) || x == -INFINITY)
return 0;
else
return std::exp(x);
}
template<typename T>
static MatrixXd DSn_retract(const MatrixXd &X, const MatrixXd &eta, T& Y, double tau = 1.0)
{
Y = X.cwiseProduct((tau * (eta.cwiseQuotient(X))).unaryExpr(&Exp));
sinkhorn<T>(Y);
return Y.cwiseMax(FLT_EPSILON);
}
template<typename T>
static bool DSn_check(T &X, double tol = 0.000001)
{
bool isBirkhoff = (!(X.array() < -tol).any()); // check if any negative exists
for (size_t i = 0; i < X.rows() && isBirkhoff; i++) // check all rows sum to 1
isBirkhoff = (isApproximatelyZero(1.0 - X.row(i).sum(), tol));
for (size_t i = 0; i < X.cols() && isBirkhoff; i++) // check all cols sum to 1
isBirkhoff = (isApproximatelyZero(1.0 - X.col(i).sum(), tol));
return isBirkhoff;
}
static bool DSn_check(vector<MatrixXd> &X)
{
for (size_t i = 0; i < X.size(); i++)
if (!DSn_check(X[i]))
return false;
return true;
}
static bool DSn_check_tangent(MatrixXd &Z)
{
const double tau = 0.000001;
VectorXd one = VectorXd::Ones(Z.rows());
return ((Z*one).norm()<tau && (Z.transpose()*one).norm()<tau);
}
static MatrixXd DSn_rand(size_t N)
{
MatrixXd result = MatrixXd::Random(N, N) + MatrixXd::Ones(N, N);
sinkhorn<MatrixXd>(result, 100, 0.000001);
return result;
}
static vector<MatrixXd> DSn_rand(size_t N, size_t numMatrices)
{
vector<MatrixXd> result;
result.resize(numMatrices);
for (size_t i = 0; i < numMatrices; i++)
result[i] = DSn_rand(N);
return result;
}
// perturb the DS. this function is especially useful for birkhoff because the manifold is not
// well defined towards the vertices
static void DSn_via_perturb(MatrixXd &X, double perturbation = 0.001)
{
X += (perturbation*(MatrixXd::Random(X.rows(), X.cols()) + MatrixXd::Ones(X.rows(), X.cols()))/2);
sinkhorn(X);
}
static void DSn_via_perturb(std::vector<MatrixXd> &X, double perturbation = 0.001)
{
for (size_t i = 0; i < X.size(); i++)
DSn_via_perturb(X[i]);
}
class BirkhoffParameterization : public ceres::LocalParameterization {
public:
BirkhoffParameterization(int _N) : N(_N) {}
virtual ~BirkhoffParameterization() {}
virtual bool Plus(const double* x,
const double* delta,
double* x_plus_delta) const
{
const Map<const MatrixXd> P0(x, N, N);
const Map<const MatrixXd> U(delta, N, N);
// retract back on the manifold
Map<MatrixXd> xPlusDelta(x_plus_delta, N, N);
DSn_retract(P0, U, xPlusDelta);
return true;
};
// if MultiplyByJacobian is implemented ComputeJacobian is not called.
virtual bool ComputeJacobian(const double* x, double* jacobian) const
{
return false;
}
virtual bool MultiplyByJacobian(const double *x, const int num_rows, const double *global_matrix, double *local_matrix) const
{
return Egrad2Rgrad(x, num_rows, global_matrix, local_matrix);
}
virtual bool Egrad2Rgrad(const double *x, const int num_rows, const double *egrad, double *rgrad) const
{
const Map<const MatrixXd> P0(x, N, N);
const Map<const MatrixXd> U(egrad, N, N);
Map<MatrixXd> UProj(rgrad, N, N);
MatrixXd UP0 = U.cwiseProduct(P0);
DSn_project_TxM(P0, UP0, UProj);
return true;
}
virtual int GlobalSize() const { return N * N; }
virtual int LocalSize() const { return N * N; }
int N;
};