/* ---------------------------------------------------------------------------- * GTSAM Copyright 2010, Georgia Tech Research Corporation, * Atlanta, Georgia 30332-0415 * All Rights Reserved * Authors: Frank Dellaert, et al. (see THANKS for the full author list) * See LICENSE for the license information * -------------------------------------------------------------------------- */ /** * @file DiscreteFactorGraph.h * @date Feb 14, 2011 * @author Duy-Nguyen Ta * @author Frank Dellaert */ #pragma once #include #include #include #include #include #include #include #include #include namespace gtsam { // Forward declarations class DiscreteFactorGraph; class DiscreteConditional; class DiscreteBayesNet; class DiscreteEliminationTree; class DiscreteBayesTree; class DiscreteJunctionTree; /** Main elimination function for DiscreteFactorGraph */ GTSAM_EXPORT std::pair, DecisionTreeFactor::shared_ptr> EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& keys); /* ************************************************************************* */ template<> struct EliminationTraits { typedef DiscreteFactor FactorType; ///< Type of factors in factor graph typedef DiscreteFactorGraph FactorGraphType; ///< Type of the factor graph (e.g. DiscreteFactorGraph) typedef DiscreteConditional ConditionalType; ///< Type of conditionals from elimination typedef DiscreteBayesNet BayesNetType; ///< Type of Bayes net from sequential elimination typedef DiscreteEliminationTree EliminationTreeType; ///< Type of elimination tree typedef DiscreteBayesTree BayesTreeType; ///< Type of Bayes tree typedef DiscreteJunctionTree JunctionTreeType; ///< Type of Junction tree /// The default dense elimination function static std::pair, boost::shared_ptr > DefaultEliminate(const FactorGraphType& factors, const Ordering& keys) { return EliminateDiscrete(factors, keys); } }; /* ************************************************************************* */ /** * A Discrete Factor Graph is a factor graph where all factors are Discrete, i.e. * Factor == DiscreteFactor */ class GTSAM_EXPORT DiscreteFactorGraph: public FactorGraph, public EliminateableFactorGraph { public: typedef DiscreteFactorGraph This; ///< Typedef to this class typedef FactorGraph Base; ///< Typedef to base factor graph type typedef EliminateableFactorGraph BaseEliminateable; ///< Typedef to base elimination class typedef boost::shared_ptr shared_ptr; ///< shared_ptr to this class using Values = DiscreteValues; ///< backwards compatibility /** A map from keys to values */ typedef KeyVector Indices; /** Default constructor */ DiscreteFactorGraph() {} /** Construct from iterator over factors */ template DiscreteFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) : Base(firstFactor, lastFactor) {} /** Construct from container of factors (shared_ptr or plain objects) */ template explicit DiscreteFactorGraph(const CONTAINER& factors) : Base(factors) {} /** Implicit copy/downcast constructor to override explicit template container constructor */ template DiscreteFactorGraph(const FactorGraph& graph) : Base(graph) {} /// Destructor virtual ~DiscreteFactorGraph() {} /// @name Testable /// @{ bool equals(const This& fg, double tol = 1e-9) const; /// @} /** Add a decision-tree factor */ template void add(Args&&... args) { emplace_shared(std::forward(args)...); } /** Return the set of variables involved in the factors (set union) */ KeySet keys() const; /** return product of all factors as a single factor */ DecisionTreeFactor product() const; /** * Evaluates the factor graph given values, returns the joint probability of * the factor graph given specific instantiation of values */ double operator()(const DiscreteValues& values) const; /// print void print( const std::string& s = "DiscreteFactorGraph", const KeyFormatter& formatter = DefaultKeyFormatter) const override; /** Solve the factor graph by performing variable elimination in COLAMD order using * the dense elimination function specified in \c function, * followed by back-substitution resulting from elimination. Is equivalent * to calling graph.eliminateSequential()->optimize(). */ DiscreteValues optimize() const; // /** Permute the variables in the factors */ // GTSAM_EXPORT void permuteWithInverse(const Permutation& inversePermutation); // // /** Apply a reduction, which is a remapping of variable indices. */ // GTSAM_EXPORT void reduceWithInverse(const internal::Reduction& inverseReduction); /// @name Wrapper support /// @{ /** * @brief Render as markdown tables * * @param keyFormatter GTSAM-style Key formatter. * @param names optional, a map from Key to category names. * @return std::string a (potentially long) markdown string. */ std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter, const DiscreteFactor::Names& names = {}) const; /** * @brief Render as html tables * * @param keyFormatter GTSAM-style Key formatter. * @param names optional, a map from Key to category names. * @return std::string a (potentially long) html string. */ std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter, const DiscreteFactor::Names& names = {}) const; /// @} }; // \ DiscreteFactorGraph /// traits template<> struct traits : public Testable {}; } // \ namespace gtsam