From bb15697cf9d23a4eb2da87675573e7b3f2b4136f Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Tue, 27 Oct 2009 20:23:19 +0000 Subject: [PATCH] Prototype framework for Bayes chain (new name for Chordal Bayes net) and Bayes tree class hierarchy --- .cproject | 41 +++++++++++++++++---------- cpp/BayesChain.h | 28 +++++++++++++++++++ cpp/BayesTree.h | 44 +++++++++++++++++++++++++++++ cpp/Makefile.am | 16 +++++++---- cpp/SymbolicBayesChain-inl.h | 17 ++++++++++++ cpp/SymbolicBayesChain.cpp | 43 ++++++++++++++++++++++++++++ cpp/SymbolicBayesChain.h | 51 ++++++++++++++++++++++++++++++++++ cpp/testBayesTree.cpp | 28 +++++++++++++++++++ cpp/testSymbolicBayesChain.cpp | 26 +++++++++++++++++ 9 files changed, 274 insertions(+), 20 deletions(-) create mode 100644 cpp/BayesChain.h create mode 100644 cpp/BayesTree.h create mode 100644 cpp/SymbolicBayesChain-inl.h create mode 100644 cpp/SymbolicBayesChain.cpp create mode 100644 cpp/SymbolicBayesChain.h create mode 100644 cpp/testBayesTree.cpp create mode 100644 cpp/testSymbolicBayesChain.cpp diff --git a/.cproject b/.cproject index b7c7c5027..d80047a9d 100644 --- a/.cproject +++ b/.cproject @@ -300,7 +300,6 @@ make - install true true @@ -308,7 +307,6 @@ make - check true true @@ -316,6 +314,7 @@ make + check true true @@ -323,7 +322,6 @@ make - testSimpleCamera.run true true @@ -339,6 +337,7 @@ make + testVSLAMFactor.run true true @@ -346,7 +345,6 @@ make - testCalibratedCamera.run true true @@ -354,6 +352,7 @@ make + testConditionalGaussian.run true true @@ -361,7 +360,6 @@ make - testPose2.run true true @@ -377,7 +375,6 @@ make - testRot3.run true true @@ -385,6 +382,7 @@ make + testNonlinearOptimizer.run true true @@ -392,7 +390,6 @@ make - testLinearFactor.run true true @@ -400,7 +397,6 @@ make - testConstrainedNonlinearFactorGraph.run true true @@ -408,7 +404,6 @@ make - testLinearFactorGraph.run true true @@ -416,6 +411,7 @@ make + testNonlinearFactorGraph.run true true @@ -423,7 +419,6 @@ make - testPose3.run true true @@ -431,6 +426,7 @@ make + testConstrainedLinearFactorGraph.run true true @@ -438,6 +434,7 @@ make + testVectorConfig.run true true @@ -445,6 +442,7 @@ make + testPoint2.run true true @@ -452,7 +450,6 @@ make - testNonlinearFactor.run true true @@ -460,7 +457,6 @@ make - timeLinearFactor.run true true @@ -468,7 +464,6 @@ make - timeLinearFactorGraph.run true true @@ -476,14 +471,30 @@ make - testChordalBayesNet.run true true true + +make + +testBayesTree.run +true +false +true + + +make + +testSymbolicBayesChain.run +true +false +true + make + install true true @@ -491,6 +502,7 @@ make + clean true true @@ -498,6 +510,7 @@ make + check true true diff --git a/cpp/BayesChain.h b/cpp/BayesChain.h new file mode 100644 index 000000000..8d90e9726 --- /dev/null +++ b/cpp/BayesChain.h @@ -0,0 +1,28 @@ +/** + * @file BayesChain + * @brief Bayes Chain, the result of eliminating a factor graph + * @author Frank Dellaert + */ + +// \callgraph + +#pragma once + +#include +#include +#include + +namespace gtsam { + + /** + * Bayes Chain, the result of eliminating a factor graph + * This is the base class for SymbolicBayesChain, DiscreteBayesChain, and GaussianBayesChain + * Corresponding to what is used for the "Conditional" template argument: + * a ConditionalProbabilityTable, a ConditionalGaussian, or a SymbolicConditional. + */ + template + class BayesChain { + public: + }; + +} /// namespace gtsam diff --git a/cpp/BayesTree.h b/cpp/BayesTree.h new file mode 100644 index 000000000..a81d20ef2 --- /dev/null +++ b/cpp/BayesTree.h @@ -0,0 +1,44 @@ +/** + * @file BayesTree + * @brief Bayes Tree is a tree of cliques of a Bayes Chain + * @author Frank Dellaert + */ + +// \callgraph + +#pragma once + +#include +#include +#include + +#include "Testable.h" +#include "BayesChain.h" + +namespace gtsam { + +/** + * Bayes tree + * Templated on the Conditional class, the type of node in the underlying Bayes chain. + * This could be a ConditionalProbabilityTable, a ConditionalGaussian, or a SymbolicConditional + */ +template +class BayesTree : public Testable > +{ +public: + + /** Create a Bayes Tree from a SymbolicBayesChain */ + BayesTree(BayesChain& bayesChain) {} + + /** Destructor */ + virtual ~BayesTree() {} + + /** print */ + void print(const std::string& s = "") const {} + + /** check equality */ + bool equals(const BayesTree& other, double tol = 1e-9) const { return false; } + +}; // BayesTree + +} /// namespace gtsam diff --git a/cpp/Makefile.am b/cpp/Makefile.am index aab0c2880..e0d65aa38 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -86,25 +86,29 @@ timeLinearFactorGraph: CXXFLAGS += -I /opt/local/include -I .. timeLinearFactorGraph: LDFLAGS += SmallExample.o -L.libs -lgtsam -L../CppUnitLite -lCppUnitLite # graphs -sources += ChordalBayesNet.cpp sources += LinearFactorGraph.cpp +sources += SymbolicBayesChain.cpp ChordalBayesNet.cpp sources += ConstrainedNonlinearFactorGraph.cpp ConstrainedLinearFactorGraph.cpp -check_PROGRAMS += testChordalBayesNet testFactorgraph -check_PROGRAMS += testLinearFactorGraph testNonlinearFactorGraph testNonlinearOptimizer +check_PROGRAMS += testFactorgraph testLinearFactorGraph testNonlinearFactorGraph testNonlinearOptimizer +check_PROGRAMS += testSymbolicBayesChain testChordalBayesNet testBayesTree check_PROGRAMS += testConstrainedNonlinearFactorGraph testConstrainedLinearFactorGraph -testChordalBayesNet_SOURCES = $(example) testChordalBayesNet.cpp testFactorgraph_SOURCES = testFactorgraph.cpp testLinearFactorGraph_SOURCES = $(example) testLinearFactorGraph.cpp testNonlinearFactorGraph_SOURCES = $(example) testNonlinearFactorGraph.cpp -testNonlinearOptimizer_SOURCES = $(example) testNonlinearOptimizer.cpp +testNonlinearOptimizer_SOURCES = $(example) testNonlinearOptimizer.cpp +testSymbolicBayesChain_SOURCES = $(example) testSymbolicBayesChain.cpp +testChordalBayesNet_SOURCES = $(example) testChordalBayesNet.cpp +testBayesTree_SOURCES = $(example) testBayesTree.cpp testConstrainedNonlinearFactorGraph_SOURCES = $(example) testConstrainedNonlinearFactorGraph.cpp testConstrainedLinearFactorGraph_SOURCES = $(example) testConstrainedLinearFactorGraph.cpp -testChordalBayesNet_LDADD = libgtsam.la testFactorgraph_LDADD = libgtsam.la testLinearFactorGraph_LDADD = libgtsam.la testNonlinearFactorGraph_LDADD = libgtsam.la testNonlinearOptimizer_LDADD = libgtsam.la +testSymbolicBayesChain_LDADD = libgtsam.la +testChordalBayesNet_LDADD = libgtsam.la +testBayesTree_LDADD = libgtsam.la testConstrainedNonlinearFactorGraph_LDADD = libgtsam.la testConstrainedLinearFactorGraph_LDADD = libgtsam.la diff --git a/cpp/SymbolicBayesChain-inl.h b/cpp/SymbolicBayesChain-inl.h new file mode 100644 index 000000000..ebdb25b1d --- /dev/null +++ b/cpp/SymbolicBayesChain-inl.h @@ -0,0 +1,17 @@ +/** + * @file SymbolicBayesChain-inl.h + * @brief Template definitions for SymbolicBayesChain + * @author Frank Dellaert + */ + +#include "SymbolicBayesChain.h" + +using namespace std; +using namespace gtsam; + +/* ************************************************************************* */ +template +SymbolicBayesChain::SymbolicBayesChain(const FactorGraph& factorGraph) { +} + +/* ************************************************************************* */ diff --git a/cpp/SymbolicBayesChain.cpp b/cpp/SymbolicBayesChain.cpp new file mode 100644 index 000000000..58e8625ed --- /dev/null +++ b/cpp/SymbolicBayesChain.cpp @@ -0,0 +1,43 @@ +/** + * @file SymbolicBayesChain.cpp + * @brief Chordal Bayes Net, the result of eliminating a factor graph + * @author Frank Dellaert + */ + +#include +#include +#include + +#include "SymbolicBayesChain.h" + +using namespace std; +using namespace gtsam; + +// trick from some reading group +#define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL) + +/* ************************************************************************* */ +void SymbolicBayesChain::print(const string& s) const { + // BOOST_FOREACH(string key, keys) { + // const_iterator it = nodes.find(key); + // it->second->print("\nNode[" + key + "]"); + // } +} + +/* ************************************************************************* */ +bool SymbolicBayesChain::equals(const SymbolicBayesChain& cbn, double tol) const { + // const_iterator it1 = nodes.begin(), it2 = cbn.nodes.begin(); + // + // if(nodes.size() != cbn.nodes.size()) return false; + // for(; it1 != nodes.end(); it1++, it2++){ + // const string& j1 = it1->first, j2 = it2->first; + // ConditionalGaussian::shared_ptr node1 = it1->second, node2 = it2->second; + // if (j1 != j2) return false; + // if (!node1->equals(*node2,tol)) + // return false; + // } + // return true; + return false; +} + +/* ************************************************************************* */ diff --git a/cpp/SymbolicBayesChain.h b/cpp/SymbolicBayesChain.h new file mode 100644 index 000000000..271be6d89 --- /dev/null +++ b/cpp/SymbolicBayesChain.h @@ -0,0 +1,51 @@ +/** + * @file SymbolicBayesChain.h + * @brief Symbolic Chordal Bayes Net, the result of eliminating a factor graph + * @author Frank Dellaert + */ + +// \callgraph + +#pragma once + +#include +#include +#include + +#include "Testable.h" +#include "BayesChain.h" +#include "FactorGraph.h" + +namespace gtsam { + + /** + * Conditional node for use in a symbolic Bayes chain + */ + class SymbolicConditional { + }; + + /** + * Symbolic Bayes Chain, the (symbolic) result of eliminating a factor graph + */ + class SymbolicBayesChain: public BayesChain , + public Testable { + public: + + /** + * Construct from any factor graph + */ + template + SymbolicBayesChain(const FactorGraph& factorGraph); + + /** Destructor */ + virtual ~SymbolicBayesChain() { + } + + /** print */ + void print(const std::string& s = "") const; + + /** check equality */ + bool equals(const SymbolicBayesChain& other, double tol = 1e-9) const; + }; + +} /// namespace gtsam diff --git a/cpp/testBayesTree.cpp b/cpp/testBayesTree.cpp new file mode 100644 index 000000000..fa5223fd5 --- /dev/null +++ b/cpp/testBayesTree.cpp @@ -0,0 +1,28 @@ +/** + * @file testBayesTree.cpp + * @brief Unit tests for Bayes Tree + * @author Frank Dellaert + */ + +#include + +#include "SymbolicBayesChain-inl.h" +#include "smallExample.h" +#include "BayesTree.h" + +using namespace gtsam; + +/* ************************************************************************* */ +TEST( BayesTree, constructor ) +{ + LinearFactorGraph factorGraph = createLinearFactorGraph(); + SymbolicBayesChain symbolicBayesChain(factorGraph); + BayesTree bayesTree(symbolicBayesChain); +} + +/* ************************************************************************* */ +int main() { + TestResult tr; + return TestRegistry::runAllTests(tr); +} +/* ************************************************************************* */ diff --git a/cpp/testSymbolicBayesChain.cpp b/cpp/testSymbolicBayesChain.cpp new file mode 100644 index 000000000..705ba90b4 --- /dev/null +++ b/cpp/testSymbolicBayesChain.cpp @@ -0,0 +1,26 @@ +/** + * @file testSymbolicBayesChain.cpp + * @brief Unit tests for a symbolic Bayes chain + * @author Frank Dellaert + */ + +#include + +#include "smallExample.h" +#include "SymbolicBayesChain-inl.h" + +using namespace gtsam; + +/* ************************************************************************* */ +TEST( SymbolicBayesChain, constructor ) +{ + LinearFactorGraph factorGraph = createLinearFactorGraph(); + SymbolicBayesChain symbolicChordalBayesNet(factorGraph); +} + +/* ************************************************************************* */ +int main() { + TestResult tr; + return TestRegistry::runAllTests(tr); +} +/* ************************************************************************* */