| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |  * @file   BayesNet-inl.h | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  |  * @brief  Bayes net template definitions | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  |  * @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-11-09 06:50:26 +08:00
										 |  |  | #include "FactorGraph-inl.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-09 06:49:57 +08:00
										 |  |  | #include "Conditional.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; | 
					
						
							| 
									
										
										
										
											2009-12-20 19:56:33 +08:00
										 |  |  | 		BOOST_REVERSE_FOREACH(sharedConditional conditional,conditionals_) | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 			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-09 06:50:26 +08:00
										 |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	void BayesNet<Conditional>::push_back(const BayesNet<Conditional> bn) { | 
					
						
							|  |  |  | 		BOOST_FOREACH(sharedConditional conditional,bn.conditionals_) | 
					
						
							|  |  |  | 			push_back(conditional); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	void BayesNet<Conditional>::push_front(const BayesNet<Conditional> bn) { | 
					
						
							|  |  |  | 		BOOST_FOREACH(sharedConditional conditional,bn.conditionals_) | 
					
						
							|  |  |  | 			push_front(conditional); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							|  |  |  | 	Ordering BayesNet<Conditional>::ordering() const { | 
					
						
							|  |  |  | 		Ordering ord; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 		BOOST_FOREACH(sharedConditional conditional,conditionals_) | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		   ord.push_back(conditional->key()); | 
					
						
							|  |  |  | 		return ord; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	template<class Conditional> | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 	typename BayesNet<Conditional>::sharedConditional | 
					
						
							|  |  |  | 	BayesNet<Conditional>::operator[](const std::string& key) const { | 
					
						
							|  |  |  | 		const_iterator it = find_if(conditionals_.begin(),conditionals_.end(),onKey<Conditional>(key)); | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 		if (it == conditionals_.end()) throw(invalid_argument( | 
					
						
							|  |  |  | 						"BayesNet::operator['"+key+"']: not found")); | 
					
						
							|  |  |  | 		return *it; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-09 06:50:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |