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 #include <gtsam/discrete/Ring.h>
24 
25 #include <iomanip>
26 #include <limits>
27 #include <map>
28 #include <string>
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 
58  AlgebraicDecisionTree(double leaf = 1.0) : Base(leaf) {}
59 
60  // Explicitly non-explicit constructor
62 
64  AlgebraicDecisionTree(const L& label, double y1, double y2)
65  : Base(label, y1, y2) {}
66 
80  AlgebraicDecisionTree(const typename Base::LabelC& labelC, double y1,
81  double y2)
82  : Base(labelC, y1, y2) {}
83 
109  (const std::vector<typename Base::LabelC>& labelCs,
110  const std::vector<double>& ys) {
111  this->root_ =
112  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
113  }
114 
124  (const std::vector<typename Base::LabelC>& labelCs,
125  const std::string& table) {
126  // Convert string to doubles
127  std::vector<double> ys;
128  std::istringstream iss(table);
129  std::copy(std::istream_iterator<double>(iss),
130  std::istream_iterator<double>(), std::back_inserter(ys));
131 
132  // now call recursive Create
133  this->root_ =
134  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
135  }
136 
144  template <typename Iterator>
146  : Base(nullptr) {
147  this->root_ = compose(begin, end, label);
148  }
149 
156  template <typename M>
158  const std::map<M, L>& map) {
159  // Functor for label conversion so we can use `convertFrom`.
160  std::function<L(const M&)> L_of_M = [&map](const M& label) -> L {
161  return map.at(label);
162  };
163  std::function<double(const double&)> op = Ring::id;
164  this->root_ = DecisionTree<L, double>::convertFrom(other.root_, L_of_M, op);
165  }
166 
178  template <typename X, typename Func>
180  : Base(other, f) {}
181 
184  return this->apply(g, &Ring::add);
185  }
186 
189  return this->apply(&Ring::negate);
190  }
191 
194  return *this + (-g);
195  }
196 
199  return this->apply(g, &Ring::mul);
200  }
201 
204  return this->apply(g, &Ring::div);
205  }
206 
208  double sum() const {
209  double sum = 0;
210  auto visitor = [&](double y) { sum += y; };
211  this->visit(visitor);
212  return sum;
213  }
214 
221  AlgebraicDecisionTree normalize() const { return (*this) / this->sum(); }
222 
224  double min() const {
226  auto visitor = [&](double x) { min = x < min ? x : min; };
227  this->visit(visitor);
228  return min;
229  }
230 
232  double max() const {
233  // Get the most negative value
235  auto visitor = [&](double x) { max = x > max ? x : max; };
236  this->visit(visitor);
237  return max;
238  }
239 
241  AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
242  return this->combine(label, cardinality, &Ring::add);
243  }
244 
246  AlgebraicDecisionTree sum(const typename Base::LabelC& labelC) const {
247  return this->combine(labelC, &Ring::add);
248  }
249 
251  void print(const std::string& s = "",
252  const typename Base::LabelFormatter& labelFormatter =
253  &DefaultFormatter) const {
254  auto valueFormatter = [](const double& v) {
255  std::stringstream ss;
256  ss << std::setw(4) << std::setprecision(8) << v;
257  return ss.str();
258  };
259  Base::print(s, labelFormatter, valueFormatter);
260  }
261 
263  bool equals(const AlgebraicDecisionTree& other, double tol = 1e-9) const {
264  // lambda for comparison of two doubles upto some tolerance.
265  auto compare = [tol](double a, double b) {
266  return std::abs(a - b) < tol;
267  };
268  return Base::equals(other, compare);
269  }
270  };
271 
272 template <typename T>
274  : public Testable<AlgebraicDecisionTree<T>> {};
275 } // namespace gtsam
compare
bool compare
Definition: SolverComparer.cpp:98
gtsam::DecisionTree< Key, double >::LabelFormatter
std::function< std::string(Key)> LabelFormatter
Definition: DecisionTree.h:70
gtsam::AlgebraicDecisionTree::operator-
AlgebraicDecisionTree operator-() const
Definition: AlgebraicDecisionTree.h:188
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:179
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.
Ring::div
static double div(const double &a, const double &b)
Definition: Ring.h:32
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(double leaf=1.0)
Definition: AlgebraicDecisionTree.h:58
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:203
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::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:263
gtsam::AlgebraicDecisionTree::max
double max() const
Find the maximum values amongst all leaves.
Definition: AlgebraicDecisionTree.h:232
Ring::mul
static double mul(const double &a, const double &b)
Definition: Ring.h:31
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:183
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:221
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:193
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:80
L
MatrixXd L
Definition: LLT_example.cpp:6
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:241
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:208
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
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:157
Ring::add
static double add(const double &a, const double &b)
Definition: Ring.h:27
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:251
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const L &label, double y1, double y2)
Definition: AlgebraicDecisionTree.h:64
gtsam::DecisionTree< Key, double >::LabelC
std::pair< Key, size_t > LabelC
Definition: DecisionTree.h:80
Ring.h
Real Ring definition.
leaf
Definition: testExpressionFactor.cpp:42
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::AlgebraicDecisionTree::min
double min() const
Find the minimum values amongst all leaves.
Definition: AlgebraicDecisionTree.h:224
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:1049
gtsam::AlgebraicDecisionTree::operator*
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:198
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const Base &add)
Definition: AlgebraicDecisionTree.h:61
gtsam::valueFormatter
static std::string valueFormatter(const double &v)
Definition: DecisionTreeFactor.cpp:248
max
#define max(a, b)
Definition: datatypes.h:20
Ring::negate
static double negate(const double &x)
Definition: Ring.h:36
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:145
Ring::id
static double id(const double &x)
Definition: Ring.h:35
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:246
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:05