| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    BayesTree | 
					
						
							|  |  |  |  * @brief   Bayes Tree is a tree of cliques of a Bayes Chain | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2010-08-27 23:31:20 +08:00
										 |  |  | //#include <boost/serialization/map.hpp>
 | 
					
						
							|  |  |  | //#include <boost/serialization/list.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-12-09 05:40:09 +08:00
										 |  |  | #include <stdexcept>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <deque>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/types.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/FactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/BayesNet.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Bayes tree | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 	 * Templated on the CONDITIONAL class, the type of node in the underlying Bayes chain. | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	 * This could be a ConditionalProbabilityTable, a GaussianConditional, or a SymbolicConditional | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 	template<class CONDITIONAL> | 
					
						
							|  |  |  | 	class BayesTree: public Testable<BayesTree<CONDITIONAL> > { | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-22 08:06:54 +08:00
										 |  |  | 	  typedef boost::shared_ptr<BayesTree<CONDITIONAL> > shared_ptr; | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		typedef boost::shared_ptr<CONDITIONAL> sharedConditional; | 
					
						
							|  |  |  | 		typedef boost::shared_ptr<BayesNet<CONDITIONAL> > sharedBayesNet; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 		/** A Clique in the tree is an incomplete Bayes net: the variables
 | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 		 * in the Bayes net are the frontal nodes, and the variables conditioned | 
					
						
							|  |  |  | 		 * on is the separator. We also have pointers up and down the tree. | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		struct Clique: public BayesNet<CONDITIONAL> { | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 12:41:01 +08:00
										 |  |  | 			typedef typename boost::shared_ptr<Clique> shared_ptr; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			typedef typename boost::weak_ptr<Clique> weak_ptr; | 
					
						
							|  |  |  | 			weak_ptr parent_; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 			std::list<shared_ptr> children_; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 			std::list<Index> separator_; /** separator keys */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 			typename CONDITIONAL::Factor::shared_ptr cachedFactor_; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 			friend class BayesTree<CONDITIONAL>; | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 			//* Constructor */
 | 
					
						
							| 
									
										
										
										
											2010-07-31 15:19:03 +08:00
										 |  |  | 			Clique(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 			Clique(const sharedConditional& conditional); | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 			Clique(const BayesNet<CONDITIONAL>& bayesNet); | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 06:51:12 +08:00
										 |  |  | 			/** return keys in frontal:separator order */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 			std::vector<Index> keys() const; | 
					
						
							| 
									
										
										
										
											2009-11-09 06:51:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 12:41:01 +08:00
										 |  |  | 			/** print this node */ | 
					
						
							| 
									
										
										
										
											2009-11-21 14:07:46 +08:00
										 |  |  | 			void print(const std::string& s = "") const; | 
					
						
							| 
									
										
										
										
											2009-11-08 12:41:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 			/** The size *includes* the separator */ | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 			size_t size() const { | 
					
						
							|  |  |  | 				return this->conditionals_.size() + separator_.size(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2009-11-02 13:17:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 12:41:01 +08:00
										 |  |  | 			/** is this the root of a Bayes tree ? */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			inline bool isRoot() const { return parent_.expired();} | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			/** return the const reference of children */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			std::list<shared_ptr>& children() { return children_; } | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			const std::list<shared_ptr>& children() const { return children_; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			/** Reference the cached factor */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 			typename CONDITIONAL::Factor::shared_ptr& cachedFactor() { return cachedFactor_; } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 02:05:12 +08:00
										 |  |  | 			/** The size of subtree rooted at this clique, i.e., nr of Cliques */ | 
					
						
							|  |  |  | 			size_t treeSize() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 			/** print this node and entire subtree below it */ | 
					
						
							| 
									
										
										
										
											2009-11-23 02:06:28 +08:00
										 |  |  | 			void printTree(const std::string& indent="") const; | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			/** Permute the variables in the whole subtree rooted at this clique */ | 
					
						
							|  |  |  | 			void permuteWithInverse(const Permutation& inversePermutation); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/** Permute variables when they only appear in the separators.  In this
 | 
					
						
							|  |  |  | 			 * case the running intersection property will be used to prevent always | 
					
						
							|  |  |  | 			 * traversing the whole tree.  Returns whether any separator variables in | 
					
						
							|  |  |  | 			 * this subtree were reordered. | 
					
						
							|  |  |  | 			 */ | 
					
						
							|  |  |  | 			bool permuteSeparatorWithInverse(const Permutation& inversePermutation); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 			/** return the conditional P(S|Root) on the separator given the root */ | 
					
						
							|  |  |  | 			// TODO: create a cached version
 | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 			BayesNet<CONDITIONAL> shortcut(shared_ptr root); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 			/** return the marginal P(C) of the clique */ | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | 			FactorGraph<typename CONDITIONAL::Factor> marginal(shared_ptr root); | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 			/** return the joint P(C1,C2), where C1==this. TODO: not a method? */ | 
					
						
							|  |  |  | 			FactorGraph<typename CONDITIONAL::Factor> joint(shared_ptr C2, shared_ptr root); | 
					
						
							| 
									
										
										
										
											2009-11-23 00:46:29 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 00:46:29 +08:00
										 |  |  | 		// typedef for shared pointers to cliques
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 		typedef boost::shared_ptr<Clique> sharedClique; | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 00:46:29 +08:00
										 |  |  | 		// A convenience class for a list of shared cliques
 | 
					
						
							|  |  |  | 		struct Cliques : public std::list<sharedClique>, public Testable<Cliques> { | 
					
						
							| 
									
										
										
										
											2009-11-23 01:40:24 +08:00
										 |  |  | 			void print(const std::string& s = "Cliques") const; | 
					
						
							|  |  |  | 			bool equals(const Cliques& other, double tol = 1e-9) const; | 
					
						
							| 
									
										
										
										
											2009-11-23 00:46:29 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/** clique statistics */ | 
					
						
							|  |  |  | 		struct CliqueStats { | 
					
						
							|  |  |  | 			double avgConditionalSize; | 
					
						
							|  |  |  | 			std::size_t maxConditionalSize; | 
					
						
							|  |  |  | 			double avgSeparatorSize; | 
					
						
							|  |  |  | 			std::size_t maxSeparatorSize; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** store all the sizes  */ | 
					
						
							|  |  |  | 		struct CliqueData { | 
					
						
							|  |  |  | 			std::vector<std::size_t> conditionalSizes; | 
					
						
							|  |  |  | 			std::vector<std::size_t> separatorSizes; | 
					
						
							|  |  |  | 			CliqueStats getStats() const; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	protected: | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 		/** Map from keys to Clique */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		typedef std::deque<sharedClique> Nodes; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 		Nodes nodes_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/** private helper method for saving the Tree to a text file in GraphViz format */ | 
					
						
							|  |  |  | 		void saveGraph(std::ostream &s, sharedClique clique, | 
					
						
							|  |  |  | 				int parentnum = 0) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Gather data on a single clique */ | 
					
						
							|  |  |  | 		void getCliqueData(CliqueData& stats, sharedClique clique) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 	protected: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-09 08:13:44 +08:00
										 |  |  | 		/** Root clique */ | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 		sharedClique root_; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 		/** remove a clique: warning, can result in a forest */ | 
					
						
							|  |  |  | 		void removeClique(sharedClique clique); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** add a clique (top down) */ | 
					
						
							| 
									
										
										
										
											2009-11-08 01:24:05 +08:00
										 |  |  | 		sharedClique addClique(const sharedConditional& conditional, | 
					
						
							| 
									
										
										
										
											2009-11-21 14:07:46 +08:00
										 |  |  | 				sharedClique parent_clique = sharedClique()); | 
					
						
							| 
									
										
										
										
											2009-11-04 11:22:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-13 15:09:27 +08:00
										 |  |  | 		/** add a clique (bottom up) */ | 
					
						
							|  |  |  | 		sharedClique addClique(const sharedConditional& conditional, | 
					
						
							|  |  |  | 				std::list<sharedClique>& child_cliques); | 
					
						
							| 
									
										
										
										
											2009-11-21 11:38:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		/** Add a conditional to the front of a clique, i.e. a conditional whose
 | 
					
						
							|  |  |  | 		 * parents are already in the clique or its separators.  This function does | 
					
						
							|  |  |  | 		 * not check for this condition, it just updates the data structures. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		void addToCliqueFront(const sharedConditional& conditional, const sharedClique& clique); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Fill the nodes index for a subtree */ | 
					
						
							|  |  |  | 		void fillNodesIndex(const sharedClique& subtree); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Create an empty Bayes Tree */ | 
					
						
							|  |  |  | 		BayesTree(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		/** Create a Bayes Tree from a Bayes Net */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		BayesTree(const BayesNet<CONDITIONAL>& bayesNet); | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Create a Bayes Tree from a Bayes Net and some subtrees. The Bayes net corresponds to the | 
					
						
							|  |  |  | 		 * new root clique and the subtrees are connected to the root clique. | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		BayesTree(const BayesNet<CONDITIONAL>& bayesNet, std::list<BayesTree<CONDITIONAL> > subtrees); | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 		/** Destructor */ | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 		virtual ~BayesTree() { | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Constructing Bayes trees | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/** Insert a new conditional */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		void insert(const sharedConditional& conditional); | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Insert a new clique corresponding to the given Bayes net.
 | 
					
						
							|  |  |  | 		 * It is the caller's responsibility to decide whether the given Bayes net is a valid clique, | 
					
						
							|  |  |  | 		 * i.e. all the variables (frontal and separator) are connected | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		sharedClique insert(const BayesNet<CONDITIONAL>& bayesNet, | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 				std::list<sharedClique>& children, bool isRootClique = false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Hang a new subtree off of the existing tree.  This finds the appropriate | 
					
						
							|  |  |  | 		 * parent clique for the subtree (which may be the root), and updates the | 
					
						
							|  |  |  | 		 * nodes index with the new cliques in the subtree.  None of the frontal | 
					
						
							|  |  |  | 		 * variables in the subtree may appear in the separators of the existing | 
					
						
							|  |  |  | 		 * BayesTree. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		void insert(const sharedClique& subtree); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Querying Bayes trees | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-01-23 07:52:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 		/** check equality */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		bool equals(const BayesTree<CONDITIONAL>& other, double tol = 1e-9) const; | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 		/**
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		 * Find parent clique of a conditional.  It will look at all parents and | 
					
						
							|  |  |  | 		 * return the one with the lowest index in the ordering. | 
					
						
							| 
									
										
										
										
											2010-01-22 10:27:26 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<class CONTAINER> | 
					
						
							|  |  |  | 		Index findParentClique(const CONTAINER& parents) const; | 
					
						
							| 
									
										
										
										
											2010-01-20 09:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 		/** number of cliques */ | 
					
						
							|  |  |  | 		inline size_t size() const { | 
					
						
							| 
									
										
										
										
											2010-01-20 12:23:35 +08:00
										 |  |  | 			if(root_) | 
					
						
							|  |  |  | 				return root_->treeSize(); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				return 0; | 
					
						
							| 
									
										
										
										
											2009-11-07 23:58:45 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-31 22:12:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 		/** return root clique */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		const sharedClique& root() const { return root_;	} | 
					
						
							|  |  |  | 		sharedClique& root() { return root_; } | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-06 13:55:29 +08:00
										 |  |  | 		/** find the clique to which key belongs */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 		sharedClique operator[](Index key) const { | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 			return nodes_.at(key); | 
					
						
							| 
									
										
										
										
											2009-11-06 13:55:29 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/** Gather data on all cliques */ | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 		CliqueData getCliqueData() const; | 
					
						
							| 
									
										
										
										
											2010-01-23 07:52:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 		/** return marginal on any variable */ | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | 		typename CONDITIONAL::Factor::shared_ptr marginal(Index key) const; | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** return marginal on any variable, as a Bayes Net */ | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | 		typename BayesNet<CONDITIONAL>::shared_ptr marginalBayesNet(Index key) const; | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-23 06:11:23 +08:00
										 |  |  | 		/** return joint on two variables */ | 
					
						
							|  |  |  | 		typename FactorGraph<typename CONDITIONAL::Factor>::shared_ptr joint(Index key1, Index key2) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** return joint on two variables as a BayesNet */ | 
					
						
							|  |  |  | 		typename BayesNet<CONDITIONAL>::shared_ptr jointBayesNet(Index key1, Index key2) const; | 
					
						
							| 
									
										
										
										
											2009-11-23 00:04:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-07 14:11:19 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Read only with side effects | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** print */ | 
					
						
							|  |  |  | 		void print(const std::string& s = "") const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** saves the Tree to a text file in GraphViz format */ | 
					
						
							|  |  |  | 		void saveGraph(const std::string& s) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Altering Bayes trees | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		/** Remove all nodes */ | 
					
						
							| 
									
										
										
										
											2010-01-20 12:23:35 +08:00
										 |  |  | 		void clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Remove path from clique to root and return that path as factors | 
					
						
							|  |  |  | 		 * plus a list of orphaned subtree roots. Used in removeTop below. | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 02:13:04 +08:00
										 |  |  | 		void removePath(sharedClique clique, BayesNet<CONDITIONAL>& bn, Cliques& orphans); | 
					
						
							| 
									
										
										
										
											2009-11-23 00:04:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 		/**
 | 
					
						
							| 
									
										
										
										
											2010-01-19 06:37:44 +08:00
										 |  |  | 		 * Given a list of keys, turn "contaminated" part of the tree back into a factor graph. | 
					
						
							| 
									
										
										
										
											2009-11-23 08:02:06 +08:00
										 |  |  | 		 * Factors and orphans are added to the in/out arguments. | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<class CONTAINER> | 
					
						
							|  |  |  | 		void removeTop(const CONTAINER& keys, BayesNet<CONDITIONAL>& bn, Cliques& orphans); | 
					
						
							| 
									
										
										
										
											2009-11-23 01:34:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 07:15:36 +08:00
										 |  |  | 	}; // BayesTree
 | 
					
						
							| 
									
										
										
										
											2009-10-28 04:23:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } /// namespace gtsam
 |