From cc5a2c31834dab84f13c38e3f3b9f6e549572cf4 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 7 Nov 2009 21:03:30 +0000 Subject: [PATCH] Renamed double-templated functions to _eliminate and _eliminateOne, and created FactorGraph-specific eliminateOne methods to make life easier --- cpp/BayesTree-inl.h | 4 ++-- cpp/ConstrainedLinearFactorGraph.cpp | 3 +-- cpp/FactorGraph-inl.h | 6 +++--- cpp/FactorGraph.h | 4 ++-- cpp/LinearFactorGraph.cpp | 3 +-- cpp/LinearFactorGraph.h | 9 +++++++++ cpp/SymbolicFactorGraph.cpp | 2 +- cpp/SymbolicFactorGraph.h | 10 ++++++++++ cpp/testConstrainedLinearFactorGraph.cpp | 3 +-- cpp/testLinearFactorGraph.cpp | 11 ++++------- cpp/testSymbolicFactorGraph.cpp | 3 +-- 11 files changed, 35 insertions(+), 23 deletions(-) diff --git a/cpp/BayesTree-inl.h b/cpp/BayesTree-inl.h index 536de14ab..ba776cb9a 100644 --- a/cpp/BayesTree-inl.h +++ b/cpp/BayesTree-inl.h @@ -54,7 +54,7 @@ namespace gtsam { sharedBayesNet p_S_R(new BayesNet); if (parent_==NULL || parent_->parent_==NULL) return p_S_R; - // If not, calculate the parent shortcut P(S_p|R) + // If not the base case, calculate the parent shortcut P(S_p|R) sharedBayesNet p_Sp_R = parent_->shortcut(); return p_S_R; @@ -160,7 +160,7 @@ namespace gtsam { ordering.reverse(); // eliminate to get marginal - sharedBayesNet chordalBayesNet = eliminate(graph,ordering); + sharedBayesNet chordalBayesNet = _eliminate(graph,ordering); return chordalBayesNet->back(); // the root is the marginal } diff --git a/cpp/ConstrainedLinearFactorGraph.cpp b/cpp/ConstrainedLinearFactorGraph.cpp index 544ab5b87..bfb8dbc6f 100644 --- a/cpp/ConstrainedLinearFactorGraph.cpp +++ b/cpp/ConstrainedLinearFactorGraph.cpp @@ -83,8 +83,7 @@ GaussianBayesNet::shared_ptr ConstrainedLinearFactorGraph::eliminate(const Order } else { - ConditionalGaussian::shared_ptr cg = - eliminateOne(*this,key); + ConditionalGaussian::shared_ptr cg = eliminateOne(key); cbn->push_back(cg); } } diff --git a/cpp/FactorGraph-inl.h b/cpp/FactorGraph-inl.h index f17355125..7040d61e3 100644 --- a/cpp/FactorGraph-inl.h +++ b/cpp/FactorGraph-inl.h @@ -221,7 +221,7 @@ FactorGraph::removeAndCombineFactors(const string& key) /* eliminate one node from the factor graph */ /* ************************************************************************* */ template -boost::shared_ptr eliminateOne(FactorGraph& graph, const string& key) { +boost::shared_ptr _eliminateOne(FactorGraph& graph, const string& key) { // combine the factors of all nodes connected to the variable to be eliminated // if no factors are connected to key, returns an empty factor @@ -247,12 +247,12 @@ boost::shared_ptr eliminateOne(FactorGraph& graph, const st /* ************************************************************************* */ template boost::shared_ptr > -eliminate(FactorGraph& factorGraph, const Ordering& ordering) +_eliminate(FactorGraph& factorGraph, const Ordering& ordering) { boost::shared_ptr > bayesNet (new BayesNet()); // empty BOOST_FOREACH(string key, ordering) { - boost::shared_ptr cg = eliminateOne(factorGraph,key); + boost::shared_ptr cg = _eliminateOne(factorGraph,key); bayesNet->push_back(cg); } diff --git a/cpp/FactorGraph.h b/cpp/FactorGraph.h index 5b00a3bdb..e04d0819b 100644 --- a/cpp/FactorGraph.h +++ b/cpp/FactorGraph.h @@ -130,7 +130,7 @@ namespace gtsam { * and adds a new factor on the separator to the factor graph */ template - boost::shared_ptr eliminateOne(FactorGraph& factorGraph, const std::string& key); + boost::shared_ptr _eliminateOne(FactorGraph& factorGraph, const std::string& key); /** * eliminate factor graph using the given (not necessarily complete) @@ -138,7 +138,7 @@ namespace gtsam { */ template boost::shared_ptr > - eliminate(FactorGraph& factorGraph, const Ordering& ordering); + _eliminate(FactorGraph& factorGraph, const Ordering& ordering); } // namespace gtsam diff --git a/cpp/LinearFactorGraph.cpp b/cpp/LinearFactorGraph.cpp index 476678140..4e1a4c729 100644 --- a/cpp/LinearFactorGraph.cpp +++ b/cpp/LinearFactorGraph.cpp @@ -46,8 +46,7 @@ LinearFactorGraph::eliminate(const Ordering& ordering) { GaussianBayesNet::shared_ptr chordalBayesNet (new GaussianBayesNet()); // empty BOOST_FOREACH(string key, ordering) { - ConditionalGaussian::shared_ptr cg = - eliminateOne(*this, key); + ConditionalGaussian::shared_ptr cg = eliminateOne(key); chordalBayesNet->push_back(cg); } return chordalBayesNet; diff --git a/cpp/LinearFactorGraph.h b/cpp/LinearFactorGraph.h index 1aa88b4eb..d7a56c7c5 100644 --- a/cpp/LinearFactorGraph.h +++ b/cpp/LinearFactorGraph.h @@ -61,6 +61,15 @@ namespace gtsam { */ std::set find_separator(const std::string& key) const; + /** + * Eliminate a single node yielding a conditional Gaussian + * Eliminates the factors from the factor graph through findAndRemoveFactors + * and adds a new factor on the separator to the factor graph + */ + inline ConditionalGaussian::shared_ptr eliminateOne(const std::string& key){ + return _eliminateOne(*this, key); + } + /** * eliminate factor graph in place(!) in the given order, yielding * a chordal Bayes net. Allows for passing an incomplete ordering diff --git a/cpp/SymbolicFactorGraph.cpp b/cpp/SymbolicFactorGraph.cpp index 6c8306d63..afe83cccd 100644 --- a/cpp/SymbolicFactorGraph.cpp +++ b/cpp/SymbolicFactorGraph.cpp @@ -26,7 +26,7 @@ namespace gtsam { BOOST_FOREACH(string key, ordering) { SymbolicConditional::shared_ptr conditional = - eliminateOne(*this,key); + _eliminateOne(*this,key); bayesNet->push_back(conditional); } diff --git a/cpp/SymbolicFactorGraph.h b/cpp/SymbolicFactorGraph.h index e1ab5c9ec..b6ac3948d 100644 --- a/cpp/SymbolicFactorGraph.h +++ b/cpp/SymbolicFactorGraph.h @@ -16,6 +16,7 @@ namespace gtsam { class SymbolicBayesNet; + class SymbolicConditional; /** Symbolic Factor Graph */ class SymbolicFactorGraph: public FactorGraph { @@ -40,6 +41,15 @@ namespace gtsam { } } + /** + * Eliminate a single node yielding a conditional Gaussian + * Eliminates the factors from the factor graph through findAndRemoveFactors + * and adds a new factor on the separator to the factor graph + */ + inline boost::shared_ptr eliminateOne(const std::string& key){ + return _eliminateOne(*this, key); + } + /** * eliminate factor graph in place(!) in the given order, yielding * a chordal Bayes net diff --git a/cpp/testConstrainedLinearFactorGraph.cpp b/cpp/testConstrainedLinearFactorGraph.cpp index 70ca52df8..a372b4a10 100644 --- a/cpp/testConstrainedLinearFactorGraph.cpp +++ b/cpp/testConstrainedLinearFactorGraph.cpp @@ -256,8 +256,7 @@ TEST( ConstrainedLinearFactorGraph, eliminate_multi_constraint ) CHECK(fg.nrFactors() == 0); // eliminate the linear factor - ConditionalGaussian::shared_ptr cg3 = - eliminateOne(fg,"z"); + ConditionalGaussian::shared_ptr cg3 = fg.eliminateOne("z"); CHECK(cg3->nrParents() == 0); CHECK(fg.size() == 0); diff --git a/cpp/testLinearFactorGraph.cpp b/cpp/testLinearFactorGraph.cpp index 90bec931a..816748ff6 100644 --- a/cpp/testLinearFactorGraph.cpp +++ b/cpp/testLinearFactorGraph.cpp @@ -190,8 +190,7 @@ TEST( LinearFactorGraph, combine_factors_x2 ) TEST( LinearFactorGraph, eliminateOne_x1 ) { LinearFactorGraph fg = createLinearFactorGraph(); - ConditionalGaussian::shared_ptr actual = - eliminateOne(fg,"x1"); + ConditionalGaussian::shared_ptr actual = fg.eliminateOne("x1"); // create expected Conditional Gaussian Matrix R11 = Matrix_(2,2, @@ -219,8 +218,7 @@ TEST( LinearFactorGraph, eliminateOne_x1 ) TEST( LinearFactorGraph, eliminateOne_x2 ) { LinearFactorGraph fg = createLinearFactorGraph(); - ConditionalGaussian::shared_ptr actual = - eliminateOne(fg,"x2"); + ConditionalGaussian::shared_ptr actual = fg.eliminateOne("x2"); // create expected Conditional Gaussian Matrix R11 = Matrix_(2,2, @@ -247,8 +245,7 @@ TEST( LinearFactorGraph, eliminateOne_x2 ) TEST( LinearFactorGraph, eliminateOne_l1 ) { LinearFactorGraph fg = createLinearFactorGraph(); - ConditionalGaussian::shared_ptr actual = - eliminateOne(fg,"l1"); + ConditionalGaussian::shared_ptr actual = fg.eliminateOne("l1"); // create expected Conditional Gaussian Matrix R11 = Matrix_(2,2, @@ -424,7 +421,7 @@ TEST( LinearFactorGraph, CONSTRUCTOR_GaussianBayesNet ) // Base FactorGraph only FactorGraph fg3(*CBN); boost::shared_ptr > CBN3 = - eliminate(fg3,ord); + _eliminate(fg3,ord); CHECK(CBN->equals(*CBN3)); } diff --git a/cpp/testSymbolicFactorGraph.cpp b/cpp/testSymbolicFactorGraph.cpp index 4ce1258d4..460a5011c 100644 --- a/cpp/testSymbolicFactorGraph.cpp +++ b/cpp/testSymbolicFactorGraph.cpp @@ -111,8 +111,7 @@ TEST( LinearFactorGraph, eliminateOne ) SymbolicFactorGraph fg(factorGraph); // eliminate - SymbolicConditional::shared_ptr actual = - eliminateOne(fg,"x1"); + SymbolicConditional::shared_ptr actual = fg.eliminateOne("x1"); // create expected symbolic Conditional SymbolicConditional expected("x1","l1","x2");