gtsam  4.1.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>
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 VariableIndex&> OptionalVariableIndex;
93 
95  typedef boost::optional<Ordering::OrderingType> OptionalOrderingType;
96 
117  boost::shared_ptr<BayesNetType> eliminateSequential(
118  OptionalOrderingType orderingType = boost::none,
119  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
120  OptionalVariableIndex variableIndex = boost::none) const;
121 
136  boost::shared_ptr<BayesNetType> eliminateSequential(
137  const Ordering& ordering,
138  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
139  OptionalVariableIndex variableIndex = boost::none) const;
140 
157  boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
158  OptionalOrderingType orderingType = boost::none,
159  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
160  OptionalVariableIndex variableIndex = boost::none) const;
161 
171  boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
172  const Ordering& ordering,
173  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
174  OptionalVariableIndex variableIndex = boost::none) const;
175 
180  std::pair<boost::shared_ptr<BayesNetType>, boost::shared_ptr<FactorGraphType> >
182  const Ordering& ordering,
183  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
184  OptionalVariableIndex variableIndex = boost::none) const;
185 
190  std::pair<boost::shared_ptr<BayesNetType>, boost::shared_ptr<FactorGraphType> >
192  const KeyVector& variables,
193  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
194  OptionalVariableIndex variableIndex = boost::none) const;
195 
200  std::pair<boost::shared_ptr<BayesTreeType>, boost::shared_ptr<FactorGraphType> >
202  const Ordering& ordering,
203  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
204  OptionalVariableIndex variableIndex = boost::none) const;
205 
210  std::pair<boost::shared_ptr<BayesTreeType>, boost::shared_ptr<FactorGraphType> >
212  const KeyVector& variables,
213  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
214  OptionalVariableIndex variableIndex = boost::none) const;
215 
225  boost::shared_ptr<BayesNetType> marginalMultifrontalBayesNet(
226  boost::variant<const Ordering&, const KeyVector&> variables,
227  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
228  OptionalVariableIndex variableIndex = boost::none) const;
229 
240  boost::shared_ptr<BayesNetType> marginalMultifrontalBayesNet(
241  boost::variant<const Ordering&, const KeyVector&> variables,
242  const Ordering& marginalizedVariableOrdering,
243  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
244  OptionalVariableIndex variableIndex = boost::none) const;
245 
255  boost::shared_ptr<BayesTreeType> marginalMultifrontalBayesTree(
256  boost::variant<const Ordering&, const KeyVector&> variables,
257  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
258  OptionalVariableIndex variableIndex = boost::none) const;
259 
270  boost::shared_ptr<BayesTreeType> marginalMultifrontalBayesTree(
271  boost::variant<const Ordering&, const KeyVector&> variables,
272  const Ordering& marginalizedVariableOrdering,
273  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
274  OptionalVariableIndex variableIndex = boost::none) const;
275 
277  boost::shared_ptr<FactorGraphType> marginal(
278  const KeyVector& variables,
279  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
280  OptionalVariableIndex variableIndex = boost::none) const;
281 
282  private:
283 
284  // Access the derived factor graph class
285  const FactorGraphType& asDerived() const { return static_cast<const FactorGraphType&>(*this); }
286 
287  // Access the derived factor graph class
288  FactorGraphType& asDerived() { return static_cast<FactorGraphType&>(*this); }
289 
290  public:
292  boost::shared_ptr<BayesNetType> eliminateSequential(
293  const Ordering& ordering,
294  const Eliminate& function,
295  OptionalVariableIndex variableIndex,
296  OptionalOrderingType orderingType) const {
297  return eliminateSequential(ordering, function, variableIndex);
298  }
299 
301  boost::shared_ptr<BayesNetType> eliminateSequential(
302  const Eliminate& function,
303  OptionalVariableIndex variableIndex = boost::none,
304  OptionalOrderingType orderingType = boost::none) const {
305  return eliminateSequential(orderingType, function, variableIndex);
306  }
307 
309  boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
310  const Ordering& ordering,
311  const Eliminate& function,
312  OptionalVariableIndex variableIndex,
313  OptionalOrderingType orderingType) const {
314  return eliminateMultifrontal(ordering, function, variableIndex);
315  }
316 
318  boost::shared_ptr<BayesTreeType> eliminateMultifrontal(
319  const Eliminate& function,
320  OptionalVariableIndex variableIndex = boost::none,
321  OptionalOrderingType orderingType = boost::none) const {
322  return eliminateMultifrontal(orderingType, function, variableIndex);
323  }
324 
326  boost::shared_ptr<BayesNetType> marginalMultifrontalBayesNet(
327  boost::variant<const Ordering&, const KeyVector&> variables,
328  boost::none_t,
329  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
330  OptionalVariableIndex variableIndex = boost::none) const {
331  return marginalMultifrontalBayesNet(variables, function, variableIndex);
332  }
333 
335  boost::shared_ptr<BayesTreeType> marginalMultifrontalBayesTree(
336  boost::variant<const Ordering&, const KeyVector&> variables,
337  boost::none_t,
338  const Eliminate& function = EliminationTraitsType::DefaultEliminate,
339  OptionalVariableIndex variableIndex = boost::none) const {
340  return marginalMultifrontalBayesTree(variables, function, variableIndex);
341  }
342  };
343 
344 }
gtsam::GaussianFactorGraph
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:68
gtsam::Ordering
Definition: Ordering.h:34
gtsam::EliminateableFactorGraph
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms.
Definition: EliminateableFactorGraph.h:57
VariableIndex.h
gtsam::EliminateableFactorGraph::OptionalVariableIndex
boost::optional< const VariableIndex & > OptionalVariableIndex
Typedef for an optional variable index as an argument to elimination functions.
Definition: EliminateableFactorGraph.h:92
gtsam::EliminateableFactorGraph::eliminateMultifrontal
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Do multifrontal elimination of all variables to produce a Bayes tree.
Definition: EliminateableFactorGraph-inst.h:83
gtsam::EliminateableFactorGraph::marginalMultifrontalBayesTree
boost::shared_ptr< BayesTreeType > marginalMultifrontalBayesTree(boost::variant< const Ordering &, const KeyVector & > variables, 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:284
gtsam::EliminateableFactorGraph::EliminationTraitsType
EliminationTraits< FactorGraphType > EliminationTraitsType
Typedef to the specific EliminationTraits for this graph.
Definition: EliminateableFactorGraph.h:67
gtsam::EliminateableFactorGraph::marginalMultifrontalBayesNet
boost::shared_ptr< BayesNetType > marginalMultifrontalBayesNet(boost::variant< const Ordering &, const KeyVector & > variables, 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:217
gtsam::EliminateableFactorGraph::marginalMultifrontalBayesNet
boost::shared_ptr< BayesNetType > marginalMultifrontalBayesNet(boost::variant< const Ordering &, const KeyVector & > variables, boost::none_t, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Definition: EliminateableFactorGraph.h:326
gtsam::EliminateableFactorGraph::Eliminate
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
gtsam::EliminateableFactorGraph::EliminationResult
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
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::EliminateableFactorGraph::JunctionTreeType
EliminationTraitsType::JunctionTreeType JunctionTreeType
Junction tree type that can do multifrontal elimination of this graph.
Definition: EliminateableFactorGraph.h:82
gtsam::EliminateableFactorGraph::OptionalOrderingType
boost::optional< Ordering::OrderingType > OptionalOrderingType
Typedef for an optional ordering type.
Definition: EliminateableFactorGraph.h:95
gtsam::EliminateableFactorGraph::marginalMultifrontalBayesTree
boost::shared_ptr< BayesTreeType > marginalMultifrontalBayesTree(boost::variant< const Ordering &, const KeyVector & > variables, boost::none_t, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Definition: EliminateableFactorGraph.h:335
gtsam::EliminateableFactorGraph::BayesNetType
EliminationTraitsType::BayesNetType BayesNetType
Bayes net type produced by sequential elimination.
Definition: EliminateableFactorGraph.h:73
gtsam::EliminateableFactorGraph::eliminateSequential
boost::shared_ptr< BayesNetType > eliminateSequential(const Eliminate &function, OptionalVariableIndex variableIndex=boost::none, OptionalOrderingType orderingType=boost::none) const
Definition: EliminateableFactorGraph.h:301
gtsam::EliminateableFactorGraph::eliminateMultifrontal
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(const Eliminate &function, OptionalVariableIndex variableIndex=boost::none, OptionalOrderingType orderingType=boost::none) const
Definition: EliminateableFactorGraph.h:318
gtsam::EliminateableFactorGraph::eliminateMultifrontal
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(const Ordering &ordering, const Eliminate &function, OptionalVariableIndex variableIndex, OptionalOrderingType orderingType) const
Definition: EliminateableFactorGraph.h:309
gtsam::EliminateableFactorGraph::marginal
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:351
gtsam::EliminateableFactorGraph::eliminatePartialSequential
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:138
gtsam::EliminateableFactorGraph::eliminateSequential
boost::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Do sequential elimination of all variables to produce a Bayes net.
Definition: EliminateableFactorGraph-inst.h:30
gtsam::EliminateableFactorGraph::ConditionalType
EliminationTraitsType::ConditionalType ConditionalType
Conditional type stored in the Bayes net produced by elimination.
Definition: EliminateableFactorGraph.h:70
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::EliminateableFactorGraph::EliminationTreeType
EliminationTraitsType::EliminationTreeType EliminationTreeType
Elimination tree type that can do sequential elimination of this graph.
Definition: EliminateableFactorGraph.h:76
gtsam::EliminationTraits
Traits class for eliminateable factor graphs, specifies the types that result from elimination,...
Definition: EliminateableFactorGraph.h:36
gtsam::EliminateableFactorGraph::eliminateSequential
boost::shared_ptr< BayesNetType > eliminateSequential(const Ordering &ordering, const Eliminate &function, OptionalVariableIndex variableIndex, OptionalOrderingType orderingType) const
Definition: EliminateableFactorGraph.h:292
gtsam::EliminateableFactorGraph::BayesTreeType
EliminationTraitsType::BayesTreeType BayesTreeType
Bayes tree type produced by multifrontal elimination.
Definition: EliminateableFactorGraph.h:79
gtsam::EliminateableFactorGraph::eliminatePartialMultifrontal
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:177