gtsam
Loading...
Searching...
No Matches
gtsam::DiscreteFactor Class Referenceabstract

Detailed Description

Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.

DecisionTreeFactor, which represent a discrete distribution over a set of variables.

  • Hybrid factors, such as
Inheritance diagram for gtsam::DiscreteFactor:

Wrapper support

using Names = DiscreteValues::Names
 Translation table from values to strings.
virtual std::string markdown (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const =0
 Render as markdown table.
virtual std::string html (const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const =0
 Render as html table.

Public Member Functions

Standard Constructors
 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.
Testable
virtual bool equals (const DiscreteFactor &lf, double tol=1e-9) const
 equals
void print (const std::string &s="DiscreteFactor\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
 print
Standard Interface
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
virtual double evaluate (const Assignment< Key > &values) const =0
 Calculate probability for given values.
double operator() (const DiscreteValues &values) const
 Find value for given assignment of values to variables.
virtual double error (const DiscreteValues &values) const
 Error is just -log(value).
double error (const HybridValues &c) const override
 All factor types need to implement an error function.
virtual AlgebraicDecisionTree< Key > errorTree () const
 Compute error for each assignment and return as a tree.
virtual DiscreteFactor::shared_ptr operator* (double s) const =0
 Multiply with a scalar.
virtual DecisionTreeFactor operator* (const DecisionTreeFactor &) const =0
 Multiply in a DecisionTreeFactor and return the result as DecisionTreeFactor.
virtual DiscreteFactor::shared_ptr multiply (const DiscreteFactor::shared_ptr &df) const
 Multiply in a DiscreteFactor and return the result as DiscreteFactor, both via shared pointers.
virtual DiscreteFactor::shared_ptr operator/ (const DiscreteFactor::shared_ptr &df) const =0
 divide by DiscreteFactor::shared_ptr f (safely)
virtual DecisionTreeFactor toDecisionTreeFactor () const =0
virtual TableFactor toTableFactor () const
 Convert to a sparse table, falling back through a decision tree.
virtual DiscreteFactor::shared_ptr sum (size_t nrFrontals) const =0
 Create new factor by summing all values with the same separator values.
virtual DiscreteFactor::shared_ptr sum (const Ordering &keys) const =0
 Create new factor by summing all values with the same separator values.
virtual double max () const =0
 Find the maximum value in the factor.
virtual DiscreteFactor::shared_ptr max (size_t nrFrontals) const =0
 Create new factor by maximizing over all values with the same separator.
virtual DiscreteFactor::shared_ptr max (const Ordering &keys) const =0
 Create new factor by maximizing over all values with the same separator.
DiscreteFactor::shared_ptr scale () const
 Scale the factor values by the maximum to prevent underflow/overflow.
virtual uint64_t nrValues () const =0
 Get the number of non-zero values contained in this factor.
virtual DiscreteFactor::shared_ptr restrict (const DiscreteValues &assignment) const =0
 Restrict the factor to the given assignment.
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 KeyVector & keys () 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
KeyVector & keys ()
iterator begin ()
 Iterator at beginning of involved variable keys.
iterator end ()
 Iterator at end of involved variable keys.

Public Types

typedef DiscreteFactor This
 This class.
typedef std::shared_ptr< DiscreteFactor > shared_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)>
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

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 Typedef Documentation

◆ UnaryAssignment

using gtsam::DiscreteFactor::UnaryAssignment
Initial value:
std::function<double(const Assignment<Key>&, const double&)>
An assignment from labels to value index (size_t).
Definition Assignment.h:37

Constructor & Destructor Documentation

◆ DiscreteFactor()

template<typename CONTAINER>
gtsam::DiscreteFactor::DiscreteFactor ( const CONTAINER & keys,
const std::map< Key, size_t > cardinalities = {} )
inline

Construct from container of keys and map of cardinalities.

This constructor is used internally from derived factor constructors, either from a container of keys or from a boost::assign::list_of.

Member Function Documentation

◆ equals()

bool DiscreteFactor::equals ( const DiscreteFactor & lf,
double tol = 1e-9 ) const
virtual

◆ error() [1/2]

double DiscreteFactor::error ( const DiscreteValues & values) const
virtual

Error is just -log(value).

Reimplemented in gtsam::DecisionTreeFactor, gtsam::DiscreteConditional, and gtsam::TableFactor.

◆ error() [2/2]

double DiscreteFactor::error ( const HybridValues & hybridValues) const
overridevirtual

All factor types need to implement an error function.

In factor graphs, this is the negative log-likelihood.

Reimplemented from gtsam::Factor.

Reimplemented in gtsam::TableFactor.

◆ errorTree()

AlgebraicDecisionTree< Key > DiscreteFactor::errorTree ( ) const
virtual

Compute error for each assignment and return as a tree.

Reimplemented in gtsam::Constraint.

◆ evaluate()

virtual double gtsam::DiscreteFactor::evaluate ( const Assignment< Key > & values) const
pure virtual

Calculate probability for given values.

Calls specialized evaluation under the hood.

Note: Uses Assignment<Key> as it is the base class of DiscreteValues.

Parameters
valuesDiscrete assignment.
Returns
double

Implemented in gtsam::AllDiff, gtsam::BinaryAllDiff, gtsam::DecisionTreeFactor, gtsam::DiscreteConditional, gtsam::Domain, gtsam::SingleValue, gtsam::TableDistribution, and gtsam::TableFactor.

◆ html()

virtual std::string gtsam::DiscreteFactor::html ( const KeyFormatter & keyFormatter = DefaultKeyFormatter,
const Names & names = {} ) const
pure virtual

Render as html table.

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::DiscreteConditional, and gtsam::TableFactor.

◆ markdown()

virtual std::string gtsam::DiscreteFactor::markdown ( const KeyFormatter & keyFormatter = DefaultKeyFormatter,
const Names & names = {} ) const
pure virtual

Render as markdown table.

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::DiscreteConditional, and gtsam::TableFactor.

◆ max() [1/3]

virtual double gtsam::DiscreteFactor::max ( ) const
pure virtual

Find the maximum value in the factor.

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::TableDistribution, and gtsam::TableFactor.

◆ max() [2/3]

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::max ( const Ordering & keys) const
pure virtual

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::DiscreteConditional, gtsam::TableDistribution, and gtsam::TableFactor.

◆ max() [3/3]

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::max ( size_t nrFrontals) const
pure virtual

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::TableDistribution, and gtsam::TableFactor.

◆ multiply()

DiscreteFactor::shared_ptr DiscreteFactor::multiply ( const DiscreteFactor::shared_ptr & df) const
virtual

Multiply in a DiscreteFactor and return the result as DiscreteFactor, both via shared pointers.

Parameters
dfDiscreteFactor shared_ptr
Returns
DiscreteFactor::shared_ptr

Reimplemented in gtsam::DecisionTreeFactor, and gtsam::TableFactor.

◆ nrValues()

virtual uint64_t gtsam::DiscreteFactor::nrValues ( ) const
pure virtual

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

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::Domain, gtsam::TableDistribution, and gtsam::TableFactor.

◆ operator*() [1/2]

◆ operator*() [2/2]

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::operator* ( double s) const
pure virtual

◆ operator/()

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::operator/ ( const DiscreteFactor::shared_ptr & df) const
pure virtual

◆ print()

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

◆ restrict()

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::restrict ( const DiscreteValues & assignment) const
pure virtual

Restrict the factor to the given assignment.

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, and gtsam::TableFactor.

◆ scale()

DiscreteFactor::shared_ptr DiscreteFactor::scale ( ) const

Scale the factor values by the maximum to prevent underflow/overflow.

Returns
DiscreteFactor::shared_ptr

◆ sum() [1/2]

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::sum ( const Ordering & keys) const
pure virtual

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::TableDistribution, and gtsam::TableFactor.

◆ sum() [2/2]

virtual DiscreteFactor::shared_ptr gtsam::DiscreteFactor::sum ( size_t nrFrontals) const
pure virtual

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

Implemented in gtsam::Constraint, gtsam::DecisionTreeFactor, gtsam::TableDistribution, and gtsam::TableFactor.

◆ toDecisionTreeFactor()

virtual DecisionTreeFactor gtsam::DiscreteFactor::toDecisionTreeFactor ( ) const
pure virtual

◆ toTableFactor()

TableFactor DiscreteFactor::toTableFactor ( ) const
virtual

Convert to a sparse table, falling back through a decision tree.

Reimplemented in gtsam::AllDiff, gtsam::DecisionTreeFactor, gtsam::TableDistribution, and gtsam::TableFactor.


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