Merge branch 'ring' into discrete-elimination-refactor
						commit
						f9a9801912
					
				| 
						 | 
					@ -20,12 +20,12 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <gtsam/base/Testable.h>
 | 
					#include <gtsam/base/Testable.h>
 | 
				
			||||||
#include <gtsam/discrete/DecisionTree-inl.h>
 | 
					#include <gtsam/discrete/DecisionTree-inl.h>
 | 
				
			||||||
 | 
					#include <gtsam/discrete/Ring.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <algorithm>
 | 
					#include <iomanip>
 | 
				
			||||||
#include <limits>
 | 
					#include <limits>
 | 
				
			||||||
#include <map>
 | 
					#include <map>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <iomanip>
 | 
					 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace gtsam {
 | 
					namespace gtsam {
 | 
				
			||||||
| 
						 | 
					@ -55,26 +55,6 @@ namespace gtsam {
 | 
				
			||||||
   public:
 | 
					   public:
 | 
				
			||||||
    using Base = DecisionTree<L, double>;
 | 
					    using Base = DecisionTree<L, double>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /** The Real ring with addition and multiplication */
 | 
					 | 
				
			||||||
    struct Ring {
 | 
					 | 
				
			||||||
      static inline double zero() { return 0.0; }
 | 
					 | 
				
			||||||
      static inline double one() { return 1.0; }
 | 
					 | 
				
			||||||
      static inline double add(const double& a, const double& b) {
 | 
					 | 
				
			||||||
        return a + b;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      static inline double max(const double& a, const double& b) {
 | 
					 | 
				
			||||||
        return std::max(a, b);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      static inline double mul(const double& a, const double& b) {
 | 
					 | 
				
			||||||
        return a * b;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      static inline double div(const double& a, const double& b) {
 | 
					 | 
				
			||||||
        return a / b;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      static inline double id(const double& x) { return x; }
 | 
					 | 
				
			||||||
      static inline double negate(const double& x) { return -x; }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    AlgebraicDecisionTree(double leaf = 1.0) : Base(leaf) {}
 | 
					    AlgebraicDecisionTree(double leaf = 1.0) : Base(leaf) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Explicitly non-explicit constructor
 | 
					    // Explicitly non-explicit constructor
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,11 +21,12 @@
 | 
				
			||||||
#include <gtsam/discrete/AlgebraicDecisionTree.h>
 | 
					#include <gtsam/discrete/AlgebraicDecisionTree.h>
 | 
				
			||||||
#include <gtsam/discrete/DiscreteFactor.h>
 | 
					#include <gtsam/discrete/DiscreteFactor.h>
 | 
				
			||||||
#include <gtsam/discrete/DiscreteKey.h>
 | 
					#include <gtsam/discrete/DiscreteKey.h>
 | 
				
			||||||
 | 
					#include <gtsam/discrete/Ring.h>
 | 
				
			||||||
#include <gtsam/inference/Ordering.h>
 | 
					#include <gtsam/inference/Ordering.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <algorithm>
 | 
					#include <algorithm>
 | 
				
			||||||
#include <memory>
 | 
					 | 
				
			||||||
#include <map>
 | 
					#include <map>
 | 
				
			||||||
 | 
					#include <memory>
 | 
				
			||||||
#include <stdexcept>
 | 
					#include <stdexcept>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <utility>
 | 
					#include <utility>
 | 
				
			||||||
| 
						 | 
					@ -139,8 +140,8 @@ namespace gtsam {
 | 
				
			||||||
    double error(const DiscreteValues& values) const override;
 | 
					    double error(const DiscreteValues& values) const override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// multiply two factors
 | 
					    /// multiply two factors
 | 
				
			||||||
    DecisionTreeFactor operator*(const DecisionTreeFactor& f) const {
 | 
					    DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override {
 | 
				
			||||||
      return apply(f, ADT::Ring::mul);
 | 
					      return apply(f, Ring::mul);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    DiscreteFactor::shared_ptr operator*(
 | 
					    DiscreteFactor::shared_ptr operator*(
 | 
				
			||||||
| 
						 | 
					@ -169,22 +170,22 @@ namespace gtsam {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create new factor by summing all values with the same separator values
 | 
					    /// Create new factor by summing all values with the same separator values
 | 
				
			||||||
    DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override {
 | 
					    DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override {
 | 
				
			||||||
      return combine(nrFrontals, ADT::Ring::add);
 | 
					      return combine(nrFrontals, Ring::add);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create new factor by summing all values with the same separator values
 | 
					    /// Create new factor by summing all values with the same separator values
 | 
				
			||||||
    DiscreteFactor::shared_ptr sum(const Ordering& keys) const override {
 | 
					    DiscreteFactor::shared_ptr sum(const Ordering& keys) const override {
 | 
				
			||||||
      return combine(keys, ADT::Ring::add);
 | 
					      return combine(keys, Ring::add);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create new factor by maximizing over all values with the same separator.
 | 
					    /// Create new factor by maximizing over all values with the same separator.
 | 
				
			||||||
    DiscreteFactor::shared_ptr max(size_t nrFrontals) const override {
 | 
					    DiscreteFactor::shared_ptr max(size_t nrFrontals) const override {
 | 
				
			||||||
      return combine(nrFrontals, ADT::Ring::max);
 | 
					      return combine(nrFrontals, Ring::max);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Create new factor by maximizing over all values with the same separator.
 | 
					    /// Create new factor by maximizing over all values with the same separator.
 | 
				
			||||||
    DiscreteFactor::shared_ptr max(const Ordering& keys) const override {
 | 
					    DiscreteFactor::shared_ptr max(const Ordering& keys) const override {
 | 
				
			||||||
      return combine(keys, ADT::Ring::max);
 | 
					      return combine(keys, Ring::max);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// @}
 | 
					    /// @}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,7 @@
 | 
				
			||||||
#include <gtsam/base/Testable.h>
 | 
					#include <gtsam/base/Testable.h>
 | 
				
			||||||
#include <gtsam/base/debug.h>
 | 
					#include <gtsam/base/debug.h>
 | 
				
			||||||
#include <gtsam/discrete/DiscreteConditional.h>
 | 
					#include <gtsam/discrete/DiscreteConditional.h>
 | 
				
			||||||
 | 
					#include <gtsam/discrete/Ring.h>
 | 
				
			||||||
#include <gtsam/discrete/Signature.h>
 | 
					#include <gtsam/discrete/Signature.h>
 | 
				
			||||||
#include <gtsam/hybrid/HybridValues.h>
 | 
					#include <gtsam/hybrid/HybridValues.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -107,7 +108,7 @@ DiscreteConditional DiscreteConditional::operator*(
 | 
				
			||||||
  // Finally, add parents to keys, in order
 | 
					  // Finally, add parents to keys, in order
 | 
				
			||||||
  for (auto&& dk : parents) discreteKeys.push_back(dk);
 | 
					  for (auto&& dk : parents) discreteKeys.push_back(dk);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ADT product = ADT::apply(other, ADT::Ring::mul);
 | 
					  ADT product = ADT::apply(other, Ring::mul);
 | 
				
			||||||
  return DiscreteConditional(newFrontals.size(), discreteKeys, product);
 | 
					  return DiscreteConditional(newFrontals.size(), discreteKeys, product);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					/* ----------------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 * 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    Ring.h
 | 
				
			||||||
 | 
					 * @brief   Real Ring definition
 | 
				
			||||||
 | 
					 * @author  Varun Agrawal
 | 
				
			||||||
 | 
					 * @date    Dec 8, 2024
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <algorithm>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** The Real ring with addition and multiplication */
 | 
				
			||||||
 | 
					struct Ring {
 | 
				
			||||||
 | 
					  static inline double zero() { return 0.0; }
 | 
				
			||||||
 | 
					  static inline double one() { return 1.0; }
 | 
				
			||||||
 | 
					  static inline double add(const double& a, const double& b) { return a + b; }
 | 
				
			||||||
 | 
					  static inline double max(const double& a, const double& b) {
 | 
				
			||||||
 | 
					    return std::max(a, b);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  static inline double mul(const double& a, const double& b) { return a * b; }
 | 
				
			||||||
 | 
					  static inline double div(const double& a, const double& b) {
 | 
				
			||||||
 | 
					    return (a == 0 || b == 0) ? 0 : (a / b);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  static inline double id(const double& x) { return x; }
 | 
				
			||||||
 | 
					  static inline double negate(const double& x) { return -x; }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <gtsam/discrete/DiscreteFactor.h>
 | 
					#include <gtsam/discrete/DiscreteFactor.h>
 | 
				
			||||||
#include <gtsam/discrete/DiscreteKey.h>
 | 
					#include <gtsam/discrete/DiscreteKey.h>
 | 
				
			||||||
 | 
					#include <gtsam/discrete/Ring.h>
 | 
				
			||||||
#include <gtsam/inference/Ordering.h>
 | 
					#include <gtsam/inference/Ordering.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <Eigen/Sparse>
 | 
					#include <Eigen/Sparse>
 | 
				
			||||||
| 
						 | 
					@ -99,21 +100,6 @@ class GTSAM_EXPORT TableFactor : public DiscreteFactor {
 | 
				
			||||||
  using Binary = std::function<double(const double, const double)>;
 | 
					  using Binary = std::function<double(const double, const double)>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 public:
 | 
					 public:
 | 
				
			||||||
  /** The Real ring with addition and multiplication */
 | 
					 | 
				
			||||||
  struct Ring {
 | 
					 | 
				
			||||||
    static inline double zero() { return 0.0; }
 | 
					 | 
				
			||||||
    static inline double one() { return 1.0; }
 | 
					 | 
				
			||||||
    static inline double add(const double& a, const double& b) { return a + b; }
 | 
					 | 
				
			||||||
    static inline double max(const double& a, const double& b) {
 | 
					 | 
				
			||||||
      return std::max(a, b);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    static inline double mul(const double& a, const double& b) { return a * b; }
 | 
					 | 
				
			||||||
    static inline double div(const double& a, const double& b) {
 | 
					 | 
				
			||||||
      return (a == 0 || b == 0) ? 0 : (a / b);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    static inline double id(const double& x) { return x; }
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  /// @name Standard Constructors
 | 
					  /// @name Standard Constructors
 | 
				
			||||||
  /// @{
 | 
					  /// @{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +23,7 @@
 | 
				
			||||||
#include <gtsam/base/Testable.h>
 | 
					#include <gtsam/base/Testable.h>
 | 
				
			||||||
#include <gtsam/base/serializationTestHelpers.h>
 | 
					#include <gtsam/base/serializationTestHelpers.h>
 | 
				
			||||||
#include <gtsam/discrete/DecisionTree-inl.h>
 | 
					#include <gtsam/discrete/DecisionTree-inl.h>
 | 
				
			||||||
 | 
					#include <gtsam/discrete/Ring.h>
 | 
				
			||||||
#include <gtsam/discrete/Signature.h>
 | 
					#include <gtsam/discrete/Signature.h>
 | 
				
			||||||
#include <gtsam/inference/Symbol.h>
 | 
					#include <gtsam/inference/Symbol.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -124,14 +125,6 @@ struct traits<DT> : public Testable<DT> {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
GTSAM_CONCEPT_TESTABLE_INST(DT)
 | 
					GTSAM_CONCEPT_TESTABLE_INST(DT)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Ring {
 | 
					 | 
				
			||||||
  static inline int zero() { return 0; }
 | 
					 | 
				
			||||||
  static inline int one() { return 1; }
 | 
					 | 
				
			||||||
  static inline int id(const int& a) { return a; }
 | 
					 | 
				
			||||||
  static inline int add(const int& a, const int& b) { return a + b; }
 | 
					 | 
				
			||||||
  static inline int mul(const int& a, const int& b) { return a * b; }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/* ************************************************************************** */
 | 
					/* ************************************************************************** */
 | 
				
			||||||
// Check that creating decision trees respects key order.
 | 
					// Check that creating decision trees respects key order.
 | 
				
			||||||
TEST(DecisionTree, ConstructorOrder) {
 | 
					TEST(DecisionTree, ConstructorOrder) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue