| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    SymbolicConditional.h | 
					
						
							|  |  |  |  * @brief   Symbolic Conditional node for use in Bayes nets | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-30 11:48:32 +08:00
										 |  |  | #include <list>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2009-10-30 11:48:32 +08:00
										 |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | #include <boost/foreach.hpp> // TODO: make cpp file
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | #include <boost/serialization/list.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Conditional.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | 	 * Conditional node for use in a Bayes net | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	class SymbolicConditional: public Conditional { | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | 	private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		std::list<std::string> parents_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-30 11:48:32 +08:00
										 |  |  | 		/** convenience typename for a shared pointer to this class */ | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		typedef boost::shared_ptr<SymbolicConditional> shared_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:55:07 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Empty Constructor to make serialization possible | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		SymbolicConditional(){} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * No parents | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		SymbolicConditional(const std::string& key) : | 
					
						
							|  |  |  | 			Conditional(key) { | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Single parent | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		SymbolicConditional(const std::string& key, const std::string& parent) : | 
					
						
							|  |  |  | 			Conditional(key) { | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | 			parents_.push_back(parent); | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Two parents | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		SymbolicConditional(const std::string& key, const std::string& parent1, | 
					
						
							|  |  |  | 				const std::string& parent2) : | 
					
						
							|  |  |  | 			Conditional(key) { | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | 			parents_.push_back(parent1); | 
					
						
							|  |  |  | 			parents_.push_back(parent2); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 14:34:28 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Three parents | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		SymbolicConditional(const std::string& key, const std::string& parent1, | 
					
						
							|  |  |  | 				const std::string& parent2, const std::string& parent3) : | 
					
						
							|  |  |  | 			Conditional(key) { | 
					
						
							|  |  |  | 			parents_.push_back(parent1); | 
					
						
							|  |  |  | 			parents_.push_back(parent2); | 
					
						
							|  |  |  | 			parents_.push_back(parent3); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * A list | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		SymbolicConditional(const std::string& key, | 
					
						
							|  |  |  | 				const std::list<std::string>& parents) : | 
					
						
							|  |  |  | 			Conditional(key), parents_(parents) { | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** print */ | 
					
						
							|  |  |  | 		void print(const std::string& s = "SymbolicConditional") const { | 
					
						
							| 
									
										
										
										
											2009-11-03 14:29:56 +08:00
										 |  |  | 			std::cout << s << " P(" << key_; | 
					
						
							|  |  |  | 			if (parents_.size()>0) std::cout << " |"; | 
					
						
							| 
									
										
										
										
											2009-10-29 22:34:34 +08:00
										 |  |  | 			BOOST_FOREACH(std::string parent, parents_) std::cout << " " << parent; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 			std::cout << ")" << std::endl; | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** check equality */ | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		bool equals(const Conditional& c, double tol = 1e-9) const { | 
					
						
							|  |  |  | 			if (!Conditional::equals(c)) return false; | 
					
						
							|  |  |  | 			const SymbolicConditional* p = dynamic_cast<const SymbolicConditional*> (&c); | 
					
						
							|  |  |  | 			if (p == NULL) return false; | 
					
						
							|  |  |  | 			return parents_ == p->parents_; | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		/** return parents */ | 
					
						
							| 
									
										
										
										
											2009-11-01 00:56:06 +08:00
										 |  |  | 		std::list<std::string> parents() const { return parents_;} | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		/** find the number of parents */ | 
					
						
							|  |  |  | 		size_t nrParents() const { | 
					
						
							|  |  |  | 			return parents_.size(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	private: | 
					
						
							|  |  |  | 		/** Serialization function */ | 
					
						
							|  |  |  | 		friend class boost::serialization::access; | 
					
						
							|  |  |  | 		template<class Archive> | 
					
						
							|  |  |  | 		void serialize(Archive & ar, const unsigned int version) { | 
					
						
							|  |  |  | 			ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this)); | 
					
						
							|  |  |  | 			ar & BOOST_SERIALIZATION_NVP(parents_); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-28 10:57:38 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | } /// namespace gtsam
 |