23#include <boost/tuple/tuple.hpp>
28 template<
class FACTORGRAPH>
29 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
39 return eliminateSequential(orderingType, function, computedVariableIndex);
44 if (orderingType == Ordering::METIS) {
46 return eliminateSequential(computedOrdering, function, variableIndex);
49 return eliminateSequential(computedOrdering, function, variableIndex);
55 template<
class FACTORGRAPH>
56 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
64 return eliminateSequential(ordering, function, computedVariableIndex);
66 gttic(eliminateSequential);
69 boost::shared_ptr<BayesNetType> bayesNet;
70 boost::shared_ptr<FactorGraphType> factorGraph;
71 boost::tie(bayesNet,factorGraph) = etree.eliminate(function);
73 if(!factorGraph->empty())
81 template <
class FACTORGRAPH>
94 return eliminateMultifrontal(orderingType, function,
95 computedVariableIndex);
100 if (orderingType == Ordering::METIS) {
102 return eliminateMultifrontal(computedOrdering, function, variableIndex);
105 return eliminateMultifrontal(computedOrdering, function, variableIndex);
111 template<
class FACTORGRAPH>
112 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
120 return eliminateMultifrontal(ordering, function, computedVariableIndex);
122 gttic(eliminateMultifrontal);
124 EliminationTreeType etree(asDerived(), *variableIndex, ordering);
125 JunctionTreeType junctionTree(etree);
126 boost::shared_ptr<BayesTreeType> bayesTree;
127 boost::shared_ptr<FactorGraphType> factorGraph;
128 boost::tie(bayesTree,factorGraph) = junctionTree.eliminate(function);
130 if(!factorGraph->empty())
138 template<
class FACTORGRAPH>
139 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, boost::shared_ptr<FACTORGRAPH> >
144 gttic(eliminatePartialSequential);
147 return etree.eliminate(function);
151 return eliminatePartialSequential(ordering, function, computedVariableIndex);
156 template<
class FACTORGRAPH>
157 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, boost::shared_ptr<FACTORGRAPH> >
162 gttic(eliminatePartialSequential);
167 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
168 return eliminatePartialSequential(ordering, function, variableIndex);
172 return eliminatePartialSequential(variables, function, computedVariableIndex);
177 template<
class FACTORGRAPH>
178 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, boost::shared_ptr<FACTORGRAPH> >
183 gttic(eliminatePartialMultifrontal);
187 return junctionTree.eliminate(function);
191 return eliminatePartialMultifrontal(ordering, function, computedVariableIndex);
196 template<
class FACTORGRAPH>
197 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, boost::shared_ptr<FACTORGRAPH> >
202 gttic(eliminatePartialMultifrontal);
207 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
208 return eliminatePartialMultifrontal(ordering, function, variableIndex);
212 return eliminatePartialMultifrontal(variables, function, computedVariableIndex);
217 template<
class FACTORGRAPH>
218 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
220 boost::variant<const Ordering&, const KeyVector&> variables,
226 return marginalMultifrontalBayesNet(variables, function, index);
230 bool unmarginalizedAreOrdered = (boost::get<const Ordering&>(&variables) != 0);
232 unmarginalizedAreOrdered ?
233 boost::get<const Ordering&>(&variables) : boost::get<const
KeyVector&>(&variables);
239 const size_t nVars = variablesOrOrdering->size();
240 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
241 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
244 return marginalMultifrontalBayesNet(marginalVarsOrdering, marginalizationOrdering, function, *variableIndex);
249 template<
class FACTORGRAPH>
250 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
252 boost::variant<const Ordering&, const KeyVector&> variables,
253 const Ordering& marginalizedVariableOrdering,
259 return marginalMultifrontalBayesNet(variables, marginalizedVariableOrdering, function, index);
261 gttic(marginalMultifrontalBayesNet);
264 boost::shared_ptr<BayesTreeType> bayesTree;
265 boost::shared_ptr<FactorGraphType> factorGraph;
266 boost::tie(bayesTree,factorGraph) =
267 eliminatePartialMultifrontal(marginalizedVariableOrdering, function, *variableIndex);
269 if(
const Ordering* varsAsOrdering = boost::get<const Ordering&>(&variables))
273 return factorGraph->eliminateSequential(*varsAsOrdering, function);
278 return factorGraph->eliminateSequential(Ordering::COLAMD, function);
284 template<
class FACTORGRAPH>
285 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
287 boost::variant<const Ordering&, const KeyVector&> variables,
293 return marginalMultifrontalBayesTree(variables, function, computedVariableIndex);
297 bool unmarginalizedAreOrdered = (boost::get<const Ordering&>(&variables) != 0);
299 unmarginalizedAreOrdered ?
300 boost::get<const Ordering&>(&variables) : boost::get<const KeyVector&>(&variables);
306 const size_t nVars = variablesOrOrdering->size();
307 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
308 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
311 return marginalMultifrontalBayesTree(marginalVarsOrdering, marginalizationOrdering, function, *variableIndex);
316 template<
class FACTORGRAPH>
317 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
319 boost::variant<const Ordering&, const KeyVector&> variables,
320 const Ordering& marginalizedVariableOrdering,
326 return marginalMultifrontalBayesTree(variables, marginalizedVariableOrdering, function, computedVariableIndex);
328 gttic(marginalMultifrontalBayesTree);
331 boost::shared_ptr<BayesTreeType> bayesTree;
332 boost::shared_ptr<FactorGraphType> factorGraph;
333 boost::tie(bayesTree,factorGraph) =
334 eliminatePartialMultifrontal(marginalizedVariableOrdering, function, *variableIndex);
336 if(
const Ordering* varsAsOrdering = boost::get<const Ordering&>(&variables))
340 return factorGraph->eliminateMultifrontal(*varsAsOrdering, function);
345 return factorGraph->eliminateMultifrontal(Ordering::COLAMD, function);
351 template<
class FACTORGRAPH>
352 boost::shared_ptr<FACTORGRAPH>
363 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - variables.size());
366 return eliminatePartialMultifrontal(marginalizationOrdering, function, *variableIndex).second;
372 return marginal(variables, function, computedVariableIndex);
Exceptions that may be thrown by inference algorithms.
Variable elimination algorithms for factor graphs.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
std::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
Definition: EliminateableFactorGraph.h:89
EliminationTraitsType::JunctionTreeType JunctionTreeType
Junction tree type that can do multifrontal elimination of this graph.
Definition: EliminateableFactorGraph.h:82
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:286
boost::optional< const VariableIndex & > OptionalVariableIndex
Typedef for an optional variable index as an argument to elimination functions.
Definition: EliminateableFactorGraph.h:92
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:353
EliminationTraitsType::EliminationTreeType EliminationTreeType
Elimination tree type that can do sequential elimination of this graph.
Definition: EliminateableFactorGraph.h:76
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
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:84
boost::optional< Ordering::OrderingType > OptionalOrderingType
Typedef for an optional ordering type.
Definition: EliminateableFactorGraph.h:95
EliminationTraitsType::BayesTreeType BayesTreeType
Bayes tree type produced by multifrontal elimination.
Definition: EliminateableFactorGraph.h:79
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:140
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:219
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:179
An inference algorithm was called with inconsistent arguments.
Definition: inferenceExceptions.h:29
Definition: Ordering.h:34
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:82
static Ordering ColamdConstrainedLast(const FACTOR_GRAPH &graph, const KeyVector &constrainLast, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition: Ordering.h:101
static Ordering ColamdConstrainedFirst(const FACTOR_GRAPH &graph, const KeyVector &constrainFirst, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition: Ordering.h:128
static GTSAM_EXPORT Ordering Metis(const MetisIndex &met)
Compute an ordering determined by METIS from a VariableIndex.
Definition: Ordering.cpp:216
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:43