gtsam  4.0.0
gtsam
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 #include <boost/make_shared.hpp>
28 
29 namespace gtsam {
30 
31 // Forward declarations
32 class DiscreteFactorGraph;
33 class DiscreteFactor;
34 class DiscreteConditional;
35 class DiscreteBayesNet;
36 class DiscreteEliminationTree;
37 class DiscreteBayesTree;
38 class DiscreteJunctionTree;
39 
41 GTSAM_EXPORT std::pair<boost::shared_ptr<DiscreteConditional>, DecisionTreeFactor::shared_ptr>
42 EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& keys);
43 
44 /* ************************************************************************* */
46 {
54  static std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> >
56  DefaultEliminate(const FactorGraphType& factors, const Ordering& keys) {
57  return EliminateDiscrete(factors, keys); }
58 };
59 
60 /* ************************************************************************* */
65 class GTSAM_EXPORT DiscreteFactorGraph: public FactorGraph<DiscreteFactor>,
66 public EliminateableFactorGraph<DiscreteFactorGraph> {
67 public:
68 
72  typedef boost::shared_ptr<This> shared_ptr;
73 
75  typedef KeyVector Indices;
76  typedef Assignment<Key> Values;
77  typedef boost::shared_ptr<Values> sharedValues;
78 
81 
83  template<typename ITERATOR>
84  DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) : Base(firstFactor, lastFactor) {}
85 
87  template<class CONTAINER>
88  explicit DiscreteFactorGraph(const CONTAINER& factors) : Base(factors) {}
89 
91  template<class DERIVEDFACTOR>
93 
96 
97  bool equals(const This& fg, double tol = 1e-9) const;
98 
100 
101  template<class SOURCE>
102  void add(const DiscreteKey& j, SOURCE table) {
103  DiscreteKeys keys;
104  keys.push_back(j);
105  push_back(boost::make_shared<DecisionTreeFactor>(keys, table));
106  }
107 
108  template<class SOURCE>
109  void add(const DiscreteKey& j1, const DiscreteKey& j2, SOURCE table) {
110  DiscreteKeys keys;
111  keys.push_back(j1);
112  keys.push_back(j2);
113  push_back(boost::make_shared<DecisionTreeFactor>(keys, table));
114  }
115 
117  template<class SOURCE>
118  void add(const DiscreteKeys& keys, SOURCE table) {
119  push_back(boost::make_shared<DecisionTreeFactor>(keys, table));
120  }
121 
123  KeySet keys() const;
124 
126  DecisionTreeFactor product() const;
127 
129  double operator()(const DiscreteFactor::Values & values) const;
130 
132  void print(const std::string& s = "DiscreteFactorGraph",
133  const KeyFormatter& formatter =DefaultKeyFormatter) const;
134 
139  DiscreteFactor::sharedValues optimize() const;
140 
141 
142 // /** Permute the variables in the factors */
143 // GTSAM_EXPORT void permuteWithInverse(const Permutation& inversePermutation);
144 //
145 // /** Apply a reduction, which is a remapping of variable indices. */
146 // GTSAM_EXPORT void reduceWithInverse(const internal::Reduction& inverseReduction);
147 
148 }; // \ DiscreteFactorGraph
149 
151 template<> struct traits<DiscreteFactorGraph> : public Testable<DiscreteFactorGraph> {};
152 
153 } // \ namespace gtsam
EliminateableFactorGraph< This > BaseEliminateable
Typedef to base elimination class.
Definition: DiscreteFactorGraph.h:71
DiscreteEliminationTree EliminationTreeType
Type of elimination tree.
Definition: DiscreteFactorGraph.h:51
DiscreteBayesNet BayesNetType
Type of Bayes net from sequential elimination.
Definition: DiscreteFactorGraph.h:50
A Discrete Factor Graph is a factor graph where all factors are Discrete, i.e.
Definition: DiscreteFactorGraph.h:65
DiscreteJunctionTree JunctionTreeType
Type of Junction tree.
Definition: DiscreteFactorGraph.h:53
Point3 optimize(const NonlinearFactorGraph &graph, const Values &values, Key landmarkKey)
Optimize for triangulation.
Definition: triangulation.cpp:73
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
Definition: DiscreteFactorGraph.cpp:105
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:33
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms.
Definition: EliminateableFactorGraph.h:56
Template to create a binary predicate.
Definition: Testable.h:110
Definition: DiscreteJunctionTree.h:48
Variable ordering for the elimination algorithm.
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
A thin wrapper around std::set that uses boost's fast_pool_allocator.
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
FactorGraph< DiscreteFactor > Base
Typedef to base factor graph type.
Definition: DiscreteFactorGraph.h:70
DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Construct from iterator over factors.
Definition: DiscreteFactorGraph.h:84
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
KeyVector Indices
A map from keys to values.
Definition: DiscreteFactorGraph.h:75
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:38
void add(const DiscreteKeys &keys, SOURCE table)
add shared discreteFactor immediately from arguments
Definition: DiscreteFactorGraph.h:118
DiscreteFactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition: DiscreteFactorGraph.h:88
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
Definition: DiscreteEliminationTree.h:27
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
A Bayes tree representing a Discrete density.
Definition: DiscreteBayesTree.h:49
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: BayesTree.h:32
A Bayes net made from linear-Discrete densities.
Definition: DiscreteBayesNet.h:29
DiscreteFactor FactorType
Type of factors in factor graph.
Definition: DiscreteFactorGraph.h:47
DiscreteFactorGraph(const FactorGraph< DERIVEDFACTOR > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition: DiscreteFactorGraph.h:92
DiscreteBayesTree BayesTreeType
Type of Bayes tree.
Definition: DiscreteFactorGraph.h:52
DiscreteFactorGraph()
Default constructor.
Definition: DiscreteFactorGraph.h:80
Variable elimination algorithms for factor graphs.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
DiscreteFactorGraph FactorGraphType
Type of the factor graph (e.g. DiscreteFactorGraph)
Definition: DiscreteFactorGraph.h:48
DiscreteConditional ConditionalType
Type of conditionals from elimination.
Definition: DiscreteFactorGraph.h:49
DiscreteFactorGraph This
Typedef to this class.
Definition: DiscreteFactorGraph.h:69
Factor Graph Base Class.
Definition: Ordering.h:34
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: BayesTreeCliqueBase.h:32
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteFactorGraph.h:72
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:56
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:37