| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |  * @file   GaussianBayesNet.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief  Chordal Bayes Net, the result of eliminating a factor graph | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdarg.h>
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-10-27 22:14:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | #include "GaussianBayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-27 22:14:36 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-30 12:54:11 +08:00
										 |  |  | // Explicitly instantiate so we don't have to include everywhere
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | #include "BayesNet-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | template class BayesNet<GaussianConditional>; | 
					
						
							| 
									
										
										
										
											2009-10-30 12:54:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | // trick from some reading group
 | 
					
						
							|  |  |  | #define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL) 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #define REVERSE_FOREACH_PAIR( KEY, VAL, COL) BOOST_REVERSE_FOREACH (boost::tie(KEY,VAL),COL)
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | GaussianBayesNet scalarGaussian(const string& key, double mu, double sigma) { | 
					
						
							|  |  |  | 	GaussianBayesNet bn; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	GaussianConditional::shared_ptr | 
					
						
							|  |  |  | 		conditional(new GaussianConditional(key, Vector_(1,mu), eye(1), Vector_(1,sigma))); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	bn.push_back(conditional); | 
					
						
							|  |  |  | 	return bn; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | GaussianBayesNet simpleGaussian(const string& key, const Vector& mu, double sigma) { | 
					
						
							|  |  |  | 	GaussianBayesNet bn; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 	size_t n = mu.size(); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	GaussianConditional::shared_ptr | 
					
						
							|  |  |  | 		conditional(new GaussianConditional(key, mu, eye(n), repeat(n,sigma))); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	bn.push_back(conditional); | 
					
						
							|  |  |  | 	return bn; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void push_front(GaussianBayesNet& bn, const string& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 		const string& name1, Matrix S, Vector sigmas) { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	GaussianConditional::shared_ptr cg(new GaussianConditional(key, d, R, name1, S, sigmas)); | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  | 	bn.push_front(cg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void push_front(GaussianBayesNet& bn, const string& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 		const string& name1, Matrix S, const string& name2, Matrix T, Vector sigmas) { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	GaussianConditional::shared_ptr cg(new GaussianConditional(key, d, R, name1, S, name2, T, sigmas)); | 
					
						
							| 
									
										
										
										
											2009-11-12 14:09:03 +08:00
										 |  |  | 	bn.push_front(cg); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | VectorConfig optimize(const GaussianBayesNet& bn) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |   VectorConfig result; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	 | 
					
						
							|  |  |  |   /** solve each node in turn in topological sort order (parents first)*/ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	BOOST_REVERSE_FOREACH(GaussianConditional::shared_ptr cg, bn) { | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |     Vector x = cg->solve(result); // Solve for that variable
 | 
					
						
							|  |  |  |     result.insert(cg->key(),x);   // store result in partial solution
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */   | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | pair<Matrix,Vector> matrix(const GaussianBayesNet& bn)  { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // add the dimensions of all variables to get matrix dimension
 | 
					
						
							|  |  |  |   // and at the same time create a mapping from keys to indices
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  |   size_t N=0; map<string,size_t> mapping; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |   BOOST_FOREACH(GaussianConditional::shared_ptr cg,bn) { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  |     mapping.insert(make_pair(cg->key(),N)); | 
					
						
							|  |  |  |     N += cg->dim(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create matrix and copy in values
 | 
					
						
							|  |  |  |   Matrix R = zeros(N,N); | 
					
						
							|  |  |  |   Vector d(N); | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	string key; size_t I; | 
					
						
							|  |  |  |   FOREACH_PAIR(key,I,mapping) { | 
					
						
							|  |  |  |     // find corresponding conditional
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |     GaussianConditional::shared_ptr cg = bn[key]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      | 
					
						
							|  |  |  |     // get RHS and copy to d
 | 
					
						
							|  |  |  |     const Vector& d_ = cg->get_d(); | 
					
						
							|  |  |  |     const size_t n = d_.size(); | 
					
						
							|  |  |  |     for (size_t i=0;i<n;i++) | 
					
						
							|  |  |  |       d(I+i) = d_(i); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // get leading R matrix and copy to R
 | 
					
						
							|  |  |  |     const Matrix& R_ = cg->get_R(); | 
					
						
							|  |  |  |     for (size_t i=0;i<n;i++) | 
					
						
							|  |  |  |       for(size_t j=0;j<n;j++) | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  |       	R(I+i,I+j) = R_(i,j); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // loop over S matrices and copy them into R
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |     GaussianConditional::const_iterator keyS = cg->parentsBegin(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     for (; keyS!=cg->parentsEnd(); keyS++) { | 
					
						
							|  |  |  |       Matrix S = keyS->second;                   // get S matrix      
 | 
					
						
							|  |  |  |       const size_t m = S.size1(), n = S.size2(); // find S size
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  |       const size_t J = mapping[keyS->first];     // find column index
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |       for (size_t i=0;i<m;i++) | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  |       	for(size_t j=0;j<n;j++) | 
					
						
							|  |  |  |       		R(I+i,J+j) = S(i,j); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     } // keyS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   } // keyI
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return make_pair(R,d); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |