gtsam
Loading...
Searching...
No Matches
gtsam::TableFactor Class Reference

Detailed Description

A discrete probabilistic factor optimized for sparsity.

Uses sparse_table_ to store only the nonzero probabilities. Computes the assigned value for the key using the ordering which the nonzero probabilties are stored in. (lazy cartesian product)

Inheritance diagram for gtsam::TableFactor:

Testable

static double safe_div (const double &a, const double &b)
bool equals (const DiscreteFactor &other, double tol=1e-9) const override
 equality
void print (const std::string &s="TableFactor:\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
 print
Eigen::SparseVector< double > sparseTable () const
 Getter for the underlying sparse vector.
double evaluate (const Assignment< Key > &values) const override
 Evaluate probability distribution, is just look up in TableFactor.
double error (const DiscreteValues &values) const override
 Calculate error for DiscreteValues x, is -log(probability).
DiscreteFactor::shared_ptr operator* (double s) const override
 multiply with a scalar
TableFactor operator* (const TableFactor &f) const
 multiply two TableFactors
DecisionTreeFactor operator* (const DecisionTreeFactor &f) const override
 multiply with DecisionTreeFactor
virtual DiscreteFactor::shared_ptr multiply (const DiscreteFactor::shared_ptr &f) const override
 Multiply factors, DiscreteFactor::shared_ptr edition.
TableFactor operator/ (const TableFactor &f) const
 divide by factor f (safely)
DiscreteFactor::shared_ptr operator/ (const DiscreteFactor::shared_ptr &f) const override
 divide by DiscreteFactor::shared_ptr f (safely)
DecisionTreeFactor toDecisionTreeFactor () const override
 Convert into a decisiontree.
TableFactor toTableFactor () const override
 Return this sparse table representation.
TableFactor choose (const DiscreteValues parentAssignments, DiscreteKeys parent_keys) const
 Create a TableFactor that is a subset of this TableFactor.
DiscreteFactor::shared_ptr sum (size_t nrFrontals) const override
 Create new factor by summing all values with the same separator values.
DiscreteFactor::shared_ptr sum (const Ordering &keys) const override
 Create new factor by summing all values with the same separator values.
double max () const override
 Find the maximum value in the factor.
DiscreteFactor::shared_ptr max (size_t nrFrontals) const override
 Create new factor by maximizing over all values with the same separator.
DiscreteFactor::shared_ptr max (const Ordering &keys) const override
 Create new factor by maximizing over all values with the same separator.

Public Member Functions

Standard Constructors
 TableFactor ()
 Default constructor for I/O.
 TableFactor (const DiscreteKeys &keys, const TableFactor &potentials)
 Constructor from DiscreteKeys and TableFactor.
 TableFactor (const DiscreteKeys &keys, const Eigen::SparseVector< double > &table)
 Constructor from sparse_table.
 TableFactor (const DiscreteKeys &keys, const std::vector< double > &table)
 Constructor from doubles.
 TableFactor (const DiscreteKeys &keys, const std::string &table)
 Constructor from string.
template<class SOURCE>
 TableFactor (const DiscreteKey &key, SOURCE table)
 Single-key specialization.
 TableFactor (const DiscreteKey &key, const std::vector< double > &row)
 Single-key specialization, with vector of doubles.
 TableFactor (const DiscreteKeys &keys, const DecisionTreeFactor &dtf)
 Constructor from DecisionTreeFactor.
 TableFactor (const DecisionTreeFactor &dtf)
 TableFactor (const DiscreteKeys &keys, const DecisionTree< Key, double > &dtree)
 Constructor from DecisionTree<Key, double>/AlgebraicDecisionTree.
 TableFactor (const DiscreteConditional &c)
 Construct from a DiscreteConditional type.
Advanced Interface
TableFactor apply (Unary op) const
 Apply unary operator op(*this) where op accepts the discrete value.
TableFactor apply (UnaryAssignment op) const
 Apply unary operator op(*this) where op accepts the discrete assignment and the value at that assignment.
TableFactor apply (const TableFactor &f, Binary op) const
 Apply binary operator (*this) "op" f.
uint64_t unionRep (const DiscreteKeys &keys, const DiscreteValues &assign, const uint64_t idx) const
 Create unique representation of union modes.
std::unordered_map< uint64_t, AssignValList > createMap (const DiscreteKeys &contract, const DiscreteKeys &free) const
 Create a hash map of input factor with assignment of contract modes as keys and vector of hashed assignment of free modes and value as values.
uint64_t uniqueRep (const DiscreteKeys &keys, const uint64_t idx) const
 Create unique representation.
uint64_t uniqueRep (const DiscreteValues &assignments) const
 Create unique representation with DiscreteValues.
DiscreteValues findAssignments (const uint64_t idx) const
 Find DiscreteValues for corresponding index.
double findValue (const DiscreteValues &values) const
 Find value for corresponding DiscreteValues.
shared_ptr combine (size_t nrFrontals, Binary op) const
 Combine frontal variables using binary operator "op".
shared_ptr combine (const Ordering &keys, Binary op) const
 Combine frontal variables in an Ordering using binary operator "op".
std::vector< std::pair< DiscreteValues, double > > enumerate () const
 Enumerate all values into a map from values to double.
TableFactor prune (size_t maxNrAssignments) const
 Prune the decision tree of discrete variables.
uint64_t nrValues () const override
 Get the number of non-zero values contained in this factor.
DiscreteFactor::shared_ptr restrict (const DiscreteValues &assignment) const override
 Restrict the factor to the given assignment.
Wrapper support
std::string markdown (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
 Render as markdown table.
std::string html (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
 Render as html table.
HybridValues methods.
double error (const HybridValues &values) const override
 Calculate error for HybridValues x, is -log(probability) Simply dispatches to DiscreteValues version.
Public Member Functions inherited from gtsam::DiscreteFactor
 DiscreteFactor ()
 Default constructor creates empty factor.
template<typename CONTAINER>
 DiscreteFactor (const CONTAINER &keys, const std::map< Key, size_t > cardinalities={})
 Construct from container of keys and map of cardinalities.
DiscreteKeys discreteKeys () const
 Return all the discrete keys associated with this factor.
std::map< Key, size_t > cardinalities () const
size_t cardinality (Key j) const
double operator() (const DiscreteValues &values) const
 Find value for given assignment of values to variables.
virtual AlgebraicDecisionTree< KeyerrorTree () const
 Compute error for each assignment and return as a tree.
DiscreteFactor::shared_ptr scale () const
 Scale the factor values by the maximum to prevent underflow/overflow.
Public Member Functions inherited from gtsam::Factor
virtual ~Factor ()=default
 Default destructor.
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
 Iterator at beginning of involved variable keys.
const_iterator end () const
 Iterator at end of involved variable keys.
size_t size () const
virtual void printKeys (const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
 print only keys
bool equals (const This &other, double tol=1e-9) const
 check equality
KeyVectorkeys ()
iterator begin ()
 Iterator at beginning of involved variable keys.
iterator end ()
 Iterator at end of involved variable keys.

Static Public Member Functions

static Eigen::SparseVector< double > Convert (const DiscreteKeys &keys, const std::vector< double > &table)
 Convert probability table given as doubles to SparseVector.
static Eigen::SparseVector< double > Convert (const DiscreteKeys &keys, const std::string &table)
 Convert probability table given as string to SparseVector.

Public Types

typedef TableFactor This
typedef DiscreteFactor Base
 Typedef to base class.
typedef std::shared_ptr< TableFactorshared_ptr
typedef Eigen::SparseVector< double >::InnerIterator SparseIt
typedef std::vector< std::pair< DiscreteValues, double > > AssignValList
Public Types inherited from gtsam::DiscreteFactor
typedef DiscreteFactor This
 This class.
typedef std::shared_ptr< DiscreteFactorshared_ptr
 shared_ptr to this class
typedef Factor Base
 Our base class.
using Values = DiscreteValues
 backwards compatibility
using Unary = std::function<double(const double&)>
using UnaryAssignment
using Binary = std::function<double(const double, const double)>
using Names = DiscreteValues::Names
 Translation table from values to strings.
Public Types inherited from gtsam::Factor
typedef KeyVector::iterator iterator
 Iterator over keys.
typedef KeyVector::const_iterator const_iterator
 Const iterator over keys.

Protected Attributes

Eigen::SparseVector< double > sparse_table_
 SparseVector of nonzero probabilities.
Protected Attributes inherited from gtsam::DiscreteFactor
std::map< Key, size_t > cardinalities_
 Map of Keys and their cardinalities.
Protected Attributes inherited from gtsam::Factor
KeyVector keys_
 The keys involved in this factor.

Additional Inherited Members

 Factor ()
 Default constructor for I/O.
template<typename CONTAINER>
 Factor (const CONTAINER &keys)
 Construct factor from container of keys.
template<typename ITERATOR>
 Factor (ITERATOR first, ITERATOR last)
 Construct factor from iterator keys.
template<typename CONTAINER>
static Factor FromKeys (const CONTAINER &keys)
 Construct factor from container of keys.
template<typename ITERATOR>
static Factor FromIterators (ITERATOR first, ITERATOR last)
 Construct factor from iterator keys.

Member Function Documentation

◆ apply() [1/3]

TableFactor gtsam::TableFactor::apply ( const TableFactor & f,
Binary op ) const

Apply binary operator (*this) "op" f.

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

◆ apply() [2/3]

TableFactor gtsam::TableFactor::apply ( Unary op) const

Apply unary operator op(*this) where op accepts the discrete value.

Parameters
opa unary operator that operates on TableFactor

◆ apply() [3/3]

TableFactor gtsam::TableFactor::apply ( UnaryAssignment op) const

Apply unary operator op(*this) where op accepts the discrete assignment and the value at that assignment.

Parameters
opa unary operator that operates on TableFactor

◆ combine() [1/2]

TableFactor::shared_ptr gtsam::TableFactor::combine ( const Ordering & keys,
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 TableFactor
Returns
shared pointer to newly created TableFactor

◆ combine() [2/2]

TableFactor::shared_ptr gtsam::TableFactor::combine ( size_t nrFrontals,
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 TableFactor
Returns
shared pointer to newly created TableFactor

◆ Convert()

Eigen::SparseVector< double > gtsam::TableFactor::Convert ( const DiscreteKeys & keys,
const std::vector< double > & table )
static

Convert probability table given as doubles to SparseVector.

Example: {0, 1, 1, 0, 0, 1, 0} -> values: {1, 1, 1}, indices: {1, 2, 5}

◆ equals()

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

equality

Reimplemented from gtsam::DiscreteFactor.

◆ error() [1/2]

double gtsam::TableFactor::error ( const DiscreteValues & values) const
overridevirtual

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

Reimplemented from gtsam::DiscreteFactor.

◆ error() [2/2]

double gtsam::TableFactor::error ( const HybridValues & values) const
overridevirtual

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

Reimplemented from gtsam::DiscreteFactor.

◆ evaluate()

double gtsam::TableFactor::evaluate ( const Assignment< Key > & values) const
overridevirtual

Evaluate probability distribution, is just look up in TableFactor.

Implements gtsam::DiscreteFactor.

◆ html()

string gtsam::TableFactor::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.

◆ markdown()

string gtsam::TableFactor::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.

◆ max() [1/3]

double gtsam::TableFactor::max ( ) const
overridevirtual

Find the maximum value in the factor.

Implements gtsam::DiscreteFactor.

◆ max() [2/3]

DiscreteFactor::shared_ptr gtsam::TableFactor::max ( const Ordering & keys) const
overridevirtual

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

Implements gtsam::DiscreteFactor.

◆ max() [3/3]

DiscreteFactor::shared_ptr gtsam::TableFactor::max ( size_t nrFrontals) const
overridevirtual

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

Implements gtsam::DiscreteFactor.

◆ multiply()

DiscreteFactor::shared_ptr gtsam::TableFactor::multiply ( const DiscreteFactor::shared_ptr & f) const
overridevirtual

Multiply factors, DiscreteFactor::shared_ptr edition.

This method accepts DiscreteFactor::shared_ptr and uses dynamic dispatch and specializations to perform the most efficient multiplication.

While converting a DecisionTreeFactor to a TableFactor is efficient, the reverse is not. Hence we specialize the code to return a TableFactor always.

Parameters
fThe factor to multiply with.
Returns
DiscreteFactor::shared_ptr

Reimplemented from gtsam::DiscreteFactor.

◆ nrValues()

uint64_t gtsam::TableFactor::nrValues ( ) const
inlineoverridevirtual

Get the number of non-zero values contained in this factor.

It could be much smaller than prod_{key}(cardinality(key)).

Implements gtsam::DiscreteFactor.

◆ operator*() [1/2]

DecisionTreeFactor gtsam::TableFactor::operator* ( const DecisionTreeFactor & f) const
overridevirtual

multiply with DecisionTreeFactor

Implements gtsam::DiscreteFactor.

◆ operator*() [2/2]

DiscreteFactor::shared_ptr gtsam::TableFactor::operator* ( double s) const
inlineoverridevirtual

multiply with a scalar

Implements gtsam::DiscreteFactor.

◆ operator/()

DiscreteFactor::shared_ptr gtsam::TableFactor::operator/ ( const DiscreteFactor::shared_ptr & f) const
overridevirtual

divide by DiscreteFactor::shared_ptr f (safely)

Implements gtsam::DiscreteFactor.

◆ print()

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

print

Reimplemented from gtsam::DiscreteFactor.

◆ prune()

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

Prune the decision tree of discrete variables.

Pruning will set the values 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
TableFactor

◆ restrict()

DiscreteFactor::shared_ptr gtsam::TableFactor::restrict ( const DiscreteValues & assignment) const
overridevirtual

Restrict the factor to the given assignment.

Implements gtsam::DiscreteFactor.

◆ sum() [1/2]

DiscreteFactor::shared_ptr gtsam::TableFactor::sum ( const Ordering & keys) const
overridevirtual

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

Implements gtsam::DiscreteFactor.

◆ sum() [2/2]

DiscreteFactor::shared_ptr gtsam::TableFactor::sum ( size_t nrFrontals) const
overridevirtual

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

Implements gtsam::DiscreteFactor.

◆ toDecisionTreeFactor()

DecisionTreeFactor gtsam::TableFactor::toDecisionTreeFactor ( ) const
overridevirtual

Convert into a decisiontree.

Implements gtsam::DiscreteFactor.

◆ toTableFactor()

TableFactor gtsam::TableFactor::toTableFactor ( ) const
inlineoverridevirtual

Return this sparse table representation.

Reimplemented from gtsam::DiscreteFactor.


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