Classes | Public Types | Protected Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
gtsam::TableFactor Class Reference

#include <TableFactor.h>

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

Classes

struct  Ring
 

Public Types

typedef std::vector< std::pair< DiscreteValues, double > > AssignValList
 
typedef DiscreteFactor Base
 Typedef to base class. More...
 
using Binary = std::function< double(const double, const double)>
 
typedef std::shared_ptr< TableFactorshared_ptr
 
typedef Eigen::SparseVector< double >::InnerIterator SparseIt
 
typedef TableFactor 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 Member Functions

Standard Constructors
 TableFactor ()
 
 TableFactor (const DiscreteKeys &keys, const TableFactor &potentials)
 
 TableFactor (const DiscreteKeys &keys, const Eigen::SparseVector< double > &table)
 
 TableFactor (const DiscreteKeys &keys, const std::vector< double > &table)
 
 TableFactor (const DiscreteKeys &keys, const std::string &table)
 
template<class SOURCE >
 TableFactor (const DiscreteKey &key, SOURCE table)
 Single-key specialization. More...
 
 TableFactor (const DiscreteKey &key, const std::vector< double > &row)
 Single-key specialization, with vector of doubles. More...
 
Advanced Interface
TableFactor apply (const TableFactor &f, Binary op) const
 
DiscreteKeys contractDkeys (const TableFactor &f) const
 Return keys in contract mode. More...
 
DiscreteKeys freeDkeys (const TableFactor &f) const
 Return keys in free mode. More...
 
DiscreteKeys unionDkeys (const TableFactor &f) const
 Return union of DiscreteKeys in two factors. More...
 
uint64_t unionRep (const DiscreteKeys &keys, const DiscreteValues &assign, const uint64_t idx) const
 Create unique representation of union modes. More...
 
std::unordered_map< uint64_t, AssignValListcreateMap (const DiscreteKeys &contract, const DiscreteKeys &free) const
 
uint64_t uniqueRep (const DiscreteKeys &keys, const uint64_t idx) const
 Create unique representation. More...
 
uint64_t uniqueRep (const DiscreteValues &assignments) const
 Create unique representation with DiscreteValues. More...
 
DiscreteValues findAssignments (const uint64_t idx) const
 Find DiscreteValues for corresponding index. More...
 
double findValue (const DiscreteValues &values) const
 Find value for corresponding DiscreteValues. More...
 
shared_ptr combine (size_t nrFrontals, Binary op) const
 
shared_ptr combine (const Ordering &keys, 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...
 
TableFactor prune (size_t maxNrAssignments) const
 Prune the decision tree of discrete variables. More...
 
Wrapper support
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)
 
double error (const DiscreteValues &values) const
 Error is just -log(value) More...
 
- 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 ()
 

Protected Attributes

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

Private Member Functions

DiscreteKey discreteKey (size_t i) const
 Return ith key in keys_ as a DiscreteKey. More...
 
size_t keyValueForIndex (Key target_key, uint64_t index) const
 Uses lazy cartesian product to find nth entry in the cartesian product of arrays in O(1) Example) v0 | v1 | val 0 | 0 | 10 0 | 1 | 21 1 | 0 | 32 1 | 1 | 43 keyValueForIndex(v1, 2) = 0. More...
 

Static Private Member Functions

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

Private Attributes

std::map< Key, size_tdenominators_
 Map of Keys and their denominators used in keyValueForIndex. More...
 
DiscreteKeys sorted_dkeys_
 Sorted DiscreteKeys to use internally. More...
 

Testable

bool equals (const DiscreteFactor &other, double tol=1e-9) const override
 equality More...
 
void print (const std::string &s="TableFactor:\, const KeyFormatter &formatter=DefaultKeyFormatter) const override
 print More...
 
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...
 
TableFactor operator* (const TableFactor &f) const
 multiply two TableFactors More...
 
DecisionTreeFactor operator* (const DecisionTreeFactor &f) const override
 multiple with DecisionTreeFactor More...
 
size_t cardinality (Key j) const
 
TableFactor operator/ (const TableFactor &f) const
 divide by factor f (safely) More...
 
DecisionTreeFactor toDecisionTreeFactor () const override
 Convert into a decisiontree. More...
 
TableFactor choose (const DiscreteValues assignments, DiscreteKeys parent_keys) const
 Create a TableFactor that is a subset of this TableFactor. 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

- Protected Member Functions inherited from gtsam::Factor
 Factor ()
 
template<typename CONTAINER >
 Factor (const CONTAINER &keys)
 
template<typename ITERATOR >
 Factor (ITERATOR first, ITERATOR last)
 
- 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)
 

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)

Definition at line 45 of file TableFactor.h.

Member Typedef Documentation

◆ AssignValList

typedef std::vector<std::pair<DiscreteValues, double> > gtsam::TableFactor::AssignValList

Definition at line 96 of file TableFactor.h.

◆ Base

Typedef to base class.

Definition at line 93 of file TableFactor.h.

◆ Binary

using gtsam::TableFactor::Binary = std::function<double(const double, const double)>

Definition at line 97 of file TableFactor.h.

◆ shared_ptr

typedef std::shared_ptr<TableFactor> gtsam::TableFactor::shared_ptr

Definition at line 94 of file TableFactor.h.

◆ SparseIt

typedef Eigen::SparseVector<double>::InnerIterator gtsam::TableFactor::SparseIt

Definition at line 95 of file TableFactor.h.

◆ This

Definition at line 92 of file TableFactor.h.

Constructor & Destructor Documentation

◆ TableFactor() [1/7]

gtsam::TableFactor::TableFactor ( )

Default constructor for I/O

Definition at line 31 of file TableFactor.cpp.

◆ TableFactor() [2/7]

gtsam::TableFactor::TableFactor ( const DiscreteKeys keys,
const TableFactor potentials 
)

Constructor from DiscreteKeys and TableFactor

Definition at line 34 of file TableFactor.cpp.

◆ TableFactor() [3/7]

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

Constructor from sparse_table

Definition at line 45 of file TableFactor.cpp.

◆ TableFactor() [4/7]

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

Constructor from doubles

Definition at line 129 of file TableFactor.h.

◆ TableFactor() [5/7]

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

Constructor from string

Definition at line 133 of file TableFactor.h.

◆ TableFactor() [6/7]

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

Single-key specialization.

Definition at line 138 of file TableFactor.h.

◆ TableFactor() [7/7]

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

Single-key specialization, with vector of doubles.

Definition at line 142 of file TableFactor.h.

Member Function Documentation

◆ apply()

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

Definition at line 218 of file TableFactor.cpp.

◆ cardinality()

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

Definition at line 183 of file TableFactor.h.

◆ choose()

TableFactor gtsam::TableFactor::choose ( const DiscreteValues  assignments,
DiscreteKeys  parent_keys 
) const

Create a TableFactor that is a subset of this TableFactor.

Definition at line 148 of file TableFactor.cpp.

◆ combine() [1/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

Definition at line 352 of file TableFactor.cpp.

◆ combine() [2/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

Definition at line 383 of file TableFactor.cpp.

◆ contractDkeys()

DiscreteKeys gtsam::TableFactor::contractDkeys ( const TableFactor f) const

Return keys in contract mode.

Definition at line 252 of file TableFactor.cpp.

◆ Convert() [1/2]

Eigen::SparseVector< double > gtsam::TableFactor::Convert ( const std::vector< double > &  table)
staticprivate

Convert probability table given as doubles to SparseVector. Example) {0, 1, 1, 0, 0, 1, 0} -> values: {1, 1, 1}, indices: {1, 2, 5}

Definition at line 60 of file TableFactor.cpp.

◆ Convert() [2/2]

Eigen::SparseVector< double > gtsam::TableFactor::Convert ( const std::string &  table)
staticprivate

Convert probability table given as string to SparseVector.

Definition at line 76 of file TableFactor.cpp.

◆ createMap()

unordered_map< uint64_t, TableFactor::AssignValList > gtsam::TableFactor::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.

Definition at line 297 of file TableFactor.cpp.

◆ discreteKey()

DiscreteKey gtsam::TableFactor::discreteKey ( size_t  i) const
inlineprivate

Return ith key in keys_ as a DiscreteKey.

Parameters
iith key in keys_
Returns
DiscreteKey

Definition at line 79 of file TableFactor.h.

◆ discreteKeys()

DiscreteKeys gtsam::TableFactor::discreteKeys ( ) const

Return all the discrete keys associated with this factor.

Definition at line 439 of file TableFactor.cpp.

◆ enumerate()

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

Enumerate all values into a map from values to double.

Definition at line 424 of file TableFactor.cpp.

◆ equals()

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

equality

Implements gtsam::DiscreteFactor.

Definition at line 86 of file TableFactor.cpp.

◆ error() [1/2]

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

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

Definition at line 122 of file TableFactor.cpp.

◆ 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.

Definition at line 127 of file TableFactor.cpp.

◆ evaluate()

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

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

Definition at line 163 of file TableFactor.h.

◆ findAssignments()

DiscreteValues gtsam::TableFactor::findAssignments ( const uint64_t  idx) const

Find DiscreteValues for corresponding index.

Definition at line 343 of file TableFactor.cpp.

◆ findValue()

double gtsam::TableFactor::findValue ( const DiscreteValues values) const

Find value for corresponding DiscreteValues.

Definition at line 109 of file TableFactor.cpp.

◆ freeDkeys()

DiscreteKeys gtsam::TableFactor::freeDkeys ( const TableFactor f) const

Return keys in free mode.

Definition at line 262 of file TableFactor.cpp.

◆ 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.

Definition at line 482 of file TableFactor.cpp.

◆ keyValueForIndex()

size_t gtsam::TableFactor::keyValueForIndex ( Key  target_key,
uint64_t  index 
) const
private

Uses lazy cartesian product to find nth entry in the cartesian product of arrays in O(1) Example) v0 | v1 | val 0 | 0 | 10 0 | 1 | 21 1 | 0 | 32 1 | 1 | 43 keyValueForIndex(v1, 2) = 0.

Parameters
target_keynth entry's key to find out its assigned value
indexnth entry in the sparse vector
Returns
TableFactor

Definition at line 418 of file TableFactor.cpp.

◆ 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.

Definition at line 452 of file TableFactor.cpp.

◆ max() [1/2]

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

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

Definition at line 208 of file TableFactor.h.

◆ max() [2/2]

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

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

Definition at line 213 of file TableFactor.h.

◆ operator()()

double gtsam::TableFactor::operator() ( const DiscreteValues values) const
overridevirtual

Evaluate probability distribution, sugar.

Implements gtsam::DiscreteFactor.

Definition at line 96 of file TableFactor.cpp.

◆ operator*() [1/2]

TableFactor gtsam::TableFactor::operator* ( const TableFactor f) const
inline

multiply two TableFactors

Definition at line 174 of file TableFactor.h.

◆ operator*() [2/2]

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

multiple with DecisionTreeFactor

Implements gtsam::DiscreteFactor.

Definition at line 132 of file TableFactor.cpp.

◆ operator/()

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

divide by factor f (safely)

Definition at line 186 of file TableFactor.h.

◆ print()

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

print

Reimplemented from gtsam::DiscreteFactor.

Definition at line 201 of file TableFactor.cpp.

◆ 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

Definition at line 515 of file TableFactor.cpp.

◆ safe_div()

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

Definition at line 193 of file TableFactor.cpp.

◆ sum() [1/2]

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

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

Definition at line 198 of file TableFactor.h.

◆ sum() [2/2]

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

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

Definition at line 203 of file TableFactor.h.

◆ toDecisionTreeFactor()

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

Convert into a decisiontree.

Implements gtsam::DiscreteFactor.

Definition at line 137 of file TableFactor.cpp.

◆ unionDkeys()

DiscreteKeys gtsam::TableFactor::unionDkeys ( const TableFactor f) const

Return union of DiscreteKeys in two factors.

Definition at line 272 of file TableFactor.cpp.

◆ unionRep()

uint64_t gtsam::TableFactor::unionRep ( const DiscreteKeys keys,
const DiscreteValues assign,
const uint64_t  idx 
) const

Create unique representation of union modes.

Definition at line 281 of file TableFactor.cpp.

◆ uniqueRep() [1/2]

uint64_t gtsam::TableFactor::uniqueRep ( const DiscreteKeys keys,
const uint64_t  idx 
) const

Create unique representation.

Definition at line 320 of file TableFactor.cpp.

◆ uniqueRep() [2/2]

uint64_t gtsam::TableFactor::uniqueRep ( const DiscreteValues assignments) const

Create unique representation with DiscreteValues.

Definition at line 332 of file TableFactor.cpp.

Member Data Documentation

◆ cardinalities_

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

Map of Keys and their cardinalities.

Definition at line 48 of file TableFactor.h.

◆ denominators_

std::map<Key, size_t> gtsam::TableFactor::denominators_
private

Map of Keys and their denominators used in keyValueForIndex.

Definition at line 54 of file TableFactor.h.

◆ sorted_dkeys_

DiscreteKeys gtsam::TableFactor::sorted_dkeys_
private

Sorted DiscreteKeys to use internally.

Definition at line 56 of file TableFactor.h.

◆ sparse_table_

Eigen::SparseVector<double> gtsam::TableFactor::sparse_table_
protected

SparseVector of nonzero probabilities.

Definition at line 50 of file TableFactor.h.


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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:47:12