/* ---------------------------------------------------------------------------- * 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.cpp * @date Feb 14, 2011 * @author Duy-Nguyen Ta * @author Frank Dellaert */ //#define ENABLE_TIMING #include #include #include #include #include #include #include #include namespace gtsam { // Instantiate base classes template class FactorGraph; template class EliminateableFactorGraph; /* ************************************************************************* */ bool DiscreteFactorGraph::equals(const This& fg, double tol) const { return Base::equals(fg, tol); } /* ************************************************************************* */ FastSet DiscreteFactorGraph::keys() const { FastSet keys; BOOST_FOREACH(const sharedFactor& factor, *this) if (factor) keys.insert(factor->begin(), factor->end()); return keys; } /* ************************************************************************* */ DecisionTreeFactor DiscreteFactorGraph::product() const { DecisionTreeFactor result; BOOST_FOREACH(const sharedFactor& factor, *this) if (factor) result = (*factor) * result; return result; } /* ************************************************************************* */ double DiscreteFactorGraph::operator()( const DiscreteFactor::Values &values) const { double product = 1.0; BOOST_FOREACH( const sharedFactor& factor, factors_ ) product *= (*factor)(values); return product; } /* ************************************************************************* */ void DiscreteFactorGraph::print(const std::string& s, const KeyFormatter& formatter) const { std::cout << s << std::endl; std::cout << "size: " << size() << std::endl; for (size_t i = 0; i < factors_.size(); i++) { std::stringstream ss; ss << "factor " << i << ": "; if (factors_[i] != NULL) factors_[i]->print(ss.str(), formatter); } } // /* ************************************************************************* */ // void DiscreteFactorGraph::permuteWithInverse( // const Permutation& inversePermutation) { // BOOST_FOREACH(const sharedFactor& factor, factors_) { // if(factor) // factor->permuteWithInverse(inversePermutation); // } // } // // /* ************************************************************************* */ // void DiscreteFactorGraph::reduceWithInverse( // const internal::Reduction& inverseReduction) { // BOOST_FOREACH(const sharedFactor& factor, factors_) { // if(factor) // factor->reduceWithInverse(inverseReduction); // } // } /* ************************************************************************* */ DiscreteFactor::sharedValues DiscreteFactorGraph::optimize() const { gttic(DiscreteFactorGraph_optimize); return BaseEliminateable::eliminateSequential()->optimize(); } /* ************************************************************************* */ std::pair // EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { // PRODUCT: multiply all factors gttic(product); DecisionTreeFactor product; BOOST_FOREACH(const DiscreteFactor::shared_ptr& factor, factors) product = (*factor) * product; gttoc(product); // sum out frontals, this is the factor on the separator gttic(sum); DecisionTreeFactor::shared_ptr sum = product.sum(frontalKeys); gttoc(sum); // Ordering keys for the conditional so that frontalKeys are really in front Ordering orderedKeys; orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(), frontalKeys.end()); orderedKeys.insert(orderedKeys.end(), sum->keys().begin(), sum->keys().end()); // now divide product/sum to get conditional gttic(divide); DiscreteConditional::shared_ptr cond(new DiscreteConditional(product, *sum, orderedKeys)); gttoc(divide); return std::make_pair(cond, sum); } /* ************************************************************************* */ } // namespace