gtsam  4.0.0
gtsam
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/discrete/DecisionTree-inl.h>
22 
23 namespace gtsam {
24 
30  template<typename L>
31  class AlgebraicDecisionTree: public DecisionTree<L, double> {
32 
33  public:
34 
36 
38  struct Ring {
39  static inline double zero() {
40  return 0.0;
41  }
42  static inline double one() {
43  return 1.0;
44  }
45  static inline double add(const double& a, const double& b) {
46  return a + b;
47  }
48  static inline double max(const double& a, const double& b) {
49  return std::max(a, b);
50  }
51  static inline double mul(const double& a, const double& b) {
52  return a * b;
53  }
54  static inline double div(const double& a, const double& b) {
55  return a / b;
56  }
57  static inline double id(const double& x) {
58  return x;
59  }
60  };
61 
63  Super(1.0) {
64  }
65 
66  AlgebraicDecisionTree(const Super& add) :
67  Super(add) {
68  }
69 
71  AlgebraicDecisionTree(const L& label, double y1, double y2) :
72  Super(label, y1, y2) {
73  }
74 
76  AlgebraicDecisionTree(const typename Super::LabelC& labelC, double y1, double y2) :
77  Super(labelC, y1, y2) {
78  }
79 
82  (const std::vector<typename Super::LabelC>& labelCs, const std::vector<double>& ys) {
83  this->root_ = Super::create(labelCs.begin(), labelCs.end(), ys.begin(),
84  ys.end());
85  }
86 
89  (const std::vector<typename Super::LabelC>& labelCs, const std::string& table) {
90  // Convert string to doubles
91  std::vector<double> ys;
92  std::istringstream iss(table);
93  std::copy(std::istream_iterator<double>(iss),
94  std::istream_iterator<double>(), std::back_inserter(ys));
95 
96  // now call recursive Create
97  this->root_ = Super::create(labelCs.begin(), labelCs.end(), ys.begin(),
98  ys.end());
99  }
100 
102  template<typename Iterator>
103  AlgebraicDecisionTree(Iterator begin, Iterator end, const L& label) :
104  Super(NULL) {
105  this->root_ = compose(begin, end, label);
106  }
107 
109  template<typename M>
111  const std::map<M, L>& map) {
112  this->root_ = this->template convert<M, double>(other.root_, map,
113  Ring::id);
114  }
115 
118  return this->apply(g, &Ring::add);
119  }
120 
123  return this->apply(g, &Ring::mul);
124  }
125 
128  return this->apply(g, &Ring::div);
129  }
130 
132  AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
133  return this->combine(label, cardinality, &Ring::add);
134  }
135 
137  AlgebraicDecisionTree sum(const typename Super::LabelC& labelC) const {
138  return this->combine(labelC, &Ring::add);
139  }
140 
141  };
142 // AlgebraicDecisionTree
143 
144 }
145 // namespace gtsam
AlgebraicDecisionTree(const AlgebraicDecisionTree< M > &other, const std::map< M, L > &map)
Convert.
Definition: AlgebraicDecisionTree.h:110
NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const
Internal recursive function to create from keys, cardinalities, and Y values.
Definition: DecisionTree-inl.h:531
AlgebraicDecisionTree(const typename Super::LabelC &labelC, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition: AlgebraicDecisionTree.h:76
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
sum
Definition: AlgebraicDecisionTree.h:117
AlgebraicDecisionTree(Iterator begin, Iterator end, const L &label)
Create a new function splitting on a variable.
Definition: AlgebraicDecisionTree.h:103
Algebraic Decision Trees fix the range to double Just has some nice constructors and some syntactic s...
Definition: AlgebraicDecisionTree.h:31
DecisionTree apply(const Unary &op) const
apply Unary operation "op" to f
Definition: DecisionTree-inl.h:624
DecisionTree combine(const L &label, size_t cardinality, const Binary &op) const
combine subtrees on key with binary operation "op"
Definition: DecisionTree-inl.h:649
Decision Tree L = label for variables Y = function range (any algebra), e.g., bool,...
Definition: DecisionTree.h:36
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
division
Definition: AlgebraicDecisionTree.h:127
The Real ring with addition and multiplication.
Definition: AlgebraicDecisionTree.h:38
std::pair< Key, size_t > LabelC
A label annotated with cardinality.
Definition: DecisionTree.h:45
AlgebraicDecisionTree sum(const typename Super::LabelC &labelC) const
sum out variable
Definition: AlgebraicDecisionTree.h:137
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
AlgebraicDecisionTree(const L &label, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition: AlgebraicDecisionTree.h:71
AlgebraicDecisionTree operator *(const AlgebraicDecisionTree &g) const
product
Definition: AlgebraicDecisionTree.h:122
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
sum out variable
Definition: AlgebraicDecisionTree.h:132