gtsam
Loading...
Searching...
No Matches
DiscreteFactor.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
18
19#pragma once
20
21#include <gtsam/base/Testable.h>
25
26#include <string>
27namespace gtsam {
28
31class HybridValues;
32class Ordering;
33class TableFactor;
34
41class GTSAM_EXPORT DiscreteFactor : public Factor {
42 public:
43 // typedefs needed to play nice with gtsam
45 typedef std::shared_ptr<DiscreteFactor>
47 typedef Factor Base;
48
50
51 using Unary = std::function<double(const double&)>;
52 using UnaryAssignment =
53 std::function<double(const Assignment<Key>&, const double&)>;
54 using Binary = std::function<double(const double, const double)>;
55
56 protected:
58 std::map<Key, size_t> cardinalities_;
59
60 public:
63
66
72 template <typename CONTAINER>
73 DiscreteFactor(const CONTAINER& keys,
74 const std::map<Key, size_t> cardinalities = {})
75 : Base(keys), cardinalities_(cardinalities) {}
76
80
82 virtual bool equals(const DiscreteFactor& lf, double tol = 1e-9) const;
83
85 void print(
86 const std::string& s = "DiscreteFactor\n",
87 const KeyFormatter& formatter = DefaultKeyFormatter) const override {
88 Base::print(s, formatter);
89 }
90
94
96 DiscreteKeys discreteKeys() const;
97
98 std::map<Key, size_t> cardinalities() const { return cardinalities_; }
99
100 size_t cardinality(Key j) const { return cardinalities_.at(j); }
101
111 virtual double evaluate(const Assignment<Key>& values) const = 0;
112
114 double operator()(const DiscreteValues& values) const {
115 return evaluate(values);
116 }
117
119 virtual double error(const DiscreteValues& values) const;
120
125 double error(const HybridValues& c) const override;
126
128 virtual AlgebraicDecisionTree<Key> errorTree() const;
129
131 virtual DiscreteFactor::shared_ptr operator*(double s) const = 0;
132
136
141 * @param df DiscreteFactor shared_ptr
142 * @return DiscreteFactor::shared_ptr
143 */
145 const DiscreteFactor::shared_ptr& df) const;
146
149 const DiscreteFactor::shared_ptr& df) const = 0;
150
151 virtual DecisionTreeFactor toDecisionTreeFactor() const = 0;
152
154 virtual TableFactor toTableFactor() const;
155
157 virtual DiscreteFactor::shared_ptr sum(size_t nrFrontals) const = 0;
158
160 virtual DiscreteFactor::shared_ptr sum(const Ordering& keys) const = 0;
161
163 virtual double max() const = 0;
164
166 virtual DiscreteFactor::shared_ptr max(size_t nrFrontals) const = 0;
167
169 virtual DiscreteFactor::shared_ptr max(const Ordering& keys) const = 0;
170
178
183 virtual uint64_t nrValues() const = 0;
184
187 const DiscreteValues& assignment) const = 0;
188
192
195
203 virtual std::string markdown(
204 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
205 const Names& names = {}) const = 0;
206
214 virtual std::string html(
215 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
216 const Names& names = {}) const = 0;
217
219
220 private:
221#if GTSAM_ENABLE_BOOST_SERIALIZATION
223 friend class boost::serialization::access;
224 template <class ARCHIVE>
225 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
226 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
227 ar& BOOST_SERIALIZATION_NVP(cardinalities_);
228 }
229#endif
230};
231// DiscreteFactor
232
233// traits
234template <>
235struct traits<DiscreteFactor> : public Testable<DiscreteFactor> {};
236
237} // namespace gtsam
Concept check for values that can be used in unit tests.
Algebraic Decision Trees.
The base class for all factors.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
An assignment from labels to value index (size_t).
Definition Assignment.h:37
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:42
virtual DiscreteFactor::shared_ptr multiply(const DiscreteFactor::shared_ptr &f) const override
Multiply factors, DiscreteFactor::shared_ptr edition.
Definition DecisionTreeFactor.cpp:68
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:41
virtual DiscreteFactor::shared_ptr sum(const Ordering &keys) const =0
Create new factor by summing all values with the same separator values.
virtual std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const =0
Render as html table.
Factor Base
Our base class.
Definition DiscreteFactor.h:47
void print(const std::string &s="DiscreteFactor\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition DiscreteFactor.h:85
virtual DiscreteFactor::shared_ptr operator*(double s) const =0
Multiply with a scalar.
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition DiscreteFactor.h:46
virtual double max() const =0
Find the maximum value in the factor.
DiscreteFactor(const CONTAINER &keys, const std::map< Key, size_t > cardinalities={})
Construct from container of keys and map of cardinalities.
Definition DiscreteFactor.h:73
virtual DiscreteFactor::shared_ptr operator/(const DiscreteFactor::shared_ptr &df) const =0
divide by DiscreteFactor::shared_ptr f (safely)
DiscreteFactor::shared_ptr scale() const
Scale the factor values by the maximum to prevent underflow/overflow.
Definition DiscreteFactor.cpp:95
DiscreteValues Values
backwards compatibility
Definition DiscreteFactor.h:49
virtual DiscreteFactor::shared_ptr max(size_t nrFrontals) const =0
Create new factor by maximizing over all values with the same separator.
virtual double evaluate(const Assignment< Key > &values) const =0
Calculate probability for given values.
virtual DiscreteFactor::shared_ptr restrict(const DiscreteValues &assignment) const =0
Restrict the factor to the given assignment.
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
Definition DiscreteFactor.h:58
virtual DiscreteFactor::shared_ptr sum(size_t nrFrontals) const =0
Create new factor by summing all values with the same separator values.
virtual TableFactor toTableFactor() const
Convert to a sparse table, falling back through a decision tree.
Definition DiscreteFactor.cpp:90
DiscreteValues::Names Names
Translation table from values to strings.
Definition DiscreteFactor.h:194
double operator()(const DiscreteValues &values) const
Find value for given assignment of values to variables.
Definition DiscreteFactor.h:114
DiscreteFactor This
This class.
Definition DiscreteFactor.h:44
virtual DecisionTreeFactor operator*(const DecisionTreeFactor &) const =0
Multiply in a DecisionTreeFactor and return the result as DecisionTreeFactor.
virtual DiscreteFactor::shared_ptr max(const Ordering &keys) const =0
Create new factor by maximizing over all values with the same separator.
virtual std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const =0
Render as markdown table.
virtual uint64_t nrValues() const =0
Get the number of non-zero values contained in this factor.
DiscreteFactor()
Default constructor creates empty factor.
Definition DiscreteFactor.h:65
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:41
A map from keys to values.
Definition DiscreteValues.h:34
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.
Definition DiscreteValues.h:167
A discrete probabilistic factor optimized for sparsity.
Definition TableFactor.h:51
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
Factor()
Default constructor for I/O.
Definition Factor.h:94
virtual void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
Definition Factor.cpp:29
Definition Ordering.h:33
the error.