| 
									
										
										
										
											2010-10-09 06:21:48 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  |  * GaussianJunctionTree.cpp | 
					
						
							|  |  |  |  * Created on: Jul 12, 2010 | 
					
						
							|  |  |  |  * @author Kai Ni | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @brief: the Gaussian junction tree | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/ClusterTree-inl.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/JunctionTree-inl.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianJunctionTree.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 	// explicit template instantiation
 | 
					
						
							|  |  |  | 	template class JunctionTree<GaussianFactorGraph>; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 	using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * GaussianJunctionTree | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	void GaussianJunctionTree::btreeBackSubstitute(const boost::shared_ptr<const BayesTree::Clique>& current, VectorValues& config) const { | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 		// solve the bayes net in the current node
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		GaussianBayesNet::const_reverse_iterator it = current->rbegin(); | 
					
						
							|  |  |  | 		for (; it!=current->rend(); ++it) { | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 			Vector x = (*it)->solve(config); // Solve for that variable
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			config[(*it)->key()] = x;   // store result in partial solution
 | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// solve the bayes nets in the child nodes
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:21:48 +08:00
										 |  |  | 		BOOST_FOREACH(const BayesTree::sharedClique& child, current->children()) { | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:54 +08:00
										 |  |  | 			btreeBackSubstitute(child, config); | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  | 	void countDims(const boost::shared_ptr<const BayesTree<GaussianConditional>::Clique>& clique, vector<size_t>& dims) { | 
					
						
							|  |  |  | 	  BOOST_FOREACH(const boost::shared_ptr<const GaussianConditional>& cond, *clique) { | 
					
						
							|  |  |  | 	    // There should be no two conditionals on the same variable
 | 
					
						
							|  |  |  | 	    assert(dims[cond->key()] == 0); | 
					
						
							|  |  |  | 	    dims[cond->key()] = cond->dim(); | 
					
						
							|  |  |  | 	  } | 
					
						
							|  |  |  | 	  BOOST_FOREACH(const boost::shared_ptr<const BayesTree<GaussianConditional>::Clique>& child, clique->children()) { | 
					
						
							|  |  |  | 	    countDims(child, dims); | 
					
						
							|  |  |  | 	  } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	VectorValues GaussianJunctionTree::optimize() const { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	  tic("GJT optimize 1: eliminate"); | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 		// eliminate from leaves to the root
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		boost::shared_ptr<const BayesTree::Clique> rootClique(this->eliminate()); | 
					
						
							|  |  |  |     toc("GJT optimize 1: eliminate"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Allocate solution vector
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |     tic("GJT optimize 2: allocate VectorValues"); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		vector<size_t> dims(rootClique->back()->key() + 1, 0); | 
					
						
							|  |  |  | 		countDims(rootClique, dims); | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		VectorValues result(dims); | 
					
						
							|  |  |  |     toc("GJT optimize 2: allocate VectorValues"); | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// back-substitution
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     tic("GJT optimize 3: back-substitute"); | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:54 +08:00
										 |  |  | 		btreeBackSubstitute(rootClique, result); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     toc("GJT optimize 3: back-substitute"); | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | 		return result; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } //namespace gtsam
 |