gtsam  4.0.0
gtsam
EliminateableFactorGraph.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 
21 #include <boost/shared_ptr.hpp>
22 #include <boost/function.hpp>
23 #include <boost/variant.hpp>
24 #include <boost/optional.hpp>
25 
28 
29 namespace gtsam {
30 
34  template<class GRAPH>
35  struct EliminationTraits
36  {
37  // Template for deriving:
38  // typedef MyFactor FactorType; ///< Type of factors in factor graph (e.g. GaussianFactor)
39  // typedef MyFactorGraphType FactorGraphType; ///< Type of the factor graph (e.g. GaussianFactorGraph)
40  // typedef MyConditional ConditionalType; ///< Type of conditionals from elimination (e.g. GaussianConditional)
41  // typedef MyBayesNet BayesNetType; ///< Type of Bayes net from sequential elimination (e.g. GaussianBayesNet)
42  // typedef MyEliminationTree EliminationTreeType; ///< Type of elimination tree (e.g. GaussianEliminationTree)
43  // typedef MyBayesTree BayesTreeType; ///< Type of Bayes tree (e.g. GaussianBayesTree)
44  // typedef MyJunctionTree JunctionTreeType; ///< Type of Junction tree (e.g. GaussianJunctionTree)
45  // static pair<shared_ptr<ConditionalType>, shared_ptr<FactorType>
46  // DefaultEliminate(
47  // const MyFactorGraph& factors, const Ordering& keys); ///< The default dense elimination function
48  };
49 
50 
55  template<class FACTORGRAPH>
57  {
58  private:
60  typedef FACTORGRAPH FactorGraphType;
61  // Base factor type stored in this graph (private because derived classes will get this from
62  // their FactorGraph base class)
63  typedef typename EliminationTraits<FactorGraphType>::FactorType _FactorType;
64 
65  public:
68 
70  typedef typename EliminationTraitsType::ConditionalType ConditionalType;
71 
73  typedef typename EliminationTraitsType::BayesNetType BayesNetType;
74 
76  typedef typename EliminationTraitsType::EliminationTreeType EliminationTreeType;
77 
79  typedef typename EliminationTraitsType::BayesTreeType BayesTreeType;
80 
82  typedef typename EliminationTraitsType::JunctionTreeType JunctionTreeType;
83 
86  typedef std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<_FactorType> > EliminationResult;
87 
89  typedef boost::function<EliminationResult(const FactorGraphType&, const Ordering&)> Eliminate;
90 
92  typedef boost::optional<const Ordering&> OptionalOrdering;
93 
95  typedef boost::optional<const VariableIndex&> OptionalVariableIndex;
96 
98  typedef boost::optional<Ordering::OrderingType> OptionalOrderingType;
99 
124  boost::shared_ptr<BayesNetType> eliminateSequential(
125  OptionalOrdering ordering = boost::none,
126  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
127  OptionalVariableIndex variableIndex = boost::none,
128  OptionalOrderingType orderingType = boost::none) const;
129 
151  boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
152  OptionalOrdering ordering = boost::none,
153  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
154  OptionalVariableIndex variableIndex = boost::none,
155  OptionalOrderingType orderingType = boost::none) const;
156 
161  std::pair<boost::shared_ptr<BayesNetType>, boost::shared_ptr<FactorGraphType> >
163  const Ordering& ordering,
164  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
165  OptionalVariableIndex variableIndex = boost::none) const;
166 
171  std::pair<boost::shared_ptr<BayesNetType>, boost::shared_ptr<FactorGraphType> >
173  const KeyVector& variables,
174  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
175  OptionalVariableIndex variableIndex = boost::none) const;
176 
181  std::pair<boost::shared_ptr<BayesTreeType>, boost::shared_ptr<FactorGraphType> >
183  const Ordering& ordering,
184  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
185  OptionalVariableIndex variableIndex = boost::none) const;
186 
191  std::pair<boost::shared_ptr<BayesTreeType>, boost::shared_ptr<FactorGraphType> >
193  const KeyVector& variables,
194  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
195  OptionalVariableIndex variableIndex = boost::none) const;
196 
208  boost::shared_ptr<BayesNetType> marginalMultifrontalBayesNet(
209  boost::variant<const Ordering&, const KeyVector&> variables,
210  OptionalOrdering marginalizedVariableOrdering = boost::none,
211  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
212  OptionalVariableIndex variableIndex = boost::none) const;
213 
225  boost::shared_ptr<BayesTreeType> marginalMultifrontalBayesTree(
226  boost::variant<const Ordering&, const KeyVector&> variables,
227  OptionalOrdering marginalizedVariableOrdering = boost::none,
228  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
229  OptionalVariableIndex variableIndex = boost::none) const;
230 
232  boost::shared_ptr<FactorGraphType> marginal(
233  const KeyVector& variables,
234  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
235  OptionalVariableIndex variableIndex = boost::none) const;
236 
237  private:
238 
239  // Access the derived factor graph class
240  const FactorGraphType& asDerived() const { return static_cast<const FactorGraphType&>(*this); }
241 
242  // Access the derived factor graph class
243  FactorGraphType& asDerived() { return static_cast<FactorGraphType&>(*this); }
244  };
245 
246 }
std::pair< boost::shared_ptr< BayesTreeType >, boost::shared_ptr< FactorGraphType > > eliminatePartialMultifrontal(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Do multifrontal elimination of some variables, in ordering provided, to produce a Bayes tree and a re...
Definition: EliminateableFactorGraph-inst.h:152
boost::shared_ptr< BayesNetType > eliminateSequential(OptionalOrdering ordering=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none, OptionalOrderingType orderingType=boost::none) const
Do sequential elimination of all variables to produce a Bayes net.
Definition: EliminateableFactorGraph-inst.h:30
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms.
Definition: EliminateableFactorGraph.h:56
std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< _FactorType > > EliminationResult
The pair of conditional and remaining factor produced by a single dense elimination step on a subgrap...
Definition: EliminateableFactorGraph.h:86
Variable ordering for the elimination algorithm.
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:65
EliminationTraitsType::ConditionalType ConditionalType
Conditional type stored in the Bayes net produced by elimination.
Definition: EliminateableFactorGraph.h:70
boost::optional< const Ordering & > OptionalOrdering
Typedef for an optional ordering as an argument to elimination functions.
Definition: EliminateableFactorGraph.h:92
boost::optional< Ordering::OrderingType > OptionalOrderingType
Typedef for an optional ordering type.
Definition: EliminateableFactorGraph.h:98
EliminationTraitsType::JunctionTreeType JunctionTreeType
Junction tree type that can do multifrontal elimination of this graph.
Definition: EliminateableFactorGraph.h:82
boost::optional< const VariableIndex & > OptionalVariableIndex
Typedef for an optional variable index as an argument to elimination functions.
Definition: EliminateableFactorGraph.h:95
EliminationTraitsType::EliminationTreeType EliminationTreeType
Elimination tree type that can do sequential elimination of this graph.
Definition: EliminateableFactorGraph.h:76
std::pair< boost::shared_ptr< BayesNetType >, boost::shared_ptr< FactorGraphType > > eliminatePartialSequential(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Do sequential elimination of some variables, in ordering provided, to produce a Bayes net and a remai...
Definition: EliminateableFactorGraph-inst.h:113
boost::shared_ptr< BayesTreeType > marginalMultifrontalBayesTree(boost::variant< const Ordering &, const KeyVector & > variables, OptionalOrdering marginalizedVariableOrdering=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Compute the marginal of the requested variables and return the result as a Bayes tree.
Definition: EliminateableFactorGraph-inst.h:251
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
EliminationTraitsType::BayesTreeType BayesTreeType
Bayes tree type produced by multifrontal elimination.
Definition: EliminateableFactorGraph.h:79
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
Definition: EliminateableFactorGraph.h:89
Definition: Ordering.h:34
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: BayesTreeCliqueBase.h:32
boost::shared_ptr< BayesNetType > marginalMultifrontalBayesNet(boost::variant< const Ordering &, const KeyVector & > variables, OptionalOrdering marginalizedVariableOrdering=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Compute the marginal of the requested variables and return the result as a Bayes net.
Definition: EliminateableFactorGraph-inst.h:192
boost::shared_ptr< FactorGraphType > marginal(const KeyVector &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Compute the marginal factor graph of the requested variables.
Definition: EliminateableFactorGraph-inst.h:310
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrdering ordering=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none, OptionalOrderingType orderingType=boost::none) const
Do multifrontal elimination of all variables to produce a Bayes tree.
Definition: EliminateableFactorGraph-inst.h:71
EliminationTraits< FactorGraphType > EliminationTraitsType
Typedef to the specific EliminationTraits for this graph.
Definition: EliminateableFactorGraph.h:67
EliminationTraitsType::BayesNetType BayesNetType
Bayes net type produced by sequential elimination.
Definition: EliminateableFactorGraph.h:73