| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    ISAM-inl.h | 
					
						
							|  |  |  |  * @brief   Incremental update functionality (iSAM) for BayesTree. | 
					
						
							|  |  |  |  * @author  Michael Kaess | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/assign/std/list.hpp> // for operator +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Conditional.h"
 | 
					
						
							|  |  |  | #include "ISAM.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-20 12:23:35 +08:00
										 |  |  | #include "BayesTree-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** Create an empty Bayes Tree */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	ISAM<Conditional>::ISAM() : BayesTree<Conditional>() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** Create a Bayes Tree from a Bayes Net */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							| 
									
										
										
										
											2010-01-20 12:23:35 +08:00
										 |  |  | 	ISAM<Conditional>::ISAM(const BayesNet<Conditional>& bayesNet) : | 
					
						
							|  |  |  | 	  BayesTree<Conditional>(bayesNet) {} | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	template<class Factor> | 
					
						
							|  |  |  | 	void ISAM<Conditional>::update_internal(const FactorGraph<Factor>& newFactors, Cliques& orphans) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Remove the contaminated part of the Bayes tree
 | 
					
						
							| 
									
										
										
										
											2010-01-21 08:38:22 +08:00
										 |  |  | 		BayesNet<Conditional> bn; | 
					
						
							|  |  |  | 		removeTop(newFactors.keys(), bn, orphans); | 
					
						
							|  |  |  | 		FactorGraph<Factor> factors(bn); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// add the factors themselves
 | 
					
						
							|  |  |  | 		factors.push_back(newFactors); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// create an ordering for the new and contaminated factors
 | 
					
						
							|  |  |  | 		Ordering ordering; | 
					
						
							| 
									
										
										
										
											2010-02-21 23:23:27 +08:00
										 |  |  | #ifndef SORT_KEYS
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 			ordering = factors.getOrdering(); | 
					
						
							| 
									
										
										
										
											2010-02-21 23:23:27 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 			list<Symbol> keys = factors.keys(); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 			keys.sort(); // todo: correct sorting order?
 | 
					
						
							|  |  |  | 			ordering = keys; | 
					
						
							| 
									
										
										
										
											2010-02-21 23:23:27 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 		// Create Index from ordering
 | 
					
						
							|  |  |  | 		IndexTable<Symbol> index(ordering); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 		// eliminate into a Bayes net
 | 
					
						
							|  |  |  | 		BayesNet<Conditional> bayesNet = eliminate<Factor, Conditional>(factors,ordering); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// insert conditionals back in, straight into the topless bayesTree
 | 
					
						
							|  |  |  | 		typename BayesNet<Conditional>::const_reverse_iterator rit; | 
					
						
							|  |  |  | 		for ( rit=bayesNet.rbegin(); rit != bayesNet.rend(); ++rit ) | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 			this->insert(*rit, index); | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		int count = 0; | 
					
						
							|  |  |  | 		// add orphans to the bottom of the new tree
 | 
					
						
							|  |  |  | 		BOOST_FOREACH(sharedClique orphan, orphans) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 			Symbol parentRepresentative = findParentClique(orphan->separator_, index); | 
					
						
							|  |  |  | 			sharedClique parent = (*this)[parentRepresentative]; | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			parent->children_ += orphan; | 
					
						
							|  |  |  | 			orphan->parent_ = parent; // set new parent!
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	template<class Factor> | 
					
						
							|  |  |  | 	void ISAM<Conditional>::update(const FactorGraph<Factor>& newFactors) { | 
					
						
							|  |  |  | 		Cliques orphans; | 
					
						
							|  |  |  | 		this->update_internal<Factor>(newFactors, orphans); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /// namespace gtsam
 |