36 using namespace gtsam;
41 const Pose3 poseInitial;
42 const Pose3 poseOdometry( Rot3::RzRyRx(
Vector3(0.05, 0.10, -0.75)),
Point3(1.0, -0.25, 0.10) );
43 const Pose3 poseError( Rot3::RzRyRx(
Vector3(0.01, 0.02, -0.1)),
Point3(0.05, -0.05, 0.02) );
46 const SharedDiagonal noisePrior = noiseModel::Isotropic::Sigma(6, 0.10);
47 const SharedDiagonal noiseOdometery = noiseModel::Diagonal::Sigmas((
Vector(6) << 0.1, 0.1, 0.1, 0.5, 0.5, 0.5).finished());
48 const SharedDiagonal noiseLoop = noiseModel::Diagonal::Sigmas((
Vector(6) << 0.25, 0.25, 0.25, 1.0, 1.0, 1.0).finished());
66 std::set<Key> KeysToKeep;
67 for(
const auto key: linPoint.
keys()) {
68 KeysToKeep.insert(
key);
70 for(
Key key: keysToMarginalize) {
75 for(
Key key: keysToMarginalize) {
76 ordering.push_back(
key);
79 ordering.push_back(
key);
87 for(
const GaussianFactor::shared_ptr& factor: marginal) {
90 return LinearContainerForGaussianMarginals;
137 newFactors1.
addPrior(1, poseInitial, noisePrior);
142 filter.
update(newFactors1, newValues1);
159 filter.
update(newFactors2, newValues2);
187 newFactors1.
addPrior(1, poseInitial, noisePrior);
192 filter.
update(newFactors1, newValues1);
196 expected2.
insert(newValues1);
209 filter.
update(newFactors2, newValues2);
213 expected3.
insert(newValues1);
214 expected3.
insert(newValues2);
249 newFactors2.
addPrior(1, poseInitial, noisePrior);
254 filter.
update(newFactors2, newValues2);
260 allValues2.
insert(newValues2);
261 Values expected2 = BatchOptimize(allFactors2, allValues2);
274 filter.
update(newFactors3, newValues3);
281 allValues3.
insert(newValues2);
282 allValues3.
insert(newValues3);
283 Values expected3 = BatchOptimize(allFactors3, allValues3);
333 newFactors2.
addPrior(1, poseInitial, noisePrior);
338 filter.
update(newFactors2, newValues2);
344 allValues2.
insert(newValues2);
345 Values expected2 = BatchOptimize(allFactors2, allValues2);
358 filter.
update(newFactors3, newValues3);
365 allValues3.
insert(newValues2);
366 allValues3.
insert(newValues3);
367 Values expected3 = BatchOptimize(allFactors3, allValues3);
383 newFactors.
addPrior(1, poseInitial, noisePrior);
395 keysToMove.push_back(1);
396 keysToMove.push_back(2);
399 filter.
update(newFactors, newValues, keysToMove);
402 Values optimalValues = BatchOptimize(newFactors, newValues);
406 partialGraph.
addPrior(1, poseInitial, noisePrior);
411 partialValues.insert(1, optimalValues.
at<
Pose3>(1));
412 partialValues.insert(2, optimalValues.
at<
Pose3>(2));
413 partialValues.insert(3, optimalValues.
at<
Pose3>(3));
417 ordering.push_back(1);
418 ordering.push_back(2);
419 ordering.push_back(3);
434 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
435 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
436 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
440 for(
const GaussianFactor::shared_ptr& factor: result) {
449 Values expectedValues = optimalValues;
452 for(
Key key: keysToMove) {
472 Values smootherSeparatorValues;
476 Values expectedSmootherValues, expectedFilterSeparatorValues;
480 Values actualSmootherValues, actualFilterSeparatorValues;
482 filter.
synchronize(smootherSummarization, smootherSeparatorValues);
491 CHECK(
assert_equal(expectedFilterSeparatorValues, actualFilterSeparatorValues, 1
e-6));
507 newFactors.
addPrior(1, poseInitial, noisePrior);
512 filter.
update(newFactors, newValues);
516 Values smootherSeparatorValues;
520 Values expectedSmootherValues, expectedFilterSeparatorValues;
524 Values actualSmootherValues, actualFilterSeparatorValues;
526 filter.
synchronize(smootherSummarization, smootherSeparatorValues);
535 CHECK(
assert_equal(expectedFilterSeparatorValues, actualFilterSeparatorValues, 1
e-6));
541 std::cout <<
"*********************** synchronize_2 ************************" << std::endl;
559 newValues.
insert(1, value1);
560 newValues.
insert(2, value2);
562 keysToMove.push_back(1);
563 filter.
update(newFactors, newValues, keysToMove);
567 Values optimalValues = BatchOptimize(newFactors, newValues);
571 Values smootherSeparatorValues;
577 expectedSmootherFactors.
push_back(factor1);
578 expectedSmootherFactors.
push_back(factor2);
579 Values expectedSmootherValues;
581 expectedSmootherValues.
insert(1, optimalValues.
at<
Pose3>(1));
587 Values expectedFilterSeparatorValues;
588 expectedFilterSeparatorValues.
insert(2, optimalValues.
at<
Pose3>(2));
592 Values actualSmootherValues, actualFilterSeparatorValues;
594 filter.
synchronize(smootherSummarization, smootherSeparatorValues);
603 CHECK(
assert_equal(expectedFilterSeparatorValues, actualFilterSeparatorValues, 1
e-6));
609 std::cout <<
"*********************** synchronize_3 ************************" << std::endl;
631 newValues.
insert(1, value1);
632 newValues.
insert(2, value2);
633 newValues.
insert(3, value3);
636 keysToMove.push_back(1);
638 filter.
update(newFactors, newValues, keysToMove);
640 Values optimalValues = BatchOptimize(newFactors, newValues);
645 Values smootherSeparatorValues;
650 expectedSmootherFactors.
push_back(factor1);
651 expectedSmootherFactors.
push_back(factor2);
652 Values expectedSmootherValues;
654 expectedSmootherValues.
insert(1, optimalValues.
at<
Pose3>(1));
660 Values expectedFilterSeparatorValues;
661 expectedFilterSeparatorValues.
insert(2, optimalValues.
at<
Pose3>(2));
671 keysToMarginalize.push_back(3);
673 expectedFilterSummarization = CalculateMarginals(partialGraph, partialValues, keysToMarginalize);
677 Values actualSmootherValues, actualFilterSeparatorValues;
679 filter.
synchronize(smootherSummarization, smootherSeparatorValues);
688 CHECK(
assert_equal(expectedFilterSeparatorValues, actualFilterSeparatorValues, 1
e-6));
715 newValues.
insert(1, value1);
716 newValues.
insert(2, value2);
717 newValues.
insert(3, value3);
720 keysToMove.push_back(1);
722 filter.
update(newFactors, newValues, keysToMove);
724 Values optimalValuesFilter = BatchOptimize(newFactors, newValues,1);
729 Values smootherSeparatorValues;
734 expectedSmootherFactors.
push_back(factor1);
735 expectedSmootherFactors.
push_back(factor2);
736 Values expectedSmootherValues;
738 expectedSmootherValues.
insert(1, optimalValuesFilter.
at<
Pose3>(1));
745 Values expectedFilterSeparatorValues;
746 expectedFilterSeparatorValues.
insert(2, optimalValuesFilter.
at<
Pose3>(2));
751 Values partialValuesFilter;
752 partialValuesFilter.
insert(2, optimalValuesFilter.
at<
Pose3>(2));
753 partialValuesFilter.
insert(3, optimalValuesFilter.
at<
Pose3>(3));
757 orderingFilter.push_back(3);
758 orderingFilter.push_back(2);
761 keysToMarginalize.push_back(3);
763 expectedFilterSummarization = CalculateMarginals(partialGraphFilter, partialValuesFilter, keysToMarginalize);
768 Values actualSmootherValues, actualFilterSeparatorValues;
770 filter.
synchronize(smootherSummarization, smootherSeparatorValues);
780 CHECK(
assert_equal(expectedFilterSeparatorValues, actualFilterSeparatorValues, 1
e-6));
787 std::cout <<
"*********************** synchronize_5 ************************" << std::endl;
801 NonlinearFactor::shared_ptr factor4(
new BetweenFactor<Pose3>(2, 3, poseOdometry, noiseOdometery));
802 NonlinearFactor::shared_ptr factor5(
new BetweenFactor<Pose3>(3, 4, poseOdometry, noiseOdometery));
815 newValues.
insert(1, value1);
816 newValues.
insert(2, value2);
817 newValues.
insert(3, value3);
818 newValues.
insert(4, value4);
821 keysToMove.push_back(1);
823 filter.
update(newFactors, newValues, keysToMove);
827 Values smootherSeparatorValues;
832 Values actualSmootherValues, actualFilterSeparatorValues;
834 filter.
synchronize(smootherSummarization, smootherSeparatorValues);
840 expectedSmootherFactors.
push_back(factor1);
841 expectedSmootherFactors.
push_back(factor2);
843 Values optimalValues = BatchOptimize(newFactors, newValues, 1);
844 Values expectedSmootherValues;
857 keysToMove2.push_back(2);
861 filter.
update(newFactors, newValues, keysToMove2);
866 Values smootherSeparatorValues2;
883 partialValues.
insert(2, optimalValues.
at(2));
889 keysToMarginalize.push_back(1);
891 smootherSummarization2 = CalculateMarginals(partialGraph, partialValues, keysToMarginalize);
892 smootherSeparatorValues2.
insert(2, partialValues.
at(2));
898 Values actualSmootherValues2, actualFilterSeparatorValues2;
900 filter.
synchronize(smootherSummarization2, smootherSeparatorValues2);
906 expectedSmootherFactors2.
push_back(factor3);
907 expectedSmootherFactors2.
push_back(factor4);
909 Values expectedSmootherValues2;
910 expectedSmootherValues2.
insert(2, optimalValues.
at(2));
925 Values partialValuesFilter;
926 partialValuesFilter.
insert(3, optimalValues2.
at(3));
927 partialValuesFilter.
insert(4, optimalValues2.
at(4));
930 keysToMarginalize2.push_back(4);
932 NonlinearFactorGraph expectedFilterSummarization2 = CalculateMarginals(partialGraphFilter, partialValuesFilter, keysToMarginalize2);
933 Values expectedFilterSeparatorValues2;
934 expectedFilterSeparatorValues2.
insert(3, optimalValues2.
at(3));
937 CHECK(
assert_equal(expectedFilterSeparatorValues2, actualFilterSeparatorValues2, 1
e-6));
942 partialGraphTransition.
push_back(factor3);
943 partialGraphTransition.
push_back(factor4);
944 partialGraphTransition.
push_back(smootherSummarization2);
946 Values partialValuesTransition;
947 partialValuesTransition.
insert(2,optimalValues.
at(2));
948 partialValuesTransition.
insert(3,optimalValues2.
at(3));
951 keysToMarginalize3.push_back(2);
960 NonlinearFactor::shared_ptr factorEmpty;
961 expectedFilterGraph.
push_back(factorEmpty);
962 expectedFilterGraph.
push_back(factorEmpty);
963 expectedFilterGraph.
push_back(factorEmpty);
964 expectedFilterGraph.
push_back(factorEmpty);
966 expectedFilterGraph.
push_back(CalculateMarginals(partialGraphTransition, partialValuesTransition, keysToMarginalize3));
982 NonlinearFactor::shared_ptr factor4(
new BetweenFactor<Pose3>(2, 3, poseOdometry, noiseOdometery));
995 newValues.
insert(1, value1);
996 newValues.
insert(2, value2);
997 newValues.
insert(3, value3);
1002 ordering.push_back(1);
1003 ordering.push_back(2);
1004 ordering.push_back(3);
1014 for(
const GaussianFactor::shared_ptr& factor: result) {
1020 keysToMarginalize.push_back(1);
1022 actualMarginals = CalculateMarginals(factorGraph, newValues, keysToMarginalize);
1035 NonlinearFactor::shared_ptr factor4(
new BetweenFactor<Pose3>(2, 3, poseOdometry, noiseOdometery));
1048 newValues.
insert(1, value1);
1049 newValues.
insert(2, value2);
1050 newValues.
insert(3, value3);
1055 ordering.push_back(1);
1056 ordering.push_back(2);
1057 ordering.push_back(3);
1067 for(
const GaussianFactor::shared_ptr& factor: result) {
1073 keysToMarginalize.push_back(1);
1074 keysToMarginalize.push_back(2);
1076 actualMarginals = CalculateMarginals(factorGraph, newValues, keysToMarginalize);
1085 std::cout <<
"*********************** removeFactors_topology_1 ************************" << std::endl;
1093 newFactors.
addPrior(1, poseInitial, noisePrior);
1109 filter.
update(newFactors, newValues, keysToMove);
1114 std::vector<size_t> removeFactorIndices(1,1);
1119 filter.
update(noFactors, noValues, keysToMove, removeFactorIndices);
1124 expectedGraph.
addPrior(1, poseInitial, noisePrior);
1127 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
1138 std::cout <<
"*********************** removeFactors_topology_2 ************************" << std::endl;
1148 newFactors.
addPrior(1, poseInitial, noisePrior);
1164 filter.
update(newFactors, newValues, keysToMove);
1169 std::vector<size_t> removeFactorIndices(1,4);
1174 filter.
update(noFactors, noValues, keysToMove, removeFactorIndices);
1179 expectedGraph.
addPrior(1, poseInitial, noisePrior);
1185 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
1194 std::cout <<
"*********************** removeFactors_topology_3 ************************" << std::endl;
1203 newFactors.
addPrior(1, poseInitial, noisePrior);
1204 newFactors.
addPrior(1, poseInitial, noisePrior);
1219 filter.
update(newFactors, newValues, keysToMove);
1224 std::vector<size_t> removeFactorIndices(1,0);
1229 filter.
update(noFactors, noValues, keysToMove, removeFactorIndices);
1235 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
1236 expectedGraph.
addPrior(1, poseInitial, noisePrior);
1247 std::cout <<
"*********************** removeFactors_values ************************" << std::endl;
1256 newFactors.
addPrior(1, poseInitial, noisePrior);
1272 filter.
update(newFactors, newValues, keysToMove);
1277 std::vector<size_t> removeFactorIndices(1,4);
1282 filter.
update(noFactors, noValues, keysToMove, removeFactorIndices);
1289 expectedGraph.
addPrior(1, poseInitial, noisePrior);
1295 expectedGraph.
push_back(NonlinearFactor::shared_ptr());
1298 Values expectedValues = BatchOptimize(expectedGraph, newValues);
const gtsam::Symbol key('X', 0)
Provides additional testing facilities for common data structures.
Values calculateEstimate() const
A Levenberg-Marquardt Batch Filter that implements the Concurrent Filtering and Smoothing interface...
std::pair< std::shared_ptr< BayesTreeType >, std::shared_ptr< FactorGraphType > > eliminatePartialMultifrontal(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
const NonlinearFactorGraph & getFactors() const
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
static int runAllTests(TestResult &result)
A non-templated config holding any types of Manifold-group elements.
Wrap Jacobian and Hessian linear factors to allow simple injection into a nonlinear graph...
Factor Graph consisting of non-linear factors.
const ValueType at(Key j) const
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
virtual Result update(const NonlinearFactorGraph &newFactors=NonlinearFactorGraph(), const Values &newTheta=Values(), const std::optional< FastList< Key > > &keysToMove={}, const std::optional< std::vector< size_t > > &removeFactorIndices={})
const Values & getLinearizationPoint() const
NonlinearFactorGraph graph
static enum @1107 ordering
T compose(const T &t1, const T &t2)
std::shared_ptr< GaussianFactorGraph > linearize(const Values &linearizationPoint) const
Linearize a nonlinear factor graph.
static std::shared_ptr< Ordering > getOrdering(const vector< GeneralCamera > &cameras, const vector< Point3 > &landmarks)
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
static NonlinearFactorGraph expectedGraph(const SharedNoiseModel &model)
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
std::pair< std::shared_ptr< GaussianConditional >, std::shared_ptr< HessianFactor > > EliminateCholesky(const GaussianFactorGraph &factors, const Ordering &keys)
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Class compose(const Class &g) const
static ConjugateGradientParameters parameters
noiseModel::Diagonal::shared_ptr SharedDiagonal
virtual void resize(size_t size)
TEST(ConcurrentBatchFilter, equals)
void insert(Key j, const Value &val)
void getSmootherFactors(NonlinearFactorGraph &smootherFactors, Values &smootherValues) override
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
void synchronize(const NonlinearFactorGraph &smootherSummarization, const Values &smootherSummarizationValues) override
void getSummarizedFactors(NonlinearFactorGraph &filterSummarization, Values &filterSummarizationValues) override
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
iterator erase(iterator item)
std::uint64_t Key
Integer nonlinear key type.
size_t maxIterations
The maximum iterations to stop iterating (default 100)