| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SingleValue.cpp | 
					
						
							|  |  |  |  * @brief domain constraint | 
					
						
							|  |  |  |  * @date Feb 13, 2012 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 08:47:19 +08:00
										 |  |  | #include <gtsam_unstable/discrete/SingleValue.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/discrete/Domain.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 07:12:17 +08:00
										 |  |  | #include <gtsam/discrete/DecisionTreeFactor.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <boost/make_shared.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   using namespace std; | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   void SingleValue::print(const string& s, | 
					
						
							| 
									
										
										
										
											2013-11-09 00:35:28 +08:00
										 |  |  |       const KeyFormatter& formatter) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     cout << s << "SingleValue on " << "j=" << formatter(keys_[0]) | 
					
						
							|  |  |  |         << " with value " << value_ << endl; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   double SingleValue::operator()(const Values& values) const { | 
					
						
							|  |  |  |     return (double) (values.at(keys_[0]) == value_); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   DecisionTreeFactor SingleValue::toDecisionTreeFactor() const { | 
					
						
							|  |  |  |     DiscreteKeys keys; | 
					
						
							|  |  |  |     keys += DiscreteKey(keys_[0],cardinality_); | 
					
						
							|  |  |  |     vector<double> table; | 
					
						
							|  |  |  |     for (size_t i1 = 0; i1 < cardinality_; i1++) | 
					
						
							|  |  |  |       table.push_back(i1 == value_); | 
					
						
							|  |  |  |     DecisionTreeFactor converted(keys, table); | 
					
						
							|  |  |  |     return converted; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   DecisionTreeFactor SingleValue::operator*(const DecisionTreeFactor& f) const { | 
					
						
							|  |  |  |     // TODO: can we do this more efficiently?
 | 
					
						
							|  |  |  |     return toDecisionTreeFactor() * f; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   bool SingleValue::ensureArcConsistency(size_t j, | 
					
						
							|  |  |  |       vector<Domain>& domains) const { | 
					
						
							|  |  |  |     if (j != keys_[0]) throw invalid_argument( | 
					
						
							|  |  |  |         "SingleValue check on wrong domain"); | 
					
						
							|  |  |  |     Domain& D = domains[j]; | 
					
						
							|  |  |  |     if (D.isSingleton()) { | 
					
						
							|  |  |  |       if (D.firstValue() != value_) throw runtime_error("Unsatisfiable"); | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     D = Domain(discreteKey(),value_); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   Constraint::shared_ptr SingleValue::partiallyApply(const Values& values) const { | 
					
						
							|  |  |  |     Values::const_iterator it = values.find(keys_[0]); | 
					
						
							|  |  |  |     if (it != values.end() && it->second != value_) throw runtime_error( | 
					
						
							|  |  |  |         "SingleValue::partiallyApply: unsatisfiable"); | 
					
						
							|  |  |  |     return boost::make_shared < SingleValue > (keys_[0], cardinality_, value_); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   Constraint::shared_ptr SingleValue::partiallyApply( | 
					
						
							|  |  |  |       const vector<Domain>& domains) const { | 
					
						
							|  |  |  |     const Domain& Dk = domains[keys_[0]]; | 
					
						
							|  |  |  |     if (Dk.isSingleton() && !Dk.contains(value_)) throw runtime_error( | 
					
						
							|  |  |  |         "SingleValue::partiallyApply: unsatisfiable"); | 
					
						
							|  |  |  |     return boost::make_shared < SingleValue > (discreteKey(), value_); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-04-16 06:35:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | } // namespace gtsam
 |