| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |  * @file   BayesNet-inl.h | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  |  * @brief  Bayes chain template definitions | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include <boost/assign/std/vector.hpp> // for +=
 | 
					
						
							|  |  |  | using namespace boost::assign; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | #include "BayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | 	void BayesNet<Conditional>::print(const string& s) const { | 
					
						
							| 
									
										
										
										
											2009-10-30 11:48:32 +08:00
										 |  |  | 		cout << s << ":\n"; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		std::string key; | 
					
						
							|  |  |  | 		BOOST_FOREACH(conditional_ptr conditional,conditionals_) | 
					
						
							|  |  |  | 			conditional->print("Node[" + conditional->key() + "]"); | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | 	bool BayesNet<Conditional>::equals(const BayesNet& cbn, double tol) const { | 
					
						
							| 
									
										
										
										
											2009-10-31 23:24:22 +08:00
										 |  |  | 		if(size() != cbn.size()) return false; | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 		return equal(conditionals_.begin(),conditionals_.end(),cbn.conditionals_.begin(),equals_star<Conditional>(tol)); | 
					
						
							| 
									
										
										
										
											2009-10-30 12:54:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	Ordering BayesNet<Conditional>::ordering() const { | 
					
						
							|  |  |  | 		Ordering ord; | 
					
						
							|  |  |  | 		BOOST_FOREACH(conditional_ptr conditional,conditionals_) | 
					
						
							|  |  |  | 		   ord.push_back(conditional->key()); | 
					
						
							|  |  |  | 		return ord; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 	// predicate to check whether a conditional has the sought key
 | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	class HasKey { | 
					
						
							|  |  |  | 		const string& key_; | 
					
						
							|  |  |  | 	public: | 
					
						
							|  |  |  | 		HasKey(const std::string& key):key_(key) {} | 
					
						
							|  |  |  | 		bool operator()(const boost::shared_ptr<Conditional>& conditional) { | 
					
						
							|  |  |  | 			return (conditional->key()==key_); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	boost::shared_ptr<Conditional> BayesNet<Conditional>::operator[](const std::string& key) const { | 
					
						
							|  |  |  | 		const_iterator it = find_if(conditionals_.begin(),conditionals_.end(),HasKey<Conditional>(key)); | 
					
						
							|  |  |  | 		if (it == conditionals_.end()) throw(invalid_argument( | 
					
						
							|  |  |  | 						"BayesNet::operator['"+key+"']: not found")); | 
					
						
							|  |  |  | 		return *it; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |