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 <limits>
26 #include <map>
27 #include <string>
28 #include <iomanip>
29 #include <vector>
30 
31 namespace gtsam {
32 
40  template <typename L>
41  class AlgebraicDecisionTree : public DecisionTree<L, double> {
49  static std::string DefaultFormatter(const L& x) {
50  std::stringstream ss;
51  ss << x;
52  return ss.str();
53  }
54 
55  public:
57 
59  struct Ring {
60  static inline double zero() { return 0.0; }
61  static inline double one() { return 1.0; }
62  static inline double add(const double& a, const double& b) {
63  return a + b;
64  }
65  static inline double max(const double& a, const double& b) {
66  return std::max(a, b);
67  }
68  static inline double mul(const double& a, const double& b) {
69  return a * b;
70  }
71  static inline double div(const double& a, const double& b) {
72  return a / b;
73  }
74  static inline double id(const double& x) { return x; }
75  static inline double negate(const double& x) { return -x; }
76  };
77 
78  AlgebraicDecisionTree(double leaf = 1.0) : Base(leaf) {}
79 
80  // Explicitly non-explicit constructor
82 
84  AlgebraicDecisionTree(const L& label, double y1, double y2)
85  : Base(label, y1, y2) {}
86 
100  AlgebraicDecisionTree(const typename Base::LabelC& labelC, double y1,
101  double y2)
102  : Base(labelC, y1, y2) {}
103 
129  (const std::vector<typename Base::LabelC>& labelCs,
130  const std::vector<double>& ys) {
131  this->root_ =
132  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
133  }
134 
144  (const std::vector<typename Base::LabelC>& labelCs,
145  const std::string& table) {
146  // Convert string to doubles
147  std::vector<double> ys;
148  std::istringstream iss(table);
149  std::copy(std::istream_iterator<double>(iss),
150  std::istream_iterator<double>(), std::back_inserter(ys));
151 
152  // now call recursive Create
153  this->root_ =
154  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
155  }
156 
164  template <typename Iterator>
166  : Base(nullptr) {
167  this->root_ = compose(begin, end, label);
168  }
169 
176  template <typename M>
178  const std::map<M, L>& map) {
179  // Functor for label conversion so we can use `convertFrom`.
180  std::function<L(const M&)> L_of_M = [&map](const M& label) -> L {
181  return map.at(label);
182  };
183  std::function<double(const double&)> op = Ring::id;
184  this->root_ = DecisionTree<L, double>::convertFrom(other.root_, L_of_M, op);
185  }
186 
198  template <typename X, typename Func>
200  : Base(other, f) {}
201 
204  return this->apply(g, &Ring::add);
205  }
206 
209  return this->apply(&Ring::negate);
210  }
211 
214  return *this + (-g);
215  }
216 
219  return this->apply(g, &Ring::mul);
220  }
221 
224  return this->apply(g, &Ring::div);
225  }
226 
228  double sum() const {
229  double sum = 0;
230  auto visitor = [&](double y) { sum += y; };
231  this->visit(visitor);
232  return sum;
233  }
234 
241  AlgebraicDecisionTree normalize() const { return (*this) / this->sum(); }
242 
244  double min() const {
246  auto visitor = [&](double x) { min = x < min ? x : min; };
247  this->visit(visitor);
248  return min;
249  }
250 
252  double max() const {
253  // Get the most negative value
255  auto visitor = [&](double x) { max = x > max ? x : max; };
256  this->visit(visitor);
257  return max;
258  }
259 
261  AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
262  return this->combine(label, cardinality, &Ring::add);
263  }
264 
266  AlgebraicDecisionTree sum(const typename Base::LabelC& labelC) const {
267  return this->combine(labelC, &Ring::add);
268  }
269 
271  void print(const std::string& s = "",
272  const typename Base::LabelFormatter& labelFormatter =
273  &DefaultFormatter) const {
274  auto valueFormatter = [](const double& v) {
275  std::stringstream ss;
276  ss << std::setw(4) << std::setprecision(8) << v;
277  return ss.str();
278  };
279  Base::print(s, labelFormatter, valueFormatter);
280  }
281 
283  bool equals(const AlgebraicDecisionTree& other, double tol = 1e-9) const {
284  // lambda for comparison of two doubles upto some tolerance.
285  auto compare = [tol](double a, double b) {
286  return std::abs(a - b) < tol;
287  };
288  return Base::equals(other, compare);
289  }
290  };
291 
292 template <typename T>
294  : public Testable<AlgebraicDecisionTree<T>> {};
295 } // namespace gtsam
compare
bool compare
Definition: SolverComparer.cpp:98
gtsam::AlgebraicDecisionTree::Ring::id
static double id(const double &x)
Definition: AlgebraicDecisionTree.h:74
gtsam::DecisionTree< Key, double >::LabelFormatter
std::function< std::string(Key)> LabelFormatter
Definition: DecisionTree.h:70
gtsam::AlgebraicDecisionTree::operator-
AlgebraicDecisionTree operator-() const
Definition: AlgebraicDecisionTree.h:208
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const DecisionTree< L, X > &other, Func f)
Create from an arbitrary DecisionTree<L, X> by operating on it with a functional f.
Definition: AlgebraicDecisionTree.h:199
gtsam::add
static Y add(const Y &y1, const Y &y2)
Definition: HybridGaussianProductFactor.cpp:32
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:78
gtsam::DecisionTree< L, double >::equals
bool equals(const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const
Definition: DecisionTree-inl.h:972
gtsam::AlgebraicDecisionTree::operator/
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:223
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:49
Iterator
Definition: typing.h:54
gtsam::AlgebraicDecisionTree::Ring::max
static double max(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:65
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:978
gtsam::AlgebraicDecisionTree::equals
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
Definition: AlgebraicDecisionTree.h:283
gtsam::AlgebraicDecisionTree::max
double max() const
Find the maximum values amongst all leaves.
Definition: AlgebraicDecisionTree.h:252
gtsam::AlgebraicDecisionTree
Definition: AlgebraicDecisionTree.h:41
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::AlgebraicDecisionTree::operator+
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:203
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:149
table
ArrayXXf table(10, 4)
gtsam::DecisionTree< L, double >::compose
static NodePtr compose(Iterator begin, Iterator end, const L &label)
Definition: DecisionTree-inl.h:617
gtsam::AlgebraicDecisionTree::normalize
AlgebraicDecisionTree normalize() const
Helper method to perform normalization such that all leaves in the tree sum to 1.
Definition: AlgebraicDecisionTree.h:241
gtsam::AlgebraicDecisionTree::Ring::zero
static double zero()
Definition: AlgebraicDecisionTree.h:60
gtsam::DecisionTree< L, double >::visit
void visit(Func f) const
Visit all leaves in depth-first fashion.
Definition: DecisionTree-inl.h:842
gtsam::AlgebraicDecisionTree::operator-
AlgebraicDecisionTree operator-(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:213
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:100
L
MatrixXd L
Definition: LLT_example.cpp:6
gtsam::AlgebraicDecisionTree::Ring::div
static double div(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:71
Eigen::Triplet< double >
gtsam::DecisionTree< L, double >::apply
DecisionTree apply(const Unary &op) const
Definition: DecisionTree-inl.h:1000
gtsam::AlgebraicDecisionTree::sum
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
Definition: AlgebraicDecisionTree.h:261
g
void g(const string &key, int i)
Definition: testBTree.cpp:41
y
Scalar * y
Definition: level1_cplx_impl.h:124
gtsam::AlgebraicDecisionTree::sum
double sum() const
Compute sum of all values.
Definition: AlgebraicDecisionTree.h:228
gtsam::AlgebraicDecisionTree::Ring
Definition: AlgebraicDecisionTree.h:59
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::DecisionTree
a decision tree is a function from assignments to values.
Definition: DecisionTree.h:62
gtsam::DecisionTree< L, double >::create
static NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY)
Definition: DecisionTree-inl.h:729
gtsam::b
const G & b
Definition: Group.h:79
gtsam::AlgebraicDecisionTree::Ring::one
static double one()
Definition: AlgebraicDecisionTree.h:61
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:177
gtsam
traits
Definition: SFMdata.h:40
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:271
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const L &label, double y1, double y2)
Definition: AlgebraicDecisionTree.h:84
gtsam::DecisionTree< Key, double >::LabelC
std::pair< Key, size_t > LabelC
Definition: DecisionTree.h:80
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:68
gtsam::AlgebraicDecisionTree::Ring::add
static double add(const double &a, const double &b)
Definition: AlgebraicDecisionTree.h:62
gtsam::AlgebraicDecisionTree::min
double min() const
Find the minimum values amongst all leaves.
Definition: AlgebraicDecisionTree.h:244
gtsam::tol
const G double tol
Definition: Group.h:79
abs
#define abs(x)
Definition: datatypes.h:17
gtsam::AlgebraicDecisionTree::Ring::negate
static double negate(const double &x)
Definition: AlgebraicDecisionTree.h:75
gtsam::DecisionTree< L, double >::combine
DecisionTree combine(const L &label, size_t cardinality, const Binary &op) const
Definition: DecisionTree-inl.h:1049
gtsam::AlgebraicDecisionTree::operator*
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:218
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const Base &add)
Definition: AlgebraicDecisionTree.h:81
gtsam::valueFormatter
static std::string valueFormatter(const double &v)
Definition: DecisionTreeFactor.cpp:248
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:165
gtsam::DecisionTree::convertFrom
static NodePtr convertFrom(const typename DecisionTree< L, X >::NodePtr &f, std::function< Y(const X &)> Y_of_X)
Convert from a DecisionTree<L, X> to DecisionTree<L, Y>.
Definition: DecisionTree-inl.h:742
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:266
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:48