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 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
79 
81  AlgebraicDecisionTree(const L& label, double y1, double y2)
82  : Base(label, y1, y2) {}
83 
97  AlgebraicDecisionTree(const typename Base::LabelC& labelC, double y1,
98  double y2)
99  : Base(labelC, y1, y2) {}
100 
126  (const std::vector<typename Base::LabelC>& labelCs,
127  const std::vector<double>& ys) {
128  this->root_ =
129  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
130  }
131 
141  (const std::vector<typename Base::LabelC>& labelCs,
142  const std::string& table) {
143  // Convert string to doubles
144  std::vector<double> ys;
145  std::istringstream iss(table);
146  std::copy(std::istream_iterator<double>(iss),
147  std::istream_iterator<double>(), std::back_inserter(ys));
148 
149  // now call recursive Create
150  this->root_ =
151  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
152  }
153 
161  template <typename Iterator>
162  AlgebraicDecisionTree(Iterator begin, Iterator end, const L& label)
163  : Base(nullptr) {
164  this->root_ = compose(begin, end, label);
165  }
166 
173  template <typename M>
175  const std::map<M, L>& map) {
176  // Functor for label conversion so we can use `convertFrom`.
177  std::function<L(const M&)> L_of_M = [&map](const M& label) -> L {
178  return map.at(label);
179  };
180  std::function<double(const double&)> op = Ring::id;
181  this->root_ = DecisionTree<L, double>::convertFrom(other.root_, L_of_M, op);
182  }
183 
186  return this->apply(g, &Ring::add);
187  }
188 
191  return this->apply(g, &Ring::mul);
192  }
193 
196  return this->apply(g, &Ring::div);
197  }
198 
200  AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
201  return this->combine(label, cardinality, &Ring::add);
202  }
203 
205  AlgebraicDecisionTree sum(const typename Base::LabelC& labelC) const {
206  return this->combine(labelC, &Ring::add);
207  }
208 
210  void print(const std::string& s = "",
211  const typename Base::LabelFormatter& labelFormatter =
212  &DefaultFormatter) const {
213  auto valueFormatter = [](const double& v) {
214  std::stringstream ss;
215  ss << std::setw(4) << std::setprecision(8) << v;
216  return ss.str();
217  };
218  Base::print(s, labelFormatter, valueFormatter);
219  }
220 
222  bool equals(const AlgebraicDecisionTree& other, double tol = 1e-9) const {
223  // lambda for comparison of two doubles upto some tolerance.
224  auto compare = [tol](double a, double b) {
225  return std::abs(a - b) < tol;
226  };
227  return Base::equals(other, compare);
228  }
229  };
230 
231 template <typename T>
233  : public Testable<AlgebraicDecisionTree<T>> {};
234 } // namespace gtsam
compare
bool compare
Definition: SolverComparer.cpp:98
gtsam::DecisionTree< L, double >::create
NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const
Definition: DecisionTree-inl.h:690
gtsam::AlgebraicDecisionTree::Ring::id
static double id(const double &x)
Definition: AlgebraicDecisionTree.h:72
gtsam::DecisionTree< Key, double >::LabelFormatter
std::function< std::string(Key)> LabelFormatter
Definition: DecisionTree.h:71
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Testable.h
Concept check for values that can be used in unit tests.
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(double leaf=1.0)
Definition: AlgebraicDecisionTree.h:75
gtsam::DecisionTree< L, double >::equals
bool equals(const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const
Definition: DecisionTree-inl.h:898
gtsam::AlgebraicDecisionTree::operator/
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:195
x
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
Definition: gnuplot_common_settings.hh:12
gtsam::AlgebraicDecisionTree::DefaultFormatter
static std::string DefaultFormatter(const L &x)
Default method used by labelFormatter or valueFormatter when printing.
Definition: AlgebraicDecisionTree.h:47
gtsam::AlgebraicDecisionTree::Ring::max
static double max(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:63
gtsam::DecisionTree< L, double >::print
void print(const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const
GTSAM-style print.
Definition: DecisionTree-inl.h:904
gtsam::AlgebraicDecisionTree::equals
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
Definition: AlgebraicDecisionTree.h:222
gtsam::AlgebraicDecisionTree
Definition: AlgebraicDecisionTree.h:39
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::AlgebraicDecisionTree::operator+
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:185
y1
double y1(double x)
Definition: j1.c:199
gtsam::DecisionTree< L, double >::root_
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition: DecisionTree.h:150
table
ArrayXXf table(10, 4)
add
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
gtsam::AlgebraicDecisionTree::Ring::zero
static double zero()
Definition: AlgebraicDecisionTree.h:58
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const typename Base::LabelC &labelC, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition: AlgebraicDecisionTree.h:97
L
MatrixXd L
Definition: LLT_example.cpp:6
gtsam::AlgebraicDecisionTree::Ring::div
static double div(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:69
Eigen::Triplet< double >
gtsam::DecisionTree< L, double >::apply
DecisionTree apply(const Unary &op) const
Definition: DecisionTree-inl.h:921
gtsam::AlgebraicDecisionTree::sum
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
Definition: AlgebraicDecisionTree.h:200
g
void g(const string &key, int i)
Definition: testBTree.cpp:41
gtsam::AlgebraicDecisionTree::Ring
Definition: AlgebraicDecisionTree.h:57
gtsam::DecisionTree
a decision tree is a function from assignments to values.
Definition: DecisionTree.h:63
gtsam::b
const G & b
Definition: Group.h:79
gtsam::AlgebraicDecisionTree::Ring::one
static double one()
Definition: AlgebraicDecisionTree.h:59
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const AlgebraicDecisionTree< M > &other, const std::map< M, L > &map)
Definition: AlgebraicDecisionTree.h:174
gtsam
traits
Definition: chartTesting.h:28
gtsam::Testable
Definition: Testable.h:152
DecisionTree-inl.h
gtsam::traits
Definition: Group.h:36
gtsam::AlgebraicDecisionTree::print
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
Definition: AlgebraicDecisionTree.h:210
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const L &label, double y1, double y2)
Definition: AlgebraicDecisionTree.h:81
gtsam::DecisionTree< Key, double >::LabelC
std::pair< Key, size_t > LabelC
Definition: DecisionTree.h:81
gtsam::DecisionTree< L, double >::compose
NodePtr compose(Iterator begin, Iterator end, const L &label) const
Definition: DecisionTree-inl.h:581
leaf
Definition: testExpressionFactor.cpp:42
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::AlgebraicDecisionTree::Ring::mul
static double mul(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:66
gtsam::AlgebraicDecisionTree::Ring::add
static double add(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:60
gtsam::tol
const G double tol
Definition: Group.h:79
abs
#define abs(x)
Definition: datatypes.h:17
gtsam::DecisionTree< L, double >::combine
DecisionTree combine(const L &label, size_t cardinality, const Binary &op) const
Definition: DecisionTree-inl.h:969
gtsam::AlgebraicDecisionTree::operator*
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:190
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const Base &add)
Definition: AlgebraicDecisionTree.h:78
gtsam::valueFormatter
static std::string valueFormatter(const double &v)
Definition: DecisionTreeFactor.cpp:266
gtsam::DecisionTree::convertFrom
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>.
Definition: DecisionTree-inl.h:703
max
#define max(a, b)
Definition: datatypes.h:20
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(Iterator begin, Iterator end, const L &label)
Create a range of decision trees, splitting on a single variable.
Definition: AlgebraicDecisionTree.h:162
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::AlgebraicDecisionTree::sum
AlgebraicDecisionTree sum(const typename Base::LabelC &labelC) const
Definition: AlgebraicDecisionTree.h:205
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:00:25