| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |  * @file    GaussianFactorGraph.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Linear Factor Graph where all factors are Gaussians | 
					
						
							|  |  |  |  * @author  Kai Ni | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							|  |  |  | #include <boost/numeric/ublas/lu.hpp>
 | 
					
						
							|  |  |  | #include <boost/numeric/ublas/io.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <colamd/colamd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | #include "GaussianFactorGraph.h"
 | 
					
						
							|  |  |  | #include "GaussianFactorSet.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | #include "FactorGraph-inl.h"
 | 
					
						
							|  |  |  | #include "inference-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-28 18:48:48 +08:00
										 |  |  | #include "iterative.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | // trick from some reading group
 | 
					
						
							|  |  |  | #define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-30 13:45:22 +08:00
										 |  |  | // Explicitly instantiate so we don't have to include everywhere
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | template class FactorGraph<GaussianFactor>; | 
					
						
							| 
									
										
										
										
											2009-10-30 13:45:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactorGraph::GaussianFactorGraph(const GaussianBayesNet& CBN) : | 
					
						
							|  |  |  | 	FactorGraph<GaussianFactor> (CBN) { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 06:48:41 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | double GaussianFactorGraph::error(const VectorConfig& x) const { | 
					
						
							|  |  |  | 	double total_error = 0.; | 
					
						
							|  |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) | 
					
						
							|  |  |  | 		total_error += factor->error(x); | 
					
						
							|  |  |  | 	return total_error; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | Errors GaussianFactorGraph::errors(const VectorConfig& x) const { | 
					
						
							| 
									
										
										
										
											2009-12-27 06:48:41 +08:00
										 |  |  | 	Errors e; | 
					
						
							|  |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 		e.push_back(factor->error_vector(x)); | 
					
						
							| 
									
										
										
										
											2009-12-27 06:48:41 +08:00
										 |  |  | 	return e; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Errors GaussianFactorGraph::operator*(const VectorConfig& x) const { | 
					
						
							|  |  |  | 	Errors e; | 
					
						
							|  |  |  | 	BOOST_FOREACH(sharedFactor Ai,factors_) | 
					
						
							|  |  |  | 		e.push_back((*Ai)*x); | 
					
						
							|  |  |  | 	return e; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig GaussianFactorGraph::operator^(const Errors& e) const { | 
					
						
							|  |  |  | 	VectorConfig x; | 
					
						
							|  |  |  | 	// For each factor add the gradient contribution
 | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:36 +08:00
										 |  |  | 	Errors::const_iterator it = e.begin(); | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 	BOOST_FOREACH(sharedFactor Ai,factors_) | 
					
						
							| 
									
										
										
										
											2009-12-31 01:13:36 +08:00
										 |  |  | 		x += (*Ai)^(*(it++)); | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 	return x; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig GaussianFactorGraph::gradient(const VectorConfig& x) const { | 
					
						
							|  |  |  | 	const GaussianFactorGraph& A = *this; | 
					
						
							|  |  |  | 	return A^errors(x); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | set<Symbol> GaussianFactorGraph::find_separator(const Symbol& key) const | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	set<Symbol> separator; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 		factor->tally_separator(key,separator); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return separator; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | GaussianConditional::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianFactorGraph::eliminateOne(const Symbol& key) { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	return gtsam::eliminateOne<GaussianFactor,GaussianConditional>(*this, key); | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | GaussianBayesNet | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactorGraph::eliminate(const Ordering& ordering) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	GaussianBayesNet chordalBayesNet; // empty
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	BOOST_FOREACH(const Symbol& key, ordering) { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 		GaussianConditional::shared_ptr cg = eliminateOne(key); | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 		chordalBayesNet.push_back(cg); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return chordalBayesNet; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | VectorConfig GaussianFactorGraph::optimize(const Ordering& ordering) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	// eliminate all nodes in the given ordering -> chordal Bayes net
 | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | 	GaussianBayesNet chordalBayesNet = eliminate(ordering); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// calculate new configuration (using backsubstitution)
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | 	return ::optimize(chordalBayesNet); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-11 15:14:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | boost::shared_ptr<GaussianBayesNet> | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactorGraph::eliminate_(const Ordering& ordering) | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	boost::shared_ptr<GaussianBayesNet> chordalBayesNet(new GaussianBayesNet); // empty
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	BOOST_FOREACH(const Symbol& key, ordering) { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 		GaussianConditional::shared_ptr cg = eliminateOne(key); | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | 		chordalBayesNet->push_back(cg); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return chordalBayesNet; | 
					
						
							| 
									
										
										
										
											2009-11-11 15:14:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | boost::shared_ptr<VectorConfig> | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactorGraph::optimize_(const Ordering& ordering) { | 
					
						
							| 
									
										
										
										
											2009-11-11 15:14:13 +08:00
										 |  |  | 	return boost::shared_ptr<VectorConfig>(new VectorConfig(optimize(ordering))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | void GaussianFactorGraph::combine(const GaussianFactorGraph &lfg){ | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	for(const_iterator factor=lfg.factors_.begin(); factor!=lfg.factors_.end(); factor++){ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 		push_back(*factor); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactorGraph GaussianFactorGraph::combine2(const GaussianFactorGraph& lfg1, | 
					
						
							|  |  |  | 		const GaussianFactorGraph& lfg2) { | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 10:36:30 +08:00
										 |  |  | 	// create new linear factor graph equal to the first one
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph fg = lfg1; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	// add the second factors_ in the graph
 | 
					
						
							|  |  |  | 	for (const_iterator factor = lfg2.factors_.begin(); factor | 
					
						
							|  |  |  | 			!= lfg2.factors_.end(); factor++) { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 		fg.push_back(*factor); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fg; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */   | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | Dimensions GaussianFactorGraph::dimensions() const { | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 	Dimensions result; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) { | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 		Dimensions vs = factor->dimensions(); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		Symbol key; int dim; | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 		FOREACH_PAIR(key,dim,vs) result.insert(make_pair(key,dim)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */   | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactorGraph GaussianFactorGraph::add_priors(double sigma) const { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// start with this factor graph
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorGraph result = *this; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// find all variables and their dimensions
 | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 	Dimensions vs = dimensions(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// for each of the variables, add a prior
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol key; int dim; | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 	FOREACH_PAIR(key,dim,vs) { | 
					
						
							|  |  |  | 		Matrix A = eye(dim); | 
					
						
							|  |  |  | 		Vector b = zero(dim); | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 		sharedFactor prior(new GaussianFactor(key,A,b, sigma)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 		result.push_back(prior); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 02:46:01 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Errors GaussianFactorGraph::rhs() const { | 
					
						
							|  |  |  | 	Errors e; | 
					
						
							|  |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) | 
					
						
							|  |  |  | 		e.push_back(ediv(factor->get_b(),factor->get_sigmas())); | 
					
						
							|  |  |  | 	return e; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */   | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | pair<Matrix,Vector> GaussianFactorGraph::matrix(const Ordering& ordering) const { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// get all factors
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactorSet found; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) | 
					
						
							| 
									
										
										
										
											2009-10-19 00:49:10 +08:00
										 |  |  | 		found.push_back(factor); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// combine them
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactor lf(found); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Return Matrix and Vector
 | 
					
						
							|  |  |  | 	return lf.matrix(ordering); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Dimensions GaussianFactorGraph::columnIndices(const Ordering& ordering) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get the dimensions for all variables
 | 
					
						
							|  |  |  | 	Dimensions variableSet = dimensions(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Find the starting index and dimensions for all variables given the order
 | 
					
						
							|  |  |  | 	size_t j = 1; | 
					
						
							|  |  |  | 	Dimensions result; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	BOOST_FOREACH(const Symbol& key, ordering) { | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  | 		// associate key with first column index
 | 
					
						
							|  |  |  | 		result.insert(make_pair(key,j)); | 
					
						
							|  |  |  | 		// find dimension for this key
 | 
					
						
							|  |  |  | 		Dimensions::const_iterator it = variableSet.find(key); | 
					
						
							|  |  |  | 		// advance column index to next block by adding dim(key)
 | 
					
						
							|  |  |  | 		j += it->second; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 11:38:55 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | Matrix GaussianFactorGraph::sparse(const Ordering& ordering) const { | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// return values
 | 
					
						
							|  |  |  | 	list<int> I,J; | 
					
						
							|  |  |  | 	list<double> S; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  | 	// get the starting column indices for all variables
 | 
					
						
							|  |  |  | 	Dimensions indices = columnIndices(ordering); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Collect the I,J,S lists for all factors
 | 
					
						
							|  |  |  | 	int row_index = 0; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) { | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// get sparse lists for the factor
 | 
					
						
							|  |  |  | 		list<int> i1,j1; | 
					
						
							|  |  |  | 		list<double> s1; | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  | 		boost::tie(i1,j1,s1) = factor->sparse(indices); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// add row_start to every row index
 | 
					
						
							|  |  |  | 		transform(i1.begin(), i1.end(), i1.begin(), bind2nd(plus<int>(), row_index)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// splice lists from factor to the end of the global lists
 | 
					
						
							|  |  |  | 		I.splice(I.end(), i1); | 
					
						
							|  |  |  | 		J.splice(J.end(), j1); | 
					
						
							|  |  |  | 		S.splice(S.end(), s1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// advance row start
 | 
					
						
							|  |  |  | 		row_index += factor->numberOfRows(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Convert them to vectors for MATLAB
 | 
					
						
							|  |  |  | 	// TODO: just create a sparse matrix class already
 | 
					
						
							|  |  |  | 	size_t nzmax = S.size(); | 
					
						
							|  |  |  | 	Matrix ijs(3,nzmax); | 
					
						
							|  |  |  | 	copy(I.begin(),I.end(),ijs.begin2()); | 
					
						
							|  |  |  | 	copy(J.begin(),J.end(),ijs.begin2()+nzmax); | 
					
						
							|  |  |  | 	copy(S.begin(),S.end(),ijs.begin2()+2*nzmax); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// return the result
 | 
					
						
							|  |  |  | 	return ijs; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-11 04:19:15 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | VectorConfig GaussianFactorGraph::optimalUpdate(const VectorConfig& x, | 
					
						
							|  |  |  | 		const VectorConfig& d) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create a new graph on step-size
 | 
					
						
							|  |  |  | 	GaussianFactorGraph alphaGraph; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol alphaKey('\224', 1); | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | 	BOOST_FOREACH(sharedFactor factor,factors_) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		sharedFactor alphaFactor = factor->alphaFactor(alphaKey, x,d); | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | 		alphaGraph.push_back(alphaFactor); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// solve it for optimal step-size alpha
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	GaussianConditional::shared_ptr gc = alphaGraph.eliminateOne(alphaKey); | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | 	double alpha = gc->get_d()(0); | 
					
						
							| 
									
										
										
										
											2009-12-28 20:37:34 +08:00
										 |  |  | 	cout << alpha << endl; | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// return updated estimate by stepping in direction d
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  |   return expmap(x, d.scale(alpha)); | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-28 20:37:34 +08:00
										 |  |  | VectorConfig GaussianFactorGraph::steepestDescent(const VectorConfig& x0, | 
					
						
							| 
									
										
										
										
											2009-12-29 00:15:26 +08:00
										 |  |  | 		bool verbose, double epsilon, size_t maxIterations) const { | 
					
						
							|  |  |  | 	return gtsam::steepestDescent(*this, x0, verbose, epsilon, maxIterations); | 
					
						
							| 
									
										
										
										
											2009-12-11 12:59:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-28 20:37:34 +08:00
										 |  |  | boost::shared_ptr<VectorConfig> GaussianFactorGraph::steepestDescent_( | 
					
						
							| 
									
										
										
										
											2009-12-29 00:15:26 +08:00
										 |  |  | 		const VectorConfig& x0, bool verbose, double epsilon, size_t maxIterations) const { | 
					
						
							| 
									
										
										
										
											2009-12-28 20:37:34 +08:00
										 |  |  | 	return boost::shared_ptr<VectorConfig>(new VectorConfig( | 
					
						
							| 
									
										
										
										
											2009-12-29 00:15:26 +08:00
										 |  |  | 			gtsam::conjugateGradientDescent(*this, x0, verbose, epsilon, | 
					
						
							|  |  |  | 					maxIterations))); | 
					
						
							| 
									
										
										
										
											2009-12-12 12:44:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-13 11:02:14 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-28 18:48:48 +08:00
										 |  |  | VectorConfig GaussianFactorGraph::conjugateGradientDescent( | 
					
						
							| 
									
										
										
										
											2009-12-29 00:15:26 +08:00
										 |  |  | 		const VectorConfig& x0, bool verbose, double epsilon, size_t maxIterations) const { | 
					
						
							|  |  |  | 	return gtsam::conjugateGradientDescent(*this, x0, verbose, epsilon, | 
					
						
							|  |  |  | 			maxIterations); | 
					
						
							| 
									
										
										
										
											2009-12-13 11:02:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-12-28 18:48:48 +08:00
										 |  |  | boost::shared_ptr<VectorConfig> GaussianFactorGraph::conjugateGradientDescent_( | 
					
						
							| 
									
										
										
										
											2009-12-29 00:15:26 +08:00
										 |  |  | 		const VectorConfig& x0, bool verbose, double epsilon, size_t maxIterations) const { | 
					
						
							| 
									
										
										
										
											2009-12-28 18:48:48 +08:00
										 |  |  | 	return boost::shared_ptr<VectorConfig>(new VectorConfig( | 
					
						
							| 
									
										
										
										
											2009-12-29 00:15:26 +08:00
										 |  |  | 			gtsam::conjugateGradientDescent(*this, x0, verbose, epsilon, | 
					
						
							|  |  |  | 					maxIterations))); | 
					
						
							| 
									
										
										
										
											2009-12-13 11:02:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 
 |