GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
gtsam::DecisionTreeFactor Class Reference

#include <DecisionTreeFactor.h>

Inheritance diagram for gtsam::DecisionTreeFactor:
Inheritance graph
[legend]
Collaboration diagram for gtsam::DecisionTreeFactor:
Collaboration graph
[legend]

Public Types

typedef DecisionTreeFactor This
 
typedef DiscreteFactor Base
 Typedef to base class.
 
typedef std::shared_ptr< DecisionTreeFactorshared_ptr
 
typedef AlgebraicDecisionTree< KeyADT
 
using Values = DiscreteValues
 backwards compatibility
 
typedef KeyVector::iterator iterator
 Iterator over keys.
 
typedef KeyVector::const_iterator const_iterator
 Const iterator over keys.
 
using LabelFormatter = std::function< std::string(Key)>
 
using ValueFormatter = std::function< std::string(double)>
 
using CompareFunc = std::function< bool(const double &, const double &)>
 
using Unary = std::function< double(const double &)>
 
using UnaryAssignment = std::function< double(const Assignment< Key > &, const double &)>
 
using Binary = std::function< double(const double &, const double &)>
 
using LabelC = std::pair< Key, size_t >
 
using NodePtr = typename Node::Ptr
 
Wrapper support
using Names = DiscreteValues::Names
 Translation table from values to strings.
 

Public Member Functions

AlgebraicDecisionTree operator+ (const AlgebraicDecisionTree &g) const
 
AlgebraicDecisionTree operator* (const AlgebraicDecisionTree &g) const
 
AlgebraicDecisionTree operator/ (const AlgebraicDecisionTree &g) const
 
AlgebraicDecisionTree sum (const Key &label, size_t cardinality) const
 
AlgebraicDecisionTree sum (const typename Base::LabelC &labelC) const
 
void print (const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
 print method customized to value type double.
 
bool equals (const AlgebraicDecisionTree &other, double tol=1e-9) const
 Equality method customized to value type double.
 
Standard Constructors
 DecisionTreeFactor ()
 
 DecisionTreeFactor (const DiscreteKeys &keys, const ADT &potentials)
 
 DecisionTreeFactor (const DiscreteKeys &keys, const std::vector< double > &table)
 
 DecisionTreeFactor (const DiscreteKeys &keys, const std::string &table)
 
template<class SOURCE >
 DecisionTreeFactor (const DiscreteKey &key, SOURCE table)
 Single-key specialization.
 
 DecisionTreeFactor (const DiscreteKey &key, const std::vector< double > &row)
 Single-key specialization, with vector of doubles.
 
 DecisionTreeFactor (const DiscreteConditional &c)
 
Testable
bool equals (const DiscreteFactor &other, double tol=1e-9) const override
 equality
 
void print (const std::string &s="DecisionTreeFactor:\, const KeyFormatter &formatter=DefaultKeyFormatter) const override
 print
 
Advanced Interface
DecisionTreeFactor apply (const DecisionTreeFactor &f, ADT::Binary op) const
 
shared_ptr combine (size_t nrFrontals, ADT::Binary op) const
 
shared_ptr combine (const Ordering &keys, ADT::Binary op) const
 
std::vector< std::pair< DiscreteValues, double > > enumerate () const
 Enumerate all values into a map from values to double.
 
DiscreteKeys discreteKeys () const
 Return all the discrete keys associated with this factor.
 
DecisionTreeFactor prune (size_t maxNrAssignments) const
 Prune the decision tree of discrete variables. More...
 
Wrapper support
void dot (std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const
 
void dot (const std::string &name, const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const
 
std::string dot (const KeyFormatter &keyFormatter=DefaultKeyFormatter, bool showZero=true) const
 
std::string markdown (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
 Render as markdown table. More...
 
std::string html (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
 Render as html table. More...
 
HybridValues methods.
double error (const HybridValues &values) const override
 
Testable
bool equals (const This &other, double tol=1e-9) const
 check equality
 
virtual void printKeys (const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
 print only keys
 
Standard Interface
bool empty () const
 Whether the factor is empty (involves zero variables).
 
Key front () const
 First key.
 
Key back () const
 Last key.
 
const_iterator find (Key key) const
 find
 
const KeyVectorkeys () const
 Access the factor's involved variable keys.
 
const_iterator begin () const
 
const_iterator end () const
 
size_t size () const
 
Advanced Interface
KeyVectorkeys ()
 
iterator begin ()
 
iterator end ()
 
Testable
void print (const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const
 GTSAM-style print. More...
 
bool equals (const DecisionTree &other, const CompareFunc &compare=&DefaultCompare) const
 
Standard Interface
bool empty () const
 Check if tree is empty.
 
bool operator== (const DecisionTree &q) const
 
const double & operator() (const Assignment< Key > &x) const
 
void visit (Func f) const
 Visit all leaves in depth-first fashion. More...
 
void visitLeaf (Func f) const
 Visit all leaves in depth-first fashion. More...
 
void visitWith (Func f) const
 Visit all leaves in depth-first fashion. More...
 
size_t nrLeaves () const
 Return the number of leaves in the tree.
 
fold (Func f, X x0) const
 Fold a binary function over the tree, returning accumulator. More...
 
std::set< Keylabels () const
 
DecisionTree apply (const Unary &op) const
 
DecisionTree apply (const UnaryAssignment &op) const
 Apply Unary operation "op" to f while also providing the corresponding assignment. More...
 
DecisionTree apply (const DecisionTree &g, const Binary &op) const
 
DecisionTree choose (const Key &label, size_t index) const
 
DecisionTree combine (const Key &label, size_t cardinality, const Binary &op) const
 
DecisionTree combine (const LabelC &labelC, const Binary &op) const
 
void dot (std::ostream &os, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const
 
void dot (const std::string &name, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const
 
std::string dot (const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter, bool showZero=true) const
 
Advanced Interface
NodePtr compose (Iterator begin, Iterator end, const Key &label) const
 

Public Attributes

NodePtr root_
 A DecisionTree just contains the root. TODO(dellaert): make protected.
 

Protected Member Functions

NodePtr create (It begin, It end, ValueIt beginY, ValueIt endY) const
 
NodePtr convertFrom (const typename DecisionTree< M, X >::NodePtr &f, std::function< Key(const M &)> L_of_M, std::function< double(const X &)> Y_of_X) const
 Convert from a DecisionTree<M, X> to DecisionTree<L, Y>. More...
 

Static Protected Member Functions

static bool DefaultCompare (const double &a, const double &b)
 Default method for comparison of two objects of type Y.
 
Standard Constructors
template<typename CONTAINER >
static Factor FromKeys (const CONTAINER &keys)
 
template<typename ITERATOR >
static Factor FromIterators (ITERATOR first, ITERATOR last)
 

Protected Attributes

std::map< Key, size_t > cardinalities_
 
KeyVector keys_
 The keys involved in this factor.
 

Standard Interface

double evaluate (const DiscreteValues &values) const
 
double operator() (const DiscreteValues &values) const override
 Evaluate probability distribution, sugar.
 
double error (const DiscreteValues &values) const
 Calculate error for DiscreteValues x, is -log(probability).
 
DecisionTreeFactor operator* (const DecisionTreeFactor &f) const override
 multiply two factors
 
size_t cardinality (Key j) const
 
DecisionTreeFactor operator/ (const DecisionTreeFactor &f) const
 divide by factor f (safely)
 
DecisionTreeFactor toDecisionTreeFactor () const override
 Convert into a decisiontree.
 
shared_ptr sum (size_t nrFrontals) const
 Create new factor by summing all values with the same separator values.
 
shared_ptr sum (const Ordering &keys) const
 Create new factor by summing all values with the same separator values.
 
shared_ptr max (size_t nrFrontals) const
 Create new factor by maximizing over all values with the same separator.
 
shared_ptr max (const Ordering &keys) const
 Create new factor by maximizing over all values with the same separator.
 
static double safe_div (const double &a, const double &b)
 

Detailed Description

A discrete probabilistic factor.

Member Typedef Documentation

◆ LabelC

using gtsam::DecisionTree< Key , double >::LabelC = std::pair<Key , size_t>
inherited

A label annotated with cardinality

◆ NodePtr

using gtsam::DecisionTree< Key , double >::NodePtr = typename Node::Ptr
inherited

------------------—— Node base class ---------------------—— A function is a shared pointer to the root of a DT

◆ Unary

using gtsam::DecisionTree< Key , double >::Unary = std::function<double (const double &)>
inherited

Handy typedefs for unary and binary function types

Constructor & Destructor Documentation

◆ DecisionTreeFactor() [1/5]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( )

Default constructor for I/O

◆ DecisionTreeFactor() [2/5]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( const DiscreteKeys keys,
const ADT potentials 
)

Constructor from DiscreteKeys and AlgebraicDecisionTree

◆ DecisionTreeFactor() [3/5]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( const DiscreteKeys keys,
const std::vector< double > &  table 
)

Constructor from doubles

◆ DecisionTreeFactor() [4/5]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( const DiscreteKeys keys,
const std::string &  table 
)

Constructor from string

◆ DecisionTreeFactor() [5/5]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( const DiscreteConditional c)
explicit

Construct from a DiscreteConditional type

Member Function Documentation

◆ apply() [1/4]

DecisionTreeFactor gtsam::DecisionTreeFactor::apply ( const DecisionTreeFactor f,
ADT::Binary  op 
) const

Apply binary operator (*this) "op" f

Parameters
fthe second argument for op
opa binary operator that operates on AlgebraicDecisionTree

◆ apply() [2/4]

DecisionTree< Key , double > gtsam::DecisionTree< Key , double >::apply ( const Unary op) const
inherited

apply Unary operation "op" to f

◆ apply() [3/4]

DecisionTree< Key , double > gtsam::DecisionTree< Key , double >::apply ( const UnaryAssignment &  op) const
inherited

Apply Unary operation "op" to f while also providing the corresponding assignment.

Apply unary operator with assignment.

Parameters
opFunction which takes Assignment<L> and Y as input and returns object of type Y.
Returns
DecisionTree

◆ apply() [4/4]

DecisionTree< Key , double > gtsam::DecisionTree< Key , double >::apply ( const DecisionTree< Key, double > &  g,
const Binary &  op 
) const
inherited

apply binary operation "op" to f and g

◆ begin() [1/2]

const_iterator gtsam::Factor::begin ( ) const
inlineinherited

Iterator at beginning of involved variable keys

◆ begin() [2/2]

iterator gtsam::Factor::begin ( )
inlineinherited

Iterator at beginning of involved variable keys

◆ choose()

DecisionTree gtsam::DecisionTree< Key , double >::choose ( const Key label,
size_t  index 
) const
inlineinherited

create a new function where value(label)==index It's like "restrict" in Darwiche09book pg329, 330?

◆ combine() [1/4]

shared_ptr gtsam::DecisionTreeFactor::combine ( size_t  nrFrontals,
ADT::Binary  op 
) const

Combine frontal variables using binary operator "op"

Parameters
nrFrontalsnr. of frontal to combine variables in this factor
opa binary operator that operates on AlgebraicDecisionTree
Returns
shared pointer to newly created DecisionTreeFactor

◆ combine() [2/4]

shared_ptr gtsam::DecisionTreeFactor::combine ( const Ordering keys,
ADT::Binary  op 
) const

Combine frontal variables in an Ordering using binary operator "op"

Parameters
nrFrontalsnr. of frontal to combine variables in this factor
opa binary operator that operates on AlgebraicDecisionTree
Returns
shared pointer to newly created DecisionTreeFactor

◆ combine() [3/4]

DecisionTree< Key , double > gtsam::DecisionTree< Key , double >::combine ( const Key label,
size_t  cardinality,
const Binary &  op 
) const
inherited

combine subtrees on key with binary operation "op"

◆ combine() [4/4]

DecisionTree gtsam::DecisionTree< Key , double >::combine ( const LabelC labelC,
const Binary &  op 
) const
inlineinherited

combine with LabelC for convenience

◆ convertFrom()

DecisionTree< Key , double >::NodePtr gtsam::DecisionTree< Key , double >::convertFrom ( const typename DecisionTree< M, X >::NodePtr f,
std::function< Key (const M &)>  L_of_M,
std::function< double (const X &)>  Y_of_X 
) const
protectedinherited

Convert from a DecisionTree<M, X> to DecisionTree<L, Y>.

Template Parameters
MThe previous label type.
XThe previous value type.
Parameters
fThe node pointer to the root of the previous DecisionTree.
L_of_MFunctor to convert from label type M to type L.
Y_of_XFunctor to convert from value type X to type Y.
Returns
NodePtr

◆ create()

DecisionTree< Key , double >::NodePtr gtsam::DecisionTree< Key , double >::create ( It  begin,
It  end,
ValueIt  beginY,
ValueIt  endY 
) const
protectedinherited

Internal recursive function to create from keys, cardinalities, and Y values

◆ dot() [1/6]

void gtsam::DecisionTreeFactor::dot ( std::ostream &  os,
const KeyFormatter keyFormatter = DefaultKeyFormatter,
bool  showZero = true 
) const

output to graphviz format, stream version

◆ dot() [2/6]

void gtsam::DecisionTreeFactor::dot ( const std::string &  name,
const KeyFormatter keyFormatter = DefaultKeyFormatter,
bool  showZero = true 
) const

output to graphviz format, open a file

◆ dot() [3/6]

std::string gtsam::DecisionTreeFactor::dot ( const KeyFormatter keyFormatter = DefaultKeyFormatter,
bool  showZero = true 
) const

output to graphviz format string

◆ dot() [4/6]

void gtsam::DecisionTree< Key , double >::dot ( std::ostream &  os,
const LabelFormatter &  labelFormatter,
const ValueFormatter &  valueFormatter,
bool  showZero = true 
) const
inherited

output to graphviz format, stream version

◆ dot() [5/6]

void gtsam::DecisionTree< Key , double >::dot ( const std::string &  name,
const LabelFormatter &  labelFormatter,
const ValueFormatter &  valueFormatter,
bool  showZero = true 
) const
inherited

output to graphviz format, open a file

◆ dot() [6/6]

std::string gtsam::DecisionTree< Key , double >::dot ( const LabelFormatter &  labelFormatter,
const ValueFormatter &  valueFormatter,
bool  showZero = true 
) const
inherited

output to graphviz format string

◆ end() [1/2]

const_iterator gtsam::Factor::end ( ) const
inlineinherited

Iterator at end of involved variable keys

◆ end() [2/2]

iterator gtsam::Factor::end ( )
inlineinherited

Iterator at end of involved variable keys

◆ error()

double gtsam::DecisionTreeFactor::error ( const HybridValues values) const
overridevirtual

Calculate error for HybridValues x, is -log(probability) Simply dispatches to DiscreteValues version.

Reimplemented from gtsam::Factor.

◆ evaluate()

double gtsam::DecisionTreeFactor::evaluate ( const DiscreteValues values) const
inline

Calculate probability for given values x, is just look up in AlgebraicDecisionTree.

◆ fold()

X gtsam::DecisionTree< Key , double >::fold ( Func  f,
x0 
) const
inherited

Fold a binary function over the tree, returning accumulator.

Template Parameters
Xtype for accumulator.
Parameters
fbinary function: Y * X -> X returning an updated accumulator.
x0initial value for accumulator.
Returns
X final value for accumulator.
Note
X is always passed by value.
Due to pruning, leaves might not exhaust choices.

Example: auto add = [](const double& y, double x) { return y + x; }; double sum = tree.fold(add, 0.0);

◆ FromIterators()

template<typename ITERATOR >
static Factor gtsam::Factor::FromIterators ( ITERATOR  first,
ITERATOR  last 
)
inlinestaticprotectedinherited

Construct factor from iterator keys. This is called internally from derived factor static factor methods, as a workaround for not being able to call the protected constructors above.

◆ FromKeys()

template<typename CONTAINER >
static Factor gtsam::Factor::FromKeys ( const CONTAINER &  keys)
inlinestaticprotectedinherited

Construct factor from container of keys. This is called internally from derived factor static factor methods, as a workaround for not being able to call the protected constructors above.

◆ html()

std::string gtsam::DecisionTreeFactor::html ( const KeyFormatter keyFormatter = DefaultKeyFormatter,
const Names names = {} 
) const
overridevirtual

Render as html table.

Parameters
keyFormatterGTSAM-style Key formatter.
namesoptional, category names corresponding to choices.
Returns
std::string a html string.

Implements gtsam::DiscreteFactor.

Reimplemented in gtsam::DiscreteConditional.

◆ keys()

KeyVector& gtsam::Factor::keys ( )
inlineinherited
Returns
keys involved in this factor

◆ labels()

std::set< Key > gtsam::DecisionTree< Key , double >::labels ( ) const
inherited

Retrieve all unique labels as a set.

Get (partial) labels by performing a visit.

This method performs a depth-first search to go to every leaf and records the keys assignment which leads to that leaf. Since the tree can be pruned, there might be a leaf at a lower depth which results in a partial assignment (i.e. not all keys are specified).

E.g. given a tree with 3 keys, there may be a branch where the 3rd key has the same values for all the leaves. This leads to the branch being pruned so we get a leaf which is arrived at by just the first 2 keys and their assignments.

◆ markdown()

std::string gtsam::DecisionTreeFactor::markdown ( const KeyFormatter keyFormatter = DefaultKeyFormatter,
const Names names = {} 
) const
overridevirtual

Render as markdown table.

Parameters
keyFormatterGTSAM-style Key formatter.
namesoptional, category names corresponding to choices.
Returns
std::string a markdown string.

Implements gtsam::DiscreteFactor.

Reimplemented in gtsam::DiscreteConditional.

◆ operator()()

const double & gtsam::DecisionTree< Key , double >::operator() ( const Assignment< Key > &  x) const
inherited

evaluate

◆ operator*()

AlgebraicDecisionTree gtsam::AlgebraicDecisionTree< Key >::operator* ( const AlgebraicDecisionTree< Key > &  g) const
inlineinherited

product

◆ operator+()

AlgebraicDecisionTree gtsam::AlgebraicDecisionTree< Key >::operator+ ( const AlgebraicDecisionTree< Key > &  g) const
inlineinherited

sum

◆ operator/()

AlgebraicDecisionTree gtsam::AlgebraicDecisionTree< Key >::operator/ ( const AlgebraicDecisionTree< Key > &  g) const
inlineinherited

division

◆ operator==()

bool gtsam::DecisionTree< Key , double >::operator== ( const DecisionTree< Key, double > &  q) const
inherited

equality

◆ print()

void gtsam::DecisionTree< Key , double >::print ( const std::string &  s,
const LabelFormatter &  labelFormatter,
const ValueFormatter &  valueFormatter 
) const
inherited

GTSAM-style print.

Parameters
sPrefix string.
labelFormatterFunctor to format the node label.
valueFormatterFunctor to format the node value.

◆ prune()

DecisionTreeFactor gtsam::DecisionTreeFactor::prune ( size_t  maxNrAssignments) const

Prune the decision tree of discrete variables.

Pruning will set the leaves to be "pruned" to 0 indicating a 0 probability. An assignment is pruned if it is not in the top maxNrAssignments values.

A violation can occur if there are more duplicate values than maxNrAssignments. A violation here is the need to un-prune the decision tree (e.g. all assignment values are 1.0). We could have another case where some subset of duplicates exist (e.g. for a tree with 8 assignments we have 1, 1, 1, 1, 0.8, 0.7, 0.6, 0.5), but this is not a violation since the for maxNrAssignments=5 the top values are (1, 0.8).

Parameters
maxNrAssignmentsThe maximum number of assignments to keep.
Returns
DecisionTreeFactor

◆ size()

size_t gtsam::Factor::size ( ) const
inlineinherited
Returns
the number of variables involved in this factor

◆ sum() [1/2]

AlgebraicDecisionTree gtsam::AlgebraicDecisionTree< Key >::sum ( const Key label,
size_t  cardinality 
) const
inlineinherited

sum out variable

◆ sum() [2/2]

AlgebraicDecisionTree gtsam::AlgebraicDecisionTree< Key >::sum ( const typename Base::LabelC labelC) const
inlineinherited

sum out variable

◆ visit()

void gtsam::DecisionTree< Key , double >::visit ( Func  f) const
inherited

Visit all leaves in depth-first fashion.

Parameters
f(side-effect) Function taking the value of the leaf node.
Note
Due to pruning, the number of leaves may not be the same as the number of assignments. E.g. if we have a tree on 2 binary variables with all values being 1, then there are 2^2=4 assignments, but only 1 leaf.

Example: int sum = 0; auto visitor = [&](int y) { sum += y; }; tree.visit(visitor);

◆ visitLeaf()

void gtsam::DecisionTree< Key , double >::visitLeaf ( Func  f) const
inherited

Visit all leaves in depth-first fashion.

Parameters
f(side-effect) Function taking the leaf node pointer.
Note
Due to pruning, the number of leaves may not be the same as the number of assignments. E.g. if we have a tree on 2 binary variables with all values being 1, then there are 2^2=4 assignments, but only 1 leaf.

Example: int sum = 0; auto visitor = [&](const Leaf& leaf) { sum += leaf.constant(); }; tree.visitLeaf(visitor);

◆ visitWith()

void gtsam::DecisionTree< Key , double >::visitWith ( Func  f) const
inherited

Visit all leaves in depth-first fashion.

Parameters
f(side-effect) Function taking an assignment and a value.
Note
Due to pruning, the number of leaves may not be the same as the number of assignments. E.g. if we have a tree on 2 binary variables with all values being 1, then there are 2^2=4 assignments, but only 1 leaf.

Example: int sum = 0; auto visitor = [&](const Assignment<L>& assignment, int y) { sum += y; }; tree.visitWith(visitor);


The documentation for this class was generated from the following file: