|
| 1 | +/* ---------------------------------------------------------------------------- |
| 2 | +
|
| 3 | + * GTSAM Copyright 2010, Georgia Tech Research Corporation, |
| 4 | + * Atlanta, Georgia 30332-0415 |
| 5 | + * All Rights Reserved |
| 6 | + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) |
| 7 | +
|
| 8 | + * See LICENSE for the license information |
| 9 | +
|
| 10 | + * -------------------------------------------------------------------------- */ |
| 11 | + |
| 12 | +/** |
| 13 | + * @file DiscreteMarginals.cpp |
| 14 | + * @brief A class for computing marginals in a DiscreteFactorGraph |
| 15 | + * @author Abhijit Kundu |
| 16 | + * @author Richard Roberts |
| 17 | + * @author Varun Agrawal |
| 18 | + * @author Frank Dellaert |
| 19 | + * @date June 4, 2012 |
| 20 | + */ |
| 21 | + |
| 22 | +#include <gtsam/discrete/DiscreteMarginals.h> |
| 23 | + |
| 24 | +namespace gtsam { |
| 25 | + |
| 26 | +/* ************************************************************************* */ |
| 27 | +DiscreteMarginals::DiscreteMarginals(const DiscreteFactorGraph& graph) { |
| 28 | + bayesTree_ = graph.eliminateMultifrontal(); |
| 29 | +} |
| 30 | + |
| 31 | +/* ************************************************************************* */ |
| 32 | +DiscreteFactor::shared_ptr DiscreteMarginals::operator()(Key variable) const { |
| 33 | + // Compute marginal |
| 34 | + DiscreteFactor::shared_ptr marginalFactor = |
| 35 | + bayesTree_->marginalFactor(variable, &EliminateDiscrete); |
| 36 | + return marginalFactor; |
| 37 | +} |
| 38 | + |
| 39 | +/* ************************************************************************* */ |
| 40 | +Vector DiscreteMarginals::marginalProbabilities(const DiscreteKey& key) const { |
| 41 | + // Compute marginal |
| 42 | + DiscreteFactor::shared_ptr marginalFactor = this->operator()(key.first); |
| 43 | + |
| 44 | + // Create result |
| 45 | + Vector vResult(key.second); |
| 46 | + for (size_t state = 0; state < key.second; ++state) { |
| 47 | + DiscreteValues values; |
| 48 | + values[key.first] = state; |
| 49 | + vResult(state) = (*marginalFactor)(values); |
| 50 | + } |
| 51 | + return vResult; |
| 52 | +} |
| 53 | + |
| 54 | +/* ************************************************************************* */ |
| 55 | +void DiscreteMarginals::print(const std::string& s, |
| 56 | + const KeyFormatter formatter) const { |
| 57 | + std::cout << (s.empty() ? "Discrete Marginals of:" : s + " ") << std::endl; |
| 58 | + bayesTree_->print("", formatter); |
| 59 | +} |
| 60 | + |
| 61 | +} /* namespace gtsam */ |
0 commit comments