| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SubgraphPreconditioner-inl.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   Created on: Jan 17, 2010 | 
					
						
							|  |  |  |  *       Author: nikai | 
					
						
							|  |  |  |  *  Description: subgraph preconditioning conjugate gradient solver | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							|  |  |  | #include "SubgraphPreconditioner.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 03:11:22 +08:00
										 |  |  | #include "graph-inl.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | #include "iterative-inl.h"
 | 
					
						
							|  |  |  | #include "FactorGraph-inl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 	template<class G, class T> | 
					
						
							| 
									
										
										
										
											2010-01-23 08:57:54 +08:00
										 |  |  | 	SubgraphPCG<G, T>::SubgraphPCG(const G& g, const T& theta0) { | 
					
						
							|  |  |  | 		initialize(g,theta0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class G, class T> | 
					
						
							|  |  |  | 	void SubgraphPCG<G, T>::initialize(const G& g, const T& theta0) { | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-20 10:28:23 +08:00
										 |  |  | 		// generate spanning tree
 | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		PredecessorMap<Key> tree = g.template findMinimumSpanningTree<Key, Constraint>(); | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 		list<Key> keys = predecessorMap2Keys(tree); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// split the graph
 | 
					
						
							|  |  |  | 		if (verbose_) cout << "generating spanning tree and split the graph ..."; | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		g.template split<Key, Constraint>(tree, T_, C_); | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 		if (verbose_) cout << T_.size() << " and " << C_.size() << " factors" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 17:24:38 +08:00
										 |  |  | 		// make the ordering
 | 
					
						
							|  |  |  | 		list<Symbol> symbols; | 
					
						
							|  |  |  | 		symbols.resize(keys.size()); | 
					
						
							|  |  |  | 		std::transform(keys.begin(), keys.end(), symbols.begin(), key2symbol<Key>); | 
					
						
							|  |  |  | 		ordering_ = boost::shared_ptr<Ordering>(new Ordering(symbols)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// compose the approximate solution
 | 
					
						
							|  |  |  | 		Key root = keys.back(); | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		theta_bar_ = composePoses<G, Constraint, Pose, T> (T_, tree, theta0[root]); | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 	template<class G, class T> | 
					
						
							| 
									
										
										
										
											2010-01-23 08:57:54 +08:00
										 |  |  | 	boost::shared_ptr<SubgraphPreconditioner> SubgraphPCG<G, T>::linearize(const G& g, const T& theta_bar) const { | 
					
						
							|  |  |  | 		SubgraphPreconditioner::sharedFG Ab1 = T_.linearize_(theta_bar); | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		SubgraphPreconditioner::sharedFG Ab2 = C_.linearize_(theta_bar); | 
					
						
							| 
									
										
										
										
											2010-01-23 08:57:54 +08:00
										 |  |  | #ifdef TIMING
 | 
					
						
							|  |  |  | 		SubgraphPreconditioner::sharedBayesNet Rc1; | 
					
						
							|  |  |  | 		SubgraphPreconditioner::sharedConfig xbar; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 		GaussianFactorGraph sacrificialAb1 = T_.linearize(theta_bar); // duplicate !!!!!
 | 
					
						
							|  |  |  | 		SubgraphPreconditioner::sharedBayesNet Rc1 = sacrificialAb1.eliminate_(*ordering_); | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		SubgraphPreconditioner::sharedConfig xbar = gtsam::optimize_(*Rc1); | 
					
						
							| 
									
										
										
										
											2010-01-23 08:57:54 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 		return boost::shared_ptr<SubgraphPreconditioner>(new SubgraphPreconditioner(Ab1, Ab2, Rc1, xbar)); | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class G, class T> | 
					
						
							| 
									
										
										
										
											2010-01-20 10:28:23 +08:00
										 |  |  | 	VectorConfig SubgraphPCG<G, T>::optimize(SubgraphPreconditioner& system) const { | 
					
						
							| 
									
										
										
										
											2010-01-19 12:18:59 +08:00
										 |  |  | 		//TODO: 3 is hard coded here
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 		VectorConfig zeros; | 
					
						
							| 
									
										
										
										
											2010-01-20 10:28:23 +08:00
										 |  |  | 		BOOST_FOREACH(const Symbol& j, *ordering_) zeros.insert(j,zero(Pose::dim())); | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Solve the subgraph PCG
 | 
					
						
							|  |  |  | 		VectorConfig ybar = conjugateGradients<SubgraphPreconditioner, VectorConfig, | 
					
						
							|  |  |  | 				Errors> (system, zeros, verbose_, epsilon_, epsilon_abs_, maxIterations_); | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		VectorConfig xbar = system.x(ybar); | 
					
						
							|  |  |  | 		return xbar; | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 13:51:19 +08:00
										 |  |  | } |