/* ---------------------------------------------------------------------------- * 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 BayesNet * @brief Bayes network * @author Frank Dellaert */ // \callgraph #pragma once #include #include #include #include #include #include #include namespace gtsam { /** * A BayesNet is a list of conditionals, stored in elimination order, i.e. * leaves first, parents last. GaussianBayesNet and SymbolicBayesNet are * defined as typedefs of this class, using GaussianConditional and * IndexConditional as the CONDITIONAL template argument. * * todo: Symbolic using Index is a misnomer. * todo: how to handle Bayes nets with an optimize function? Currently using global functions. * \nosubgrouping */ template class BayesNet { public: typedef typename boost::shared_ptr > shared_ptr; /** We store shared pointers to Conditional densities */ typedef typename boost::shared_ptr sharedConditional; typedef typename boost::shared_ptr const_sharedConditional; typedef typename std::list Conditionals; typedef typename Conditionals::iterator iterator; typedef typename Conditionals::reverse_iterator reverse_iterator; typedef typename Conditionals::const_iterator const_iterator; typedef typename Conditionals::const_reverse_iterator const_reverse_iterator; protected: /** * Conditional densities are stored in reverse topological sort order (i.e., leaves first, * parents last), which corresponds to the elimination ordering if so obtained, * and is consistent with the column (block) ordering of an upper triangular matrix. */ Conditionals conditionals_; public: /// @name Standard Constructors /// @{ /** Default constructor as an empty BayesNet */ BayesNet() {}; /** BayesNet with 1 conditional */ BayesNet(const sharedConditional& conditional) { push_back(conditional); } /// @} /// @name Testable /// @{ /** print */ void print(const std::string& s = "") const; /** check equality */ bool equals(const BayesNet& other, double tol = 1e-9) const; /// @} /// @name Standard Interface /// @{ /** size is the number of nodes */ size_t size() const { return conditionals_.size(); } /** return keys in reverse topological sort order, i.e., elimination order */ FastList ordering() const; /** SLOW O(n) random access to Conditional by key */ sharedConditional operator[](Index key) const; /** return last node in ordering */ boost::shared_ptr front() const { return conditionals_.front(); } /** return last node in ordering */ boost::shared_ptr back() const { return conditionals_.back(); } /** return iterators. FD: breaks encapsulation? */ const_iterator begin() const {return conditionals_.begin();} /// bn); /// push_front an entire Bayes net void push_front(const BayesNet bn); /** += syntax for push_back, e.g. bayesNet += c1, c2, c3 * @param conditional The conditional to add to the back of the BayesNet */ boost::assign::list_inserter >, sharedConditional> operator+=(const sharedConditional& conditional) { return boost::assign::make_list_inserter(boost::assign_detail::call_push_back >(*this))(conditional); } /** * pop_front: remove node at the bottom, used in marginalization * For example P(ABC)=P(A|BC)P(B|C)P(C) becomes P(BC)=P(B|C)P(C) */ void pop_front() {conditionals_.pop_front();} /** Permute the variables in the BayesNet */ void permuteWithInverse(const Permutation& inversePermutation); /** * Permute the variables when only separator variables need to be permuted. * Returns true if any reordered variables appeared in the separator and * false if not. */ bool permuteSeparatorWithInverse(const Permutation& inversePermutation); iterator begin() {return conditionals_.begin();} /// void serialize(ARCHIVE & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(conditionals_); } /// @} }; // BayesNet } // namespace gtsam #include