/** * @file inference.h * @brief Contains *generic* inference algorithms that convert between templated * graphical models, i.e., factor graphs, Bayes nets, and Bayes trees * @author Frank Dellaert, Richard Roberts */ #pragma once #include #include #include #include #include #include #include namespace gtsam { class Factor; class Conditional; class Inference { private: /* Static members only, private constructor */ Inference() {} public: /** * Eliminate a factor graph in its natural ordering, i.e. eliminating * variables in order starting from 0. */ template static typename FactorGraph::bayesnet_type::shared_ptr Eliminate(const FactorGraph& factorGraph); /** * Eliminate a factor graph in its natural ordering, i.e. eliminating * variables in order starting from 0. Special fast version for symbolic * elimination. */ template static BayesNet::shared_ptr EliminateSymbolic(const FactorGraph& factorGraph); /** * Eliminate a factor graph in its natural ordering, i.e. eliminating * variables in order starting from 0. Uses an existing * variable index instead of building one from scratch. */ template static typename FactorGraph::bayesnet_type::shared_ptr Eliminate( FactorGraph& factorGraph, typename FactorGraph::variableindex_type& variableIndex); /** * Partially eliminate a factor graph, up to but not including the given * variable. */ template static typename FactorGraph::bayesnet_type::shared_ptr EliminateUntil(const FactorGraph& factorGraph, Index bound); /** * Partially eliminate a factor graph, up to but not including the given * variable. Use an existing variable index instead of building one from * scratch. */ template static typename FactorGraph::bayesnet_type::shared_ptr EliminateUntil(FactorGraph& factorGraph, Index bound, typename FactorGraph::variableindex_type& variableIndex); /** * Eliminate a single variable, updating an existing factor graph and * variable index. */ template static typename FactorGraph::bayesnet_type::sharedConditional EliminateOne(FactorGraph& factorGraph, typename FactorGraph::variableindex_type& variableIndex, Index var); /** * Eliminate a single variable, updating an existing factor graph and * variable index. This is a specialized faster version for purely * symbolic factor graphs. */ static boost::shared_ptr EliminateOneSymbolic(FactorGraph& factorGraph, VariableIndex<>& variableIndex, Index var); /** * Eliminate all variables except the specified ones. Internally this * permutes these variables to the end of the ordering, eliminates all * other variables, and then undoes the permutation. This is * inefficient if multiple marginals are needed - in that case use the * BayesTree which supports efficiently computing marginals for multiple * variables. */ template static FactorGraph Marginal(const FactorGraph& factorGraph, const VarContainer& variables); /** * Compute a permutation (variable ordering) using colamd */ template static boost::shared_ptr PermutationCOLAMD(const VariableIndexType& variableIndex) { return PermutationCOLAMD(variableIndex, std::vector()); } template static boost::shared_ptr PermutationCOLAMD(const VariableIndexType& variableIndex, const ConstraintContainer& constrainLast); // /** // * Join several factors into one. This involves determining the set of // * shared variables and the correct variable positions in the new joint // * factor. // */ // template // static typename FactorGraph::shared_factor Combine(const FactorGraph& factorGraph, // InputIterator indicesBegin, InputIterator indicesEnd); }; // ELIMINATE: FACTOR GRAPH -> BAYES NET // /** // * Eliminate a single node yielding a Conditional // * Eliminates the factors from the factor graph through findAndRemoveFactors // * and adds a new factor on the separator to the factor graph // */ // template // boost::shared_ptr // eliminateOne(FactorGraph& factorGraph, Index key); // // /** // * eliminate factor graph using the given (not necessarily complete) // * ordering, yielding a chordal Bayes net and (partially eliminated) FG // */ // template // BayesNet eliminate(FactorGraph& factorGraph, const Ordering& ordering); // FACTOR/MARGINALIZE: BAYES NET -> FACTOR GRAPH // /** // * Factor P(X) as P(not keys|keys) P(keys) // * @return P(not keys|keys) as an incomplete BayesNet, and P(keys) as a factor graph // */ // template // std::pair< BayesNet, FactorGraph > // factor(const BayesNet& bn, const Ordering& keys); // // /** // * integrate out all except ordering, might be inefficient as the ordering // * will simply be the current ordering with the keys put in the back // */ // template // FactorGraph marginalize(const BayesNet& bn, const Ordering& keys); /** * Hacked-together function to compute a Gaussian marginal for the given variable. * todo: This is inefficient! */ //std::pair marginalGaussian(const GaussianFactorGraph& fg, const Symbol& key); } /// namespace gtsam