Renamed double-templated functions to _eliminate and _eliminateOne, and created FactorGraph-specific eliminateOne methods to make life easier

release/4.3a0
Frank Dellaert 2009-11-07 21:03:30 +00:00
parent df3e5f2416
commit cc5a2c3183
11 changed files with 35 additions and 23 deletions

View File

@ -54,7 +54,7 @@ namespace gtsam {
sharedBayesNet p_S_R(new BayesNet<Conditional>);
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<Factor,Conditional>(graph,ordering);
sharedBayesNet chordalBayesNet = _eliminate<Factor,Conditional>(graph,ordering);
return chordalBayesNet->back(); // the root is the marginal
}

View File

@ -83,8 +83,7 @@ GaussianBayesNet::shared_ptr ConstrainedLinearFactorGraph::eliminate(const Order
}
else
{
ConditionalGaussian::shared_ptr cg =
eliminateOne<LinearFactor,ConditionalGaussian>(*this,key);
ConditionalGaussian::shared_ptr cg = eliminateOne(key);
cbn->push_back(cg);
}
}

View File

@ -221,7 +221,7 @@ FactorGraph<Factor>::removeAndCombineFactors(const string& key)
/* eliminate one node from the factor graph */
/* ************************************************************************* */
template<class Factor,class Conditional>
boost::shared_ptr<Conditional> eliminateOne(FactorGraph<Factor>& graph, const string& key) {
boost::shared_ptr<Conditional> _eliminateOne(FactorGraph<Factor>& 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<Conditional> eliminateOne(FactorGraph<Factor>& graph, const st
/* ************************************************************************* */
template<class Factor,class Conditional>
boost::shared_ptr<BayesNet<Conditional> >
eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering)
_eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering)
{
boost::shared_ptr<BayesNet<Conditional> > bayesNet (new BayesNet<Conditional>()); // empty
BOOST_FOREACH(string key, ordering) {
boost::shared_ptr<Conditional> cg = eliminateOne<Factor,Conditional>(factorGraph,key);
boost::shared_ptr<Conditional> cg = _eliminateOne<Factor,Conditional>(factorGraph,key);
bayesNet->push_back(cg);
}

View File

@ -130,7 +130,7 @@ namespace gtsam {
* and adds a new factor on the separator to the factor graph
*/
template<class Factor, class Conditional>
boost::shared_ptr<Conditional> eliminateOne(FactorGraph<Factor>& factorGraph, const std::string& key);
boost::shared_ptr<Conditional> _eliminateOne(FactorGraph<Factor>& factorGraph, const std::string& key);
/**
* eliminate factor graph using the given (not necessarily complete)
@ -138,7 +138,7 @@ namespace gtsam {
*/
template<class Factor, class Conditional>
boost::shared_ptr<BayesNet<Conditional> >
eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering);
_eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering);
} // namespace gtsam

View File

@ -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<LinearFactor,ConditionalGaussian>(*this, key);
ConditionalGaussian::shared_ptr cg = eliminateOne(key);
chordalBayesNet->push_back(cg);
}
return chordalBayesNet;

View File

@ -61,6 +61,15 @@ namespace gtsam {
*/
std::set<std::string> 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<LinearFactor,ConditionalGaussian>(*this, key);
}
/**
* eliminate factor graph in place(!) in the given order, yielding
* a chordal Bayes net. Allows for passing an incomplete ordering

View File

@ -26,7 +26,7 @@ namespace gtsam {
BOOST_FOREACH(string key, ordering) {
SymbolicConditional::shared_ptr conditional =
eliminateOne<SymbolicFactor,SymbolicConditional>(*this,key);
_eliminateOne<SymbolicFactor,SymbolicConditional>(*this,key);
bayesNet->push_back(conditional);
}

View File

@ -16,6 +16,7 @@
namespace gtsam {
class SymbolicBayesNet;
class SymbolicConditional;
/** Symbolic Factor Graph */
class SymbolicFactorGraph: public FactorGraph<SymbolicFactor> {
@ -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<SymbolicConditional> eliminateOne(const std::string& key){
return _eliminateOne<SymbolicFactor,SymbolicConditional>(*this, key);
}
/**
* eliminate factor graph in place(!) in the given order, yielding
* a chordal Bayes net

View File

@ -256,8 +256,7 @@ TEST( ConstrainedLinearFactorGraph, eliminate_multi_constraint )
CHECK(fg.nrFactors() == 0);
// eliminate the linear factor
ConditionalGaussian::shared_ptr cg3 =
eliminateOne<LinearFactor,ConditionalGaussian>(fg,"z");
ConditionalGaussian::shared_ptr cg3 = fg.eliminateOne("z");
CHECK(cg3->nrParents() == 0);
CHECK(fg.size() == 0);

View File

@ -190,8 +190,7 @@ TEST( LinearFactorGraph, combine_factors_x2 )
TEST( LinearFactorGraph, eliminateOne_x1 )
{
LinearFactorGraph fg = createLinearFactorGraph();
ConditionalGaussian::shared_ptr actual =
eliminateOne<LinearFactor,ConditionalGaussian>(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<LinearFactor,ConditionalGaussian>(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<LinearFactor,ConditionalGaussian>(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<LinearFactor> fg3(*CBN);
boost::shared_ptr<BayesNet<ConditionalGaussian> > CBN3 =
eliminate<LinearFactor,ConditionalGaussian>(fg3,ord);
_eliminate<LinearFactor,ConditionalGaussian>(fg3,ord);
CHECK(CBN->equals(*CBN3));
}

View File

@ -111,8 +111,7 @@ TEST( LinearFactorGraph, eliminateOne )
SymbolicFactorGraph fg(factorGraph);
// eliminate
SymbolicConditional::shared_ptr actual =
eliminateOne<SymbolicFactor,SymbolicConditional>(fg,"x1");
SymbolicConditional::shared_ptr actual = fg.eliminateOne("x1");
// create expected symbolic Conditional
SymbolicConditional expected("x1","l1","x2");