Public Types | Protected Attributes | List of all members
gtsam::DecisionTreeFactor Class Reference

#include <DecisionTreeFactor.h>

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

Public Types

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

Public Member Functions

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. More...
 
 DecisionTreeFactor (const DiscreteKey &key, const std::vector< double > &row)
 Single-key specialization, with vector of doubles. More...
 
 DecisionTreeFactor (const DiscreteConditional &c)
 
Testable
bool equals (const DiscreteFactor &other, double tol=1e-9) const override
 equality More...
 
void print (const std::string &s="DecisionTreeFactor:\, const KeyFormatter &formatter=DefaultKeyFormatter) const override
 print More...
 
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. More...
 
DiscreteKeys discreteKeys () const
 Return all the discrete keys associated with this factor. More...
 
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
 
- Public Member Functions inherited from gtsam::DiscreteFactor
 DiscreteFactor ()
 
template<typename CONTAINER >
 DiscreteFactor (const CONTAINER &keys)
 
void print (const std::string &s="DiscreteFactor\, const KeyFormatter &formatter=DefaultKeyFormatter) const override
 print More...
 
double error (const DiscreteValues &values) const
 Error is just -log(value) More...
 
double error (const HybridValues &c) const override
 
- Public Member Functions inherited from gtsam::Factor
virtual ~Factor ()=default
 Default destructor. More...
 
bool empty () const
 Whether the factor is empty (involves zero variables). More...
 
Key front () const
 First key. More...
 
Key back () const
 Last key. More...
 
const_iterator find (Key key) const
 find More...
 
const KeyVectorkeys () const
 Access the factor's involved variable keys. More...
 
const_iterator begin () const
 
const_iterator end () const
 
size_t size () const
 
virtual void printKeys (const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
 print only keys More...
 
bool equals (const This &other, double tol=1e-9) const
 check equality More...
 
KeyVectorkeys ()
 
iterator begin ()
 
iterator end ()
 
- Public Member Functions inherited from gtsam::AlgebraicDecisionTree< Key >
 AlgebraicDecisionTree (double leaf=1.0)
 
 AlgebraicDecisionTree (const Base &add)
 
 AlgebraicDecisionTree (const Key &label, double y1, double y2)
 
 AlgebraicDecisionTree (const typename Base::LabelC &labelC, double y1, double y2)
 
 AlgebraicDecisionTree (const std::vector< typename Base::LabelC > &labelCs, const std::vector< double > &ys)
 
 AlgebraicDecisionTree (const std::vector< typename Base::LabelC > &labelCs, const std::string &table)
 
 AlgebraicDecisionTree (Iterator begin, Iterator end, const Key &label)
 
 AlgebraicDecisionTree (const AlgebraicDecisionTree< M > &other, const std::map< M, Key > &map)
 
bool equals (const AlgebraicDecisionTree &other, double tol=1e-9) const
 Equality method customized to value type double. More...
 
AlgebraicDecisionTree operator* (const AlgebraicDecisionTree &g) const
 
AlgebraicDecisionTree operator+ (const AlgebraicDecisionTree &g) const
 
AlgebraicDecisionTree operator/ (const AlgebraicDecisionTree &g) const
 
void print (const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
 print method customized to value type double. More...
 
AlgebraicDecisionTree sum (const Key &label, size_t cardinality) const
 
AlgebraicDecisionTree sum (const typename Base::LabelC &labelC) const
 
- Public Member Functions inherited from gtsam::DecisionTree< Key, double >
 DecisionTree ()
 
 DecisionTree (const double &y)
 
 DecisionTree (const Key &label, const double &y1, const double &y2)
 Create tree with 2 assignments y1, y2, splitting on variable label More...
 
 DecisionTree (const LabelC &label, const double &y1, const double &y2)
 
 DecisionTree (const std::vector< LabelC > &labelCs, const std::vector< double > &ys)
 
 DecisionTree (const std::vector< LabelC > &labelCs, const std::string &table)
 
 DecisionTree (Iterator begin, Iterator end, const Key &label)
 
 DecisionTree (const Key &label, const DecisionTree &f0, const DecisionTree &f1)
 
 DecisionTree (const DecisionTree< Key, X > &other, Func Y_of_X)
 Convert from a different value type. More...
 
 DecisionTree (const DecisionTree< M, X > &other, const std::map< M, Key > &map, Func Y_of_X)
 Convert from a different value type X to value type Y, also transate labels via map from type M to L. More...
 
 DecisionTree (const NodePtr &root)
 
NodePtr compose (Iterator begin, Iterator end, const Key &label) const
 
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
 
virtual ~DecisionTree ()=default
 Make virtual. More...
 
bool empty () const
 Check if tree is empty. More...
 
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. More...
 
X 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
 

Protected Attributes

std::map< Key, size_tcardinalities_
 
- Protected Attributes inherited from gtsam::Factor
KeyVector keys_
 The keys involved in this factor. More...
 

Standard Interface

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

Additional Inherited Members

- Public Attributes inherited from gtsam::DecisionTree< Key, double >
NodePtr root_
 A DecisionTree just contains the root. TODO(dellaert): make protected. More...
 
- Protected Member Functions inherited from gtsam::Factor
 Factor ()
 
template<typename CONTAINER >
 Factor (const CONTAINER &keys)
 
template<typename ITERATOR >
 Factor (ITERATOR first, ITERATOR last)
 
- Protected Member Functions inherited from gtsam::DecisionTree< Key, double >
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...
 
NodePtr create (It begin, It end, ValueIt beginY, ValueIt endY) const
 
- Static Protected Member Functions inherited from gtsam::Factor
template<typename CONTAINER >
static Factor FromKeys (const CONTAINER &keys)
 
template<typename ITERATOR >
static Factor FromIterators (ITERATOR first, ITERATOR last)
 
- Static Protected Member Functions inherited from gtsam::DecisionTree< Key, double >
static bool DefaultCompare (const double &a, const double &b)
 Default method for comparison of two objects of type Y. More...
 

Detailed Description

A discrete probabilistic factor.

Definition at line 44 of file DecisionTreeFactor.h.

Member Typedef Documentation

◆ ADT

Definition at line 51 of file DecisionTreeFactor.h.

◆ Base

Typedef to base class.

Definition at line 49 of file DecisionTreeFactor.h.

◆ shared_ptr

Definition at line 50 of file DecisionTreeFactor.h.

◆ This

Definition at line 48 of file DecisionTreeFactor.h.

Constructor & Destructor Documentation

◆ DecisionTreeFactor() [1/7]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( )

Default constructor for I/O

Definition at line 32 of file DecisionTreeFactor.cpp.

◆ DecisionTreeFactor() [2/7]

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

Constructor from DiscreteKeys and AlgebraicDecisionTree

Definition at line 35 of file DecisionTreeFactor.cpp.

◆ DecisionTreeFactor() [3/7]

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

Constructor from doubles

Definition at line 290 of file DecisionTreeFactor.cpp.

◆ DecisionTreeFactor() [4/7]

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

Constructor from string

Definition at line 297 of file DecisionTreeFactor.cpp.

◆ DecisionTreeFactor() [5/7]

template<class SOURCE >
gtsam::DecisionTreeFactor::DecisionTreeFactor ( const DiscreteKey key,
SOURCE  table 
)
inline

Single-key specialization.

Definition at line 75 of file DecisionTreeFactor.h.

◆ DecisionTreeFactor() [6/7]

gtsam::DecisionTreeFactor::DecisionTreeFactor ( const DiscreteKey key,
const std::vector< double > &  row 
)
inline

Single-key specialization, with vector of doubles.

Definition at line 79 of file DecisionTreeFactor.h.

◆ DecisionTreeFactor() [7/7]

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

Construct from a DiscreteConditional type

Definition at line 42 of file DecisionTreeFactor.cpp.

Member Function Documentation

◆ apply()

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

Definition at line 89 of file DecisionTreeFactor.cpp.

◆ cardinality()

size_t gtsam::DecisionTreeFactor::cardinality ( Key  j) const
inline

Definition at line 122 of file DecisionTreeFactor.h.

◆ combine() [1/2]

DecisionTreeFactor::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

Definition at line 108 of file DecisionTreeFactor.cpp.

◆ combine() [2/2]

DecisionTreeFactor::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

Definition at line 135 of file DecisionTreeFactor.cpp.

◆ discreteKeys()

DiscreteKeys gtsam::DecisionTreeFactor::discreteKeys ( ) const

Return all the discrete keys associated with this factor.

Definition at line 185 of file DecisionTreeFactor.cpp.

◆ dot() [1/3]

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

output to graphviz format, stream version

Definition at line 204 of file DecisionTreeFactor.cpp.

◆ dot() [2/3]

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

output to graphviz format, open a file

Definition at line 211 of file DecisionTreeFactor.cpp.

◆ dot() [3/3]

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

output to graphviz format string

Definition at line 218 of file DecisionTreeFactor.cpp.

◆ enumerate()

std::vector< std::pair< DiscreteValues, double > > gtsam::DecisionTreeFactor::enumerate ( ) const

Enumerate all values into a map from values to double.

Definition at line 168 of file DecisionTreeFactor.cpp.

◆ equals()

bool gtsam::DecisionTreeFactor::equals ( const DiscreteFactor other,
double  tol = 1e-9 
) const
overridevirtual

equality

Implements gtsam::DiscreteFactor.

Reimplemented in gtsam::DiscreteConditional.

Definition at line 48 of file DecisionTreeFactor.cpp.

◆ error() [1/2]

double gtsam::DecisionTreeFactor::error ( const DiscreteValues values) const

Calculate error for DiscreteValues x, is -log(probability).

Definition at line 59 of file DecisionTreeFactor.cpp.

◆ error() [2/2]

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.

Definition at line 64 of file DecisionTreeFactor.cpp.

◆ evaluate()

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

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

Definition at line 103 of file DecisionTreeFactor.h.

◆ html()

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.

Definition at line 256 of file DecisionTreeFactor.cpp.

◆ markdown()

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.

Definition at line 225 of file DecisionTreeFactor.cpp.

◆ max() [1/2]

shared_ptr gtsam::DecisionTreeFactor::max ( size_t  nrFrontals) const
inline

Create new factor by maximizing over all values with the same separator.

Definition at line 143 of file DecisionTreeFactor.h.

◆ max() [2/2]

shared_ptr gtsam::DecisionTreeFactor::max ( const Ordering keys) const
inline

Create new factor by maximizing over all values with the same separator.

Definition at line 148 of file DecisionTreeFactor.h.

◆ operator()()

double gtsam::DecisionTreeFactor::operator() ( const DiscreteValues values) const
inlineoverridevirtual

Evaluate probability distribution, sugar.

Implements gtsam::DiscreteFactor.

Definition at line 108 of file DecisionTreeFactor.h.

◆ operator*()

DecisionTreeFactor gtsam::DecisionTreeFactor::operator* ( const DecisionTreeFactor f) const
inlineoverridevirtual

multiply two factors

Implements gtsam::DiscreteFactor.

Definition at line 116 of file DecisionTreeFactor.h.

◆ operator/()

DecisionTreeFactor gtsam::DecisionTreeFactor::operator/ ( const DecisionTreeFactor f) const
inline

divide by factor f (safely)

Definition at line 125 of file DecisionTreeFactor.h.

◆ print()

void gtsam::DecisionTreeFactor::print ( const std::string &  s = "DecisionTreeFactor:\n",
const KeyFormatter formatter = DefaultKeyFormatter 
) const
overridevirtual

print

Reimplemented from gtsam::Factor.

Reimplemented in gtsam::DiscreteConditional, gtsam::DiscreteDistribution, and gtsam::DiscreteLookupTable.

Definition at line 77 of file DecisionTreeFactor.cpp.

◆ 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

Definition at line 304 of file DecisionTreeFactor.cpp.

◆ safe_div()

double gtsam::DecisionTreeFactor::safe_div ( const double &  a,
const double &  b 
)
static

Definition at line 69 of file DecisionTreeFactor.cpp.

◆ sum() [1/2]

shared_ptr gtsam::DecisionTreeFactor::sum ( size_t  nrFrontals) const
inline

Create new factor by summing all values with the same separator values.

Definition at line 133 of file DecisionTreeFactor.h.

◆ sum() [2/2]

shared_ptr gtsam::DecisionTreeFactor::sum ( const Ordering keys) const
inline

Create new factor by summing all values with the same separator values.

Definition at line 138 of file DecisionTreeFactor.h.

◆ toDecisionTreeFactor()

DecisionTreeFactor gtsam::DecisionTreeFactor::toDecisionTreeFactor ( ) const
inlineoverridevirtual

Convert into a decisiontree.

Implements gtsam::DiscreteFactor.

Definition at line 130 of file DecisionTreeFactor.h.

Member Data Documentation

◆ cardinalities_

std::map<Key, size_t> gtsam::DecisionTreeFactor::cardinalities_
protected

Definition at line 54 of file DecisionTreeFactor.h.


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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:46:17