gtsam 4.1.1
gtsam
DecisionTree.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
20#pragma once
21
23
24#include <boost/function.hpp>
25#include <functional>
26#include <iostream>
27#include <map>
28#include <vector>
29
30namespace gtsam {
31
37 template<typename L, typename Y>
39
40 public:
41
43 typedef std::function<Y(const Y&)> Unary;
44 typedef std::function<Y(const Y&, const Y&)> Binary;
45
47 typedef std::pair<L,size_t> LabelC;
48
50 class Leaf;
51 class Choice;
52
54 class Node {
55 public:
56 typedef boost::shared_ptr<const Node> Ptr;
57
58#ifdef DT_DEBUG_MEMORY
59 static int nrNodes;
60#endif
61
62 // Constructor
63 Node() {
64#ifdef DT_DEBUG_MEMORY
65 std::cout << ++nrNodes << " constructed " << id() << std::endl; std::cout.flush();
66#endif
67 }
68
69 // Destructor
70 virtual ~Node() {
71#ifdef DT_DEBUG_MEMORY
72 std::cout << --nrNodes << " destructed " << id() << std::endl; std::cout.flush();
73#endif
74 }
75
76 // Unique ID for dot files
77 const void* id() const { return this; }
78
79 // everything else is virtual, no documentation here as internal
80 virtual void print(const std::string& s = "") const = 0;
81 virtual void dot(std::ostream& os, bool showZero) const = 0;
82 virtual bool sameLeaf(const Leaf& q) const = 0;
83 virtual bool sameLeaf(const Node& q) const = 0;
84 virtual bool equals(const Node& other, double tol = 1e-9) const = 0;
85 virtual const Y& operator()(const Assignment<L>& x) const = 0;
86 virtual Ptr apply(const Unary& op) const = 0;
87 virtual Ptr apply_f_op_g(const Node&, const Binary&) const = 0;
88 virtual Ptr apply_g_op_fL(const Leaf&, const Binary&) const = 0;
89 virtual Ptr apply_g_op_fC(const Choice&, const Binary&) const = 0;
90 virtual Ptr choose(const L& label, size_t index) const = 0;
91 virtual bool isLeaf() const = 0;
92 };
95 public:
96
98 typedef typename Node::Ptr NodePtr;
99
100 /* a DecisionTree just contains the root */
101 NodePtr root_;
102
103 protected:
104
106 template<typename It, typename ValueIt>
107 NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const;
108
110 template<typename M, typename X> NodePtr
111 convert(const typename DecisionTree<M, X>::NodePtr& f, const std::map<M,
112 L>& map, std::function<Y(const X&)> op);
113
115 DecisionTree();
116
117 public:
118
121
123 DecisionTree(const Y& y);
124
126 DecisionTree(const L& label, const Y& y1, const Y& y2);
127
129 DecisionTree(const LabelC& label, const Y& y1, const Y& y2);
130
132 DecisionTree(const std::vector<LabelC>& labelCs, const std::vector<Y>& ys);
133
135 DecisionTree(const std::vector<LabelC>& labelCs, const std::string& table);
136
138 template<typename Iterator>
139 DecisionTree(Iterator begin, Iterator end, const L& label);
140
142 DecisionTree(const L& label, //
143 const DecisionTree& f0, const DecisionTree& f1);
144
146 template<typename M, typename X>
147 DecisionTree(const DecisionTree<M, X>& other,
148 const std::map<M, L>& map, std::function<Y(const X&)> op);
149
153
155 void print(const std::string& s = "DecisionTree") const;
156
157 // Testable
158 bool equals(const DecisionTree& other, double tol = 1e-9) const;
159
163
165 virtual ~DecisionTree() {
166 }
167
169 bool operator==(const DecisionTree& q) const;
170
172 const Y& operator()(const Assignment<L>& x) const;
173
175 DecisionTree apply(const Unary& op) const;
176
178 DecisionTree apply(const DecisionTree& g, const Binary& op) const;
179
182 DecisionTree choose(const L& label, size_t index) const {
183 NodePtr newRoot = root_->choose(label, index);
184 return DecisionTree(newRoot);
185 }
186
188 DecisionTree combine(const L& label, size_t cardinality, const Binary& op) const;
189
191 DecisionTree combine(const LabelC& labelC, const Binary& op) const {
192 return combine(labelC.first, labelC.second, op);
193 }
194
196 void dot(std::ostream& os, bool showZero = true) const;
197
199 void dot(const std::string& name, bool showZero = true) const;
200
203
204 // internal use only
205 DecisionTree(const NodePtr& root);
206
207 // internal use only
208 template<typename Iterator> NodePtr
209 compose(Iterator begin, Iterator end, const L& label) const;
210
212
213 }; // DecisionTree
214
217 template<typename Y, typename L>
219 const typename DecisionTree<L, Y>::Unary& op) {
220 return f.apply(op);
221 }
222
223 template<typename Y, typename L>
224 DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f,
225 const DecisionTree<L, Y>& g,
226 const typename DecisionTree<L, Y>::Binary& op) {
227 return f.apply(g, op);
228 }
229
230} // namespace gtsam
An assignment from labels to a discrete value index (size_t)
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:218
Template to create a binary predicate.
Definition: Testable.h:111
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
Definition: DecisionTree-inl.h:52
Definition: DecisionTree-inl.h:142
Decision Tree L = label for variables Y = function range (any algebra), e.g., bool,...
Definition: DecisionTree.h:38
DecisionTree apply(const Unary &op) const
apply Unary operation "op" to f
Definition: DecisionTree-inl.h:624
DecisionTree choose(const L &label, size_t index) const
create a new function where value(label)==index It's like "restrict" in Darwiche09book pg329,...
Definition: DecisionTree.h:182
virtual ~DecisionTree()
Make virtual.
Definition: DecisionTree.h:165
DecisionTree combine(const LabelC &labelC, const Binary &op) const
combine with LabelC for convenience
Definition: DecisionTree.h:191
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
void dot(std::ostream &os, bool showZero=true) const
output to graphviz format, stream version
Definition: DecisionTree-inl.h:661
std::pair< L, size_t > LabelC
A label annotated with cardinality.
Definition: DecisionTree.h:47
NodePtr convert(const typename DecisionTree< M, X >::NodePtr &f, const std::map< M, L > &map, std::function< Y(const X &)> op)
Convert to a different type.
Definition: DecisionTree-inl.h:569
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
const Y & operator()(const Assignment< L > &x) const
evaluate
Definition: DecisionTree-inl.h:619
Node::Ptr NodePtr
---------------------— Node base class ------------------------—
Definition: DecisionTree.h:98
bool operator==(const DecisionTree &q) const
equality
Definition: DecisionTree-inl.h:614
void print(const std::string &s="DecisionTree") const
GTSAM-style print.
Definition: DecisionTree-inl.h:609
DecisionTree()
Default constructor.
Definition: DecisionTree-inl.h:372
std::function< Y(const Y &)> Unary
Handy typedefs for unary and binary function types.
Definition: DecisionTree.h:43
---------------------— Node base class ------------------------—
Definition: DecisionTree.h:54