From f67220773aa1bf39cd52815cc55730469245f941 Mon Sep 17 00:00:00 2001 From: Kai Ni Date: Mon, 12 Jul 2010 21:34:03 +0000 Subject: [PATCH] split JunctionTree to GaussianJunctionTree --- inference/GaussianFactorGraph.cpp | 2 +- inference/GaussianJunctionTree-inl.h | 55 +++++++++++++++++++ inference/GaussianJunctionTree.h | 42 ++++++++++++++ inference/JunctionTree-inl.h | 34 ------------ inference/JunctionTree.h | 29 +--------- inference/Makefile.am | 3 +- ...nTree.cpp => testGaussianJunctionTree.cpp} | 2 +- tests/Makefile.am | 2 +- ...nTree.cpp => testGaussianJunctionTree.cpp} | 2 +- 9 files changed, 104 insertions(+), 67 deletions(-) create mode 100644 inference/GaussianJunctionTree-inl.h create mode 100644 inference/GaussianJunctionTree.h rename inference/{testJunctionTree.cpp => testGaussianJunctionTree.cpp} (99%) rename tests/{testJunctionTree.cpp => testGaussianJunctionTree.cpp} (98%) diff --git a/inference/GaussianFactorGraph.cpp b/inference/GaussianFactorGraph.cpp index 3a4eb3430..00dad4a11 100644 --- a/inference/GaussianFactorGraph.cpp +++ b/inference/GaussianFactorGraph.cpp @@ -16,7 +16,7 @@ #include "FactorGraph-inl.h" #include "inference-inl.h" #include "iterative.h" -#include "JunctionTree-inl.h" +#include "GaussianJunctionTree-inl.h" using namespace std; using namespace gtsam; diff --git a/inference/GaussianJunctionTree-inl.h b/inference/GaussianJunctionTree-inl.h new file mode 100644 index 000000000..86c2a3562 --- /dev/null +++ b/inference/GaussianJunctionTree-inl.h @@ -0,0 +1,55 @@ +/* + * GaussianJunctionTree-inl.h + * + * Created on: Jul 12, 2010 + * Author: nikai + * Description: the Gaussian junction tree + */ + +#pragma once + +#include + +#include "JunctionTree-inl.h" +#include "GaussianJunctionTree.h" + +namespace gtsam { + + using namespace std; + + /* ************************************************************************* */ + /** + * GaussianJunctionTree + */ + template + void GaussianJunctionTree::btreeBackSubstitue(typename BayesTree::sharedClique current, VectorConfig& config) { + // solve the bayes net in the current node + typename BayesNet::const_reverse_iterator it = current->rbegin(); + for (; it!=current->rend(); it++) { + Vector x = (*it)->solve(config); // Solve for that variable + config.insert((*it)->key(),x); // store result in partial solution + } + + // solve the bayes nets in the child nodes + typedef typename BayesTree::sharedClique sharedBayesClique; + BOOST_FOREACH(sharedBayesClique child, current->children_) { + btreeBackSubstitue(child, config); + } + } + + /* ************************************************************************* */ + template + VectorConfig GaussianJunctionTree::optimize() { + // eliminate from leaves to the root + typedef JunctionTree Base; + BayesTree bayesTree; + this->eliminate(); + + // back-substitution + VectorConfig result; + btreeBackSubstitue(bayesTree.root(), result); + return result; + } + + +} //namespace gtsam diff --git a/inference/GaussianJunctionTree.h b/inference/GaussianJunctionTree.h new file mode 100644 index 000000000..742cf56bb --- /dev/null +++ b/inference/GaussianJunctionTree.h @@ -0,0 +1,42 @@ +/* + * GaussianJunctionTree.h + * + * Created on: Jul 12, 2010 + * Author: nikai + * Description: the Gaussian junction tree + */ + +#pragma once + +#include "JunctionTree.h" +#include "GaussianConditional.h" +#include "GaussianFactorGraph.h" + +namespace gtsam { + + /* ************************************************************************* */ + /** + * GaussianJunctionTree that does the optimization + */ + template + class GaussianJunctionTree: public JunctionTree { + public: + typedef JunctionTree Base; + typedef typename JunctionTree::sharedClique sharedClique; + + protected: + // back-substitute in topological sort order (parents first) + void btreeBackSubstitue(typename BayesTree::sharedClique current, VectorConfig& config); + + public : + + GaussianJunctionTree() : Base() {} + + // constructor + GaussianJunctionTree(FG& fg, const Ordering& ordering) : Base(fg, ordering) {} + + // optimize the linear graph + VectorConfig optimize(); + }; // GaussianJunctionTree + +} // namespace gtsam diff --git a/inference/JunctionTree-inl.h b/inference/JunctionTree-inl.h index 82f96033d..bb2ae5033 100644 --- a/inference/JunctionTree-inl.h +++ b/inference/JunctionTree-inl.h @@ -156,38 +156,4 @@ namespace gtsam { return root_->equals(*other.root_); } - /* ************************************************************************* */ - /** - * GaussianJunctionTree - */ - template - void GaussianJunctionTree::btreeBackSubstitue(typename BayesTree::sharedClique current, VectorConfig& config) { - // solve the bayes net in the current node - typename BayesNet::const_reverse_iterator it = current->rbegin(); - for (; it!=current->rend(); it++) { - Vector x = (*it)->solve(config); // Solve for that variable - config.insert((*it)->key(),x); // store result in partial solution - } - - // solve the bayes nets in the child nodes - typedef typename BayesTree::sharedClique sharedBayesClique; - BOOST_FOREACH(sharedBayesClique child, current->children_) { - btreeBackSubstitue(child, config); - } - } - - /* ************************************************************************* */ - template - VectorConfig GaussianJunctionTree::optimize() { - // eliminate from leaves to the root - typedef JunctionTree Base; - BayesTree bayesTree; - this->eliminate(); - - // back-substitution - VectorConfig result; - btreeBackSubstitue(bayesTree.root(), result); - return result; - } - } //namespace gtsam diff --git a/inference/JunctionTree.h b/inference/JunctionTree.h index 22a192e5a..69cb977c3 100644 --- a/inference/JunctionTree.h +++ b/inference/JunctionTree.h @@ -10,10 +10,8 @@ #include #include -#include -#include "GaussianConditional.h" -#include "GaussianFactorGraph.h" #include "BayesTree.h" +#include "SymbolicConditional.h" namespace gtsam { @@ -102,29 +100,4 @@ namespace gtsam { }; // JunctionTree - /* ************************************************************************* */ - /** - * GaussianJunctionTree that does the optimization - */ - template - class GaussianJunctionTree: public JunctionTree { - public: - typedef JunctionTree Base; - typedef typename JunctionTree::sharedClique sharedClique; - - protected: - // back-substitute in topological sort order (parents first) - void btreeBackSubstitue(typename BayesTree::sharedClique current, VectorConfig& config); - - public : - - GaussianJunctionTree() : Base() {} - - // constructor - GaussianJunctionTree(FG& fg, const Ordering& ordering) : Base(fg, ordering) {} - - // optimize the linear graph - VectorConfig optimize(); - }; // GaussianJunctionTree - } // namespace gtsam diff --git a/inference/Makefile.am b/inference/Makefile.am index cd4e5cffd..f2d4a9d2c 100644 --- a/inference/Makefile.am +++ b/inference/Makefile.am @@ -25,11 +25,12 @@ headers += inference.h inference-inl.h headers += graph.h graph-inl.h headers += FactorGraph.h FactorGraph-inl.h headers += JunctionTree.h JunctionTree-inl.h +headers += GaussianJunctionTree.h GaussianJunctionTree-inl.h headers += BayesNet.h BayesNet-inl.h headers += BayesTree.h BayesTree-inl.h headers += ISAM.h ISAM-inl.h headers += ISAM2.h ISAM2-inl.h -check_PROGRAMS += testFactorGraph testJunctionTree testOrdering +check_PROGRAMS += testFactorGraph testGaussianJunctionTree testOrdering check_PROGRAMS += testBayesTree testISAM #---------------------------------------------------------------------------------------------------- diff --git a/inference/testJunctionTree.cpp b/inference/testGaussianJunctionTree.cpp similarity index 99% rename from inference/testJunctionTree.cpp rename to inference/testGaussianJunctionTree.cpp index 2a813e7f2..4955feafd 100644 --- a/inference/testJunctionTree.cpp +++ b/inference/testGaussianJunctionTree.cpp @@ -16,7 +16,7 @@ using namespace boost::assign; #define GTSAM_MAGIC_KEY -#include "JunctionTree-inl.h" +#include "GaussianJunctionTree-inl.h" using namespace std; using namespace gtsam; diff --git a/tests/Makefile.am b/tests/Makefile.am index 155aeadfd..7a995d7bb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,7 +6,7 @@ check_PROGRAMS = testBayesNetPreconditioner testConstraintOptimizer check_PROGRAMS += testGaussianBayesNet testGaussianFactor testGaussianFactorGraph check_PROGRAMS += testGaussianISAM testGaussianISAM2 testGraph -check_PROGRAMS += testInference testIterative testJunctionTree +check_PROGRAMS += testInference testIterative testGaussianJunctionTree check_PROGRAMS += testNonlinearEquality testNonlinearFactor testNonlinearFactorGraph check_PROGRAMS += testNonlinearOptimizer testSQP testSubgraphPreconditioner check_PROGRAMS += testSymbolicBayesNet testSymbolicFactorGraph testTupleConfig diff --git a/tests/testJunctionTree.cpp b/tests/testGaussianJunctionTree.cpp similarity index 98% rename from tests/testJunctionTree.cpp rename to tests/testGaussianJunctionTree.cpp index 84c8b30f2..40df0fed1 100644 --- a/tests/testJunctionTree.cpp +++ b/tests/testGaussianJunctionTree.cpp @@ -17,7 +17,7 @@ using namespace boost::assign; #define GTSAM_MAGIC_KEY #include "smallExample.h" -#include "JunctionTree-inl.h" +#include "GaussianJunctionTree-inl.h" using namespace std; using namespace gtsam;