Repository navigation
Expand file tree
/
Copy pathCertifiableLandmarkSLAM.cpp
More file actions
424 lines (386 loc) · 15.2 KB
/
Copy pathCertifiableLandmarkSLAM.cpp
File metadata and controls
424 lines (386 loc) · 15.2 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file CertifiableLandmarkSLAM.cpp
* @brief Certifiable landmark SLAM via the Riemannian
* Staircase. Minimizes
* Sum_ij kappa_ij ||R_j - R_i R_ij||^2_F + tau_ij ||t_j - t_i - R_i
* t_ij||^2
* + Sum_ik nu_ik ||l_k - t_i - R_i v_ik||^2
* over R_i in O(d), t_i in R^d, l_k in R^d.
* @author Zhexin Xu
*
* Usage:
* ./CertifiableLandmarkSLAM --data=<g2o file> --dim=<2|3>
* [--initialization=<fast-sync|random>]
* [--solver=<staircase|mosek-monolithic|mosek-chordal>]
*/
#include <gtsam/certifiable/RiemannianStaircaseOptimizer.h>
#include <gtsam/constrained/QcqpProblem.h>
#include <gtsam/geometry/Pose2.h>
#include <gtsam/geometry/Pose3.h>
#include <gtsam/inference/Symbol.h>
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <gtsam/slam/FastSync.h>
#include <gtsam/slam/FrobeniusFactor.h>
#include <gtsam/slam/RelativeTranslationFactor.h>
#include <Eigen/SVD>
#include <chrono>
#include <fstream>
#include <iostream>
#include <limits>
#include <numeric>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include "CertifiableMosek.h"
using namespace gtsam;
using RSO = RiemannianStaircaseOptimizer;
namespace {
constexpr unsigned int kSeed = 60;
enum class InitializationMethod { FastSync, Random };
const char* initializationName(InitializationMethod method) {
switch (method) {
case InitializationMethod::FastSync:
return "fast-sync";
case InitializationMethod::Random:
return "random";
}
throw std::invalid_argument("Unknown initialization method");
}
Key rotationKey(Key poseIndex) { return Symbol('R', poseIndex); }
Key translationKey(Key poseIndex) { return Symbol('t', poseIndex); }
Key landmarkKey(Key landmarkIndex) { return Symbol('l', landmarkIndex); }
/// Certifiable methods optimize over O(d), which has two components, det = +1
/// and det = -1, so the rounded rotations can come out reflected.
template <int d>
void alignBlockDetSigns(Values& values, const std::set<Key>& poseIndices) {
size_t negative = 0;
for (Key poseIndex : poseIndices)
if (values.at<Matrix>(rotationKey(poseIndex)).determinant() < 0) ++negative;
if (negative <= poseIndices.size() / 2) return;
for (const auto& [key, M] : values.extract<Matrix>()) {
Matrix flipped = M;
flipped.col(M.cols() - 1) *= -1.0;
values.update(key, flipped);
}
}
/// One odometry edge.
template <int d>
struct Odometry {
using PoseT = typename std::conditional<d == 2, Pose2, Pose3>::type;
Key i, j;
PoseT measured;
double kappa, tau;
};
/// One landmark observation.
template <int d>
struct Observation {
Key i, l;
Eigen::Matrix<double, d, 1> measured;
double nu;
};
template <int d>
struct Dataset {
using PoseT = typename std::conditional<d == 2, Pose2, Pose3>::type;
std::vector<Odometry<d>> odometry;
std::vector<Observation<d>> observations;
std::set<Key> poseIds, landmarkIds;
};
/// Read a landmark-SLAM g2o file.
template <int d>
Dataset<d> readLandmarkG2o(const std::string& path) {
std::ifstream in(path);
if (!in) throw std::invalid_argument("readLandmarkG2o: cannot open " + path);
Dataset<d> data;
std::string line;
while (std::getline(in, line)) {
std::istringstream stream(line);
std::string tag;
stream >> tag;
if ((d == 2 && tag == "EDGE_SE2") || (d == 3 && tag == "EDGE_SE3:QUAT")) {
Odometry<d> edge;
stream >> edge.i >> edge.j;
constexpr int dim = d == 2 ? 3 : 6;
Matrix information = Matrix::Identity(dim, dim);
if constexpr (d == 2) {
double x, y, theta;
stream >> x >> y >> theta;
edge.measured = Pose2(x, y, theta);
} else {
double x, y, z, qx, qy, qz, qw;
stream >> x >> y >> z >> qx >> qy >> qz >> qw;
edge.measured =
Pose3(Rot3(Quaternion(qw, qx, qy, qz)), Point3(x, y, z));
}
for (int r = 0; r < dim; ++r)
for (int c = r; c < dim; ++c) {
double v = 0.0;
if (!(stream >> v)) v = (r == c) ? 1.0 : 0.0;
information(r, c) = v;
information(c, r) = v;
}
// Isotropic reduction of each block, in the file's
// (translation, rotation) order.
if constexpr (d == 2) {
edge.kappa = information(2, 2);
edge.tau = 2.0 / information.topLeftCorner<2, 2>().inverse().trace();
} else {
edge.kappa =
3.0 /
(2.0 * information.bottomRightCorner<3, 3>().inverse().trace());
edge.tau = 3.0 / information.topLeftCorner<3, 3>().inverse().trace();
}
data.poseIds.insert(edge.i);
data.poseIds.insert(edge.j);
data.odometry.push_back(edge);
} else if (tag == "LANDMARK2") {
// Planar landmark sightings only; the 3D datasets carry no landmarks.
if constexpr (d == 2) {
Observation<d> obs;
double dx, dy, i11, i12, i22;
if (!(stream >> obs.i >> obs.l >> dx >> dy >> i11 >> i12 >> i22))
continue;
obs.measured = Eigen::Matrix<double, d, 1>(dx, dy);
Matrix2 information;
information << i11, i12, i12, i22;
obs.nu = 2.0 / information.inverse().trace();
data.landmarkIds.insert(obs.l);
data.observations.push_back(obs);
}
}
}
return data;
}
Matrix randomBlock(int rows, int p, std::mt19937& rng, bool orthonormal) {
std::normal_distribution<double> normal(0.0, 1.0);
Matrix X(rows, p);
for (int r = 0; r < rows; ++r)
for (int c = 0; c < p; ++c) X(r, c) = normal(rng);
if (!orthonormal) return X;
Eigen::JacobiSVD<Matrix> svd(X, Eigen::ComputeThinU | Eigen::ComputeThinV);
return svd.matrixU() * svd.matrixV().transpose();
}
template <int d>
int runCertifiableLandmarkSLAM(const std::string& path,
InitializationMethod initializationMethod,
examples::CertifiableSolver solver) {
using Point = Eigen::Matrix<double, d, 1>;
using RotT = typename std::conditional<d == 2, Rot2, Rot3>::type;
const Dataset<d> data = readLandmarkG2o<d>(path);
// Build graph
NonlinearFactorGraph graph;
for (const auto& edge : data.odometry) {
const Key ri = rotationKey(edge.i), rj = rotationKey(edge.j);
graph.emplace_shared<FrobeniusBetweenFactor<RotT>>(
ri, rj, edge.measured.rotation(),
noiseModel::Isotropic::Variance(traits<RotT>::dimension,
1.0 / edge.kappa));
graph.emplace_shared<RelativeTranslationFactor<d>>(
ri, translationKey(edge.i), translationKey(edge.j),
Point(edge.measured.translation()), edge.tau);
}
for (const auto& obs : data.observations) {
if (!data.poseIds.count(obs.i)) continue;
graph.emplace_shared<RelativeTranslationFactor<d>>(
rotationKey(obs.i), translationKey(obs.i), landmarkKey(obs.l),
Point(obs.measured), obs.nu);
}
const std::set<Key>& poseIndices = data.poseIds;
const std::set<Key>& landmarkIndices = data.landmarkIds;
const size_t odometryEdges = data.odometry.size();
const size_t landmarkEdges = data.observations.size();
if (graph.empty()) {
std::cerr << "no BetweenFactor edges in " << path << "\n";
return 1;
}
std::cout << path.substr(path.find_last_of('/') + 1) << " d=" << d
<< " odometry edges=" << odometryEdges
<< " landmark edges=" << landmarkEdges
<< " poses=" << poseIndices.size()
<< " landmarks=" << landmarkIndices.size()
<< " init=" << initializationName(initializationMethod) << "\n";
if (solver != examples::CertifiableSolver::Staircase) {
const Key anchor = *poseIndices.begin();
const QcqpProblem problem = examples::makeAnchoredQcqp<RotT, Point>(
graph, rotationKey(anchor), translationKey(anchor));
const examples::MosekExampleResult result =
examples::solveMosek(problem, solver);
if (!result.solved) {
examples::printMosekSummary(result, solver,
std::numeric_limits<double>::quiet_NaN());
return 1;
}
Values rounded;
for (Key poseIndex : poseIndices) {
rounded.insert(rotationKey(poseIndex),
traits<RotT>::template FromQcqpValue<1>(
result.qcqpValues.at<Matrix>(rotationKey(poseIndex))));
rounded.insert(
translationKey(poseIndex),
traits<Point>::template FromQcqpValue<1>(
result.qcqpValues.at<Matrix>(translationKey(poseIndex))));
}
for (Key landmarkIndex : landmarkIndices) {
rounded.insert(
landmarkKey(landmarkIndex),
traits<Point>::template FromQcqpValue<1>(
result.qcqpValues.at<Matrix>(landmarkKey(landmarkIndex))));
}
examples::printMosekSummary(result, solver, graph.error(rounded));
return result.solved ? 0 : 1;
}
const int p = d;
std::mt19937 rng(kSeed);
// Initialization
const auto initializationStart = std::chrono::steady_clock::now();
Values initial;
if (initializationMethod == InitializationMethod::FastSync) {
// fastSync consumes the isotropic rotation edges; translations and
// landmarks are unconstrained by it and still need a guess.
NonlinearFactorGraph rotationGraph;
for (const auto& factor : graph)
if (std::dynamic_pointer_cast<FrobeniusBetweenFactor<RotT>>(factor))
rotationGraph.push_back(factor);
for (const auto& [key, rotation] :
fastSync<RotT>(rotationGraph).template extract<RotT>()) {
InsertQcqpValue<RotT, d>(key, rotation, &initial);
}
for (Key poseIndex : poseIndices)
initial.insert(translationKey(poseIndex), randomBlock(1, p, rng, false));
} else {
for (Key poseIndex : poseIndices) {
initial.insert(rotationKey(poseIndex), randomBlock(d, p, rng, true));
initial.insert(translationKey(poseIndex), randomBlock(1, p, rng, false));
}
}
for (Key landmarkIndex : landmarkIndices)
initial.insert(landmarkKey(landmarkIndex), randomBlock(1, p, rng, false));
const double initializationSeconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() -
initializationStart)
.count();
// Certifiable solver
RiemannianStaircaseParams params;
params.pMin = d;
params.pMax = 10;
params.eta = 1e-4;
params.verbose = true;
params.almParams->maxIterations = 100;
params.almParams->absoluteViolationTolerance = 1e-10;
params.almParams->absoluteStationarityTolerance = 1e-8;
params.almParams->lmParams.maxIterations = 1000;
params.almParams->lmParams.relativeErrorTol = 1e-12;
params.almParams->lmParams.absoluteErrorTol = 1e-12;
params.almParams->verbose = false;
const auto solveStart = std::chrono::steady_clock::now();
const auto result = RSO(graph, initial, params).optimize();
const double solveSeconds = std::chrono::duration<double>(
std::chrono::steady_clock::now() - solveStart)
.count();
// Rounding and extracting solution
const auto roundingStart = std::chrono::steady_clock::now();
Values rounded;
if (result.rounded) {
Values atRankD = result.layout.unstack(result.rounded->Yd);
alignBlockDetSigns<d>(atRankD, poseIndices);
for (auto& [key, R] : ExtractQcqpValues<RotT, d>(atRankD))
rounded.insert(key, R);
for (Key poseIndex : poseIndices) {
const Matrix row = atRankD.at<Matrix>(translationKey(poseIndex));
rounded.insert(translationKey(poseIndex), Point(row.row(0).transpose()));
}
for (Key landmarkIndex : landmarkIndices) {
const Matrix row = atRankD.at<Matrix>(landmarkKey(landmarkIndex));
rounded.insert(landmarkKey(landmarkIndex), Point(row.row(0).transpose()));
}
}
const double roundingSeconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() -
roundingStart)
.count();
std::cout << "\n========== Result ==========\n"
<< "Certified: " << (result.certified ? "yes" : "no")
<< "\n"
<< "Final rank: " << result.finalRank << "\n";
if (rounded.empty()) {
std::cout << "Rounded objective: n/a\n";
} else {
std::cout << "Rounded objective: " << graph.error(rounded) << "\n";
}
const double buildSeconds =
std::accumulate(result.qcqpBuildTimePerLevel.begin(),
result.qcqpBuildTimePerLevel.end(), 0.0);
const double nlpSeconds = std::accumulate(result.nlpTimePerLevel.begin(),
result.nlpTimePerLevel.end(), 0.0);
const double verifySeconds = std::accumulate(
result.verifyTimePerLevel.begin(), result.verifyTimePerLevel.end(), 0.0);
std::cout << "Initialization: " << initializationName(initializationMethod)
<< "\n"
<< "Initialization time: " << initializationSeconds << " s\n"
<< "QCQP build time: " << buildSeconds << " s\n"
<< "Local solve time: " << nlpSeconds << " s\n"
<< "Certificate time: " << verifySeconds << " s\n"
<< "Rounding time: " << roundingSeconds << " s\n"
<< "Total BM time: " << result.totalTime << " s\n"
<< "Solve wall time: " << solveSeconds << " s\n"
<< "End-to-end time: " << initializationSeconds + solveSeconds
<< " s\n";
return 0;
}
} // namespace
int main(int argc, char** argv) {
std::string dataPath;
std::string initialization = "random";
std::string solverName = "staircase";
int dim = 3;
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
if (arg.rfind("--data=", 0) == 0) {
dataPath = arg.substr(7);
} else if (arg.rfind("--dim=", 0) == 0) {
dim = std::stoi(arg.substr(6));
} else if (arg.rfind("--initialization=", 0) == 0) {
initialization = arg.substr(17);
} else if (arg.rfind("--solver=", 0) == 0) {
solverName = arg.substr(9);
}
}
InitializationMethod initializationMethod;
if (initialization == "fast-sync") {
initializationMethod = InitializationMethod::FastSync;
} else if (initialization == "random") {
initializationMethod = InitializationMethod::Random;
} else {
std::cerr << "Unknown initialization method: " << initialization << "\n";
return 2;
}
examples::CertifiableSolver solver;
try {
solver = examples::parseCertifiableSolver(solverName);
} catch (const std::invalid_argument& error) {
std::cerr << error.what() << "\n";
return 2;
}
if (dataPath.empty() || (dim != 2 && dim != 3)) {
std::cerr << "Usage: " << argv[0]
<< " --data=<g2o file> --dim=<2|3> "
"[--initialization=<fast-sync|random>] "
"[--solver=<staircase|mosek-monolithic|mosek-chordal>]\n";
return 2;
}
if (dim == 3) {
return runCertifiableLandmarkSLAM<3>(dataPath, initializationMethod,
solver);
} else {
return runCertifiableLandmarkSLAM<2>(dataPath, initializationMethod,
solver);
}
}