diff --git a/gtsam/discrete/DiscreteMarginals.h b/gtsam/discrete/DiscreteMarginals.h new file mode 100644 index 000000000..f877a0128 --- /dev/null +++ b/gtsam/discrete/DiscreteMarginals.h @@ -0,0 +1,62 @@ +/* ---------------------------------------------------------------------------- + + * 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 DiscreteMarginals.h + * @brief A class for computing marginals in a DiscreteFactorGraph + * @author Abhijit Kundu + * @author Richard Roberts + * @author Frank Dellaert + * @date June 4, 2012 + */ + +#pragma once + +#include + +namespace gtsam { + + /** + * A class for computing marginals of variables in a DiscreteFactorGraph + */ + class DiscreteMarginals { + + protected: + + BayesTree bayesTree_; + + public: + + /** Construct a marginals class. + * @param graph The factor graph defining the full joint density on all variables. + */ + DiscreteMarginals(const DiscreteFactorGraph& graph) { + } + + /** print */ + void print(const std::string& str = "DiscreteMarginals: ") const { + } + + /** Compute the marginal of a single variable */ + DiscreteFactor::shared_ptr operator()(Index variable) const { + DiscreteFactor::shared_ptr p; + return p; + } + + /** Compute the marginal of a single variable */ + Vector marginalProbabilities(Index variable) const { + Vector v; + return v; + } + + }; + +} /* namespace gtsam */ diff --git a/gtsam/discrete/tests/testDiscreteMarginals.cpp b/gtsam/discrete/tests/testDiscreteMarginals.cpp new file mode 100644 index 000000000..391a5f692 --- /dev/null +++ b/gtsam/discrete/tests/testDiscreteMarginals.cpp @@ -0,0 +1,62 @@ +/* ---------------------------------------------------------------------------- + + * 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 testDiscreteMarginals.cpp + * @date Feb 14, 2011 + * @author Abhijit Kundu + * @author Richard Roberts + * @author Frank Dellaert + */ + +#include +#include + +using namespace std; +using namespace gtsam; + +/* ************************************************************************* */ + +TEST( DiscreteMarginals, UGM_small ) { + size_t nrStates = 2; + DiscreteKey Cathy(1, nrStates), Heather(2, nrStates), Mark(3, nrStates), + Allison(4, nrStates); + DiscreteFactorGraph graph; + + // add node potentials + graph.add(Cathy, "1 3"); + graph.add(Heather, "9 1"); + graph.add(Mark, "1 3"); + graph.add(Allison, "9 1"); + + // add edge potentials + graph.add(Cathy & Heather, "2 1 1 2"); + graph.add(Heather & Mark, "2 1 1 2"); + graph.add(Mark & Allison, "2 1 1 2"); + + DiscreteMarginals marginals(graph); + + DiscreteFactor::shared_ptr actualC = marginals(Cathy.first); + DiscreteFactor::Values values; + values[Cathy.first] = 0; + EXPECT_DOUBLES_EQUAL( 1.944, (*actualC)(values), 1e-9); + + Vector actualCvector = marginals.marginalProbabilities(Cathy.first); + EXPECT(assert_equal(Vector_(2,0.7,0.3), actualCvector, 1e-9)); +} + +/* ************************************************************************* */ +int main() { + TestResult tr; + return TestRegistry::runAllTests(tr); +} +/* ************************************************************************* */ +