23 #include <boost/tuple/tuple.hpp>
28 template<
class FACTORGRAPH>
29 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
33 if(ordering && variableIndex) {
34 gttic(eliminateSequential);
37 boost::shared_ptr<BayesNetType> bayesNet;
38 boost::shared_ptr<FactorGraphType> factorGraph;
39 boost::tie(bayesNet,factorGraph) = etree.eliminate(
function);
41 if(!factorGraph->empty())
46 else if(!variableIndex) {
50 return eliminateSequential(ordering,
function,
VariableIndex(asDerived()));
61 template<
class FACTORGRAPH>
62 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
66 if(ordering && variableIndex) {
67 gttic(eliminateMultifrontal);
71 boost::shared_ptr<BayesTreeType> bayesTree;
72 boost::shared_ptr<FactorGraphType> factorGraph;
73 boost::tie(bayesTree,factorGraph) = junctionTree.eliminate(
function);
75 if(!factorGraph->empty())
80 else if(!variableIndex) {
84 return eliminateMultifrontal(ordering,
function,
VariableIndex(asDerived()));
95 template<
class FACTORGRAPH>
96 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, boost::shared_ptr<FACTORGRAPH> >
101 gttic(eliminatePartialSequential);
104 return etree.eliminate(
function);
107 return eliminatePartialSequential(ordering,
function,
VariableIndex(asDerived()));
112 template<
class FACTORGRAPH>
113 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, boost::shared_ptr<FACTORGRAPH> >
118 gttic(eliminatePartialSequential);
123 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
124 return eliminatePartialSequential(ordering,
function, variableIndex);
127 return eliminatePartialSequential(variables,
function,
VariableIndex(asDerived()));
132 template<
class FACTORGRAPH>
133 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, boost::shared_ptr<FACTORGRAPH> >
138 gttic(eliminatePartialMultifrontal);
142 return junctionTree.eliminate(
function);
145 return eliminatePartialMultifrontal(ordering,
function,
VariableIndex(asDerived()));
150 template<
class FACTORGRAPH>
151 std::pair<boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, boost::shared_ptr<FACTORGRAPH> >
156 gttic(eliminatePartialMultifrontal);
161 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
162 return eliminatePartialMultifrontal(ordering,
function, variableIndex);
165 return eliminatePartialMultifrontal(variables,
function,
VariableIndex(asDerived()));
170 template<
class FACTORGRAPH>
171 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
173 boost::variant<
const Ordering&,
const std::vector<Key>&> variables,
179 if(marginalizedVariableOrdering)
181 gttic(marginalMultifrontalBayesNet);
184 boost::shared_ptr<BayesTreeType> bayesTree;
185 boost::shared_ptr<FactorGraphType> factorGraph;
186 boost::tie(bayesTree,factorGraph) =
187 eliminatePartialMultifrontal(*marginalizedVariableOrdering,
function, *variableIndex);
189 if(
const Ordering* varsAsOrdering = boost::get<const Ordering&>(&variables))
193 return factorGraph->eliminateSequential(*varsAsOrdering,
function);
198 return factorGraph->eliminateSequential(boost::none,
function);
205 bool unmarginalizedAreOrdered = (boost::get<const Ordering&>(&variables) != 0);
206 const std::vector<Key>* variablesOrOrdering =
207 unmarginalizedAreOrdered ?
208 boost::get<const Ordering&>(&variables) : boost::get<
const std::vector<Key>&>(&variables);
214 const size_t nVars = variablesOrOrdering->size();
215 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
216 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
219 return marginalMultifrontalBayesNet(marginalVarsOrdering, marginalizationOrdering,
function, *variableIndex);
224 return marginalMultifrontalBayesNet(variables, marginalizedVariableOrdering,
function, index);
229 template<
class FACTORGRAPH>
230 boost::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
232 boost::variant<
const Ordering&,
const std::vector<Key>&> variables,
238 if(marginalizedVariableOrdering)
240 gttic(marginalMultifrontalBayesTree);
243 boost::shared_ptr<BayesTreeType> bayesTree;
244 boost::shared_ptr<FactorGraphType> factorGraph;
245 boost::tie(bayesTree,factorGraph) =
246 eliminatePartialMultifrontal(*marginalizedVariableOrdering,
function, *variableIndex);
248 if(
const Ordering* varsAsOrdering = boost::get<const Ordering&>(&variables))
252 return factorGraph->eliminateMultifrontal(*varsAsOrdering,
function);
257 return factorGraph->eliminateMultifrontal(boost::none,
function);
264 bool unmarginalizedAreOrdered = (boost::get<const Ordering&>(&variables) != 0);
265 const std::vector<Key>* variablesOrOrdering =
266 unmarginalizedAreOrdered ?
267 boost::get<const Ordering&>(&variables) : boost::get<
const std::vector<Key>&>(&variables);
273 const size_t nVars = variablesOrOrdering->size();
274 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
275 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
278 return marginalMultifrontalBayesTree(marginalVarsOrdering, marginalizationOrdering,
function, *variableIndex);
282 return marginalMultifrontalBayesTree(variables, marginalizedVariableOrdering,
function,
VariableIndex(asDerived()));
287 template<
class FACTORGRAPH>
288 boost::shared_ptr<FACTORGRAPH>
290 const std::vector<Key>& variables,
299 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - variables.size());
302 return eliminatePartialMultifrontal(marginalizationOrdering,
function, *variableIndex).second;
307 return marginal(variables,
function,
VariableIndex(asDerived()));
An inference algorithm was called with inconsistent arguments.
Definition: inferenceExceptions.h:29
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:134
Variable elimination algorithms for factor graphs.
boost::shared_ptr< BayesNetType > marginalMultifrontalBayesNet(boost::variant< const Ordering &, const std::vector< Key > & > 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:172
boost::optional< const VariableIndex & > OptionalVariableIndex
Typedef for an optional variable index as an argument to elimination functions.
Definition: EliminateableFactorGraph.h:95
static Ordering COLAMD(const FactorGraph< FACTOR > &graph)
Compute a fill-reducing ordering using COLAMD from a factor graph (see details for note on performanc...
Definition: Ordering.h:65
boost::shared_ptr< BayesTreeType > marginalMultifrontalBayesTree(boost::variant< const Ordering &, const std::vector< Key > & > 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:231
boost::optional< const Ordering & > OptionalOrdering
Typedef for an optional ordering as an argument to elimination functions.
Definition: EliminateableFactorGraph.h:92
static Ordering COLAMDConstrainedLast(const FactorGraph< FACTOR > &graph, const std::vector< Key > &constrainLast, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition: Ordering.h:80
static Ordering COLAMDConstrainedFirst(const FactorGraph< FACTOR > &graph, const std::vector< Key > &constrainFirst, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition: Ordering.h:102
EliminationTraitsType::JunctionTreeType JunctionTreeType
Junction tree type that can do multifrontal elimination of this graph.
Definition: EliminateableFactorGraph.h:82
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
The VariableIndex class computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:42
boost::shared_ptr< FactorGraphType > marginal(const std::vector< Key > &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
Compute the marginal factor graph of the requested variables.
Definition: EliminateableFactorGraph-inst.h:289
Exceptions that may be thrown by inference algorithms.
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrdering ordering=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:63
Definition: Ordering.h:30
EliminationTraitsType::EliminationTreeType EliminationTreeType
Elimination tree type that can do sequential elimination of this graph.
Definition: EliminateableFactorGraph.h:76
boost::shared_ptr< BayesNetType > eliminateSequential(OptionalOrdering ordering=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
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:97