| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |  * @file    GaussianConditional.h | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Conditional Gaussian Base class | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <boost/utility.hpp>
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include <boost/serialization/map.hpp>
 | 
					
						
							|  |  |  | #include <boost/serialization/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Conditional.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Matrix.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #include "Key.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | class Ordering; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * A conditional Gaussian functions as the node in a Bayes network | 
					
						
							|  |  |  |  * It has a set of parents y,z, etc. and implements a probability density on x. | 
					
						
							|  |  |  |  * The negative log-probability is given by || Rx - (d - Sy - Tz - ...)||^2 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | class GaussianConditional : public Conditional { | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	typedef std::map<Symbol, Matrix> Parents; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	typedef Parents::const_iterator const_iterator; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	typedef boost::shared_ptr<GaussianConditional> shared_ptr; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** the triangular matrix (square root information matrix) - unit normalized */ | 
					
						
							|  |  |  | 	Matrix R_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** the names and the matrices connecting to parent nodes */ | 
					
						
							|  |  |  | 	Parents parents_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** the RHS vector */ | 
					
						
							|  |  |  | 	Vector d_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** vector of standard deviations */ | 
					
						
							|  |  |  | 	Vector sigmas_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** default constructor needed for serialization */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	GaussianConditional(){} | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	GaussianConditional(const Symbol& key) : | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 		Conditional (key) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor with no parents
 | 
					
						
							|  |  |  | 	 * |Rx-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	GaussianConditional(const Symbol& key, Vector d, Matrix R, Vector sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor with only one parent
 | 
					
						
							|  |  |  | 	 * |Rx+Sy-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	GaussianConditional(const Symbol& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 			const Symbol& name1, Matrix S, Vector sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor with two parents
 | 
					
						
							|  |  |  | 	 * |Rx+Sy+Tz-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	GaussianConditional(const Symbol& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 			const Symbol& name1, Matrix S, const Symbol& name2, Matrix T, Vector sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * constructor with number of arbitrary parents | 
					
						
							|  |  |  | 	 * |Rx+sum(Ai*xi)-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	GaussianConditional(const Symbol& key, const Vector& d, | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 			const Matrix& R, const Parents& parents, Vector sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** deconstructor */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	virtual ~GaussianConditional() {} | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** print */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	void print(const std::string& = "GaussianConditional") const; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** equals function */ | 
					
						
							|  |  |  | 	bool equals(const Conditional &cg, double tol = 1e-9) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** dimension of multivariate variable */ | 
					
						
							|  |  |  | 	size_t dim() const { return R_.size2();} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** return all parents */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	std::list<Symbol> parents() const; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	/** return stuff contained in GaussianConditional */ | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	const Vector& get_d() const {return d_;} | 
					
						
							|  |  |  | 	const Matrix& get_R() const {return R_;} | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	const Vector& get_sigmas() const {return sigmas_;} | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** STL like, return the iterator pointing to the first node */ | 
					
						
							|  |  |  | 	const_iterator const parentsBegin() const { | 
					
						
							|  |  |  | 		return parents_.begin(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** STL like, return the iterator pointing to the last node */ | 
					
						
							|  |  |  | 	const_iterator const parentsEnd() const { | 
					
						
							|  |  |  | 		return parents_.end(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** find the number of parents */ | 
					
						
							|  |  |  | 	size_t nrParents() const { | 
					
						
							|  |  |  | 		return parents_.size(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** determine whether a key is among the parents */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	size_t contains(const Symbol& key) const { | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 		return parents_.count(key); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	 * solve a conditional Gaussian | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	 * @param x configuration in which the parents values (y,z,...) are known | 
					
						
							|  |  |  | 	 * @return solution x = R \ (d - Sy - Tz - ...) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	virtual Vector solve(const VectorConfig& x) const; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	 * adds a parent | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	void add(const Symbol& key, Matrix S){ | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 		parents_.insert(make_pair(key, S)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 	/** Serialization function */ | 
					
						
							|  |  |  | 	friend class boost::serialization::access; | 
					
						
							|  |  |  | 	template<class Archive> | 
					
						
							|  |  |  | 	void serialize(Archive & ar, const unsigned int version) { | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 		ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this)); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 		ar & BOOST_SERIALIZATION_NVP(R_); | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 		ar & BOOST_SERIALIZATION_NVP(parents_); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 		ar & BOOST_SERIALIZATION_NVP(d_); | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 		ar & BOOST_SERIALIZATION_NVP(sigmas_); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } |