gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
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>
22#include <gtsam/discrete/DecisionTree-inl.h>
23
24#include <algorithm>
25#include <map>
26#include <string>
27#include <vector>
28namespace gtsam {
29
37 template <typename L>
38 class GTSAM_EXPORT AlgebraicDecisionTree : public DecisionTree<L, double> {
46 static std::string DefaultFormatter(const L& x) {
47 std::stringstream ss;
48 ss << x;
49 return ss.str();
50 }
51
52 public:
54
56 struct Ring {
57 static inline double zero() { return 0.0; }
58 static inline double one() { return 1.0; }
59 static inline double add(const double& a, const double& b) {
60 return a + b;
61 }
62 static inline double max(const double& a, const double& b) {
63 return std::max(a, b);
64 }
65 static inline double mul(const double& a, const double& b) {
66 return a * b;
67 }
68 static inline double div(const double& a, const double& b) {
69 return a / b;
70 }
71 static inline double id(const double& x) { return x; }
72 };
73
74 AlgebraicDecisionTree(double leaf = 1.0) : Base(leaf) {}
75
76 // Explicitly non-explicit constructor
77 AlgebraicDecisionTree(const Base& add) : Base(add) {}
78
80 AlgebraicDecisionTree(const L& label, double y1, double y2)
81 : Base(label, y1, y2) {}
82
96 AlgebraicDecisionTree(const typename Base::LabelC& labelC, double y1,
97 double y2)
98 : Base(labelC, y1, y2) {}
99
125 (const std::vector<typename Base::LabelC>& labelCs,
126 const std::vector<double>& ys) {
127 this->root_ =
128 Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
129 }
130
140 (const std::vector<typename Base::LabelC>& labelCs,
141 const std::string& table) {
142 // Convert string to doubles
143 std::vector<double> ys;
144 std::istringstream iss(table);
145 std::copy(std::istream_iterator<double>(iss),
146 std::istream_iterator<double>(), std::back_inserter(ys));
147
148 // now call recursive Create
149 this->root_ =
150 Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
151 }
152
160 template <typename Iterator>
161 AlgebraicDecisionTree(Iterator begin, Iterator end, const L& label)
162 : Base(nullptr) {
163 this->root_ = compose(begin, end, label);
164 }
165
172 template <typename M>
174 const std::map<M, L>& map) {
175 // Functor for label conversion so we can use `convertFrom`.
176 std::function<L(const M&)> L_of_M = [&map](const M& label) -> L {
177 return map.at(label);
178 };
179 std::function<double(const double&)> op = Ring::id;
180 this->root_ = DecisionTree<L, double>::convertFrom(other.root_, L_of_M, op);
181 }
182
185 return this->apply(g, &Ring::add);
186 }
187
190 return this->apply(g, &Ring::mul);
191 }
192
195 return this->apply(g, &Ring::div);
196 }
197
199 AlgebraicDecisionTree sum(const L& label, size_t cardinality) const {
200 return this->combine(label, cardinality, &Ring::add);
201 }
202
204 AlgebraicDecisionTree sum(const typename Base::LabelC& labelC) const {
205 return this->combine(labelC, &Ring::add);
206 }
207
209 void print(const std::string& s = "",
210 const typename Base::LabelFormatter& labelFormatter =
211 &DefaultFormatter) const {
212 auto valueFormatter = [](const double& v) {
213 return (boost::format("%4.8g") % v).str();
214 };
215 Base::print(s, labelFormatter, valueFormatter);
216 }
217
219 bool equals(const AlgebraicDecisionTree& other, double tol = 1e-9) const {
220 // lambda for comparison of two doubles upto some tolerance.
221 auto compare = [tol](double a, double b) {
222 return std::abs(a - b) < tol;
223 };
224 return Base::equals(other, compare);
225 }
226 };
227
228template <typename T>
230 : public Testable<AlgebraicDecisionTree<T>> {};
231} // namespace gtsam
Concept check for values that can be used in unit tests.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition DecisionTree.h:413
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
An algebraic decision tree fixes the range of a DecisionTree to double.
Definition AlgebraicDecisionTree.h:38
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
division
Definition AlgebraicDecisionTree.h:194
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
sum out variable
Definition AlgebraicDecisionTree.h:199
AlgebraicDecisionTree(const typename Base::LabelC &labelC, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition AlgebraicDecisionTree.h:96
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
Definition AlgebraicDecisionTree.h:209
AlgebraicDecisionTree sum(const typename Base::LabelC &labelC) const
sum out variable
Definition AlgebraicDecisionTree.h:204
AlgebraicDecisionTree(const AlgebraicDecisionTree< M > &other, const std::map< M, L > &map)
Convert labels from type M to type L.
Definition AlgebraicDecisionTree.h:173
AlgebraicDecisionTree(const L &label, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition AlgebraicDecisionTree.h:80
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
Definition AlgebraicDecisionTree.h:219
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
sum
Definition AlgebraicDecisionTree.h:184
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
product
Definition AlgebraicDecisionTree.h:189
AlgebraicDecisionTree(Iterator begin, Iterator end, const L &label)
Create a range of decision trees, splitting on a single variable.
Definition AlgebraicDecisionTree.h:161
The Real ring with addition and multiplication.
Definition AlgebraicDecisionTree.h:56
a decision tree is a function from assignments to values.
Definition DecisionTree.h:61
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition DecisionTree.h:146
std::pair< L, size_t > LabelC
A label annotated with cardinality.
Definition DecisionTree.h:79