29#include <unordered_set>
34 template<
class FACTORGRAPH>
35 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
50 if (orderingType == Ordering::METIS) {
53 }
else if (orderingType == Ordering::COLAMD) {
56 }
else if (orderingType == Ordering::NATURAL) {
60 Ordering computedOrdering = EliminationTraitsType::DefaultOrderingFunc(
61 asDerived(), *variableIndex);
68 template<
class FACTORGRAPH>
69 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
82 const auto [bayesNet, factorGraph] = etree.eliminate(function);
84 if(!factorGraph->empty()) {
93 template <
class FACTORGRAPH>
107 std::cref(computedVariableIndex));
112 if (orderingType == Ordering::METIS) {
115 }
else if (orderingType == Ordering::COLAMD) {
117 return eliminateMultifrontal(computedOrdering, function, variableIndex);
118 }
else if (orderingType == Ordering::NATURAL) {
120 return eliminateMultifrontal(computedOrdering, function, variableIndex);
122 Ordering computedOrdering = EliminationTraitsType::DefaultOrderingFunc(
123 asDerived(), *variableIndex);
124 return eliminateMultifrontal(computedOrdering, function, variableIndex);
130 template<
class FACTORGRAPH>
131 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
145 const auto [bayesTree, factorGraph] = junctionTree.eliminate(function);
147 if(!factorGraph->empty()) {
156 template <
class FACTORGRAPH>
160 const std::unordered_set<Key>& fixedKeys)
const {
165 template <
class FACTORGRAPH>
166 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
172 using BayesTreeNode =
typename BayesTreeType::Node;
173 using SharedFactor =
typename FactorGraphType::sharedFactor;
178 struct ClusterEliminationData {
179 ClusterEliminationData*
const parentData;
180 size_t myIndexInParent;
182 std::shared_ptr<BayesTreeNode> bayesTreeNode;
184 std::shared_ptr<std::mutex> writeLock;
187 ClusterEliminationData(ClusterEliminationData* _parentData,
size_t nChildren)
188 : parentData(_parentData), bayesTreeNode(std::make_shared<BayesTreeNode>())
190 , writeLock(std::make_shared<std::mutex>())
195 parentData->writeLock->lock();
197 myIndexInParent = parentData->childFactors.size();
198 parentData->childFactors.push_back(SharedFactor());
200 parentData->writeLock->unlock();
206 if (parentData->parentData)
207 bayesTreeNode->parent_ = parentData->bayesTreeNode;
208 parentData->bayesTreeNode->children.push_back(bayesTreeNode);
212 static ClusterEliminationData EliminationPreOrderVisitor(
213 const SymbolicJunctionTree::sharedNode& node,
214 ClusterEliminationData& parentData) {
216 ClusterEliminationData myData(&parentData, node->nrChildren());
217 myData.bayesTreeNode->problemSize_ = node->problemSize();
223 class EliminationPostOrderVisitor {
224 const FactorGraphType& graph_;
225 const Eliminate& eliminationFunction_;
228 EliminationPostOrderVisitor(
229 const FactorGraphType& graph,
230 const Eliminate& eliminationFunction)
231 : graph_(graph), eliminationFunction_(eliminationFunction) {}
233 void operator()(
const SymbolicJunctionTree::sharedNode& node,
234 ClusterEliminationData& myData) {
237 FactorGraphType gatheredFactors;
238 gatheredFactors.
reserve(node->factors.size() + node->nrChildren());
240 for (
const auto& factor : node->factors) {
242 std::static_pointer_cast<internal::IndexedSymbolicFactor>(factor);
243 gatheredFactors.
push_back(graph_.at(indexed->index_));
245 gatheredFactors.
push_back(myData.childFactors);
247 auto eliminationResult =
248 eliminationFunction_(gatheredFactors, node->orderedFrontalKeys);
250 myData.bayesTreeNode->setEliminationResult(eliminationResult);
252 if (!eliminationResult.second->empty()) {
254 myData.parentData->writeLock->lock();
256 myData.parentData->childFactors[myData.myIndexInParent] =
257 eliminationResult.second;
259 myData.parentData->writeLock->unlock();
268 std::shared_ptr<BayesTreeType> result = std::make_shared<BayesTreeType>();
270 ClusterEliminationData rootsContainer(0, indexedJunctionTree.nrRoots());
272 EliminationPostOrderVisitor visitorPost(asDerived(), function);
276 indexedJunctionTree, rootsContainer,
277 ClusterEliminationData::EliminationPreOrderVisitor, visitorPost, 10);
281 for (
const auto& rootClique : rootsContainer.bayesTreeNode->children)
282 result->insertRoot(rootClique);
285 KeySet remainingKeys;
286 for (
const auto& factor : rootsContainer.childFactors) {
287 if (!factor || factor->empty())
continue;
288 remainingKeys.insert(factor->begin(), factor->end());
290 if (!remainingKeys.empty()) {
298 template<
class FACTORGRAPH>
299 std::pair<std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, std::shared_ptr<FACTORGRAPH> >
307 return etree.eliminate(function);
316 template<
class FACTORGRAPH>
317 std::pair<std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>, std::shared_ptr<FACTORGRAPH> >
327 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
337 template<
class FACTORGRAPH>
338 std::pair<std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, std::shared_ptr<FACTORGRAPH> >
347 return junctionTree.eliminate(function);
356 template<
class FACTORGRAPH>
357 std::pair<std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>, std::shared_ptr<FACTORGRAPH> >
367 Ordering ordering(fullOrdering.begin(), fullOrdering.begin() + variables.size());
377 template<
class FACTORGRAPH>
378 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
390 constexpr bool forceOrder =
true;
395 const size_t nVars = variables.size();
396 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
397 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
405 template<
class FACTORGRAPH>
406 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
418 const constexpr bool forceOrder =
false;
423 const size_t nVars = variables.size();
424 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
425 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
433 template<
class FACTORGRAPH>
434 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
437 const Ordering& marginalizedVariableOrdering,
448 const auto [bayesTree, factorGraph] =
453 return factorGraph->eliminateSequential(variables, function);
458 template<
class FACTORGRAPH>
459 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesNetType>
462 const Ordering& marginalizedVariableOrdering,
473 const auto [bayesTree, factorGraph] =
477 return factorGraph->eliminateSequential(Ordering::COLAMD, function);
482 template<
class FACTORGRAPH>
483 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
495 constexpr bool forceOrder =
true;
500 const size_t nVars = variables.size();
501 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
502 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
510 template<
class FACTORGRAPH>
511 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
523 constexpr bool forceOrder =
false;
528 const size_t nVars = variables.size();
529 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - nVars);
530 Ordering marginalVarsOrdering(totalOrdering.end() - nVars, totalOrdering.end());
538 template<
class FACTORGRAPH>
539 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
542 const Ordering& marginalizedVariableOrdering,
553 const auto [bayesTree, factorGraph] =
558 return factorGraph->eliminateMultifrontal(variables, function);
563 template<
class FACTORGRAPH>
564 std::shared_ptr<typename EliminateableFactorGraph<FACTORGRAPH>::BayesTreeType>
567 const Ordering& marginalizedVariableOrdering,
578 const auto [bayesTree, factorGraph] =
582 return factorGraph->eliminateMultifrontal(Ordering::COLAMD, function);
587 template<
class FACTORGRAPH>
588 std::shared_ptr<FACTORGRAPH>
599 Ordering marginalizationOrdering(totalOrdering.begin(), totalOrdering.end() - variables.size());
608 return marginal(variables, function, std::cref(computedVariableIndex));
Exceptions that may be thrown by inference algorithms.
Variable elimination algorithms for factor graphs.
Build a symbolic junction tree that stores original factor indices.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
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:91
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Traverse a forest depth-first with pre-order and post-order visits.
Definition treeTraversal-inst.h:181
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition types.h:87
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition FactorGraph.h:147
void reserve(size_t size)
Reserve space for the specified number of factors if you know in advance how many there will be (work...
Definition FactorGraph.h:143
std::pair< std::shared_ptr< BayesNetType >, std::shared_ptr< FactorGraphType > > eliminatePartialSequential(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Do sequential elimination of some variables, in ordering provided, to produce a Bayes net and a remai...
Definition EliminateableFactorGraph-inst.h:300
std::pair< std::shared_ptr< BayesTreeType >, std::shared_ptr< FactorGraphType > > eliminatePartialMultifrontal(const Ordering &ordering, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Do multifrontal elimination of some variables, in ordering provided, to produce a Bayes tree and a re...
Definition EliminateableFactorGraph-inst.h:339
EliminationTraitsType::JunctionTreeType JunctionTreeType
Definition EliminateableFactorGraph.h:84
std::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
Definition EliminateableFactorGraph.h:91
EliminationTraitsType::BayesTreeType BayesTreeType
Bayes tree type produced by multifrontal elimination.
Definition EliminateableFactorGraph.h:81
std::shared_ptr< FactorGraphType > marginal(const KeyVector &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Compute the marginal factor graph of the requested variables.
Definition EliminateableFactorGraph-inst.h:589
std::shared_ptr< BayesNetType > marginalMultifrontalBayesNet(const Ordering &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Compute the marginal of the requested variables and return the result as a Bayes net.
Definition EliminateableFactorGraph-inst.h:379
IndexedJunctionTree buildIndexedJunctionTree(const Ordering &ordering, const std::unordered_set< Key > &fixedKeys={}) const
Build an IndexedJunctionTree for this factor graph and a fixed ordering.
Definition EliminateableFactorGraph-inst.h:158
EliminationTraitsType::EliminationTreeType EliminationTreeType
Elimination tree type that can do sequential elimination of this graph.
Definition EliminateableFactorGraph.h:78
std::shared_ptr< BayesTreeType > marginalMultifrontalBayesTree(const Ordering &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Compute the marginal of the requested variables and return the result as a Bayes tree.
Definition EliminateableFactorGraph-inst.h:484
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Do sequential elimination of all variables to produce a Bayes net.
Definition EliminateableFactorGraph-inst.h:36
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Do multifrontal elimination of all variables to produce a Bayes tree.
Definition EliminateableFactorGraph-inst.h:96
std::optional< std::reference_wrapper< const VariableIndex > > OptionalVariableIndex
Typedef for an optional variable index as an argument to elimination functions It is an optional to a...
Definition EliminateableFactorGraph.h:95
std::optional< Ordering::OrderingType > OptionalOrderingType
Typedef for an optional ordering type.
Definition EliminateableFactorGraph.h:98
An inference algorithm was called with inconsistent arguments.
Definition inferenceExceptions.h:32
static Ordering Natural(const FACTOR_GRAPH &fg)
Return a natural Ordering. Typically used by iterative solvers.
Definition Ordering.h:188
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:93
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:112
static Ordering Metis(const MetisIndex &met, int seed=4321)
Compute an ordering determined by METIS from a VariableIndex.
Definition Ordering.cpp:256
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:139
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition VariableIndex.h:41
A symbolic junction tree whose factors record the original factor indices from a corresponding (non-s...
Definition IndexedJunctionTree.h:58