23 #include <boost/tuple/tuple.hpp> 28 template<
class FACTORGRAPH>
29 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
34 if(ordering && variableIndex) {
35 gttic(eliminateSequential);
38 boost::shared_ptr<BayesNetType> bayesNet;
39 boost::shared_ptr<FactorGraphType> factorGraph;
40 boost::tie(bayesNet,factorGraph) = etree.eliminate(
function);
42 if(!factorGraph->empty())
47 else if(!variableIndex) {
52 return eliminateSequential(ordering,
function, computedVariableIndex, orderingType);
58 if (orderingType == Ordering::METIS) {
60 return eliminateSequential(computedOrdering,
function, variableIndex, orderingType);
63 return eliminateSequential(computedOrdering,
function, variableIndex, orderingType);
69 template<
class FACTORGRAPH>
70 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
75 if(ordering && variableIndex) {
76 gttic(eliminateMultifrontal);
80 boost::shared_ptr<BayesTreeType> bayesTree;
81 boost::shared_ptr<FactorGraphType> factorGraph;
82 boost::tie(bayesTree,factorGraph) = junctionTree.eliminate(
function);
84 if(!factorGraph->empty())
89 else if(!variableIndex) {
94 return eliminateMultifrontal(ordering,
function, computedVariableIndex, orderingType);
100 if (orderingType == Ordering::METIS) {
102 return eliminateMultifrontal(computedOrdering,
function, variableIndex, orderingType);
105 return eliminateMultifrontal(computedOrdering,
function, variableIndex, orderingType);
111 template<
class FACTORGRAPH>
112 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, boost::shared_ptr<FACTORGRAPH> >
117 gttic(eliminatePartialSequential);
120 return etree.eliminate(
function);
124 return eliminatePartialSequential(ordering,
function, computedVariableIndex);
129 template<
class FACTORGRAPH>
130 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, boost::shared_ptr<FACTORGRAPH> >
135 gttic(eliminatePartialSequential);
140 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
141 return eliminatePartialSequential(ordering,
function, variableIndex);
145 return eliminatePartialSequential(variables,
function, computedVariableIndex);
150 template<
class FACTORGRAPH>
151 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, boost::shared_ptr<FACTORGRAPH> >
156 gttic(eliminatePartialMultifrontal);
160 return junctionTree.eliminate(
function);
164 return eliminatePartialMultifrontal(ordering,
function, computedVariableIndex);
169 template<
class FACTORGRAPH>
170 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, boost::shared_ptr<FACTORGRAPH> >
175 gttic(eliminatePartialMultifrontal);
180 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
181 return eliminatePartialMultifrontal(ordering,
function, variableIndex);
185 return eliminatePartialMultifrontal(variables,
function, computedVariableIndex);
190 template<
class FACTORGRAPH>
191 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
193 boost::variant<const Ordering&, const KeyVector&> variables,
199 if(marginalizedVariableOrdering)
201 gttic(marginalMultifrontalBayesNet);
204 boost::shared_ptr<BayesTreeType> bayesTree;
205 boost::shared_ptr<FactorGraphType> factorGraph;
206 boost::tie(bayesTree,factorGraph) =
207 eliminatePartialMultifrontal(*marginalizedVariableOrdering,
function, *variableIndex);
209 if(
const Ordering* varsAsOrdering = boost::get<const Ordering&>(&variables))
213 return factorGraph->eliminateSequential(*varsAsOrdering,
function);
218 return factorGraph->eliminateSequential(boost::none,
function);
225 bool unmarginalizedAreOrdered = (boost::get<const Ordering&>(&variables) != 0);
227 unmarginalizedAreOrdered ?
228 boost::get<const Ordering&>(&variables) : boost::get<const KeyVector&>(&variables);
234 const size_t nVars = variablesOrOrdering->size();
235 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
236 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
239 return marginalMultifrontalBayesNet(marginalVarsOrdering, marginalizationOrdering,
function, *variableIndex);
244 return marginalMultifrontalBayesNet(variables, marginalizedVariableOrdering,
function, index);
249 template<
class FACTORGRAPH>
250 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
252 boost::variant<const Ordering&, const KeyVector&> variables,
258 if(marginalizedVariableOrdering)
260 gttic(marginalMultifrontalBayesTree);
263 boost::shared_ptr<BayesTreeType> bayesTree;
264 boost::shared_ptr<FactorGraphType> factorGraph;
265 boost::tie(bayesTree,factorGraph) =
266 eliminatePartialMultifrontal(*marginalizedVariableOrdering,
function, *variableIndex);
268 if(
const Ordering* varsAsOrdering = boost::get<const Ordering&>(&variables))
272 return factorGraph->eliminateMultifrontal(*varsAsOrdering,
function);
277 return factorGraph->eliminateMultifrontal(boost::none,
function);
284 bool unmarginalizedAreOrdered = (boost::get<const Ordering&>(&variables) != 0);
286 unmarginalizedAreOrdered ?
287 boost::get<const Ordering&>(&variables) : boost::get<const KeyVector&>(&variables);
293 const size_t nVars = variablesOrOrdering->size();
294 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
295 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
298 return marginalMultifrontalBayesTree(marginalVarsOrdering, marginalizationOrdering,
function, *variableIndex);
303 return marginalMultifrontalBayesTree(variables, marginalizedVariableOrdering,
function, computedVariableIndex);
308 template<
class FACTORGRAPH>
309 boost::shared_ptr<FACTORGRAPH>
320 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - variables.size());
323 return eliminatePartialMultifrontal(marginalizationOrdering,
function, *variableIndex).second;
329 return marginal(variables,
function, computedVariableIndex);
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
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:43
Exceptions that may be thrown by inference algorithms.
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
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
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:212
An inference algorithm was called with inconsistent arguments.
Definition: inferenceExceptions.h:29
Variable elimination algorithms for factor graphs.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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
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
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