| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  *  @file Constraint.h | 
					
						
							|  |  |  |  *  @date May 15, 2012 | 
					
						
							|  |  |  |  *  @author Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteFactor.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  | #include <gtsam/discrete/DiscreteValues.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | #include <gtsam_unstable/dllexport.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | class Domain; | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  | using Domains = std::map<Key, Domain>; | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2025-01-31 11:10:50 +08:00
										 |  |  |  * Base class for constraint factors | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |  * Derived classes include SingleValue, BinaryAllDiff, and AllDiff. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-02-14 08:47:12 +08:00
										 |  |  | class GTSAM_UNSTABLE_EXPORT Constraint : public DiscreteFactor { | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |   typedef std::shared_ptr<Constraint> shared_ptr; | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |  protected: | 
					
						
							|  |  |  |   /// Construct unary constraint factor.
 | 
					
						
							| 
									
										
										
										
											2023-01-08 12:11:34 +08:00
										 |  |  |   Constraint(Key j) : DiscreteFactor(KeyVector{j}) {} | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Construct binary constraint factor.
 | 
					
						
							| 
									
										
										
										
											2023-01-08 12:11:34 +08:00
										 |  |  |   Constraint(Key j1, Key j2) : DiscreteFactor(KeyVector{j1, j2}) {} | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Construct n-way constraint factor.
 | 
					
						
							|  |  |  |   Constraint(const KeyVector& js) : DiscreteFactor(js) {} | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// construct from container
 | 
					
						
							|  |  |  |   template <class KeyIterator> | 
					
						
							|  |  |  |   Constraint(KeyIterator beginKey, KeyIterator endKey) | 
					
						
							|  |  |  |       : DiscreteFactor(beginKey, endKey) {} | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |  public: | 
					
						
							|  |  |  |   /// @name Standard Constructors
 | 
					
						
							|  |  |  |   /// @{
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Default constructor for I/O
 | 
					
						
							|  |  |  |   Constraint(); | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Virtual destructor
 | 
					
						
							|  |  |  |   ~Constraint() override {} | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// @}
 | 
					
						
							|  |  |  |   /// @name Standard Interface
 | 
					
						
							|  |  |  |   /// @{
 | 
					
						
							| 
									
										
										
										
											2021-11-18 23:17:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /*
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |    * Ensure Arc-consistency by checking every possible value of domain j. | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |    * @param j domain to be checked | 
					
						
							| 
									
										
										
										
											2025-01-07 02:38:45 +08:00
										 |  |  |    * @param (in/out) domains all domains, but only domains->at(j) will be | 
					
						
							|  |  |  |    * checked. | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |    * @return true if domains->at(j) was changed, false otherwise. | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   virtual bool ensureArcConsistency(Key j, Domains* domains) const = 0; | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Partially apply known values
 | 
					
						
							| 
									
										
										
										
											2021-12-14 02:46:53 +08:00
										 |  |  |   virtual shared_ptr partiallyApply(const DiscreteValues&) const = 0; | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// Partially apply known values, domain version
 | 
					
						
							| 
									
										
										
										
											2021-11-21 04:52:12 +08:00
										 |  |  |   virtual shared_ptr partiallyApply(const Domains&) const = 0; | 
					
						
							| 
									
										
										
										
											2025-01-07 00:17:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /// Multiply factors, DiscreteFactor::shared_ptr edition
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr multiply( | 
					
						
							|  |  |  |       const DiscreteFactor::shared_ptr& df) const override { | 
					
						
							|  |  |  |     return std::make_shared<DecisionTreeFactor>( | 
					
						
							|  |  |  |         this->operator*(df->toDecisionTreeFactor())); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 10:52:49 +08:00
										 |  |  |   /// Multiply by a scalar
 | 
					
						
							|  |  |  |   virtual DiscreteFactor::shared_ptr operator*(double s) const override { | 
					
						
							|  |  |  |     return this->toDecisionTreeFactor() * s; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Multiply by a DecisionTreeFactor and return a DecisionTreeFactor
 | 
					
						
							|  |  |  |   DecisionTreeFactor operator*(const DecisionTreeFactor& dtf) const override { | 
					
						
							|  |  |  |     return this->toDecisionTreeFactor() * dtf; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 02:35:45 +08:00
										 |  |  |   /// divide by DiscreteFactor::shared_ptr f (safely)
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr operator/( | 
					
						
							|  |  |  |       const DiscreteFactor::shared_ptr& df) const override { | 
					
						
							|  |  |  |     return this->toDecisionTreeFactor() / df; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 02:38:45 +08:00
										 |  |  |   /// Get the number of non-zero values contained in this factor.
 | 
					
						
							|  |  |  |   uint64_t nrValues() const override { return 1; }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override { | 
					
						
							|  |  |  |     return toDecisionTreeFactor().sum(nrFrontals); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr sum(const Ordering& keys) const override { | 
					
						
							|  |  |  |     return toDecisionTreeFactor().sum(keys); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 10:52:49 +08:00
										 |  |  |   /// Find the max value
 | 
					
						
							|  |  |  |   double max() const override { return toDecisionTreeFactor().max(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 02:38:45 +08:00
										 |  |  |   DiscreteFactor::shared_ptr max(size_t nrFrontals) const override { | 
					
						
							|  |  |  |     return toDecisionTreeFactor().max(nrFrontals); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr max(const Ordering& keys) const override { | 
					
						
							|  |  |  |     return toDecisionTreeFactor().max(keys); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-03 01:23:27 +08:00
										 |  |  |   /// Compute error for each assignment and return as a tree
 | 
					
						
							|  |  |  |   AlgebraicDecisionTree<Key> errorTree() const override { | 
					
						
							|  |  |  |     throw std::runtime_error("Constraint::error not implemented"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Compute error for each assignment and return as a tree
 | 
					
						
							|  |  |  |   DiscreteFactor::shared_ptr restrict( | 
					
						
							|  |  |  |       const DiscreteValues& assignment) const override { | 
					
						
							|  |  |  |     throw std::runtime_error("Constraint::restrict not implemented"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  |   /// @}
 | 
					
						
							| 
									
										
										
										
											2021-12-25 02:27:02 +08:00
										 |  |  |   /// @name Wrapper support
 | 
					
						
							|  |  |  |   /// @{
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Render as markdown table.
 | 
					
						
							| 
									
										
										
										
											2022-01-03 10:34:22 +08:00
										 |  |  |   std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter, | 
					
						
							|  |  |  |                        const Names& names = {}) const override { | 
					
						
							| 
									
										
										
										
											2023-01-21 05:04:12 +08:00
										 |  |  |     return "`Constraint` on " + std::to_string(size()) + " variables\n"; | 
					
						
							| 
									
										
										
										
											2021-12-25 02:27:02 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-09 21:19:44 +08:00
										 |  |  |   /// Render as html table.
 | 
					
						
							|  |  |  |   std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter, | 
					
						
							|  |  |  |                    const Names& names = {}) const override { | 
					
						
							| 
									
										
										
										
											2023-01-21 05:04:12 +08:00
										 |  |  |     return "<p>Constraint on " + std::to_string(size()) + " variables</p>"; | 
					
						
							| 
									
										
										
										
											2022-01-09 21:19:44 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 02:27:02 +08:00
										 |  |  |   /// @}
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2012-05-15 17:51:26 +08:00
										 |  |  | // DiscreteFactor
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 04:08:01 +08:00
										 |  |  | }  // namespace gtsam
 |