Renamed double-templated functions to _eliminate and _eliminateOne, and created FactorGraph-specific eliminateOne methods to make life easier
parent
df3e5f2416
commit
cc5a2c3183
|
@ -54,7 +54,7 @@ namespace gtsam {
|
||||||
sharedBayesNet p_S_R(new BayesNet<Conditional>);
|
sharedBayesNet p_S_R(new BayesNet<Conditional>);
|
||||||
if (parent_==NULL || parent_->parent_==NULL) return p_S_R;
|
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();
|
sharedBayesNet p_Sp_R = parent_->shortcut();
|
||||||
|
|
||||||
return p_S_R;
|
return p_S_R;
|
||||||
|
@ -160,7 +160,7 @@ namespace gtsam {
|
||||||
ordering.reverse();
|
ordering.reverse();
|
||||||
|
|
||||||
// eliminate to get marginal
|
// 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
|
return chordalBayesNet->back(); // the root is the marginal
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,8 +83,7 @@ GaussianBayesNet::shared_ptr ConstrainedLinearFactorGraph::eliminate(const Order
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ConditionalGaussian::shared_ptr cg =
|
ConditionalGaussian::shared_ptr cg = eliminateOne(key);
|
||||||
eliminateOne<LinearFactor,ConditionalGaussian>(*this,key);
|
|
||||||
cbn->push_back(cg);
|
cbn->push_back(cg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,7 +221,7 @@ FactorGraph<Factor>::removeAndCombineFactors(const string& key)
|
||||||
/* eliminate one node from the factor graph */
|
/* eliminate one node from the factor graph */
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
template<class Factor,class Conditional>
|
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
|
// combine the factors of all nodes connected to the variable to be eliminated
|
||||||
// if no factors are connected to key, returns an empty factor
|
// 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>
|
template<class Factor,class Conditional>
|
||||||
boost::shared_ptr<BayesNet<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::shared_ptr<BayesNet<Conditional> > bayesNet (new BayesNet<Conditional>()); // empty
|
||||||
|
|
||||||
BOOST_FOREACH(string key, ordering) {
|
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);
|
bayesNet->push_back(cg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -130,7 +130,7 @@ namespace gtsam {
|
||||||
* and adds a new factor on the separator to the factor graph
|
* and adds a new factor on the separator to the factor graph
|
||||||
*/
|
*/
|
||||||
template<class Factor, class Conditional>
|
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)
|
* eliminate factor graph using the given (not necessarily complete)
|
||||||
|
@ -138,7 +138,7 @@ namespace gtsam {
|
||||||
*/
|
*/
|
||||||
template<class Factor, class Conditional>
|
template<class Factor, class Conditional>
|
||||||
boost::shared_ptr<BayesNet<Conditional> >
|
boost::shared_ptr<BayesNet<Conditional> >
|
||||||
eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering);
|
_eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering);
|
||||||
|
|
||||||
} // namespace gtsam
|
} // namespace gtsam
|
||||||
|
|
||||||
|
|
|
@ -46,8 +46,7 @@ LinearFactorGraph::eliminate(const Ordering& ordering)
|
||||||
{
|
{
|
||||||
GaussianBayesNet::shared_ptr chordalBayesNet (new GaussianBayesNet()); // empty
|
GaussianBayesNet::shared_ptr chordalBayesNet (new GaussianBayesNet()); // empty
|
||||||
BOOST_FOREACH(string key, ordering) {
|
BOOST_FOREACH(string key, ordering) {
|
||||||
ConditionalGaussian::shared_ptr cg =
|
ConditionalGaussian::shared_ptr cg = eliminateOne(key);
|
||||||
eliminateOne<LinearFactor,ConditionalGaussian>(*this, key);
|
|
||||||
chordalBayesNet->push_back(cg);
|
chordalBayesNet->push_back(cg);
|
||||||
}
|
}
|
||||||
return chordalBayesNet;
|
return chordalBayesNet;
|
||||||
|
|
|
@ -61,6 +61,15 @@ namespace gtsam {
|
||||||
*/
|
*/
|
||||||
std::set<std::string> find_separator(const std::string& key) const;
|
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
|
* eliminate factor graph in place(!) in the given order, yielding
|
||||||
* a chordal Bayes net. Allows for passing an incomplete ordering
|
* a chordal Bayes net. Allows for passing an incomplete ordering
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace gtsam {
|
||||||
|
|
||||||
BOOST_FOREACH(string key, ordering) {
|
BOOST_FOREACH(string key, ordering) {
|
||||||
SymbolicConditional::shared_ptr conditional =
|
SymbolicConditional::shared_ptr conditional =
|
||||||
eliminateOne<SymbolicFactor,SymbolicConditional>(*this,key);
|
_eliminateOne<SymbolicFactor,SymbolicConditional>(*this,key);
|
||||||
bayesNet->push_back(conditional);
|
bayesNet->push_back(conditional);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
class SymbolicBayesNet;
|
class SymbolicBayesNet;
|
||||||
|
class SymbolicConditional;
|
||||||
|
|
||||||
/** Symbolic Factor Graph */
|
/** Symbolic Factor Graph */
|
||||||
class SymbolicFactorGraph: public FactorGraph<SymbolicFactor> {
|
class SymbolicFactorGraph: public FactorGraph<SymbolicFactor> {
|
||||||
|
@ -41,6 +42,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
|
* eliminate factor graph in place(!) in the given order, yielding
|
||||||
* a chordal Bayes net
|
* a chordal Bayes net
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -256,8 +256,7 @@ TEST( ConstrainedLinearFactorGraph, eliminate_multi_constraint )
|
||||||
CHECK(fg.nrFactors() == 0);
|
CHECK(fg.nrFactors() == 0);
|
||||||
|
|
||||||
// eliminate the linear factor
|
// eliminate the linear factor
|
||||||
ConditionalGaussian::shared_ptr cg3 =
|
ConditionalGaussian::shared_ptr cg3 = fg.eliminateOne("z");
|
||||||
eliminateOne<LinearFactor,ConditionalGaussian>(fg,"z");
|
|
||||||
CHECK(cg3->nrParents() == 0);
|
CHECK(cg3->nrParents() == 0);
|
||||||
CHECK(fg.size() == 0);
|
CHECK(fg.size() == 0);
|
||||||
|
|
||||||
|
|
|
@ -190,8 +190,7 @@ TEST( LinearFactorGraph, combine_factors_x2 )
|
||||||
TEST( LinearFactorGraph, eliminateOne_x1 )
|
TEST( LinearFactorGraph, eliminateOne_x1 )
|
||||||
{
|
{
|
||||||
LinearFactorGraph fg = createLinearFactorGraph();
|
LinearFactorGraph fg = createLinearFactorGraph();
|
||||||
ConditionalGaussian::shared_ptr actual =
|
ConditionalGaussian::shared_ptr actual = fg.eliminateOne("x1");
|
||||||
eliminateOne<LinearFactor,ConditionalGaussian>(fg,"x1");
|
|
||||||
|
|
||||||
// create expected Conditional Gaussian
|
// create expected Conditional Gaussian
|
||||||
Matrix R11 = Matrix_(2,2,
|
Matrix R11 = Matrix_(2,2,
|
||||||
|
@ -219,8 +218,7 @@ TEST( LinearFactorGraph, eliminateOne_x1 )
|
||||||
TEST( LinearFactorGraph, eliminateOne_x2 )
|
TEST( LinearFactorGraph, eliminateOne_x2 )
|
||||||
{
|
{
|
||||||
LinearFactorGraph fg = createLinearFactorGraph();
|
LinearFactorGraph fg = createLinearFactorGraph();
|
||||||
ConditionalGaussian::shared_ptr actual =
|
ConditionalGaussian::shared_ptr actual = fg.eliminateOne("x2");
|
||||||
eliminateOne<LinearFactor,ConditionalGaussian>(fg,"x2");
|
|
||||||
|
|
||||||
// create expected Conditional Gaussian
|
// create expected Conditional Gaussian
|
||||||
Matrix R11 = Matrix_(2,2,
|
Matrix R11 = Matrix_(2,2,
|
||||||
|
@ -247,8 +245,7 @@ TEST( LinearFactorGraph, eliminateOne_x2 )
|
||||||
TEST( LinearFactorGraph, eliminateOne_l1 )
|
TEST( LinearFactorGraph, eliminateOne_l1 )
|
||||||
{
|
{
|
||||||
LinearFactorGraph fg = createLinearFactorGraph();
|
LinearFactorGraph fg = createLinearFactorGraph();
|
||||||
ConditionalGaussian::shared_ptr actual =
|
ConditionalGaussian::shared_ptr actual = fg.eliminateOne("l1");
|
||||||
eliminateOne<LinearFactor,ConditionalGaussian>(fg,"l1");
|
|
||||||
|
|
||||||
// create expected Conditional Gaussian
|
// create expected Conditional Gaussian
|
||||||
Matrix R11 = Matrix_(2,2,
|
Matrix R11 = Matrix_(2,2,
|
||||||
|
@ -424,7 +421,7 @@ TEST( LinearFactorGraph, CONSTRUCTOR_GaussianBayesNet )
|
||||||
// Base FactorGraph only
|
// Base FactorGraph only
|
||||||
FactorGraph<LinearFactor> fg3(*CBN);
|
FactorGraph<LinearFactor> fg3(*CBN);
|
||||||
boost::shared_ptr<BayesNet<ConditionalGaussian> > CBN3 =
|
boost::shared_ptr<BayesNet<ConditionalGaussian> > CBN3 =
|
||||||
eliminate<LinearFactor,ConditionalGaussian>(fg3,ord);
|
_eliminate<LinearFactor,ConditionalGaussian>(fg3,ord);
|
||||||
CHECK(CBN->equals(*CBN3));
|
CHECK(CBN->equals(*CBN3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,8 +111,7 @@ TEST( LinearFactorGraph, eliminateOne )
|
||||||
SymbolicFactorGraph fg(factorGraph);
|
SymbolicFactorGraph fg(factorGraph);
|
||||||
|
|
||||||
// eliminate
|
// eliminate
|
||||||
SymbolicConditional::shared_ptr actual =
|
SymbolicConditional::shared_ptr actual = fg.eliminateOne("x1");
|
||||||
eliminateOne<SymbolicFactor,SymbolicConditional>(fg,"x1");
|
|
||||||
|
|
||||||
// create expected symbolic Conditional
|
// create expected symbolic Conditional
|
||||||
SymbolicConditional expected("x1","l1","x2");
|
SymbolicConditional expected("x1","l1","x2");
|
||||||
|
|
Loading…
Reference in New Issue