gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
DiscreteFactorGraph.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
19#pragma once
20
26#include <gtsam/base/FastSet.h>
27
28#include <boost/make_shared.hpp>
29#include <string>
30#include <utility>
31#include <vector>
32
33namespace gtsam {
34
35// Forward declarations
36class DiscreteFactorGraph;
37class DiscreteConditional;
38class DiscreteBayesNet;
39class DiscreteEliminationTree;
40class DiscreteBayesTree;
41class DiscreteJunctionTree;
42
51GTSAM_EXPORT
52std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr>
53EliminateDiscrete(const DiscreteFactorGraph& factors,
54 const Ordering& frontalKeys);
55
64GTSAM_EXPORT
65std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr>
66EliminateForMPE(const DiscreteFactorGraph& factors,
67 const Ordering& frontalKeys);
68
70{
78
80 static std::pair<boost::shared_ptr<ConditionalType>,
81 boost::shared_ptr<FactorType> >
82 DefaultEliminate(const FactorGraphType& factors, const Ordering& keys) {
83 return EliminateDiscrete(factors, keys);
84 }
85
88 const FactorGraphType& graph,
89 boost::optional<const VariableIndex&> variableIndex) {
90 return Ordering::Colamd(*variableIndex);
91 }
92};
93
99class GTSAM_EXPORT DiscreteFactorGraph
100 : public FactorGraph<DiscreteFactor>,
101 public EliminateableFactorGraph<DiscreteFactorGraph> {
102 public:
107 using shared_ptr = boost::shared_ptr<This>;
108
110
111 using Indices = KeyVector;
112
115
117 template <typename ITERATOR>
118 DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
119 : Base(firstFactor, lastFactor) {}
120
122 template <class CONTAINER>
123 explicit DiscreteFactorGraph(const CONTAINER& factors) : Base(factors) {}
124
127 template <class DERIVED_FACTOR>
129
132
135
136 bool equals(const This& fg, double tol = 1e-9) const;
137
139
141 template <typename... Args>
142 void add(Args&&... args) {
143 emplace_shared<DecisionTreeFactor>(std::forward<Args>(args)...);
144 }
145
147 KeySet keys() const;
148
150 DiscreteKeys discreteKeys() const;
151
153 DecisionTreeFactor product() const;
154
159 double operator()(const DiscreteValues& values) const;
160
162 void print(
163 const std::string& s = "DiscreteFactorGraph",
164 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
165
172 DiscreteBayesNet sumProduct(
173 OptionalOrderingType orderingType = boost::none) const;
174
181 DiscreteBayesNet sumProduct(const Ordering& ordering) const;
182
189 DiscreteLookupDAG maxProduct(
190 OptionalOrderingType orderingType = boost::none) const;
191
198 DiscreteLookupDAG maxProduct(const Ordering& ordering) const;
199
207 OptionalOrderingType orderingType = boost::none) const;
208
215 DiscreteValues optimize(const Ordering& ordering) const;
216
219
227 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
228 const DiscreteFactor::Names& names = {}) const;
229
237 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
238 const DiscreteFactor::Names& names = {}) const;
239
243
244 using Base::error; // Expose error(const HybridValues&) method..
245
247}; // \ DiscreteFactorGraph
248
250template <>
251struct traits<DiscreteFactorGraph> : public Testable<DiscreteFactorGraph> {};
252
253} // namespace gtsam
A thin wrapper around std::set that uses boost's fast_pool_allocator.
Variable elimination algorithms for factor graphs.
Factor Graph Base Class.
Variable ordering for the elimination algorithm.
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
Definition DiscreteFactorGraph.cpp:200
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateForMPE(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Alternate elimination function for that creates non-normalized lookup tables.
Definition DiscreteFactorGraph.cpp:116
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:86
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:129
Point3 optimize(const NonlinearFactorGraph &graph, const Values &values, Key landmarkKey)
Optimize for triangulation.
Definition triangulation.cpp:155
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
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 concepts.h:30
Template to create a binary predicate.
Definition Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:45
A Bayes net made from discrete conditional distributions.
Definition DiscreteBayesNet.h:38
A Bayes tree representing a Discrete density.
Definition DiscreteBayesTree.h:75
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
Elimination tree for discrete factors.
Definition DiscreteEliminationTree.h:33
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:38
DiscreteValues::Names Names
Translation table from values to strings.
Definition DiscreteFactor.h:106
DiscreteFactorGraph FactorGraphType
Type of the factor graph (e.g. DiscreteFactorGraph)
Definition DiscreteFactorGraph.h:72
DiscreteJunctionTree JunctionTreeType
Type of Junction tree.
Definition DiscreteFactorGraph.h:77
DiscreteFactor FactorType
Type of factors in factor graph.
Definition DiscreteFactorGraph.h:71
static std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< FactorType > > DefaultEliminate(const FactorGraphType &factors, const Ordering &keys)
The default dense elimination function.
Definition DiscreteFactorGraph.h:82
DiscreteBayesTree BayesTreeType
Type of Bayes tree.
Definition DiscreteFactorGraph.h:76
DiscreteBayesNet BayesNetType
Type of Bayes net from sequential elimination.
Definition DiscreteFactorGraph.h:74
DiscreteConditional ConditionalType
Type of conditionals from elimination.
Definition DiscreteFactorGraph.h:73
static Ordering DefaultOrderingFunc(const FactorGraphType &graph, boost::optional< const VariableIndex & > variableIndex)
The default ordering generation function.
Definition DiscreteFactorGraph.h:87
DiscreteEliminationTree EliminationTreeType
Type of elimination tree.
Definition DiscreteFactorGraph.h:75
A Discrete Factor Graph is a factor graph where all factors are Discrete, i.e.
Definition DiscreteFactorGraph.h:101
DiscreteFactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition DiscreteFactorGraph.h:123
boost::shared_ptr< This > shared_ptr
shared_ptr to This
Definition DiscreteFactorGraph.h:107
DiscreteFactorGraph()
‍map from keys to values
Definition DiscreteFactorGraph.h:114
DiscreteFactorGraph(const FactorGraph< DERIVED_FACTOR > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition DiscreteFactorGraph.h:128
DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Construct from iterator over factors.
Definition DiscreteFactorGraph.h:118
virtual ~DiscreteFactorGraph()
Destructor.
Definition DiscreteFactorGraph.h:131
EliminateableFactorGraph< This > BaseEliminateable
for elimination
Definition DiscreteFactorGraph.h:106
void add(Args &&... args)
Add a decision-tree factor.
Definition DiscreteFactorGraph.h:142
An EliminatableClusterTree, i.e., a set of variable clusters with factors, arranged in a tree,...
Definition DiscreteJunctionTree.h:52
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:39
A DAG made from lookup tables, as defined above.
Definition DiscreteLookupDAG.h:77
A map from keys to values.
Definition DiscreteValues.h:34
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition FactorGraph.h:97
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition EliminateableFactorGraph.h:36
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms.
Definition EliminateableFactorGraph.h:57
Definition Ordering.h:34
static Ordering Colamd(const FACTOR_GRAPH &graph)
Compute a fill-reducing ordering using COLAMD from a factor graph (see details for note on performanc...
Definition Ordering.h:95