/** * @file FactorGraph.h * @brief Factor Graph Base Class * @author Carlos Nieto * @author Christian Potthast * @author Michael Kaess */ // \callgraph #pragma once #include #include //#include //#include //#include //#include #include #include #include #include #include namespace gtsam { /** * A factor graph is a bipartite graph with factor nodes connected to variable nodes. * In this class, however, only factor nodes are kept around. * * Templated on the type of factors and values structure. */ template class FactorGraph: public Testable > { public: typedef Factor factor_type; typedef boost::shared_ptr > shared_ptr; typedef typename boost::shared_ptr sharedFactor; typedef typename std::vector::iterator iterator; typedef typename std::vector::const_iterator const_iterator; protected: /** Collection of factors */ std::vector factors_; // /** // * Return an ordering in first argument, potentially using a set of // * keys that need to appear last, and potentially restricting scope // */ // void getOrdering(Ordering& ordering, const std::set& lastKeys, // boost::optional&> scope = boost::none) const; public: /** ------------------ Creating Factor Graphs ---------------------------- */ /** Default constructor */ FactorGraph() {} /** convert from Bayes net */ template FactorGraph(const BayesNet& bayesNet); // /** convert from Bayes net while permuting at the same time */ // template // FactorGraph(const BayesNet& bayesNet, const Inference::Permutation& permutation); /** convert from a derived type */ template FactorGraph(const Derived& factorGraph); /** Add a factor */ template void push_back(const boost::shared_ptr& factor); /** push back many factors */ void push_back(const FactorGraph& factors); /** ------------------ Querying Factor Graphs ---------------------------- */ /** print out graph */ void print(const std::string& s = "FactorGraph") const; /** Check equality */ bool equals(const FactorGraph& fg, double tol = 1e-9) const; /** const cast to the underlying vector of factors */ operator const std::vector&() const { return factors_; } /** STL begin and end, so we can use BOOST_FOREACH */ inline const_iterator begin() const { return factors_.begin();} inline const_iterator end() const { return factors_.end(); } /** Get a specific factor by index */ inline sharedFactor operator[](size_t i) const { assert(i getOrdering_() const; // Ordering getOrdering(const std::set& scope) const; // Ordering getConstrainedOrdering(const std::set& lastKeys) const; // /** // * find the minimum spanning tree using boost graph library // */ // template PredecessorMap // SL-NEEDED? findMinimumSpanningTree() const; // // /** // * Split the graph into two parts: one corresponds to the given spanning tree, // * and the other corresponds to the rest of the factors // */ // SL-NEEDED? template void split(const PredecessorMap& tree, // FactorGraph& Ab1, FactorGraph& Ab2) const; // /** // * find the minimum spanning tree using DSF // */ // std::pair , FactorGraph > // SL-NEEDED? splitMinimumSpanningTree() const; // /** // * Check consistency of the index map, useful for debugging // */ // void checkGraphConsistency() const; /** ----------------- Modifying Factor Graphs ---------------------------- */ /** STL begin and end, so we can use BOOST_FOREACH */ inline iterator begin() { return factors_.begin();} inline iterator end() { return factors_.end(); } /** * Reserve space for the specified number of factors if you know in * advance how many there will be (works like std::vector::reserve). */ void reserve(size_t size) { factors_.reserve(size); } /** delete factor without re-arranging indexes by inserting a NULL pointer */ inline void remove(size_t i) { factors_[i].reset();} /** replace a factor by index */ void replace(size_t index, sharedFactor factor); // /** // * Find all the factors that involve the given node and remove them // * from the factor graph // * @param key the key for the given node // */ // std::vector findAndRemoveFactors(varid_t key); // // /** remove singleton variables and the related factors */ // std::pair, std::set > removeSingletons(); private: /** Serialization function */ friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(factors_); } }; // FactorGraph /** * static function that combines two factor graphs * @param const &fg1 Linear factor graph * @param const &fg2 Linear factor graph * @return a new combined factor graph */ template FactorGraph combine(const FactorGraph& fg1, const FactorGraph& fg2); // /** // * Extract and combine all the factors that involve a given node // * Put this here as not all Factors have a combine constructor // * @param key the key for the given node // * @return the combined linear factor // */ // template boost::shared_ptr // removeAndCombineFactors(FactorGraph& factorGraph, const varid_t& key); /** * These functions are defined here because they are templated on an * additional parameter. Putting them in the -inl.h file would mean these * would have to be explicitly instantiated for any possible derived factor * type. */ /* ************************************************************************* */ template template FactorGraph::FactorGraph(const Derived& factorGraph) { factors_.reserve(factorGraph.size()); BOOST_FOREACH(const typename Derived::sharedFactor& factor, factorGraph) { this->push_back(factor); } } /* ************************************************************************* */ template template inline void FactorGraph::push_back(const boost::shared_ptr& factor) { #ifndef NDEBUG factors_.push_back(boost::dynamic_pointer_cast(factor)); // add the actual factor #else factors_.push_back(boost::static_pointer_cast(factor)); #endif } } // namespace gtsam