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,48 +23,62 @@
#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
class DiscreteConditional; class DiscreteConditional;
class VectorValues; class VectorValues;
/* ************************************************************************* */ /* ************************************************************************* */
/** 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>
typedef BayesTreeCliqueBase<DiscreteBayesTreeClique, DiscreteFactorGraph> Base; 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) {}
//** evaluate conditional probability of subtree for given Values */ /// print index signature only
double evaluate(const DiscreteConditional::Values & values) const; void printSignature(
}; const std::string& s = "Clique: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const {
conditional_->printSignature(s, formatter);
}
/* ************************************************************************* */ //** evaluate conditional probability of subtree for given Values */
/** A Bayes tree representing a Discrete density */ double evaluate(const DiscreteConditional::Values& values) const;
class GTSAM_EXPORT DiscreteBayesTree : };
public BayesTree<DiscreteBayesTreeClique>
{
private:
typedef BayesTree<DiscreteBayesTreeClique> Base;
public: /* ************************************************************************* */
typedef DiscreteBayesTree This; /** A Bayes tree representing a Discrete density */
typedef boost::shared_ptr<This> shared_ptr; class GTSAM_EXPORT DiscreteBayesTree
: public BayesTree<DiscreteBayesTreeClique> {
private:
typedef BayesTree<DiscreteBayesTreeClique> Base;
/** Default constructor, creates an empty Bayes tree */ public:
DiscreteBayesTree() {} typedef DiscreteBayesTree This;
typedef boost::shared_ptr<This> shared_ptr;
/** Check equality */ /** Default constructor, creates an empty Bayes tree */
bool equals(const This& other, double tol = 1e-9) const; DiscreteBayesTree() {}
};
} /** Check equality */
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
/// @{ /// @{