24#include <boost/function.hpp>
37 template<
typename L,
typename Y>
43 typedef std::function<Y(
const Y&)>
Unary;
44 typedef std::function<Y(
const Y&,
const Y&)> Binary;
56 typedef boost::shared_ptr<const Node> Ptr;
65 std::cout << ++nrNodes <<
" constructed " << id() << std::endl; std::cout.flush();
72 std::cout << --nrNodes <<
" destructed " << id() << std::endl; std::cout.flush();
77 const void* id()
const {
return this; }
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;
106 template<
typename It,
typename ValueIt>
107 NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY)
const;
110 template<
typename M,
typename X>
NodePtr
112 L>& map, std::function<Y(
const X&)> op);
132 DecisionTree(
const std::vector<LabelC>& labelCs,
const std::vector<Y>& ys);
135 DecisionTree(
const std::vector<LabelC>& labelCs,
const std::string& table);
138 template<
typename Iterator>
139 DecisionTree(Iterator begin, Iterator end,
const L& label);
146 template<
typename M,
typename X>
148 const std::map<M, L>& map, std::function<Y(
const X&)> op);
155 void print(
const std::string& s =
"DecisionTree")
const;
192 return combine(labelC.first, labelC.second, op);
196 void dot(std::ostream& os,
bool showZero =
true)
const;
199 void dot(
const std::string& name,
bool showZero =
true)
const;
208 template<
typename Iterator>
NodePtr
209 compose(Iterator begin, Iterator end,
const L& label)
const;
217 template<
typename Y,
typename L>
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);
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