| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | #include "SymbolMap.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
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianBayesNet scalarGaussian(const Symbol& key, double mu, double sigma) { | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	GaussianBayesNet bn; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	GaussianConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-01-27 12:39:35 +08:00
										 |  |  | 		conditional(new GaussianConditional(key, Vector_(1,mu)/sigma, eye(1)/sigma, ones(1))); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	bn.push_back(conditional); | 
					
						
							|  |  |  | 	return bn; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianBayesNet simpleGaussian(const Symbol& key, const Vector& mu, double sigma) { | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	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 | 
					
						
							| 
									
										
										
										
											2010-01-27 12:39:35 +08:00
										 |  |  | 		conditional(new GaussianConditional(key, mu/sigma, eye(n)/sigma, ones(n))); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | void push_front(GaussianBayesNet& bn, const Symbol& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 		const Symbol& 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | void push_front(GaussianBayesNet& bn, const Symbol& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 		const Symbol& name1, Matrix S, const Symbol& 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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  |   return *optimize_(bn); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | boost::shared_ptr<VectorConfig> optimize_(const GaussianBayesNet& bn) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	boost::shared_ptr<VectorConfig> result(new VectorConfig); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-09 10:37:58 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | VectorConfig backSubstitute(const GaussianBayesNet& bn, const VectorConfig& y) { | 
					
						
							| 
									
										
										
										
											2010-01-31 12:39:41 +08:00
										 |  |  | 	VectorConfig x = y; | 
					
						
							|  |  |  | 	backSubstituteInPlace(bn,x); | 
					
						
							|  |  |  | 	return x; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // (R*x)./sigmas = y by solving x=inv(R)*(y.*sigmas)
 | 
					
						
							|  |  |  | void backSubstituteInPlace(const GaussianBayesNet& bn, VectorConfig& y) { | 
					
						
							|  |  |  | 	VectorConfig& x = y; | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 	/** solve each node in turn in topological sort order (parents first)*/ | 
					
						
							|  |  |  | 	BOOST_REVERSE_FOREACH(GaussianConditional::shared_ptr cg, bn) { | 
					
						
							|  |  |  | 		// i^th part of R*x=y, x=inv(R)*y
 | 
					
						
							|  |  |  | 		// (Rii*xi + R_i*x(i+1:))./si = yi <-> xi = inv(Rii)*(yi.*si - R_i*x(i+1:))
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		const Symbol& i = cg->key(); | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 		Vector zi = emul(y[i],cg->get_sigmas()); | 
					
						
							|  |  |  | 		GaussianConditional::const_iterator it; | 
					
						
							|  |  |  | 		for (it = cg->parentsBegin(); it!= cg->parentsEnd(); it++) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 			const Symbol& j = it->first; | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 			const Matrix& Rij = it->second; | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 			multiplyAdd(-1.0,Rij,x[j],zi); | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 		x[i] = gtsam::backSubstituteUpper(cg->get_R(), zi); | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-09 10:37:58 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // gy=inv(L)*gx by solving L*gy=gx.
 | 
					
						
							|  |  |  | // gy=inv(R'*inv(Sigma))*gx
 | 
					
						
							|  |  |  | // gz'*R'=gx', gy = gz.*sigmas
 | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | VectorConfig backSubstituteTranspose(const GaussianBayesNet& bn, | 
					
						
							|  |  |  | 		const VectorConfig& gx) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize gy from gx
 | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 	// TODO: used to insert zeros if gx did not have an entry for a variable in bn
 | 
					
						
							|  |  |  | 	VectorConfig gy = gx; | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// we loop from first-eliminated to last-eliminated
 | 
					
						
							|  |  |  | 	// i^th part of L*gy=gx is done block-column by block-column of L
 | 
					
						
							|  |  |  | 	BOOST_FOREACH(GaussianConditional::shared_ptr cg, bn) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		const Symbol& j = cg->key(); | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 		gy[j] = gtsam::backSubstituteUpper(gy[j],cg->get_R()); | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 		GaussianConditional::const_iterator it; | 
					
						
							|  |  |  | 		for (it = cg->parentsBegin(); it!= cg->parentsEnd(); it++) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 			const Symbol& i = it->first; | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 			const Matrix& Rij = it->second; | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 			transposeMultiplyAdd(-1.0,Rij,gy[j],gy[i]); | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Scale gy
 | 
					
						
							|  |  |  | 	BOOST_FOREACH(GaussianConditional::shared_ptr cg, bn) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		const Symbol& j = cg->key(); | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 		gy[j] = emul(gy[j],cg->get_sigmas()); | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return gy; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */   | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  |   size_t N=0; SymbolMap<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); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol key; size_t I; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  |   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
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2010-01-05 22:14:49 +08:00
										 |  |  |     // get sigmas
 | 
					
						
							|  |  |  |     Vector sigmas = cg->get_sigmas(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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++) | 
					
						
							| 
									
										
										
										
											2010-01-05 22:14:49 +08:00
										 |  |  |       d(I+i) = d_(i)/sigmas(i); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 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++) | 
					
						
							| 
									
										
										
										
											2010-01-05 22:14:49 +08:00
										 |  |  |       	R(I+i,I+j) = R_(i,j)/sigmas(i); | 
					
						
							| 
									
										
										
										
											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++) | 
					
						
							| 
									
										
										
										
											2010-01-05 22:14:49 +08:00
										 |  |  |       		R(I+i,J+j) = S(i,j)/sigmas(i); | 
					
						
							| 
									
										
										
										
											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
 |