Constructor from BayesNet now in FactorGraph base class
FactorGraph::eliminate Removed eliminate_partiallyrelease/4.3a0
							parent
							
								
									e650676da7
								
							
						
					
					
						commit
						3d334401f5
					
				|  | @ -22,6 +22,18 @@ using namespace std; | ||||||
| 
 | 
 | ||||||
| namespace gtsam { | namespace gtsam { | ||||||
| 
 | 
 | ||||||
|  | /* ************************************************************************* */ | ||||||
|  | template<class Factor> | ||||||
|  | template<class Conditional> | ||||||
|  | FactorGraph<Factor>::FactorGraph(const BayesNet<Conditional>& bayesNet) | ||||||
|  | { | ||||||
|  | 	typename BayesNet<Conditional>::const_iterator it = bayesNet.begin(); | ||||||
|  | 	for(; it != bayesNet.end(); it++) { | ||||||
|  | 		typename boost::shared_ptr<Factor>::shared_ptr factor(new Factor(*it)); | ||||||
|  | 		push_back(factor); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| template<class Factor> | template<class Factor> | ||||||
| void FactorGraph<Factor>::print(const string& s) const { | void FactorGraph<Factor>::print(const string& s) const { | ||||||
|  | @ -229,5 +241,25 @@ boost::shared_ptr<Conditional> FactorGraph<Factor>::eliminateOne(const std::stri | ||||||
| 	return conditional; | 	return conditional; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /* ************************************************************************* */ | ||||||
|  | // This doubly templated function is generic. There is a LinearFactorGraph
 | ||||||
|  | // version that returns a more specific GaussianBayesNet.
 | ||||||
|  | // Note, you will need to include this file to instantiate the function.
 | ||||||
|  | /* ************************************************************************* */ | ||||||
|  | template<class Factor> | ||||||
|  | template<class Conditional> | ||||||
|  | boost::shared_ptr<BayesNet<Conditional> > | ||||||
|  | FactorGraph<Factor>::eliminate(const Ordering& ordering) | ||||||
|  | { | ||||||
|  | 	boost::shared_ptr<BayesNet<Conditional> > bayesNet (new BayesNet<Conditional>()); // empty
 | ||||||
|  | 
 | ||||||
|  | 	BOOST_FOREACH(string key, ordering) { | ||||||
|  | 		boost::shared_ptr<Conditional> cg = eliminateOne<Conditional>(key); | ||||||
|  | 		bayesNet->push_back(cg); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return bayesNet; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -16,6 +16,7 @@ | ||||||
| #include <boost/serialization/shared_ptr.hpp> | #include <boost/serialization/shared_ptr.hpp> | ||||||
| 
 | 
 | ||||||
| #include "Testable.h" | #include "Testable.h" | ||||||
|  | #include "BayesNet.h" | ||||||
| 
 | 
 | ||||||
| namespace gtsam { | namespace gtsam { | ||||||
| 
 | 
 | ||||||
|  | @ -43,6 +44,13 @@ namespace gtsam { | ||||||
| 
 | 
 | ||||||
| 	public: | 	public: | ||||||
| 
 | 
 | ||||||
|  | 		/** Default constructor */ | ||||||
|  | 		FactorGraph() {} | ||||||
|  | 
 | ||||||
|  | 		/** convert from Bayes net */ | ||||||
|  | 		template<class Conditional> | ||||||
|  | 		FactorGraph(const BayesNet<Conditional>& bayesNet); | ||||||
|  | 
 | ||||||
| 		/** print out graph */ | 		/** print out graph */ | ||||||
| 		void print(const std::string& s = "FactorGraph") const; | 		void print(const std::string& s = "FactorGraph") const; | ||||||
| 
 | 
 | ||||||
|  | @ -111,6 +119,13 @@ namespace gtsam { | ||||||
| 		template<class Conditional> | 		template<class Conditional> | ||||||
| 		boost::shared_ptr<Conditional> eliminateOne(const std::string& key); | 		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(const Ordering& ordering); | ||||||
|  | 
 | ||||||
| 	private: | 	private: | ||||||
| 
 | 
 | ||||||
| 		/** Serialization function */ | 		/** Serialization function */ | ||||||
|  |  | ||||||
|  | @ -23,24 +23,10 @@ using namespace gtsam; | ||||||
| template class FactorGraph<LinearFactor>; | template class FactorGraph<LinearFactor>; | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| LinearFactorGraph::LinearFactorGraph(const GaussianBayesNet& CBN) | LinearFactorGraph::LinearFactorGraph(const GaussianBayesNet& CBN) : | ||||||
| { | 	FactorGraph<LinearFactor> (CBN) { | ||||||
| 	setCBN(CBN); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ |  | ||||||
| void LinearFactorGraph::setCBN(const GaussianBayesNet& CBN) |  | ||||||
| { |  | ||||||
| 	clear(); |  | ||||||
| 	GaussianBayesNet::const_iterator it = CBN.begin(); |  | ||||||
| 	for(; it != CBN.end(); it++) { |  | ||||||
| 		LinearFactor::shared_ptr lf(new LinearFactor(*it)); |  | ||||||
| 		push_back(lf); |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* ************************************************************************* */ |  | ||||||
| /* find the separators                                                       */ |  | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| set<string> LinearFactorGraph::find_separator(const string& key) const | set<string> LinearFactorGraph::find_separator(const string& key) const | ||||||
| { | { | ||||||
|  | @ -51,35 +37,18 @@ set<string> LinearFactorGraph::find_separator(const string& key) const | ||||||
| 	return separator; | 	return separator; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ |  | ||||||
| // eliminate factor graph using the given (not necessarily complete)
 |  | ||||||
| // ordering, yielding a chordal Bayes net and partially eliminated FG
 |  | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| GaussianBayesNet::shared_ptr | GaussianBayesNet::shared_ptr | ||||||
| LinearFactorGraph::eliminate_partially(const Ordering& ordering) | 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<ConditionalGaussian>(key); | ||||||
| 		chordalBayesNet->push_back(cg); | 		chordalBayesNet->push_back(cg); | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	return chordalBayesNet; | 	return chordalBayesNet; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ |  | ||||||
| /** eliminate factor graph in the given order, yielding a chordal Bayes net  */  |  | ||||||
| /* ************************************************************************* */ |  | ||||||
| GaussianBayesNet::shared_ptr |  | ||||||
| LinearFactorGraph::eliminate(const Ordering& ordering) |  | ||||||
| { |  | ||||||
| 	GaussianBayesNet::shared_ptr chordalBayesNet = eliminate_partially(ordering); |  | ||||||
| 	return chordalBayesNet; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* ************************************************************************* */ |  | ||||||
| /** optimize the linear factor graph                                          */  |  | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| VectorConfig LinearFactorGraph::optimize(const Ordering& ordering) | VectorConfig LinearFactorGraph::optimize(const Ordering& ordering) | ||||||
| { | { | ||||||
|  | @ -92,8 +61,6 @@ VectorConfig LinearFactorGraph::optimize(const Ordering& ordering) | ||||||
| 	return *newConfig; | 	return *newConfig; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ |  | ||||||
| /** combine two factor graphs                                                 */  |  | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| void LinearFactorGraph::combine(const LinearFactorGraph &lfg){ | void LinearFactorGraph::combine(const LinearFactorGraph &lfg){ | ||||||
| 	for(const_iterator factor=lfg.factors_.begin(); factor!=lfg.factors_.end(); factor++){ | 	for(const_iterator factor=lfg.factors_.begin(); factor!=lfg.factors_.end(); factor++){ | ||||||
|  | @ -102,11 +69,9 @@ void LinearFactorGraph::combine(const LinearFactorGraph &lfg){ | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
| /** combine two factor graphs                                                */  |  | ||||||
| /* ************************************************************************* */ |  | ||||||
| 
 |  | ||||||
| LinearFactorGraph LinearFactorGraph::combine2(const LinearFactorGraph& lfg1, | LinearFactorGraph LinearFactorGraph::combine2(const LinearFactorGraph& lfg1, | ||||||
| 		const LinearFactorGraph& lfg2) { | 		const LinearFactorGraph& lfg2) { | ||||||
|  | 
 | ||||||
| 	// create new linear factor graph equal to the first one
 | 	// create new linear factor graph equal to the first one
 | ||||||
| 	LinearFactorGraph fg = lfg1; | 	LinearFactorGraph fg = lfg1; | ||||||
| 
 | 
 | ||||||
|  | @ -115,13 +80,11 @@ LinearFactorGraph LinearFactorGraph::combine2(const LinearFactorGraph& lfg1, | ||||||
| 			!= lfg2.factors_.end(); factor++) { | 			!= lfg2.factors_.end(); factor++) { | ||||||
| 		fg.push_back(*factor); | 		fg.push_back(*factor); | ||||||
| 	} | 	} | ||||||
| 
 |  | ||||||
| 	return fg; | 	return fg; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */   | /* ************************************************************************* */   | ||||||
| // find all variables and their dimensions
 |  | ||||||
| VariableSet LinearFactorGraph::variables() const { | VariableSet LinearFactorGraph::variables() const { | ||||||
| 	VariableSet result; | 	VariableSet result; | ||||||
| 	BOOST_FOREACH(shared_factor factor,factors_) { | 	BOOST_FOREACH(shared_factor factor,factors_) { | ||||||
|  |  | ||||||
|  | @ -55,12 +55,6 @@ namespace gtsam { | ||||||
| 			return exp(-0.5 * error(c)); | 			return exp(-0.5 * error(c)); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|     /**
 |  | ||||||
|      * given a chordal bayes net, sets the linear factor graph identical to that CBN |  | ||||||
|      * FD: imperative !! |  | ||||||
|      */ |  | ||||||
|     void setCBN(const GaussianBayesNet& CBN); |  | ||||||
| 
 |  | ||||||
|     /**
 |     /**
 | ||||||
|      * find the separator, i.e. all the nodes that have at least one |      * find the separator, i.e. all the nodes that have at least one | ||||||
|      * common factor with the given node. FD: not used AFAIK. |      * common factor with the given node. FD: not used AFAIK. | ||||||
|  | @ -69,15 +63,10 @@ namespace gtsam { | ||||||
| 
 | 
 | ||||||
|     /**
 |     /**
 | ||||||
|      * 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. Allows for passing an incomplete ordering | ||||||
|      */ |  | ||||||
|     boost::shared_ptr<GaussianBayesNet> eliminate(const Ordering& ordering); |  | ||||||
| 		 |  | ||||||
|     /**
 |  | ||||||
|      * Same as eliminate but allows for passing an incomplete ordering |  | ||||||
|      * that does not completely eliminate the graph |      * that does not completely eliminate the graph | ||||||
|      */ |      */ | ||||||
|     boost::shared_ptr<GaussianBayesNet> eliminate_partially(const Ordering& ordering); |     boost::shared_ptr<GaussianBayesNet> eliminate(const Ordering& ordering); | ||||||
| 		 | 		 | ||||||
|     /**
 |     /**
 | ||||||
|      * optimize a linear factor graph |      * optimize a linear factor graph | ||||||
|  |  | ||||||
|  | @ -18,6 +18,7 @@ using namespace boost::assign; | ||||||
| #include "Ordering.h" | #include "Ordering.h" | ||||||
| #include "smallExample.h" | #include "smallExample.h" | ||||||
| #include "GaussianBayesNet.h" | #include "GaussianBayesNet.h" | ||||||
|  | #include <FactorGraph-inl.h> // needed for FactorGraph::eliminate | ||||||
| 
 | 
 | ||||||
| using namespace gtsam; | using namespace gtsam; | ||||||
| 
 | 
 | ||||||
|  | @ -394,10 +395,17 @@ TEST( LinearFactorGraph, CONSTRUCTOR_GaussianBayesNet ) | ||||||
|   Ordering ord; |   Ordering ord; | ||||||
|   ord += "x2","l1","x1"; |   ord += "x2","l1","x1"; | ||||||
|   GaussianBayesNet::shared_ptr CBN = fg.eliminate(ord); |   GaussianBayesNet::shared_ptr CBN = fg.eliminate(ord); | ||||||
|  | 
 | ||||||
|  |   // True LinearFactorGraph
 | ||||||
|   LinearFactorGraph fg2(*CBN); |   LinearFactorGraph fg2(*CBN); | ||||||
|   GaussianBayesNet::shared_ptr CBN2 = fg2.eliminate(ord); |   GaussianBayesNet::shared_ptr CBN2 = fg2.eliminate(ord); | ||||||
| 
 |  | ||||||
|   CHECK(CBN->equals(*CBN2)); |   CHECK(CBN->equals(*CBN2)); | ||||||
|  | 
 | ||||||
|  |   // Base FactorGraph only
 | ||||||
|  |   FactorGraph<LinearFactor> fg3(*CBN); | ||||||
|  |   boost::shared_ptr<BayesNet<ConditionalGaussian> > CBN3 = | ||||||
|  |   		fg3.eliminate<ConditionalGaussian>(ord); | ||||||
|  |   CHECK(CBN->equals(*CBN3)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* ************************************************************************* */ | /* ************************************************************************* */ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue