gtsam  4.1.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
gtsam::EliminateDiscrete
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
Definition: DiscreteFactorGraph.cpp:105
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:34
gtsam::equals
Template to create a binary predicate.
Definition: Testable.h:110
gtsam::optimize
Point3 optimize(const NonlinearFactorGraph &graph, const Values &values, Key landmarkKey)
Optimize for triangulation.
Definition: triangulation.cpp:73
gtsam::EliminationTraits< DiscreteFactorGraph >::FactorGraphType
DiscreteFactorGraph FactorGraphType
Type of the factor graph (e.g. DiscreteFactorGraph)
Definition: DiscreteFactorGraph.h:48
gtsam::EliminationTraits< DiscreteFactorGraph >::FactorType
DiscreteFactor FactorType
Type of factors in factor graph.
Definition: DiscreteFactorGraph.h:47
gtsam::Ordering
Definition: Ordering.h:34
gtsam::EliminateableFactorGraph
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms.
Definition: EliminateableFactorGraph.h:57
gtsam::DiscreteFactorGraph::DiscreteFactorGraph
DiscreteFactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition: DiscreteFactorGraph.h:88
gtsam::DecisionTreeFactor
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:38
EliminateableFactorGraph.h
Variable elimination algorithms for factor graphs.
gtsam::EliminationTraits< DiscreteFactorGraph >::BayesTreeType
DiscreteBayesTree BayesTreeType
Type of Bayes tree.
Definition: DiscreteFactorGraph.h:52
gtsam::Assignment
An assignment from labels to value index (size_t).
Definition: Assignment.h:34
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
gtsam::DiscreteBayesNet
A Bayes net made from linear-Discrete densities.
Definition: DiscreteBayesNet.h:31
gtsam::DiscreteFactor
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition: DiscreteFactor.h:34
gtsam::DiscreteFactorGraph::DiscreteFactorGraph
DiscreteFactorGraph(const FactorGraph< DERIVEDFACTOR > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition: DiscreteFactorGraph.h:92
gtsam::DiscreteFactorGraph::Base
FactorGraph< DiscreteFactor > Base
Typedef to base factor graph type.
Definition: DiscreteFactorGraph.h:70
gtsam::DiscreteFactorGraph::shared_ptr
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteFactorGraph.h:72
FactorGraph.h
Factor Graph Base Class.
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::EliminationTraits< DiscreteFactorGraph >::JunctionTreeType
DiscreteJunctionTree JunctionTreeType
Type of Junction tree.
Definition: DiscreteFactorGraph.h:53
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
gtsam::DiscreteConditional
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:36
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
gtsam::DiscreteFactorGraph::Indices
KeyVector Indices
A map from keys to values.
Definition: DiscreteFactorGraph.h:75
gtsam::EliminationTraits< DiscreteFactorGraph >::EliminationTreeType
DiscreteEliminationTree EliminationTreeType
Type of elimination tree.
Definition: DiscreteFactorGraph.h:51
gtsam::DiscreteFactorGraph::add
void add(const DiscreteKeys &keys, SOURCE table)
add shared discreteFactor immediately from arguments
Definition: DiscreteFactorGraph.h:118
gtsam::DiscreteJunctionTree
Definition: DiscreteJunctionTree.h:49
gtsam::FactorGraph
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: FactorGraph.h:94
gtsam::DiscreteFactorGraph::This
DiscreteFactorGraph This
Typedef to this class.
Definition: DiscreteFactorGraph.h:69
gtsam::FastSet< Key >
gtsam::DiscreteFactorGraph
A Discrete Factor Graph is a factor graph where all factors are Discrete, i.e.
Definition: DiscreteFactorGraph.h:66
DiscreteBayesNet.h
gtsam::KeyFormatter
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:35
DecisionTreeFactor.h
gtsam::DiscreteEliminationTree
Definition: DiscreteEliminationTree.h:29
gtsam::DiscreteBayesTree
A Bayes tree representing a Discrete density.
Definition: DiscreteBayesTree.h:67
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
gtsam::DiscreteFactorGraph::DiscreteFactorGraph
DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Construct from iterator over factors.
Definition: DiscreteFactorGraph.h:84
gtsam::DiscreteFactorGraph::DiscreteFactorGraph
DiscreteFactorGraph()
Default constructor.
Definition: DiscreteFactorGraph.h:80
Ordering.h
Variable ordering for the elimination algorithm.
FastSet.h
A thin wrapper around std::set that uses boost's fast_pool_allocator.
gtsam::EliminationTraits
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: EliminateableFactorGraph.h:36
gtsam::EliminationTraits< DiscreteFactorGraph >::DefaultEliminate
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
gtsam::DiscreteFactorGraph::BaseEliminateable
EliminateableFactorGraph< This > BaseEliminateable
Typedef to base elimination class.
Definition: DiscreteFactorGraph.h:71
gtsam::EliminationTraits< DiscreteFactorGraph >::BayesNetType
DiscreteBayesNet BayesNetType
Type of Bayes net from sequential elimination.
Definition: DiscreteFactorGraph.h:50
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:37
gtsam::EliminationTraits< DiscreteFactorGraph >::ConditionalType
DiscreteConditional ConditionalType
Type of conditionals from elimination.
Definition: DiscreteFactorGraph.h:49