added printSignature and evaluate

release/4.3a0
Frank dellaert 2020-07-12 14:59:43 -04:00
parent 3f4bf163e7
commit 968b207135
4 changed files with 71 additions and 36 deletions

View File

@ -45,6 +45,16 @@ namespace gtsam {
return Base::equals(other, tol); return Base::equals(other, tol);
} }
/* ************************************************************************* */
double DiscreteBayesTree::evaluate(
const DiscreteConditional::Values& values) const {
double result = 1.0;
for (const auto& root : roots_) {
result *= root->evaluate(values);
}
return result;
}
} // \namespace gtsam } // \namespace gtsam

View File

@ -11,7 +11,8 @@
/** /**
* @file DiscreteBayesTree.h * @file DiscreteBayesTree.h
* @brief Discrete Bayes Tree, the result of eliminating a DiscreteJunctionTree * @brief Discrete Bayes Tree, the result of eliminating a
* DiscreteJunctionTree
* @brief DiscreteBayesTree * @brief DiscreteBayesTree
* @author Frank Dellaert * @author Frank Dellaert
* @author Richard Roberts * @author Richard Roberts
@ -22,8 +23,11 @@
#include <gtsam/discrete/DiscreteBayesNet.h> #include <gtsam/discrete/DiscreteBayesNet.h>
#include <gtsam/discrete/DiscreteFactorGraph.h> #include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/inference/BayesTree.h> #include <gtsam/inference/BayesTree.h>
#include <gtsam/inference/Conditional.h>
#include <gtsam/inference/BayesTreeCliqueBase.h> #include <gtsam/inference/BayesTreeCliqueBase.h>
#include <string>
namespace gtsam { namespace gtsam {
// Forward declarations // Forward declarations
@ -32,16 +36,25 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
/** A clique in a DiscreteBayesTree */ /** A clique in a DiscreteBayesTree */
class GTSAM_EXPORT DiscreteBayesTreeClique : class GTSAM_EXPORT DiscreteBayesTreeClique
public BayesTreeCliqueBase<DiscreteBayesTreeClique, DiscreteFactorGraph> : public BayesTreeCliqueBase<DiscreteBayesTreeClique, DiscreteFactorGraph> {
{
public: public:
typedef DiscreteBayesTreeClique This; typedef DiscreteBayesTreeClique This;
typedef BayesTreeCliqueBase<DiscreteBayesTreeClique, DiscreteFactorGraph> Base; typedef BayesTreeCliqueBase<DiscreteBayesTreeClique, DiscreteFactorGraph>
Base;
typedef boost::shared_ptr<This> shared_ptr; typedef boost::shared_ptr<This> shared_ptr;
typedef boost::weak_ptr<This> weak_ptr; typedef boost::weak_ptr<This> weak_ptr;
DiscreteBayesTreeClique() {} DiscreteBayesTreeClique() {}
DiscreteBayesTreeClique(const boost::shared_ptr<DiscreteConditional>& conditional) : Base(conditional) {} DiscreteBayesTreeClique(
const boost::shared_ptr<DiscreteConditional>& conditional)
: Base(conditional) {}
/// print index signature only
void printSignature(
const std::string& s = "Clique: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const {
conditional_->printSignature(s, formatter);
}
//** evaluate conditional probability of subtree for given Values */ //** evaluate conditional probability of subtree for given Values */
double evaluate(const DiscreteConditional::Values& values) const; double evaluate(const DiscreteConditional::Values& values) const;
@ -49,9 +62,8 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
/** A Bayes tree representing a Discrete density */ /** A Bayes tree representing a Discrete density */
class GTSAM_EXPORT DiscreteBayesTree : class GTSAM_EXPORT DiscreteBayesTree
public BayesTree<DiscreteBayesTreeClique> : public BayesTree<DiscreteBayesTreeClique> {
{
private: private:
typedef BayesTree<DiscreteBayesTreeClique> Base; typedef BayesTree<DiscreteBayesTreeClique> Base;
@ -64,6 +76,9 @@ namespace gtsam {
/** Check equality */ /** Check equality */
bool equals(const This& other, double tol = 1e-9) const; bool equals(const This& other, double tol = 1e-9) const;
//** evaluate probability for given Values */
double evaluate(const DiscreteConditional::Values& values) const;
}; };
} } // namespace gtsam

View File

@ -24,6 +24,8 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp> #include <boost/make_shared.hpp>
#include <string>
namespace gtsam { namespace gtsam {
/** /**
@ -92,6 +94,13 @@ public:
/// @name Standard Interface /// @name Standard Interface
/// @{ /// @{
/// print index signature only
void printSignature(
const std::string& s = "Discrete Conditional: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const {
static_cast<const BaseConditional*>(this)->print(s, formatter);
}
/// Evaluate, just look up in AlgebraicDecisonTree /// Evaluate, just look up in AlgebraicDecisonTree
virtual double operator()(const Values& values) const { virtual double operator()(const Values& values) const {
return Potentials::operator()(values); return Potentials::operator()(values);

View File

@ -65,6 +65,8 @@ namespace gtsam {
Conditional(size_t nrFrontals) : nrFrontals_(nrFrontals) {} Conditional(size_t nrFrontals) : nrFrontals_(nrFrontals) {}
/// @} /// @}
public:
/// @name Testable /// @name Testable
/// @{ /// @{
@ -76,7 +78,6 @@ namespace gtsam {
/// @} /// @}
public:
/// @name Standard Interface /// @name Standard Interface
/// @{ /// @{