| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    ChordalBayesNet.h | 
					
						
							|  |  |  |  * @brief   Chordal Bayes Net, the result of eliminating a factor graph | 
					
						
							|  |  |  |  * @brief   ChordalBayesNet | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							|  |  |  | #include <boost/serialization/map.hpp>
 | 
					
						
							|  |  |  | #include <boost/serialization/list.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "ConditionalGaussian.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** Chordal Bayes Net, the result of eliminating a factor graph */ | 
					
						
							|  |  |  | class ChordalBayesNet | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	typedef boost::shared_ptr<ChordalBayesNet> shared_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** nodes keys stored in topological sort order, i.e. from parents to children */ | 
					
						
							|  |  |  | 	std::list<std::string> keys; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** nodes stored on key */ | 
					
						
							| 
									
										
										
										
											2009-08-27 10:00:26 +08:00
										 |  |  | 	typedef std::map<std::string,ConditionalGaussian::shared_ptr> Nodes; | 
					
						
							|  |  |  | 	Nodes nodes; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-27 10:00:26 +08:00
										 |  |  | 	typedef Nodes::iterator iterator; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** Construct an empty net */ | 
					
						
							|  |  |  | 	ChordalBayesNet() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** Copy Constructor */ | 
					
						
							|  |  |  | 	ChordalBayesNet(const ChordalBayesNet& cbn_in) : keys(cbn_in.keys), nodes(cbn_in.nodes) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** Destructor */ | 
					
						
							|  |  |  | 	virtual ~ChordalBayesNet() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** insert: use reverse topological sort (i.e. parents last) */ | 
					
						
							|  |  |  | 	void insert(const std::string& key, ConditionalGaussian::shared_ptr node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** delete */ | 
					
						
							|  |  |  | 	void erase(const std::string& key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** return node with given key */ | 
					
						
							| 
									
										
										
										
											2009-08-28 04:04:10 +08:00
										 |  |  | 	inline ConditionalGaussian::shared_ptr get (const std::string& key) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		const_iterator cg = nodes.find(key); // get node
 | 
					
						
							| 
									
										
										
										
											2009-08-28 22:33:19 +08:00
										 |  |  | 		assert( cg != nodes.end() ); | 
					
						
							| 
									
										
										
										
											2009-08-28 04:04:10 +08:00
										 |  |  | 		return cg->second; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	inline ConditionalGaussian::shared_ptr operator[](const std::string& key) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		const_iterator cg = nodes.find(key); // get node
 | 
					
						
							| 
									
										
										
										
											2009-08-28 22:33:19 +08:00
										 |  |  | 		assert( cg != nodes.end() ); | 
					
						
							| 
									
										
										
										
											2009-08-28 04:04:10 +08:00
										 |  |  | 		return cg->second; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** return begin and end of the nodes. FD: breaks encapsulation? */ | 
					
						
							| 
									
										
										
										
											2009-08-27 10:00:26 +08:00
										 |  |  | 	typedef Nodes::const_iterator const_iterator; | 
					
						
							| 
									
										
										
										
											2009-08-23 06:01:17 +08:00
										 |  |  | 	const_iterator const begin() const {return nodes.begin();} | 
					
						
							|  |  |  | 	const_iterator const end()   const {return nodes.end();} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** optimize */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	boost::shared_ptr<VectorConfig> optimize() const; | 
					
						
							|  |  |  | 	boost::shared_ptr<VectorConfig> optimize(const boost::shared_ptr<VectorConfig> &c) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** print */ | 
					
						
							|  |  |  | 	void print() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** check equality */ | 
					
						
							|  |  |  | 	bool equals(const ChordalBayesNet& cbn) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | 	/** size is the number of nodes */ | 
					
						
							|  |  |  | 	size_t size() const {return nodes.size();} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Return (dense) upper-triangular matrix representation | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	std::pair<Matrix,Vector> matrix() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	/** Serialization function */ | 
					
						
							|  |  |  | 	friend class boost::serialization::access; | 
					
						
							|  |  |  | 	template<class Archive> | 
					
						
							|  |  |  | 	void serialize(Archive & ar, const unsigned int version) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ar & BOOST_SERIALIZATION_NVP(keys); | 
					
						
							|  |  |  | 		ar & BOOST_SERIALIZATION_NVP(nodes); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } /// namespace gtsam
 |