23#include <gtsam/dllexport.h>
31#include <Eigen/Sparse>
44struct GTSAM_EXPORT ShonanAveragingParameters {
46 using Rot =
typename std::conditional<d == 2, Rot2, Rot3>::type;
47 using Anchor = std::pair<size_t, Rot>;
62 LevenbergMarquardtParams::CeresDefaults(),
63 const std::string &method =
"JACOBI",
70 void setOptimalityThreshold(
double value) { optimalityThreshold = value; }
71 double getOptimalityThreshold()
const {
return optimalityThreshold; }
73 void setAnchor(
size_t index,
const Rot &value) { anchor = {index, value}; }
74 std::pair<size_t, Rot> getAnchor()
const {
return anchor; }
76 void setAnchorWeight(
double value) { alpha = value; }
77 double getAnchorWeight()
const {
return alpha; }
79 void setKarcherWeight(
double value) { beta = value; }
80 double getKarcherWeight()
const {
return beta; }
82 void setGaugesWeight(
double value) { gamma = value; }
83 double getGaugesWeight()
const {
return gamma; }
85 void setUseHuber(
bool value) { useHuber = value; }
86 bool getUseHuber()
const {
return useHuber; }
88 void setCertifyOptimality(
bool value) { certifyOptimality = value; }
89 bool getCertifyOptimality()
const {
return certifyOptimality; }
92 void print(
const std::string &s =
"")
const {
93 std::cout << (s.empty() ? s : s +
" ");
94 std::cout <<
" ShonanAveragingParameters: " << std::endl;
95 std::cout <<
" alpha: " <<
alpha << std::endl;
96 std::cout <<
" beta: " <<
beta << std::endl;
97 std::cout <<
" gamma: " <<
gamma << std::endl;
98 std::cout <<
" useHuber: " <<
useHuber << std::endl;
102using ShonanAveragingParameters2 = ShonanAveragingParameters<2>;
103using ShonanAveragingParameters3 = ShonanAveragingParameters<3>;
123 using Sparse = Eigen::SparseMatrix<double>;
127 using Rot =
typename Parameters::Rot;
130 using Measurements = std::vector<BinaryMeasurement<Rot>>;
133 Parameters parameters_;
134 Measurements measurements_;
144 std::shared_ptr<LevenbergMarquardtOptimizer> createOptimizerAt(
145 size_t p,
const Values &initial,
146 const std::optional<Ordering> &ordering)
const;
149 std::pair<Values, double> run(
const Values &initial,
size_t min_p,
151 const std::optional<Ordering> &ordering)
const;
157 Sparse buildQ()
const;
160 Sparse buildD()
const;
169 const Parameters ¶meters = Parameters());
183 return measurements_[k];
193 double k = 1.345)
const {
194 Measurements robustMeasurements;
198 std::dynamic_pointer_cast<noiseModel::Robust>(model);
203 robust_model = model;
206 robust_model = noiseModel::Robust::Create(
207 noiseModel::mEstimator::Huber::Create(k), model);
211 robustMeasurements.push_back(meas);
213 return robustMeasurements;
217 const Rot &
measured(
size_t k)
const {
return measurements_[k].measured(); }
226 Sparse
D()
const {
return D_; }
227 Matrix
denseD()
const {
return Matrix(D_); }
228 Sparse
Q()
const {
return Q_; }
229 Matrix
denseQ()
const {
return Matrix(Q_); }
230 Sparse
L()
const {
return L_; }
231 Matrix
denseL()
const {
return Matrix(L_); }
234 Sparse computeLambda(
const Matrix &S)
const;
247 Sparse computeA(
const Values &values)
const;
250 Sparse computeA(
const Matrix &S)
const;
258 static Matrix StiefelElementMatrix(
const Values &values);
264 double computeMinEigenValue(
const Values &values,
265 Vector *minEigenVector)
const;
274 Vector& minEigenVector)
const {
282 double computeMinEigenValueAP(
const Values &values,
283 Vector *minEigenVector =
nullptr)
const;
299 const Vector &minEigenVector);
309 size_t p,
const Values &values,
const Vector &minEigenVector,
310 double minEigenValue,
double gradienTolerance = 1e-2,
311 double preconditionedGradNormTolerance = 1e-4)
const;
364 std::shared_ptr<LevenbergMarquardtOptimizer> createOptimizerAt(
365 size_t p,
const Values &initial)
const;
391 for (
const auto& it : values.
extract<T>()) {
405 double cost(
const Values &values)
const;
414 Values initializeRandomly(std::mt19937 &rng)
const;
417 Values initializeRandomly()
const;
426 std::pair<Values, double> run(
const Values &initial,
size_t min_p = d,
427 size_t max_p = 10)
const;
435 std::pair<Values, double> run(
size_t min_p = d,
size_t max_p = 10)
const;
447 template <
typename T>
450 bool useRobustModel =
false)
const {
460 ShonanAveraging2(
const Measurements &measurements,
461 const Parameters ¶meters = Parameters());
462 explicit ShonanAveraging2(std::string g2oFile,
463 const Parameters ¶meters = Parameters());
464 ShonanAveraging2(
const BetweenFactorPose2s &factors,
465 const Parameters ¶meters = Parameters());
470 ShonanAveraging3(
const Measurements &measurements,
471 const Parameters ¶meters = Parameters());
472 explicit ShonanAveraging3(std::string g2oFile,
473 const Parameters ¶meters = Parameters());
476 ShonanAveraging3(
const BetweenFactorPose3s &factors,
477 const Parameters ¶meters = Parameters());
typedef and functions to augment Eigen's MatrixXd
typedef and functions to augment Eigen's VectorXd
3D rotation represented as a rotation matrix or quaternion
Parameters for Levenberg-Marquardt trust-region scheme.
Binary measurement represents a measurement between two keys in a graph. A binary measurement is simi...
utility functions for loading datasets
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
static SO Lift(size_t n, const Eigen::MatrixBase< Derived > &R)
Definition SOn.h:105
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
This class performs Levenberg-Marquardt nonlinear optimization.
Definition LevenbergMarquardtOptimizer.h:35
Parameters for Levenberg-Marquardt optimization.
Definition LevenbergMarquardtParams.h:36
Definition NonlinearFactorGraph.h:57
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
void insert(Key j, const Value &val)
Add a variable with the given j, throws KeyAlreadyExists<J> if j is already present.
Definition Values.cpp:170
Values extract(const KeyVector &keys) const
Returns a new Values holding copies of the values at the given keys, whatever their types.
Definition Values.cpp:252
Definition BinaryMeasurement.h:37
Parameters governing optimization etc.
Definition ShonanAveraging.h:44
double alpha
Definition ShonanAveraging.h:53
void print(const std::string &s="") const
Print the parameters and flags used for rotation averaging.
Definition ShonanAveraging.h:92
LevenbergMarquardtParams lm
Definition ShonanAveraging.h:50
bool useHuber
Definition ShonanAveraging.h:57
double optimalityThreshold
Definition ShonanAveraging.h:51
double beta
Definition ShonanAveraging.h:54
double gamma
Definition ShonanAveraging.h:55
Anchor anchor
Definition ShonanAveraging.h:52
bool certifyOptimality
Definition ShonanAveraging.h:59
Sparse D() const
Sparse version of D.
Definition ShonanAveraging.h:226
std::pair< double, Vector > computeMinEigenVector(const Values &values) const
Compute minimum eigenvalue for optimality check.
Definition ShonanAveraging.cpp:789
const BinaryMeasurement< Rot > & measurement(size_t k) const
k^th binary measurement
Definition ShonanAveraging.h:182
Measurements makeNoiseModelRobust(const Measurements &measurements, double k=1.345) const
Update factors to use robust Huber loss.
Definition ShonanAveraging.h:192
double computeMinEigenValue(const Values &values) const
Compute the minimum eigenvalue without requesting its eigenvector.
Definition ShonanAveraging.h:268
Values roundSolution(const Values &values) const
Project from SO(p)^N to Rot2^N or Rot3^N Values should be of type SO(p).
Definition ShonanAveraging.cpp:343
Values roundSolutionS(const Matrix &S) const
Project pxdN Stiefel manifold matrix S to Rot3^N.
std::vector< BinaryMeasurement< T > > maybeRobust(const std::vector< BinaryMeasurement< T > > &measurements, bool useRobustModel=false) const
Helper function to convert measurements to robust noise model if flag is set.
Definition ShonanAveraging.h:448
Values initializeWithDescent(size_t p, const Values &values, const Vector &minEigenVector, double minEigenValue, double gradienTolerance=1e-2, double preconditionedGradNormTolerance=1e-4) const
Given some values at p-1, return new values at p, by doing a line search along the descent direction,...
Definition ShonanAveraging.cpp:862
Sparse L() const
Sparse version of L.
Definition ShonanAveraging.h:230
static VectorValues TangentVectorValues(size_t p, const Vector &v)
Create a VectorValues with eigenvector v_i.
Definition ShonanAveraging.cpp:805
const Rot & measured(size_t k) const
k^th measurement, as a Rot.
Definition ShonanAveraging.h:217
static Values LiftwithDescent(size_t p, const Values &values, const Vector &minEigenVector)
Lift up the dimension of values in type SO(p-1) with descent direction provided by minEigenVector and...
Definition ShonanAveraging.cpp:853
Sparse Q() const
Sparse version of Q.
Definition ShonanAveraging.h:228
Matrix denseD() const
Dense version of D.
Definition ShonanAveraging.h:227
size_t nrUnknowns() const
Return number of unknowns.
Definition ShonanAveraging.h:176
Values projectFrom(size_t p, const Values &values) const
Project from SO(p) to Rot2 or Rot3 Values should be of type SO(p).
size_t numberMeasurements() const
Return number of measurements.
Definition ShonanAveraging.h:179
Matrix denseL() const
Dense version of L.
Definition ShonanAveraging.h:231
Matrix computeLambda_(const Matrix &S) const
Dense versions of computeLambda for wrapper/testing.
Definition ShonanAveraging.h:242
Sparse computeLambda(const Matrix &S) const
Version that takes pxdN Stiefel manifold elements.
Definition ShonanAveraging.cpp:478
Matrix denseQ() const
Dense version of Q.
Definition ShonanAveraging.h:229
double costAt(size_t p, const Values &values) const
Calculate cost for SO(p) Values should be of type SO(p).
Definition ShonanAveraging.cpp:194
const KeyVector & keys(size_t k) const
Keys for k^th measurement, as a vector of Key values.
Definition ShonanAveraging.h:220
ShonanAveraging(const Measurements &measurements, const Parameters ¶meters=Parameters())
Construct from set of relative measurements (given as BetweenFactor<Rot3> for now) NoiseModel must be...
Definition ShonanAveraging.cpp:126
bool checkOptimality(const Values &values) const
Check optimality.
Definition ShonanAveraging.cpp:798
Sparse computeA(const Values &values) const
Compute A matrix whose Eigenvalues we will examine.
Definition ShonanAveraging.cpp:518
Values tryOptimizingAt(size_t p, const Values &initial) const
Try to optimize at SO(p).
Definition ShonanAveraging.cpp:231
Matrix computeLambda_(const Values &values) const
Dense versions of computeLambda for wrapper/testing.
Definition ShonanAveraging.h:237
Values initializeRandomlyAt(size_t p, std::mt19937 &rng) const
Create initial Values of type SO(p).
Definition ShonanAveraging.cpp:917
double computeMinEigenValue(const Values &values, Vector *minEigenVector) const
Compute minimum eigenvalue for optimality check.
Definition ShonanAveraging.cpp:755
Matrix riemannianGradient(size_t p, const Values &values) const
Calculate the riemannian gradient of F(values) at values.
Definition ShonanAveraging.cpp:827
double computeMinEigenValue(const Values &values, Vector &minEigenVector) const
Compute the minimum eigenvalue and write its eigenvector.
Definition ShonanAveraging.h:273
Matrix computeA_(const Values &values) const
Dense version of computeA for wrapper/testing.
Definition ShonanAveraging.h:253
NonlinearFactorGraph buildGraphAt(size_t p) const
Build graph for SO(p).
Definition ShonanAveraging.cpp:169
static Values LiftTo(size_t p, const Values &values)
Lift Values of type T to SO(p).
Definition ShonanAveraging.h:389