AlgebraicDecisionTree.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #pragma once
20 
21 #include <gtsam/base/Testable.h>
23 
24 #include <algorithm>
25 #include <map>
26 #include <string>
27 #include <iomanip>
28 #include <vector>
29 namespace gtsam {
30 
38  template <typename L>
39  class GTSAM_EXPORT AlgebraicDecisionTree : public DecisionTree<L, double> {
47  static std::string DefaultFormatter(const L& x) {
48  std::stringstream ss;
49  ss << x;
50  return ss.str();
51  }
52 
53  public:
55 
57  struct Ring {
58  static inline double zero() { return 0.0; }
59  static inline double one() { return 1.0; }
60  static inline double add(const double& a, const double& b) {
61  return a + b;
62  }
63  static inline double max(const double& a, const double& b) {
64  return std::max(a, b);
65  }
66  static inline double mul(const double& a, const double& b) {
67  return a * b;
68  }
69  static inline double div(const double& a, const double& b) {
70  return a / b;
71  }
72  static inline double id(const double& x) { return x; }
73  };
74 
75  AlgebraicDecisionTree(double leaf = 1.0) : Base(leaf) {}
76 
77  // Explicitly non-explicit constructor
78  AlgebraicDecisionTree(const Base& add) : Base(add) {}
79 
81  AlgebraicDecisionTree(const L& label, double y1, double y2)
82  : Base(label, y1, y2) {}
83 
85  AlgebraicDecisionTree(const typename Base::LabelC& labelC, double y1,
86  double y2)
87  : Base(labelC, y1, y2) {}
88 
91  (const std::vector<typename Base::LabelC>& labelCs,
92  const std::vector<double>& ys) {
93  this->root_ =
94  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
95  }
96 
99  (const std::vector<typename Base::LabelC>& labelCs,
100  const std::string& table) {
101  // Convert string to doubles
102  std::vector<double> ys;
103  std::istringstream iss(table);
104  std::copy(std::istream_iterator<double>(iss),
105  std::istream_iterator<double>(), std::back_inserter(ys));
106 
107  // now call recursive Create
108  this->root_ =
109  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
110  }
111 
113  template <typename Iterator>
114  AlgebraicDecisionTree(Iterator begin, Iterator end, const L& label)
115  : Base(nullptr) {
116  this->root_ = compose(begin, end, label);
117  }
118 
125  template <typename M>
127  const std::map<M, L>& map) {
128  // Functor for label conversion so we can use `convertFrom`.
129  std::function<L(const M&)> L_of_M = [&map](const M& label) -> L {
130  return map.at(label);
131  };
132  std::function<double(const double&)> op = Ring::id;
133  this->root_ = DecisionTree<L, double>::convertFrom(other.root_, L_of_M, op);
134  }
135 
138  return this->apply(g, &Ring::add);
139  }
140 
143  return this->apply(g, &Ring::mul);
144  }
145 
148  return this->apply(g, &Ring::div);
149  }
150 
152  AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
153  return this->combine(label, cardinality, &Ring::add);
154  }
155 
157  AlgebraicDecisionTree sum(const typename Base::LabelC& labelC) const {
158  return this->combine(labelC, &Ring::add);
159  }
160 
162  void print(const std::string& s = "",
163  const typename Base::LabelFormatter& labelFormatter =
164  &DefaultFormatter) const {
165  auto valueFormatter = [](const double& v) {
166  std::stringstream ss;
167  ss << std::setw(4) << std::setprecision(8) << v;
168  return ss.str();
169  };
170  Base::print(s, labelFormatter, valueFormatter);
171  }
172 
174  bool equals(const AlgebraicDecisionTree& other, double tol = 1e-9) const {
175  // lambda for comparison of two doubles upto some tolerance.
176  auto compare = [tol](double a, double b) {
177  return std::abs(a - b) < tol;
178  };
179  return Base::equals(other, compare);
180  }
181  };
182 
183 template <typename T>
185  : public Testable<AlgebraicDecisionTree<T>> {};
186 } // namespace gtsam
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
NodePtr convertFrom(const typename DecisionTree< M, X >::NodePtr &f, std::function< L(const M &)> L_of_M, std::function< Y(const X &)> Y_of_X) const
Convert from a DecisionTree<M, X> to DecisionTree<L, Y>.
bool compare
static int id(const int &a)
#define max(a, b)
Definition: datatypes.h:20
static int mul(const int &a, const int &b)
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
static double add(const double &a, const double &b)
Concept check for values that can be used in unit tests.
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
Apply unary operator op to DecisionTree f.
Definition: DecisionTree.h:398
AlgebraicDecisionTree(const typename Base::LabelC &labelC, double y1, double y2)
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
MatrixXd L
Definition: LLT_example.cpp:6
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
static double max(const double &a, const double &b)
static std::string valueFormatter(const double &v)
void g(const string &key, int i)
Definition: testBTree.cpp:41
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
static double mul(const double &a, const double &b)
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition: DecisionTree.h:136
static double div(const double &a, const double &b)
Array< int, Dynamic, 1 > v
Array< double, 1, 3 > e(1./3., 0.5, 2.)
AlgebraicDecisionTree(Iterator begin, Iterator end, const L &label)
RealScalar s
static std::string DefaultFormatter(const L &x)
Default method used by labelFormatter or valueFormatter when printing.
const G & b
Definition: Group.h:86
AlgebraicDecisionTree sum(const typename Base::LabelC &labelC) const
static std::stringstream ss
Definition: testBTree.cpp:31
traits
Definition: chartTesting.h:28
std::function< std::string(Key)> LabelFormatter
Definition: DecisionTree.h:57
static int add(const int &a, const int &b)
ADT create(const Signature &signature)
ArrayXXf table(10, 4)
Expression< T > compose(const Expression< T > &t1, const Expression< T > &t2)
static EIGEN_DEPRECATED const end_t end
static double id(const double &x)
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
const G double tol
Definition: Group.h:86
std::pair< Key, size_t > LabelC
Definition: DecisionTree.h:67
AlgebraicDecisionTree(const AlgebraicDecisionTree< M > &other, const std::map< M, L > &map)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
#define abs(x)
Definition: datatypes.h:17
AlgebraicDecisionTree(const L &label, double y1, double y2)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:53