26 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION 27 #include <boost/serialization/nvp.hpp> 48 template<
typename L,
typename Y>
57 using LabelFormatter = std::function<std::string(L)>;
58 using ValueFormatter = std::function<std::string(Y)>;
59 using CompareFunc = std::function<bool(const Y&, const Y&)>;
62 using Unary = std::function<Y(const Y&)>;
63 using UnaryAssignment = std::function<Y(const Assignment<L>&,
const Y&)>;
64 using Binary = std::function<Y(const Y&, const Y&)>;
75 using Ptr = std::shared_ptr<const Node>;
77 #ifdef DT_DEBUG_MEMORY 83 #ifdef DT_DEBUG_MEMORY 84 std::cout << ++nrNodes <<
" constructed " << id() << std::endl;
91 #ifdef DT_DEBUG_MEMORY 92 std::cout << --nrNodes <<
" destructed " << id() << std::endl;
98 const void* id()
const {
return this; }
101 virtual void print(
const std::string& s,
102 const LabelFormatter& labelFormatter,
103 const ValueFormatter& valueFormatter)
const = 0;
104 virtual void dot(std::ostream& os,
const LabelFormatter& labelFormatter,
105 const ValueFormatter& valueFormatter,
106 bool showZero)
const = 0;
107 virtual bool sameLeaf(
const Leaf& q)
const = 0;
108 virtual bool sameLeaf(
const Node& q)
const = 0;
109 virtual bool equals(
const Node& other,
const CompareFunc& compare =
111 virtual const Y& operator()(
const Assignment<L>& x)
const = 0;
112 virtual Ptr apply(
const Unary& op)
const = 0;
113 virtual Ptr apply(
const UnaryAssignment& op,
115 virtual Ptr apply_f_op_g(
const Node&,
const Binary&)
const = 0;
116 virtual Ptr apply_g_op_fL(
const Leaf&,
const Binary&)
const = 0;
117 virtual Ptr apply_g_op_fC(
const Choice&,
const Binary&)
const = 0;
118 virtual Ptr choose(
const L& label,
size_t index)
const = 0;
119 virtual bool isLeaf()
const = 0;
122 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION 124 friend class boost::serialization::access;
125 template <
class ARCHIVE>
126 void serialize(ARCHIVE& ar,
const unsigned int ) {}
142 template<
typename It,
typename ValueIt>
143 NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY)
const;
155 template <
typename M,
typename X>
157 std::function<L(
const M&)> L_of_M,
158 std::function<Y(
const X&)> Y_of_X)
const;
177 DecisionTree(
const std::vector<LabelC>& labelCs,
const std::vector<Y>& ys);
180 DecisionTree(
const std::vector<LabelC>& labelCs,
const std::string& table);
183 template<
typename Iterator>
184 DecisionTree(Iterator begin, Iterator end,
const L& label);
197 template <
typename X,
typename Func>
210 template <
typename M,
typename X,
typename Func>
225 void print(
const std::string& s,
const LabelFormatter& labelFormatter,
226 const ValueFormatter& valueFormatter)
const;
262 template <
typename Func>
263 void visit(Func f)
const;
279 template <
typename Func>
296 template <
typename Func>
317 template <
typename Func,
typename X>
318 X
fold(Func f, X x0)
const;
321 std::set<L>
labels()
const;
348 const Binary& op)
const;
352 return combine(labelC.first, labelC.second, op);
356 void dot(std::ostream& os,
const LabelFormatter& labelFormatter,
357 const ValueFormatter& valueFormatter,
bool showZero =
true)
const;
360 void dot(
const std::string& name,
const LabelFormatter& labelFormatter,
361 const ValueFormatter& valueFormatter,
bool showZero =
true)
const;
364 std::string
dot(
const LabelFormatter& labelFormatter,
365 const ValueFormatter& valueFormatter,
366 bool showZero =
true)
const;
375 template<
typename Iterator>
NodePtr 376 compose(Iterator begin, Iterator end,
const L& label)
const;
381 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION 383 friend class boost::serialization::access;
384 template <
class ARCHIVE>
385 void serialize(ARCHIVE& ar,
const unsigned int ) {
386 ar& BOOST_SERIALIZATION_NVP(root_);
391 template <
class L,
class Y>
396 template<
typename L,
typename Y>
404 template<
typename L,
typename Y>
406 const typename DecisionTree<L, Y>::UnaryAssignment& op) {
411 template<
typename L,
typename Y>
414 const typename DecisionTree<L, Y>::Binary& op) {
415 return f.
apply(g, op);
424 template <
typename L,
typename T1,
typename T2>
virtual ~DecisionTree()=default
Make virtual.
NodePtr convertFrom(const typename DecisionTree< M, X >::NodePtr &f, std::function< L(const M &)> L_of_M, std::function< Y(const X &)> Y_of_X) const
Convert from a DecisionTree<M, X> to DecisionTree<L, Y>.
Definition: DecisionTree-inl.h:671
Typedefs for easier changing of types.
Concept check for values that can be used in unit tests.
void visit(Func f) const
Visit all leaves in depth-first fashion.
Definition: DecisionTree-inl.h:736
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:195
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
An assignment from labels to a discrete value index (size_t)
Definition: Testable.h:152
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
Apply unary operator op to DecisionTree f.
Definition: DecisionTree.h:398
DecisionTree()
Definition: DecisionTree-inl.h:465
std::function< double(const double &)> Unary
Definition: DecisionTree.h:62
std::set< L > labels() const
Definition: DecisionTree-inl.h:853
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition: DecisionTree.h:136
Definition: Testable.h:112
DecisionTree combine(const L &label, size_t cardinality, const Binary &op) const
Definition: DecisionTree-inl.h:937
void visitWith(Func f) const
Visit all leaves in depth-first fashion.
Definition: DecisionTree-inl.h:816
Definition: DecisionTree.h:49
size_t nrLeaves() const
Return the number of leaves in the tree.
Definition: DecisionTree-inl.h:823
std::pair< DecisionTree< L, T1 >, DecisionTree< L, T2 > > unzip(const DecisionTree< L, std::pair< T1, T2 > > &input)
unzip a DecisionTree with std::pair values.
Definition: DecisionTree.h:425
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
void visitLeaf(Func f) const
Visit all leaves in depth-first fashion.
Definition: DecisionTree-inl.h:773
Definition: DecisionTree-inl.h:173
X fold(Func f, X x0) const
Fold a binary function over the tree, returning accumulator.
Definition: DecisionTree-inl.h:833
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const DecisionTree< L, Y > &g, const typename DecisionTree< L, Y >::Binary &op)
Apply binary operator op to DecisionTree f.
Definition: DecisionTree.h:412
DecisionTree apply(const Unary &op) const
Definition: DecisionTree-inl.h:889
NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const
Definition: DecisionTree-inl.h:633
typename Node::Ptr NodePtr
Definition: DecisionTree.h:133
Definition: Assignment.h:37
DecisionTree combine(const LabelC &labelC, const Binary &op) const
Definition: DecisionTree.h:351
Definition: chartTesting.h:28
bool empty() const
Check if tree is empty.
Definition: DecisionTree.h:240
Definition: DecisionTree-inl.h:51
bool operator==(const DecisionTree &q) const
Definition: DecisionTree-inl.h:879
Definition: DecisionTree.h:74
std::pair< Key, size_t > LabelC
Definition: DecisionTree.h:67
DecisionTree choose(const L &label, size_t index) const
Definition: DecisionTree.h:341
static bool DefaultCompare(const Y &a, const Y &b)
Default method for comparison of two objects of type Y.
Definition: DecisionTree.h:52