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
20#pragma once
21
26#include <gtsam/base/FastSet.h>
27
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace gtsam {
33
34// Forward declarations
41
50GTSAM_EXPORT
51std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr>
53 const Ordering& frontalKeys);
54
63GTSAM_EXPORT
64std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr>
66 const Ordering& frontalKeys);
67
69{
77
79 static std::pair<std::shared_ptr<ConditionalType>,
80 std::shared_ptr<FactorType> >
81 DefaultEliminate(const FactorGraphType& factors, const Ordering& keys) {
82 return EliminateDiscrete(factors, keys);
83 }
84
87 const FactorGraphType& graph,
88 std::optional<std::reference_wrapper<const VariableIndex>> variableIndex) {
89 return Ordering::Colamd((*variableIndex).get());
90 }
91};
92
98class GTSAM_EXPORT DiscreteFactorGraph
99 : public FactorGraph<DiscreteFactor>,
100 public EliminateableFactorGraph<DiscreteFactorGraph> {
101 public:
106 using shared_ptr = std::shared_ptr<This>;
107
109
110 using Indices = KeyVector;
111
114
116 template <typename ITERATOR>
117 DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
118 : Base(firstFactor, lastFactor) {}
119
121 template <class CONTAINER>
122 explicit DiscreteFactorGraph(const CONTAINER& factors) : Base(factors) {}
123
126 template <class DERIVED_FACTOR>
128
131
134
135 bool equals(const This& fg, double tol = 1e-9) const;
136
138
139 //TODO(Varun): Make compatible with TableFactor
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 DiscreteFactor::shared_ptr product() const;
154
162 DiscreteFactor::shared_ptr scaledProduct() const;
163
168 double operator()(const DiscreteValues& values) const;
169
171 void print(
172 const std::string& s = "DiscreteFactorGraph",
173 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
174
181 DiscreteBayesNet sumProduct(
182 OptionalOrderingType orderingType = {}) const;
183
190 DiscreteBayesNet sumProduct(const Ordering& ordering) const;
191
198 DiscreteLookupDAG maxProduct(
199 OptionalOrderingType orderingType = {}) const;
200
207 DiscreteLookupDAG maxProduct(const Ordering& ordering) const;
208
215 DiscreteValues optimize(
216 OptionalOrderingType orderingType = {}) const;
217
224 DiscreteValues optimize(const Ordering& ordering) const;
225
228
236 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
237 const DiscreteFactor::Names& names = {}) const;
238
246 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
247 const DiscreteFactor::Names& names = {}) const;
248
252
253 using Base::error; // Expose error(const HybridValues&) method..
254
256}; // \ DiscreteFactorGraph
257
259template <>
260struct traits<DiscreteFactorGraph> : public Testable<DiscreteFactorGraph> {};
261
262} // namespace gtsam
A thin wrapper around std::set that uses boost's fast_pool_allocator.
Factor Graph Base Class.
Variable elimination algorithms for factor graphs.
std::pair< DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr > EliminateForMPE(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Alternate elimination function for that creates non-normalized lookup tables.
Definition DiscreteFactorGraph.cpp:129
std::pair< DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
Definition DiscreteFactorGraph.cpp:207
Global functions in a separate testing namespace.
Definition chartTesting.h:28
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition DiscreteValues.cpp:160
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
Point3 optimize(const NonlinearFactorGraph &graph, const Values &values, Key landmarkKey)
Optimize for triangulation.
Definition triangulation.cpp:178
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:155
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
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
A Bayes net made from discrete conditional distributions.
Definition DiscreteBayesNet.h:38
A Bayes tree representing a Discrete distribution.
Definition DiscreteBayesTree.h:74
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:41
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition DiscreteFactor.h:46
DiscreteFactorGraph FactorGraphType
Type of the factor graph (e.g. DiscreteFactorGraph).
Definition DiscreteFactorGraph.h:71
DiscreteJunctionTree JunctionTreeType
Type of Junction tree.
Definition DiscreteFactorGraph.h:76
DiscreteFactor FactorType
Type of factors in factor graph.
Definition DiscreteFactorGraph.h:70
DiscreteBayesTree BayesTreeType
Type of Bayes tree.
Definition DiscreteFactorGraph.h:75
DiscreteBayesNet BayesNetType
Type of Bayes net from sequential elimination.
Definition DiscreteFactorGraph.h:73
DiscreteConditional ConditionalType
Type of conditionals from elimination.
Definition DiscreteFactorGraph.h:72
static Ordering DefaultOrderingFunc(const FactorGraphType &graph, std::optional< std::reference_wrapper< const VariableIndex > > variableIndex)
The default ordering generation function.
Definition DiscreteFactorGraph.h:86
DiscreteEliminationTree EliminationTreeType
Type of elimination tree.
Definition DiscreteFactorGraph.h:74
static std::pair< std::shared_ptr< ConditionalType >, std::shared_ptr< FactorType > > DefaultEliminate(const FactorGraphType &factors, const Ordering &keys)
The default dense elimination function.
Definition DiscreteFactorGraph.h:81
A Discrete Factor Graph is a factor graph where all factors are Discrete, i.e.
Definition DiscreteFactorGraph.h:100
DiscreteFactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects).
Definition DiscreteFactorGraph.h:122
DiscreteValues Values
backwards compatibility
Definition DiscreteFactorGraph.h:108
FactorGraph< DiscreteFactor > Base
base factor graph type
Definition DiscreteFactorGraph.h:103
DiscreteFactorGraph()
Default constructor.
Definition DiscreteFactorGraph.h:113
DiscreteFactorGraph(const FactorGraph< DERIVED_FACTOR > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition DiscreteFactorGraph.h:127
DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Construct from iterator over factors.
Definition DiscreteFactorGraph.h:117
virtual ~DiscreteFactorGraph()
Destructor.
Definition DiscreteFactorGraph.h:130
DiscreteFactorGraph This
this class
Definition DiscreteFactorGraph.h:102
EliminateableFactorGraph< This > BaseEliminateable
for elimination
Definition DiscreteFactorGraph.h:104
void add(Args &&... args)
Add a decision-tree factor.
Definition DiscreteFactorGraph.h:142
std::shared_ptr< This > shared_ptr
shared_ptr to This
Definition DiscreteFactorGraph.h:106
An EliminatableClusterTree, i.e., a set of variable clusters with factors, arranged in a tree,...
Definition DiscreteJunctionTree.h:54
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
FactorGraph()
Definition FactorGraph.h:103
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Definition FactorGraph.h:153
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition EliminateableFactorGraph.h:38
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms.
Definition EliminateableFactorGraph.h:59
Definition Ordering.h:33
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:93