| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file ConstrainedLinearFactorGraph.h | 
					
						
							|  |  |  |  * @brief A modified version of LinearFactorGraph that can handle | 
					
						
							|  |  |  |  * linear constraints. | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef CONSTRAINEDLINEARFACTORGRAPH_H_
 | 
					
						
							|  |  |  | #define CONSTRAINEDLINEARFACTORGRAPH_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "LinearFactorGraph.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  | #include "GaussianBayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | #include "LinearConstraint.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ConstrainedLinearFactorGraph: public LinearFactorGraph { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     std::vector<LinearConstraint::shared_ptr> constraints_; /// collection of equality factors
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     // iterators for equality constraints - same interface as linear factors
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     typedef std::vector<LinearConstraint::shared_ptr>::const_iterator constraint_const_iterator; | 
					
						
							|  |  |  |     typedef std::vector<LinearConstraint::shared_ptr>::iterator constraint_iterator; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Default constructor | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 	ConstrainedLinearFactorGraph(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Copy from linear factor graph | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	ConstrainedLinearFactorGraph(const LinearFactorGraph& lfg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	virtual ~ConstrainedLinearFactorGraph(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Add a constraint to the graph | 
					
						
							|  |  |  | 	 * @param constraint is a shared pointer to a linear constraint between nodes in the graph | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	void push_back_constraint(LinearConstraint::shared_ptr constraint); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 23:32:05 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * STL-like indexing into the constraint vector | 
					
						
							|  |  |  | 	 * @param i index of the target constraint | 
					
						
							|  |  |  | 	 * @return the constraint to be returned | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | 	LinearConstraint::shared_ptr constraint_at(const size_t i) const {return constraints_.at(i);} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** return the iterator pointing to the first equality factor */ | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     constraint_const_iterator constraint_begin() const { | 
					
						
							|  |  |  |       return constraints_.begin(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** return the iterator pointing to the last factor */ | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     constraint_const_iterator constraint_end() const { | 
					
						
							|  |  |  |       return constraints_.end(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  |     /** clear the factor graph - re-implemented to include equality factors */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     void clear(){ | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  |       factors_.clear(); | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |       constraints_.clear(); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  |     /** size - reimplemented to include the equality factors_ */ | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     inline size_t size() const { return factors_.size() + constraints_.size(); } | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Check equality - checks equality constraints as well*/ | 
					
						
							|  |  |  |     bool equals(const LinearFactorGraph& fg, double tol=1e-9) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * eliminate factor graph in place(!) in the given order, yielding | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |      * a chordal Bayes net.  Note that even with constraints, | 
					
						
							|  |  |  |      * a constrained factor graph can produce a CBN, because | 
					
						
							|  |  |  |      * constrained conditional gaussian is a subclass of conditional | 
					
						
							|  |  |  |      * gaussian, with a different solving procedure. | 
					
						
							|  |  |  |      * @param ordering is the order to eliminate the variables | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-01 03:53:20 +08:00
										 |  |  |     GaussianBayesNet::shared_ptr eliminate(const Ordering& ordering); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-14 23:32:05 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Picks one of the contraints in a set of constraints to eliminate | 
					
						
							|  |  |  |      * Currently just picks the first one - should probably be optimized | 
					
						
							|  |  |  |      * @param constraints is a set of constraints of which one will be eliminated | 
					
						
							|  |  |  |      * @return one of the constraints to use | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 	LinearConstraint::shared_ptr pick_constraint( | 
					
						
							|  |  |  | 			const std::vector<LinearConstraint::shared_ptr>& constraints) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Eliminates the specified constraint for the selected variable, | 
					
						
							|  |  |  | 	 * and then performs a change of variables on all the constraints in the separator | 
					
						
							|  |  |  | 	 * and reinserts them into the graph. | 
					
						
							|  |  |  | 	 * @param key is the identifier for the node to eliminate | 
					
						
							|  |  |  | 	 * @param separator is the set of constraints to transform and reinsert | 
					
						
							|  |  |  | 	 * @param constraint is the primary constraint to eliminate | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	void update_constraints(const std::string& key, | 
					
						
							|  |  |  | 			const std::vector<LinearConstraint::shared_ptr>& separator, | 
					
						
							|  |  |  | 			const LinearConstraint::shared_ptr& constraint); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |      * Eliminates a node with a constraint on it | 
					
						
							|  |  |  |      * Other factors have a change of variables performed via Schur complement to remove the | 
					
						
							|  |  |  |      * eliminated node. | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     ConstrainedConditionalGaussian::shared_ptr eliminate_constraint(const std::string& key); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |      * optimize a linear factor graph | 
					
						
							|  |  |  |      * @param ordering fg in order | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     VectorConfig optimize(const Ordering& ordering); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |      * Determines if a node has any constraints attached to it | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  |     bool is_constrained(const std::string& key) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Prints the contents of the factor graph with optional name string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     void print(const std::string& s="") const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2009-10-14 23:32:05 +08:00
										 |  |  |      * Pulls out all constraints attached to a particular node | 
					
						
							|  |  |  |      * Note: this removes the constraints in place | 
					
						
							|  |  |  |      * @param key of the node to pull constraints on | 
					
						
							|  |  |  |      * @return a set of constraints | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-14 23:32:05 +08:00
										 |  |  |     std::vector<LinearConstraint::shared_ptr> find_constraints_and_remove(const std::string& key); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * This function returns the best ordering for this linear factor | 
					
						
							|  |  |  |      * graph, computed using colamd for the linear factors with all | 
					
						
							|  |  |  |      * of the equality factors eliminated first | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     Ordering getOrdering() const; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* CONSTRAINEDLINEARFACTORGRAPH_H_ */
 |