BIG: eliminate and eliminateOne now doubly templated functions, not methods.

Minor: Standardized on new shared_ptr naming convention:
shared_factor -> sharedFactor
conditional_ptr -> sharedConditional
node_ptr -> sharedClique
release/4.3a0
Frank Dellaert 2009-11-07 19:31:39 +00:00
parent f5d186522e
commit df3e5f2416
13 changed files with 110 additions and 107 deletions

View File

@ -300,7 +300,6 @@
<buildTargets> <buildTargets>
<target name="install" path="wrap" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="install" path="wrap" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>install</buildTarget> <buildTarget>install</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -308,7 +307,6 @@
</target> </target>
<target name="check" path="wrap" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="check" path="wrap" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>check</buildTarget> <buildTarget>check</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -316,7 +314,7 @@
</target> </target>
<target name="check" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="check" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments>-k</buildArguments> <buildArguments/>
<buildTarget>check</buildTarget> <buildTarget>check</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -324,7 +322,6 @@
</target> </target>
<target name="testSimpleCamera.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testSimpleCamera.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSimpleCamera.run</buildTarget> <buildTarget>testSimpleCamera.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -340,6 +337,7 @@
</target> </target>
<target name="testVSLAMFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testVSLAMFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testVSLAMFactor.run</buildTarget> <buildTarget>testVSLAMFactor.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -347,7 +345,6 @@
</target> </target>
<target name="testCalibratedCamera.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testCalibratedCamera.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testCalibratedCamera.run</buildTarget> <buildTarget>testCalibratedCamera.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -355,6 +352,7 @@
</target> </target>
<target name="testConditionalGaussian.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testConditionalGaussian.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testConditionalGaussian.run</buildTarget> <buildTarget>testConditionalGaussian.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -362,7 +360,6 @@
</target> </target>
<target name="testPose2.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testPose2.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testPose2.run</buildTarget> <buildTarget>testPose2.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -378,7 +375,6 @@
</target> </target>
<target name="testRot3.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testRot3.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testRot3.run</buildTarget> <buildTarget>testRot3.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -386,6 +382,7 @@
</target> </target>
<target name="testNonlinearOptimizer.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testNonlinearOptimizer.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testNonlinearOptimizer.run</buildTarget> <buildTarget>testNonlinearOptimizer.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -393,7 +390,6 @@
</target> </target>
<target name="testLinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testLinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testLinearFactor.run</buildTarget> <buildTarget>testLinearFactor.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -401,7 +397,6 @@
</target> </target>
<target name="testConstrainedNonlinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testConstrainedNonlinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testConstrainedNonlinearFactorGraph.run</buildTarget> <buildTarget>testConstrainedNonlinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -409,7 +404,6 @@
</target> </target>
<target name="testLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testLinearFactorGraph.run</buildTarget> <buildTarget>testLinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -417,6 +411,7 @@
</target> </target>
<target name="testNonlinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testNonlinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testNonlinearFactorGraph.run</buildTarget> <buildTarget>testNonlinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -424,7 +419,6 @@
</target> </target>
<target name="testPose3.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testPose3.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testPose3.run</buildTarget> <buildTarget>testPose3.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -432,6 +426,7 @@
</target> </target>
<target name="testConstrainedLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testConstrainedLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testConstrainedLinearFactorGraph.run</buildTarget> <buildTarget>testConstrainedLinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -439,6 +434,7 @@
</target> </target>
<target name="testVectorConfig.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testVectorConfig.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testVectorConfig.run</buildTarget> <buildTarget>testVectorConfig.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -446,6 +442,7 @@
</target> </target>
<target name="testPoint2.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testPoint2.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testPoint2.run</buildTarget> <buildTarget>testPoint2.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -453,7 +450,6 @@
</target> </target>
<target name="testNonlinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testNonlinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testNonlinearFactor.run</buildTarget> <buildTarget>testNonlinearFactor.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -461,7 +457,6 @@
</target> </target>
<target name="timeLinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="timeLinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>timeLinearFactor.run</buildTarget> <buildTarget>timeLinearFactor.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -469,7 +464,6 @@
</target> </target>
<target name="timeLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="timeLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>timeLinearFactorGraph.run</buildTarget> <buildTarget>timeLinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -477,7 +471,6 @@
</target> </target>
<target name="testGaussianBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testGaussianBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testGaussianBayesNet.run</buildTarget> <buildTarget>testGaussianBayesNet.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -485,6 +478,7 @@
</target> </target>
<target name="testBayesTree.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testBayesTree.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testBayesTree.run</buildTarget> <buildTarget>testBayesTree.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -492,7 +486,6 @@
</target> </target>
<target name="testSymbolicBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testSymbolicBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSymbolicBayesNet.run</buildTarget> <buildTarget>testSymbolicBayesNet.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -500,6 +493,7 @@
</target> </target>
<target name="testSymbolicFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testSymbolicFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSymbolicFactorGraph.run</buildTarget> <buildTarget>testSymbolicFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -507,7 +501,6 @@
</target> </target>
<target name="testVector.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testVector.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testVector.run</buildTarget> <buildTarget>testVector.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -515,7 +508,6 @@
</target> </target>
<target name="testMatrix.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testMatrix.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testMatrix.run</buildTarget> <buildTarget>testMatrix.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -523,6 +515,7 @@
</target> </target>
<target name="install" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="install" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>install</buildTarget> <buildTarget>install</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -530,6 +523,7 @@
</target> </target>
<target name="clean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="clean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>clean</buildTarget> <buildTarget>clean</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
@ -537,6 +531,7 @@
</target> </target>
<target name="check" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="check" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>check</buildTarget> <buildTarget>check</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>

View File

@ -23,7 +23,7 @@ namespace gtsam {
void BayesNet<Conditional>::print(const string& s) const { void BayesNet<Conditional>::print(const string& s) const {
cout << s << ":\n"; cout << s << ":\n";
std::string key; std::string key;
BOOST_FOREACH(conditional_ptr conditional,conditionals_) BOOST_FOREACH(sharedConditional conditional,conditionals_)
conditional->print("Node[" + conditional->key() + "]"); conditional->print("Node[" + conditional->key() + "]");
} }
@ -38,26 +38,17 @@ namespace gtsam {
template<class Conditional> template<class Conditional>
Ordering BayesNet<Conditional>::ordering() const { Ordering BayesNet<Conditional>::ordering() const {
Ordering ord; Ordering ord;
BOOST_FOREACH(conditional_ptr conditional,conditionals_) BOOST_FOREACH(sharedConditional conditional,conditionals_)
ord.push_back(conditional->key()); ord.push_back(conditional->key());
return ord; return ord;
} }
/* ************************************************************************* */ /* ************************************************************************* */
// predicate to check whether a conditional has the sought key
template<class Conditional>
class HasKey {
const string& key_;
public:
HasKey(const std::string& key):key_(key) {}
bool operator()(const boost::shared_ptr<Conditional>& conditional) {
return (conditional->key()==key_);
}
};
template<class Conditional> template<class Conditional>
boost::shared_ptr<Conditional> BayesNet<Conditional>::operator[](const std::string& key) const { typename BayesNet<Conditional>::sharedConditional
const_iterator it = find_if(conditionals_.begin(),conditionals_.end(),HasKey<Conditional>(key)); BayesNet<Conditional>::operator[](const std::string& key) const {
const_iterator it = find_if(conditionals_.begin(),conditionals_.end(),onKey<Conditional>(key));
if (it == conditionals_.end()) throw(invalid_argument( if (it == conditionals_.end()) throw(invalid_argument(
"BayesNet::operator['"+key+"']: not found")); "BayesNet::operator['"+key+"']: not found"));
return *it; return *it;

View File

@ -31,8 +31,8 @@ namespace gtsam {
public: public:
/** We store shared pointers to Conditional densities */ /** We store shared pointers to Conditional densities */
typedef typename boost::shared_ptr<Conditional> conditional_ptr; typedef typename boost::shared_ptr<Conditional> sharedConditional;
typedef typename std::list<conditional_ptr> Conditionals; typedef typename std::list<sharedConditional> Conditionals;
typedef typename Conditionals::const_iterator const_iterator; typedef typename Conditionals::const_iterator const_iterator;
typedef typename Conditionals::const_reverse_iterator const_reverse_iterator; typedef typename Conditionals::const_reverse_iterator const_reverse_iterator;
@ -55,12 +55,12 @@ namespace gtsam {
bool equals(const BayesNet& other, double tol = 1e-9) const; bool equals(const BayesNet& other, double tol = 1e-9) const;
/** push_back: use reverse topological sort (i.e. parents last / elimination order) */ /** push_back: use reverse topological sort (i.e. parents last / elimination order) */
inline void push_back(const boost::shared_ptr<Conditional>& conditional) { inline void push_back(const sharedConditional& conditional) {
conditionals_.push_back(conditional); conditionals_.push_back(conditional);
} }
/** push_front: use topological sort (i.e. parents first / reverse elimination order) */ /** push_front: use topological sort (i.e. parents first / reverse elimination order) */
inline void push_front(const boost::shared_ptr<Conditional>& conditional) { inline void push_front(const sharedConditional& conditional) {
conditionals_.push_front(conditional); conditionals_.push_front(conditional);
} }
@ -73,9 +73,9 @@ namespace gtsam {
Ordering ordering() const; Ordering ordering() const;
/** SLOW O(n) random access to Conditional by key */ /** SLOW O(n) random access to Conditional by key */
conditional_ptr operator[](const std::string& key) const; sharedConditional operator[](const std::string& key) const;
inline conditional_ptr back() { return conditionals_.back(); } inline sharedConditional back() { return conditionals_.back(); }
/** return iterators. FD: breaks encapsulation? */ /** return iterators. FD: breaks encapsulation? */
inline const_iterator const begin() const {return conditionals_.begin();} inline const_iterator const begin() const {return conditionals_.begin();}

View File

@ -14,7 +14,7 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class Conditional> template<class Conditional>
BayesTree<Conditional>::Clique::Clique(const boost::shared_ptr<Conditional>& conditional) { BayesTree<Conditional>::Clique::Clique(const sharedConditional& conditional) {
separator_ = conditional->parents(); separator_ = conditional->parents();
this->push_back(conditional); this->push_back(conditional);
} }
@ -91,8 +91,7 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class Conditional> template<class Conditional>
void BayesTree<Conditional>::insert void BayesTree<Conditional>::insert(const sharedConditional& conditional)
(const boost::shared_ptr<Conditional>& conditional)
{ {
// get key and parents // get key and parents
string key = conditional->key(); string key = conditional->key();
@ -129,7 +128,8 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class Conditional> template<class Conditional>
template<class Factor> template<class Factor>
boost::shared_ptr<Conditional> BayesTree<Conditional>::marginal(const string& key) const { typename BayesTree<Conditional>::sharedConditional
BayesTree<Conditional>::marginal(const string& key) const {
// get clique containing key, and remove all factors below key // get clique containing key, and remove all factors below key
sharedClique clique = (*this)[key]; sharedClique clique = (*this)[key];
@ -160,9 +160,7 @@ namespace gtsam {
ordering.reverse(); ordering.reverse();
// eliminate to get marginal // eliminate to get marginal
boost::shared_ptr<BayesNet<Conditional> > bayesNet; sharedBayesNet chordalBayesNet = eliminate<Factor,Conditional>(graph,ordering);
typename boost::shared_ptr<BayesNet<Conditional> > chordalBayesNet =
graph.eliminate(bayesNet,ordering);
return chordalBayesNet->back(); // the root is the marginal return chordalBayesNet->back(); // the root is the marginal
} }

View File

@ -49,4 +49,17 @@ namespace gtsam {
/** return the number of parents */ /** return the number of parents */
virtual std::size_t nrParents() const = 0; virtual std::size_t nrParents() const = 0;
}; };
// predicate to check whether a conditional has the sought key
template<class Conditional>
class onKey {
const std::string& key_;
public:
onKey(const std::string& key):key_(key) {}
bool operator()(const typename Conditional::shared_ptr& conditional) {
return (conditional->key()==key_);
}
};
} }

View File

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

View File

@ -29,7 +29,7 @@ FactorGraph<Factor>::FactorGraph(const BayesNet<Conditional>& bayesNet)
{ {
typename BayesNet<Conditional>::const_iterator it = bayesNet.begin(); typename BayesNet<Conditional>::const_iterator it = bayesNet.begin();
for(; it != bayesNet.end(); it++) { for(; it != bayesNet.end(); it++) {
typename boost::shared_ptr<Factor>::shared_ptr factor(new Factor(*it)); sharedFactor factor(new Factor(*it));
push_back(factor); push_back(factor);
} }
} }
@ -56,7 +56,7 @@ bool FactorGraph<Factor>::equals
/** check whether the factors_ are the same */ /** check whether the factors_ are the same */
for (size_t i = 0; i < factors_.size(); i++) { for (size_t i = 0; i < factors_.size(); i++) {
// TODO: Doesn't this force order of factor insertion? // TODO: Doesn't this force order of factor insertion?
shared_factor f1 = factors_[i], f2 = fg.factors_[i]; sharedFactor f1 = factors_[i], f2 = fg.factors_[i];
if (f1 == NULL && f2 == NULL) continue; if (f1 == NULL && f2 == NULL) continue;
if (f1 == NULL || f2 == NULL) return false; if (f1 == NULL || f2 == NULL) return false;
if (!f1->equals(*f2, tol)) return false; if (!f1->equals(*f2, tol)) return false;
@ -75,7 +75,7 @@ size_t FactorGraph<Factor>::nrFactors() const {
/* ************************************************************************* */ /* ************************************************************************* */
template<class Factor> template<class Factor>
void FactorGraph<Factor>::push_back(shared_factor factor) { void FactorGraph<Factor>::push_back(sharedFactor factor) {
factors_.push_back(factor); // add the actual factor factors_.push_back(factor); // add the actual factor
if (factor==NULL) return; if (factor==NULL) return;
@ -184,11 +184,11 @@ list<int> FactorGraph<Factor>::factors(const string& key) const {
template<class Factor> template<class Factor>
vector<boost::shared_ptr<Factor> > vector<boost::shared_ptr<Factor> >
FactorGraph<Factor>::findAndRemoveFactors(const string& key) { FactorGraph<Factor>::findAndRemoveFactors(const string& key) {
vector<boost::shared_ptr<Factor> > found; vector<sharedFactor> found;
Indices::iterator it = indices_.find(key); Indices::iterator it = indices_.find(key);
if (it == indices_.end()) if (it == indices_.end())
throw(std::invalid_argument throw(invalid_argument
("FactorGraph::findAndRemoveFactors invalid key: " + key)); ("FactorGraph::findAndRemoveFactors invalid key: " + key));
list<int> *indices_ptr; // pointer to indices list in indices_ map list<int> *indices_ptr; // pointer to indices list in indices_ map
@ -206,14 +206,13 @@ FactorGraph<Factor>::findAndRemoveFactors(const string& key) {
/* find factors and remove them from the factor graph: O(n) */ /* find factors and remove them from the factor graph: O(n) */
/* ************************************************************************* */ /* ************************************************************************* */
template<class Factor> template<class Factor>
boost::shared_ptr<Factor> typename FactorGraph<Factor>::sharedFactor
FactorGraph<Factor>::removeAndCombineFactors(const string& key) FactorGraph<Factor>::removeAndCombineFactors(const string& key)
{ {
bool verbose = false; bool verbose = false;
if (verbose) cout << "FactorGraph::removeAndCombineFactors" << endl; if (verbose) cout << "FactorGraph::removeAndCombineFactors" << endl;
typedef typename boost::shared_ptr<Factor> shared_factor; vector<sharedFactor> found = findAndRemoveFactors(key);
vector<shared_factor> found = findAndRemoveFactors(key); sharedFactor new_factor(new Factor(found));
shared_factor new_factor(new Factor(found));
if (verbose) cout << "FactorGraph::removeAndCombineFactors done" << endl; if (verbose) cout << "FactorGraph::removeAndCombineFactors done" << endl;
return new_factor; return new_factor;
} }
@ -221,21 +220,20 @@ FactorGraph<Factor>::removeAndCombineFactors(const string& key)
/* ************************************************************************* */ /* ************************************************************************* */
/* eliminate one node from the factor graph */ /* eliminate one node from the factor graph */
/* ************************************************************************* */ /* ************************************************************************* */
template<class Factor> template<class Factor,class Conditional>
template<class Conditional> boost::shared_ptr<Conditional> eliminateOne(FactorGraph<Factor>& graph, const string& key) {
boost::shared_ptr<Conditional> FactorGraph<Factor>::eliminateOne(const std::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
shared_factor joint_factor = removeAndCombineFactors(key); boost::shared_ptr<Factor> joint_factor = graph.removeAndCombineFactors(key);
// eliminate that joint factor // eliminate that joint factor
shared_factor factor; boost::shared_ptr<Factor> factor;
boost::shared_ptr<Conditional> conditional; boost::shared_ptr<Conditional> conditional;
boost::tie(conditional, factor) = joint_factor->eliminate(key); boost::tie(conditional, factor) = joint_factor->eliminate(key);
// add new factor on separator back into the graph // add new factor on separator back into the graph
if (!factor->empty()) push_back(factor); if (!factor->empty()) graph.push_back(factor);
// return the conditional Gaussian // return the conditional Gaussian
return conditional; return conditional;
@ -245,16 +243,16 @@ boost::shared_ptr<Conditional> FactorGraph<Factor>::eliminateOne(const std::stri
// This doubly templated function is generic. There is a LinearFactorGraph // This doubly templated function is generic. There is a LinearFactorGraph
// version that returns a more specific GaussianBayesNet. // version that returns a more specific GaussianBayesNet.
// Note, you will need to include this file to instantiate the function. // Note, you will need to include this file to instantiate the function.
// TODO: get rid of summy argument
/* ************************************************************************* */ /* ************************************************************************* */
template<class Factor> template<class Factor,class Conditional>
template<class Conditional>
boost::shared_ptr<BayesNet<Conditional> > boost::shared_ptr<BayesNet<Conditional> >
FactorGraph<Factor>::eliminate(boost::shared_ptr<BayesNet<Conditional> > xxx, 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<Conditional>(key); boost::shared_ptr<Conditional> cg = eliminateOne<Factor,Conditional>(factorGraph,key);
bayesNet->push_back(cg); bayesNet->push_back(cg);
} }

View File

@ -30,13 +30,13 @@ namespace gtsam {
*/ */
template<class Factor> class FactorGraph: public Testable<FactorGraph<Factor> > { template<class Factor> class FactorGraph: public Testable<FactorGraph<Factor> > {
public: public:
typedef typename boost::shared_ptr<Factor> shared_factor; typedef typename boost::shared_ptr<Factor> sharedFactor;
typedef typename std::vector<shared_factor>::iterator iterator; typedef typename std::vector<sharedFactor>::iterator iterator;
typedef typename std::vector<shared_factor>::const_iterator const_iterator; typedef typename std::vector<sharedFactor>::const_iterator const_iterator;
protected: protected:
/** Collection of factors */ /** Collection of factors */
std::vector<shared_factor> factors_; std::vector<sharedFactor> factors_;
/** For each variable a list of factor indices connected to it */ /** For each variable a list of factor indices connected to it */
typedef std::map<std::string, std::list<int> > Indices; typedef std::map<std::string, std::list<int> > Indices;
@ -73,7 +73,7 @@ namespace gtsam {
} }
/** Get a specific factor by index */ /** Get a specific factor by index */
inline shared_factor operator[](size_t i) const { inline sharedFactor operator[](size_t i) const {
return factors_[i]; return factors_[i];
} }
@ -84,7 +84,7 @@ namespace gtsam {
size_t nrFactors() const; size_t nrFactors() const;
/** Add a factor */ /** Add a factor */
void push_back(shared_factor factor); void push_back(sharedFactor factor);
/** /**
* Compute colamd ordering * Compute colamd ordering
@ -102,29 +102,14 @@ namespace gtsam {
* from the factor graph * from the factor graph
* @param key the key for the given node * @param key the key for the given node
*/ */
std::vector<shared_factor> findAndRemoveFactors(const std::string& key); std::vector<sharedFactor> findAndRemoveFactors(const std::string& key);
/** /**
* extract and combine all the factors that involve a given node * extract and combine all the factors that involve a given node
* @param key the key for the given node * @param key the key for the given node
* @return the combined linear factor * @return the combined linear factor
*/ */
shared_factor removeAndCombineFactors(const std::string& key); sharedFactor removeAndCombineFactors(const std::string& key);
/**
* Eliminate a single node yielding a Conditional
* Eliminates the factors from the factor graph through findAndRemoveFactors
* and adds a new factor on the separator to the factor graph
*/
template<class Conditional>
boost::shared_ptr<Conditional> eliminateOne(const std::string& key);
/**
* eliminate factor graph using the given (not necessarily complete)
* ordering, yielding a chordal Bayes net and (partially eliminated) FG
*/
template<class Conditional>
boost::shared_ptr<BayesNet<Conditional> > eliminate(boost::shared_ptr<BayesNet<Conditional> > bayesNet, const Ordering& ordering);
private: private:
@ -136,5 +121,24 @@ namespace gtsam {
ar & BOOST_SERIALIZATION_NVP(indices_); ar & BOOST_SERIALIZATION_NVP(indices_);
} }
}; // FactorGraph }; // FactorGraph
/** doubly templated functions */
/**
* Eliminate a single node yielding a Conditional
* Eliminates the factors from the factor graph through findAndRemoveFactors
* 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);
/**
* eliminate factor graph using the given (not necessarily complete)
* ordering, yielding a chordal Bayes net and (partially eliminated) FG
*/
template<class Factor, class Conditional>
boost::shared_ptr<BayesNet<Conditional> >
eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering);
} // namespace gtsam } // namespace gtsam

View File

@ -34,7 +34,7 @@ LinearFactorGraph::LinearFactorGraph(const GaussianBayesNet& CBN) :
set<string> LinearFactorGraph::find_separator(const string& key) const set<string> LinearFactorGraph::find_separator(const string& key) const
{ {
set<string> separator; set<string> separator;
BOOST_FOREACH(shared_factor factor,factors_) BOOST_FOREACH(sharedFactor factor,factors_)
factor->tally_separator(key,separator); factor->tally_separator(key,separator);
return separator; return separator;
@ -46,7 +46,8 @@ 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 = eliminateOne<ConditionalGaussian>(key); ConditionalGaussian::shared_ptr cg =
eliminateOne<LinearFactor,ConditionalGaussian>(*this, key);
chordalBayesNet->push_back(cg); chordalBayesNet->push_back(cg);
} }
return chordalBayesNet; return chordalBayesNet;
@ -90,7 +91,7 @@ LinearFactorGraph LinearFactorGraph::combine2(const LinearFactorGraph& lfg1,
/* ************************************************************************* */ /* ************************************************************************* */
Dimensions LinearFactorGraph::dimensions() const { Dimensions LinearFactorGraph::dimensions() const {
Dimensions result; Dimensions result;
BOOST_FOREACH(shared_factor factor,factors_) { BOOST_FOREACH(sharedFactor factor,factors_) {
Dimensions vs = factor->dimensions(); Dimensions vs = factor->dimensions();
string key; int dim; string key; int dim;
FOREACH_PAIR(key,dim,vs) result.insert(make_pair(key,dim)); FOREACH_PAIR(key,dim,vs) result.insert(make_pair(key,dim));
@ -112,7 +113,7 @@ LinearFactorGraph LinearFactorGraph::add_priors(double sigma) const {
FOREACH_PAIR(key,dim,vs) { FOREACH_PAIR(key,dim,vs) {
Matrix A = eye(dim); Matrix A = eye(dim);
Vector b = zero(dim); Vector b = zero(dim);
shared_factor prior(new LinearFactor(key,A,b, sigma)); sharedFactor prior(new LinearFactor(key,A,b, sigma));
result.push_back(prior); result.push_back(prior);
} }
return result; return result;
@ -123,7 +124,7 @@ pair<Matrix,Vector> LinearFactorGraph::matrix(const Ordering& ordering) const {
// get all factors // get all factors
LinearFactorSet found; LinearFactorSet found;
BOOST_FOREACH(shared_factor factor,factors_) BOOST_FOREACH(sharedFactor factor,factors_)
found.push_back(factor); found.push_back(factor);
// combine them // combine them
@ -145,7 +146,7 @@ Matrix LinearFactorGraph::sparse(const Ordering& ordering) const {
// Collect the I,J,S lists for all factors // Collect the I,J,S lists for all factors
int row_index = 0; int row_index = 0;
BOOST_FOREACH(shared_factor factor,factors_) { BOOST_FOREACH(sharedFactor factor,factors_) {
// get sparse lists for the factor // get sparse lists for the factor
list<int> i1,j1; list<int> i1,j1;

View File

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

View File

@ -256,7 +256,8 @@ 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 = fg.eliminateOne<ConditionalGaussian>("z"); ConditionalGaussian::shared_ptr cg3 =
eliminateOne<LinearFactor,ConditionalGaussian>(fg,"z");
CHECK(cg3->nrParents() == 0); CHECK(cg3->nrParents() == 0);
CHECK(fg.size() == 0); CHECK(fg.size() == 0);

View File

@ -191,7 +191,7 @@ TEST( LinearFactorGraph, eliminateOne_x1 )
{ {
LinearFactorGraph fg = createLinearFactorGraph(); LinearFactorGraph fg = createLinearFactorGraph();
ConditionalGaussian::shared_ptr actual = ConditionalGaussian::shared_ptr actual =
fg.eliminateOne<ConditionalGaussian>("x1"); eliminateOne<LinearFactor,ConditionalGaussian>(fg,"x1");
// create expected Conditional Gaussian // create expected Conditional Gaussian
Matrix R11 = Matrix_(2,2, Matrix R11 = Matrix_(2,2,
@ -220,7 +220,7 @@ TEST( LinearFactorGraph, eliminateOne_x2 )
{ {
LinearFactorGraph fg = createLinearFactorGraph(); LinearFactorGraph fg = createLinearFactorGraph();
ConditionalGaussian::shared_ptr actual = ConditionalGaussian::shared_ptr actual =
fg.eliminateOne<ConditionalGaussian>("x2"); eliminateOne<LinearFactor,ConditionalGaussian>(fg,"x2");
// create expected Conditional Gaussian // create expected Conditional Gaussian
Matrix R11 = Matrix_(2,2, Matrix R11 = Matrix_(2,2,
@ -248,7 +248,7 @@ TEST( LinearFactorGraph, eliminateOne_l1 )
{ {
LinearFactorGraph fg = createLinearFactorGraph(); LinearFactorGraph fg = createLinearFactorGraph();
ConditionalGaussian::shared_ptr actual = ConditionalGaussian::shared_ptr actual =
fg.eliminateOne<ConditionalGaussian>("l1"); eliminateOne<LinearFactor,ConditionalGaussian>(fg,"l1");
// create expected Conditional Gaussian // create expected Conditional Gaussian
Matrix R11 = Matrix_(2,2, Matrix R11 = Matrix_(2,2,
@ -383,8 +383,8 @@ TEST( LinearFactorGraph, matrix )
); );
Vector b1 = Vector_(8,-1., -1., 2., -1., 0., 1., -1., 1.5); Vector b1 = Vector_(8,-1., -1., 2., -1., 0., 1., -1., 1.5);
EQUALITY(A,A1); // currently fails EQUALITY(A,A1);
CHECK(b==b1); // currently fails CHECK(b==b1);
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -400,9 +400,10 @@ TEST( LinearFactorGraph, sparse )
Matrix ijs = fg.sparse(ord); Matrix ijs = fg.sparse(ord);
EQUALITY(ijs, Matrix_(3, 14, EQUALITY(ijs, Matrix_(3, 14,
+1., 2., 3., 4., 3., 4., 5.,6., 5., 6., 7., 8.,7.,8., // f(x1) f(x2,x1) f(l1,x1) f(x2,l1)
+5., 6., 1., 2., 5., 6., 3.,4., 5., 6., 1., 2.,3.,4., +1., 2., 3., 4., 3., 4., 5.,6., 5., 6., 7., 8.,7.,8.,
10.,10., 10.,10.,-10.,-10., 5.,5.,-5.,-5., -5.,-5.,5.,5.)); +5., 6., 1., 2., 5., 6., 3.,4., 5., 6., 1., 2.,3.,4.,
10.,10., 10.,10.,-10.,-10., 5.,5.,-5.,-5., -5.,-5.,5.,5.));
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -422,9 +423,8 @@ TEST( LinearFactorGraph, CONSTRUCTOR_GaussianBayesNet )
// Base FactorGraph only // Base FactorGraph only
FactorGraph<LinearFactor> fg3(*CBN); FactorGraph<LinearFactor> fg3(*CBN);
boost::shared_ptr<BayesNet<ConditionalGaussian> > dummy;
boost::shared_ptr<BayesNet<ConditionalGaussian> > CBN3 = boost::shared_ptr<BayesNet<ConditionalGaussian> > CBN3 =
fg3.eliminate<ConditionalGaussian>(dummy,ord); eliminate<LinearFactor,ConditionalGaussian>(fg3,ord);
CHECK(CBN->equals(*CBN3)); CHECK(CBN->equals(*CBN3));
} }

View File

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