-
Notifications
You must be signed in to change notification settings - Fork 33
/
clipper.cpp
264 lines (204 loc) · 7.56 KB
/
clipper.cpp
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
/**
* @file clipper.cpp
* @brief CLIPPER data association framework
* @author Parker Lusk <plusk@mit.edu>
* @date 19 March 2022
*/
#include <iostream>
#include "clipper/clipper.h"
#include "clipper/utils.h"
namespace clipper {
CLIPPER::CLIPPER(const invariants::PairwiseInvariantPtr& invariant, const Params& params)
: invariant_(invariant), params_(params)
{}
// ----------------------------------------------------------------------------
void CLIPPER::scorePairwiseConsistency(const invariants::Data& D1,
const invariants::Data& D2, const Association& A)
{
if (A.size() == 0) A_ = utils::createAllToAll(D1.cols(), D2.cols());
else A_ = A;
const size_t m = A_.rows();
Eigen::MatrixXd M = Eigen::MatrixXd::Zero(m, m);
#pragma omp parallel for shared(A_, D1, D2, M_, C_) if(parallelize_)
for (size_t k=0; k<m*(m-1)/2; ++k) {
size_t i, j; std::tie(i, j) = utils::k2ij(k, m);
if (A_(i,0) == A_(j,0) || A_(i,1) == A_(j,1)) {
// violates distinctness constraint
continue;
}
//
// Evaluate the consistency of geometric invariants associated with ei, ej
//
// points to extract invariant from in D1
const auto& d1i = D1.col(A_(i,0));
const auto& d1j = D1.col(A_(j,0));
// points to extract invariant from in D2
const auto& d2i = D2.col(A_(i,1));
const auto& d2j = D2.col(A_(j,1));
const double scr = (*invariant_)(d1i, d1j, d2i, d2j);
if (scr > params_.affinityeps) { // does not violate inconsistency constraint
M(i,j) = scr;
}
}
// Identity on diagonal is taken care of implicitly in findDenseClique()
// M += Eigen::MatrixXd::Identity(m, m);
M_ = M.sparseView();
C_ = M_;
C_.coeffs() = 1;
}
// ----------------------------------------------------------------------------
void CLIPPER::solve(const Eigen::VectorXd& _u0)
{
Eigen::VectorXd u0;
if (_u0.size() == 0) {
u0 = utils::randvec(M_.cols());
} else {
u0 = _u0;
}
findDenseClique(u0);
}
// ----------------------------------------------------------------------------
Association CLIPPER::getInitialAssociations()
{
return A_;
}
// ----------------------------------------------------------------------------
Association CLIPPER::getSelectedAssociations()
{
return utils::selectInlierAssociations(soln_, A_);
}
// ----------------------------------------------------------------------------
Affinity CLIPPER::getAffinityMatrix()
{
Affinity M = SpAffinity(M_.selfadjointView<Eigen::Upper>())
+ Affinity::Identity(M_.rows(), M_.cols());
return M;
}
// ----------------------------------------------------------------------------
Constraint CLIPPER::getConstraintMatrix()
{
Constraint C = SpConstraint(C_.selfadjointView<Eigen::Upper>())
+ Constraint::Identity(C_.rows(), C_.cols());
return C;
}
// ----------------------------------------------------------------------------
void CLIPPER::setMatrixData(const Affinity& M, const Constraint& C)
{
Eigen::MatrixXd MM = M.triangularView<Eigen::Upper>();
MM.diagonal().setZero();
M_ = MM.sparseView();
Eigen::MatrixXd CC = C.triangularView<Eigen::Upper>();
CC.diagonal().setZero();
C_ = CC.sparseView();
}
// ----------------------------------------------------------------------------
void CLIPPER::setSparseMatrixData(const SpAffinity& M, const SpConstraint& C)
{
M_ = M;
C_ = C;
}
// ----------------------------------------------------------------------------
// Private Methods
// ----------------------------------------------------------------------------
void CLIPPER::findDenseClique(const Eigen::VectorXd& u0)
{
const auto t1 = std::chrono::high_resolution_clock::now();
//
// Initialization
//
const size_t n = M_.cols();
const Eigen::VectorXd ones = Eigen::VectorXd::Ones(n);
// one step of power method to have a good scaling of u
Eigen::VectorXd u = M_.selfadjointView<Eigen::Upper>() * u0 + u0;
u /= u.norm();
// initial value of d
double d = 0; // zero if there are no active constraints
Eigen::VectorXd Cbu = ones * u.sum() - C_.selfadjointView<Eigen::Upper>() * u - u;
const auto idxD = ((Cbu.array()>params_.eps) && (u.array()>params_.eps));
if (idxD.sum() > 0) {
Eigen::VectorXd Mu = M_.selfadjointView<Eigen::Upper>() * u + u;
const Eigen::VectorXd num = idxD.select(Mu, std::numeric_limits<double>::infinity());
const Eigen::VectorXd den = idxD.select(Cbu, 1);
d = (num.array() / den.array()).minCoeff();
}
// initialize memory
Eigen::VectorXd gradF = Eigen::VectorXd(n);
Eigen::VectorXd gradFnew = Eigen::VectorXd(n);
Eigen::VectorXd unew = Eigen::VectorXd(n);
Eigen::VectorXd Mu = Eigen::VectorXd(n);
Eigen::VectorXd num = Eigen::VectorXd(n);
Eigen::VectorXd den = Eigen::VectorXd(n);
//
// Orthogonal projected gradient ascent with homotopy
//
double F = 0; // objective value
size_t i, j, k; // iteration counters
for (i=0; i<params_.maxoliters; ++i) {
gradF = (1 + d) * u - d * ones * u.sum() + M_.selfadjointView<Eigen::Upper>() * u + C_.selfadjointView<Eigen::Upper>() * u * d;
F = u.dot(gradF); // current objective value
//
// Orthogonal projected gradient ascent
//
for (j=0; j<params_.maxiniters; ++j) {
double alpha = 1;
//
// Backtracking line search on gradient ascent
//
double Fnew = 0, deltaF = 0;
for (k=0; k<params_.maxlsiters; ++k) {
unew = u + alpha * gradF; // gradient step
unew = unew.cwiseMax(0); // project onto positive orthant
unew.normalize(); // project onto S^n
gradFnew = (1 + d) * unew // because M/C is missing identity on diagonal
- d * ones * unew.sum()
+ M_.selfadjointView<Eigen::Upper>() * unew
+ C_.selfadjointView<Eigen::Upper>() * unew * d;
Fnew = unew.dot(gradFnew); // new objective value after step
deltaF = Fnew - F; // change in objective value
if (deltaF < -params_.eps) {
// objective value decreased---we need to backtrack, so reduce step size
alpha = alpha * params_.beta;
} else {
break; // obj value increased, stop line search
}
}
const double deltau = (unew - u).norm();
// update values
F = Fnew;
u = unew;
// check if desired accuracy has been reached by gradient ascent
if (deltau < params_.tol_u || std::abs(deltaF) < params_.tol_F) break;
}
//
// Increase d
//
Cbu = ones * u.sum() - C_.selfadjointView<Eigen::Upper>() * u - u;
const auto idxD = ((Cbu.array() > params_.eps) && (u.array() > params_.eps));
if (idxD.sum() > 0) {
Mu = M_.selfadjointView<Eigen::Upper>() * u + u;
num = idxD.select(Mu, std::numeric_limits<double>::infinity());
den = idxD.select(Cbu, 1);
const double deltad = (num.array() / den.array()).abs().minCoeff();
d += deltad;
} else {
break;
}
}
//
// Generate output
//
// estimate cluster size using largest eigenvalue
const int omega = std::round(F);
// extract indices of nodes in identified dense cluster
std::vector<int> I = utils::findIndicesOfkLargest(u, omega);
const auto t2 = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1);
const double elapsed = static_cast<double>(duration.count()) / 1e9;
// set solution
soln_.t = elapsed;
soln_.ifinal = i;
std::swap(soln_.nodes, I);
soln_.u.swap(u);
soln_.score = F;
}
} // ns clipper