| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * CSP.h | 
					
						
							|  |  |  |  * @brief Constraint Satisfaction Problem class | 
					
						
							|  |  |  |  * @date Feb 6, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 08:47:19 +08:00
										 |  |  | #include <gtsam_unstable/discrete/AllDiff.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/discrete/SingleValue.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 07:12:17 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Constraint Satisfaction Problem class | 
					
						
							|  |  |  |    * A specialization of a DiscreteFactorGraph. | 
					
						
							|  |  |  |    * It knows about CSP-specific constraints and algorithms | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2013-03-14 02:56:21 +08:00
										 |  |  |   class GTSAM_UNSTABLE_EXPORT CSP: public DiscreteFactorGraph { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** A map from keys to values */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     typedef KeyVector Indices; | 
					
						
							| 
									
										
										
										
											2013-11-09 00:35:28 +08:00
										 |  |  |     typedef Assignment<Key> Values; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     typedef boost::shared_ptr<Values> sharedValues; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //    /// Constructor
 | 
					
						
							|  |  |  | //    CSP() {
 | 
					
						
							|  |  |  | //    }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Add a unary constraint, allowing only a single value
 | 
					
						
							|  |  |  |     void addSingleValue(const DiscreteKey& dkey, size_t value) { | 
					
						
							|  |  |  |       boost::shared_ptr<SingleValue> factor(new SingleValue(dkey, value)); | 
					
						
							|  |  |  |       push_back(factor); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Add a binary AllDiff constraint
 | 
					
						
							|  |  |  |     void addAllDiff(const DiscreteKey& key1, const DiscreteKey& key2) { | 
					
						
							|  |  |  |       boost::shared_ptr<BinaryAllDiff> factor( | 
					
						
							|  |  |  |           new BinaryAllDiff(key1, key2)); | 
					
						
							|  |  |  |       push_back(factor); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Add a general AllDiff constraint
 | 
					
						
							|  |  |  |     void addAllDiff(const DiscreteKeys& dkeys) { | 
					
						
							|  |  |  |       boost::shared_ptr<AllDiff> factor(new AllDiff(dkeys)); | 
					
						
							|  |  |  |       push_back(factor); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //    /** return product of all factors as a single factor */
 | 
					
						
							|  |  |  | //    DecisionTreeFactor product() const {
 | 
					
						
							|  |  |  | //      DecisionTreeFactor result;
 | 
					
						
							| 
									
										
										
										
											2016-05-21 11:41:41 +08:00
										 |  |  | //      for(const sharedFactor& factor: *this)
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //        if (factor) result = (*factor) * result;
 | 
					
						
							|  |  |  | //      return result;
 | 
					
						
							|  |  |  | //    }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Find the best total assignment - can be expensive
 | 
					
						
							| 
									
										
										
										
											2019-10-20 13:15:20 +08:00
										 |  |  |     sharedValues optimalAssignment() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// Find the best total assignment - can be expensive
 | 
					
						
							|  |  |  |     sharedValues optimalAssignment(const Ordering& ordering) const; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | //    /*
 | 
					
						
							|  |  |  | //     * Perform loopy belief propagation
 | 
					
						
							|  |  |  | //     * True belief propagation would check for each value in domain
 | 
					
						
							|  |  |  | //     * whether any satisfying separator assignment can be found.
 | 
					
						
							|  |  |  | //     * This corresponds to hyper-arc consistency in CSP speak.
 | 
					
						
							|  |  |  | //     * This can be done by creating a mini-factor graph and search.
 | 
					
						
							|  |  |  | //     * For a nine-by-nine Sudoku, the search tree will be 8+6+6=20 levels deep.
 | 
					
						
							|  |  |  | //     * It will be very expensive to exclude values that way.
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //     void applyBeliefPropagation(size_t nrIterations = 10) const;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Apply arc-consistency ~ Approximate loopy belief propagation | 
					
						
							|  |  |  |      * We need to give the domains to a constraint, and it returns | 
					
						
							|  |  |  |      * a domain whose values don't conflict in the arc-consistency way. | 
					
						
							|  |  |  |      * TODO: should get cardinality from Indices | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     void runArcConsistency(size_t cardinality, size_t nrIterations = 10, | 
					
						
							|  |  |  |         bool print = false) const; | 
					
						
							|  |  |  |   }; // CSP
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // gtsam
 | 
					
						
							|  |  |  | 
 |