43 using namespace gtsam;
61 linearizationPoint.insert<
double>(
X(0), 0);
83 auto f0 = std::make_shared<PriorFactor<Pose2>>(
84 1,
Pose2(), noiseModel::Isotropic::Sigma(3, 0.001));
88 auto f1 = std::make_shared<BetweenFactor<Pose2>>(
89 1, 2,
Pose2(), noiseModel::Isotropic::Sigma(3, 0.1));
102 auto nonlinearFactor = std::make_shared<BetweenFactor<double>>();
105 auto discreteFactor = std::make_shared<DecisionTreeFactor>();
108 auto dcFactor = std::make_shared<MixtureFactor>();
123 auto nonlinearFactor = std::make_shared<BetweenFactor<double>>(
124 X(0),
X(1), 0.0, Isotropic::Sigma(1, 0.1));
126 auto discreteFactor = std::make_shared<DecisionTreeFactor>();
130 auto noise_model = noiseModel::Isotropic::Sigma(1, 1.0);
131 auto still = std::make_shared<MotionModel>(
X(0),
X(1), 0.0, noise_model),
132 moving = std::make_shared<MotionModel>(
X(0),
X(1), 1.0, noise_model);
134 std::vector<MotionModel::shared_ptr> components = {still, moving};
135 auto dcFactor = std::make_shared<MixtureFactor>(
139 Values linearizationPoint;
140 linearizationPoint.
insert<
double>(
X(0), 0);
141 linearizationPoint.
insert<
double>(
X(1), 1);
157 auto nonlinearFactor = std::make_shared<BetweenFactor<double>>(
158 X(0),
X(1), 0.0, Isotropic::Sigma(1, 0.1));
159 auto discreteFactor = std::make_shared<DecisionTreeFactor>();
161 auto noise_model = noiseModel::Isotropic::Sigma(1, 1.0);
162 auto still = std::make_shared<MotionModel>(
X(0),
X(1), 0.0, noise_model),
163 moving = std::make_shared<MotionModel>(
X(0),
X(1), 1.0, noise_model);
165 std::vector<MotionModel::shared_ptr> components = {still, moving};
173 contKeys = {
X(0),
X(1),
X(2)};
184 auto nonlinearFactor = std::make_shared<BetweenFactor<double>>();
191 auto discreteFactor = std::make_shared<DecisionTreeFactor>();
192 fg.push_back(discreteFactor);
198 auto dcFactor = std::make_shared<MixtureFactor>();
199 fg.push_back(dcFactor);
206 auto gaussianFactor = std::make_shared<JacobianFactor>();
212 ghfg.push_back(discreteFactor);
217 ghfg.push_back(dcFactor);
220 hgfg2.
push_back(ghfg.begin(), ghfg.end());
226 auto noise = noiseModel::Isotropic::Sigma(3, 1.0);
255 *
self.nonlinearFactorGraph.linearize(
self.linearizationPoint);
268 for (
size_t k = 0; k <
self.K; k++) ordering.push_back(
X(k));
278 TEST(GaussianElimination, Eliminate_x0) {
284 factors.
push_back(
self.linearizedFactorGraph[0]);
286 factors.push_back(
self.linearizedFactorGraph[1]);
303 TEST(HybridsGaussianElimination, Eliminate_x1) {
308 factors.
push_back(
self.linearizedFactorGraph[1]);
309 factors.push_back(
self.linearizedFactorGraph[2]);
326 Values linearizationPoint) {
328 graph.
addPrior<
double>(
X(0), 0, Isotropic::Sigma(1, 0.1));
330 auto between_x0_x1 = std::make_shared<MotionModel>(
331 X(0),
X(1),
between, Isotropic::Sigma(1, 1.0));
335 return graph.
linearize(linearizationPoint);
341 TEST(HybridGaussianElimination, EliminateHybrid_2_Variable) {
344 auto factors =
self.linearizedFactorGraph;
349 const auto [hybridConditionalMixture, factorOnModes] =
352 auto gaussianConditionalMixture =
353 dynamic_pointer_cast<
GaussianMixture>(hybridConditionalMixture->inner());
355 CHECK(gaussianConditionalMixture);
363 CHECK(discreteFactor);
365 EXPECT(discreteFactor->root_->isLeaf() ==
false);
378 for (
size_t k = 0; k <
self.K; k++) ordering.push_back(
X(k));
381 const auto [hybridBayesNet, remainingFactorGraph] =
382 linearizedFactorGraph.eliminatePartialSequential(ordering);
384 CHECK(hybridBayesNet);
393 CHECK(remainingFactorGraph);
414 for (
size_t k = 0; k <
self.K; k++) ordering.push_back(
X(k));
417 const auto [hybridBayesNet_partial, remainingFactorGraph_partial] =
418 linearizedFactorGraph.eliminatePartialSequential(ordering);
422 for (
auto& factor : (*remainingFactorGraph_partial)) {
429 for (
size_t k = 0; k <
self.K - 1; k++) ordering.push_back(
M(k));
436 for (
size_t k = 0; k <
self.K; k++) ordering.push_back(
X(k));
437 for (
size_t k = 0; k <
self.K - 1; k++) ordering.push_back(
M(k));
441 linearizedFactorGraph.eliminateSequential(ordering);
443 CHECK(hybridBayesNet);
458 dynamic_pointer_cast<DiscreteConditional>(hybridBayesNet->at(3)->inner())
464 dynamic_pointer_cast<DiscreteConditional>(hybridBayesNet->at(4)->inner())
478 for (
size_t k = 0; k <
self.K; k++) ordering.push_back(
X(k));
481 const auto [hybridBayesNet, remainingFactorGraph] =
482 linearizedFactorGraph.eliminatePartialSequential(ordering);
484 string expected_hybridFactorGraph = R
"( 555 factor 6: P( m1 | m0 ): 568 string expected_hybridBayesNet = R
"( 570 conditional 0: Hybrid P( x0 | x1 m0) 571 Discrete Keys = (m0, 2), 575 S[x1] = [ -0.0995037 ] 581 S[x1] = [ -0.0995037 ] 585 conditional 1: Hybrid P( x1 | x2 m0 m1) 586 Discrete Keys = (m0, 2), (m1, 2), 591 S[x2] = [ -0.0990196 ] 597 S[x2] = [ -0.0990196 ] 604 S[x2] = [ -0.0990196 ] 610 S[x2] = [ -0.0990196 ] 614 conditional 2: Hybrid P( x2 | m0 m1) 615 Discrete Keys = (m0, 2), (m1, 2), 664 auto priorNoise = noiseModel::Diagonal::Sigmas(
673 auto noise_model = noiseModel::Isotropic::Sigma(3, 1.0);
674 auto still = std::make_shared<PlanarMotionModel>(
X(0),
X(1),
Pose2(0, 0, 0),
676 moving = std::make_shared<PlanarMotionModel>(
X(0),
X(1),
odometry,
678 std::vector<PlanarMotionModel::shared_ptr> motion_models = {still, moving};
684 auto measurementNoise = noiseModel::Diagonal::Sigmas(
687 Rot2 bearing11 = Rot2::fromDegrees(45), bearing22 = Rot2::fromDegrees(90);
688 double range11 =
std::sqrt(4.0 + 4.0), range22 = 2.0;
692 X(0),
L(0), bearing11, range11, measurementNoise);
694 X(1),
L(1), bearing22, range22, measurementNoise);
698 initialEstimate.insert(
X(0),
Pose2(0.5, 0.0, 0.2));
699 initialEstimate.insert(
X(1),
Pose2(2.3, 0.1, -0.2));
700 initialEstimate.insert(
L(0),
Point2(1.8, 2.1));
701 initialEstimate.insert(
L(1),
Point2(4.1, 1.8));
709 const auto [hybridBayesNet, remainingFactorGraph] =
Provides additional testing facilities for common data structures.
const FastVector< sharedNode > & roots() const
Matrix< RealScalar, Dynamic, Dynamic > M
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
Nonlinear hybrid factor graph that uses type erasure.
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
static int runAllTests(TestResult &result)
T between(const T &t1, const T &t2)
Factor Graph consisting of non-linear factors.
bool assert_print_equal(const std::string &expected, const V &actual, const std::string &s="")
Eigen::Matrix< double, 1, 1 > Vector1
Point2 prior(const Point2 &x)
Prior on a single pose.
EIGEN_DONT_INLINE Scalar zero()
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)
std::pair< std::shared_ptr< BayesNetType >, std::shared_ptr< FactorGraphType > > eliminatePartialSequential(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
const GaussianFactorGraph factors
Nonlinear Mixture factor of continuous and discrete.
NonlinearFactorGraph graph
static enum @1107 ordering
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
std::shared_ptr< GaussianFactorGraph > linearize(const Values &linearizationPoint) const
Linearize a nonlinear factor graph.
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
HybridGaussianFactorGraph linearizedFactorGraph
A conditional of gaussian mixtures indexed by discrete variables, as part of a Bayes Network...
#define THROWS_EXCEPTION(condition)
GaussianFactorGraph::shared_ptr batchGFG(double between, Values linearizationPoint)
#define EXPECT(condition)
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Linear Factor Graph where all factors are Gaussians.
std::shared_ptr< This > shared_ptr
shared_ptr to this class
NonlinearFactorGraph graph2()
std::pair< HybridConditional::shared_ptr, std::shared_ptr< Factor > > EliminateHybrid(const HybridGaussianFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for HybridGaussianFactorGraph.
virtual void resize(size_t size)
#define EXPECT_LONGS_EQUAL(expected, actual)
Point2 f1(const Point3 &p, OptionalJacobian< 2, 3 > H)
const sharedFactor at(size_t i) const
std::pair< Key, size_t > DiscreteKey
std::shared_ptr< HybridGaussianFactorGraph > linearize(const Values &continuousValues) const
Linearize all the continuous factors in the HybridNonlinearFactorGraph.
Chordal Bayes Net, the result of eliminating a factor graph.
void insert(Key j, const Value &val)
Jet< T, N > sqrt(const Jet< T, N > &f)
Pose2 odometry(2.0, 0.0, 0.0)
Implementation of a discrete conditional mixture factor.
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
TEST(HybridFactorGraph, GaussianFactorGraph)
double error(const HybridValues &values) const
std::shared_ptr< HybridBayesNet > shared_ptr
DiscreteKeys is a set of keys that can be assembled using the & operator.