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 
61  AlgebraicDecisionTree(const typename Base::NodePtr root) : Base(root) {}
62 
63  // Explicitly non-explicit constructor
65 
67  AlgebraicDecisionTree(const L& label, double y1, double y2)
68  : Base(label, y1, y2) {}
69 
83  AlgebraicDecisionTree(const typename Base::LabelC& labelC, double y1,
84  double y2)
85  : Base(labelC, y1, y2) {}
86 
112  (const std::vector<typename Base::LabelC>& labelCs,
113  const std::vector<double>& ys) {
114  this->root_ =
115  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
116  }
117 
127  (const std::vector<typename Base::LabelC>& labelCs,
128  const std::string& table) {
129  // Convert string to doubles
130  std::vector<double> ys;
131  std::istringstream iss(table);
132  std::copy(std::istream_iterator<double>(iss),
133  std::istream_iterator<double>(), std::back_inserter(ys));
134 
135  // now call recursive Create
136  this->root_ =
137  Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
138  }
139 
147  template <typename Iterator>
149  : Base(nullptr) {
150  this->root_ = compose(begin, end, label);
151  }
152 
159  template <typename M>
161  const std::map<M, L>& map) {
162  // Functor for label conversion so we can use `convertFrom`.
163  std::function<L(const M&)> L_of_M = [&map](const M& label) -> L {
164  return map.at(label);
165  };
166  std::function<double(const double&)> op = Ring::id;
167  this->root_ = DecisionTree<L, double>::convertFrom(other.root_, L_of_M, op);
168  }
169 
181  template <typename X, typename Func>
183  : Base(other, f) {}
184 
187  return this->apply(g, &Ring::add);
188  }
189 
192  return this->apply(&Ring::negate);
193  }
194 
197  return *this + (-g);
198  }
199 
202  return this->apply(g, &Ring::mul);
203  }
204 
207  return this->apply(g, &Ring::div);
208  }
209 
211  double sum() const {
212  double sum = 0;
213  auto visitor = [&](double y) { sum += y; };
214  this->visit(visitor);
215  return sum;
216  }
217 
224  AlgebraicDecisionTree normalize() const { return (*this) / this->sum(); }
225 
227  double min() const {
229  auto visitor = [&](double x) { min = x < min ? x : min; };
230  this->visit(visitor);
231  return min;
232  }
233 
235  double max() const {
236  // Get the most negative value
238  auto visitor = [&](double x) { max = x > max ? x : max; };
239  this->visit(visitor);
240  return max;
241  }
242 
244  AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
245  return this->combine(label, cardinality, &Ring::add);
246  }
247 
249  AlgebraicDecisionTree sum(const typename Base::LabelC& labelC) const {
250  return this->combine(labelC, &Ring::add);
251  }
252 
254  void print(const std::string& s = "",
255  const typename Base::LabelFormatter& labelFormatter =
256  &DefaultFormatter) const {
257  auto valueFormatter = [](const double& v) {
258  std::stringstream ss;
259  ss << std::setw(4) << std::setprecision(8) << v;
260  return ss.str();
261  };
262  Base::print(s, labelFormatter, valueFormatter);
263  }
264 
266  bool equals(const AlgebraicDecisionTree& other, double tol = 1e-9) const {
267  // lambda for comparison of two doubles upto some tolerance.
268  auto compare = [tol](double a, double b) {
269  return std::abs(a - b) < tol;
270  };
271  return Base::equals(other, compare);
272  }
273  };
274 
275 template <typename T>
277  : public Testable<AlgebraicDecisionTree<T>> {};
278 } // 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:191
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:182
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::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const typename Base::NodePtr root)
Constructor which accepts root pointer.
Definition: AlgebraicDecisionTree.h:61
gtsam::DecisionTree< L, double >::equals
bool equals(const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const
Definition: DecisionTree-inl.h:975
gtsam::AlgebraicDecisionTree::operator/
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:206
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:56
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:981
gtsam::AlgebraicDecisionTree::equals
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
Definition: AlgebraicDecisionTree.h:266
gtsam::AlgebraicDecisionTree::max
double max() const
Find the maximum values amongst all leaves.
Definition: AlgebraicDecisionTree.h:235
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:186
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:622
gtsam::AlgebraicDecisionTree::normalize
AlgebraicDecisionTree normalize() const
Helper method to perform normalization such that all leaves in the tree sum to 1.
Definition: AlgebraicDecisionTree.h:224
gtsam::DecisionTree< L, double >::visit
void visit(Func f) const
Visit all leaves in depth-first fashion.
Definition: DecisionTree-inl.h:841
gtsam::AlgebraicDecisionTree::operator-
AlgebraicDecisionTree operator-(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:196
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:83
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:1003
gtsam::AlgebraicDecisionTree::sum
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
Definition: AlgebraicDecisionTree.h:244
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:211
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:733
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:160
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:254
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const L &label, double y1, double y2)
Definition: AlgebraicDecisionTree.h:67
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:227
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::DecisionTree< Key, double >::NodePtr
typename Node::Ptr NodePtr
Definition: DecisionTree.h:146
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:1052
gtsam::AlgebraicDecisionTree::operator*
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
Definition: AlgebraicDecisionTree.h:201
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
gtsam::AlgebraicDecisionTree::AlgebraicDecisionTree
AlgebraicDecisionTree(const Base &add)
Definition: AlgebraicDecisionTree.h:64
gtsam::valueFormatter
static std::string valueFormatter(const double &v)
Definition: DecisionTreeFactor.cpp:292
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:148
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:249
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:13