use optional DiscreteValues

release/4.3a0
Varun Agrawal 2024-11-03 15:32:21 -05:00
parent 8aacfa95f3
commit 5c63ac833c
2 changed files with 18 additions and 30 deletions

View File

@ -198,29 +198,24 @@ AlgebraicDecisionTree<Key> HybridBayesNet::errorTree(
} }
/* ************************************************************************* */ /* ************************************************************************* */
double HybridBayesNet::negLogConstant() const { double HybridBayesNet::negLogConstant(
const std::optional<DiscreteValues> &discrete) const {
double negLogNormConst = 0.0; double negLogNormConst = 0.0;
// Iterate over each conditional. // Iterate over each conditional.
for (auto &&conditional : *this) { for (auto &&conditional : *this) {
negLogNormConst += conditional->negLogConstant(); if (discrete.has_value()) {
} if (auto gm = conditional->asHybrid()) {
return negLogNormConst; negLogNormConst += gm->choose(*discrete)->negLogConstant();
} } else if (auto gc = conditional->asGaussian()) {
negLogNormConst += gc->negLogConstant();
/* ************************************************************************* */ } else if (auto dc = conditional->asDiscrete()) {
double HybridBayesNet::negLogConstant(const DiscreteValues &discrete) const { negLogNormConst += dc->choose(*discrete)->negLogConstant();
double negLogNormConst = 0.0; } else {
// Iterate over each conditional. throw std::runtime_error(
for (auto &&conditional : *this) { "Unknown conditional type when computing negLogConstant");
if (auto gm = conditional->asHybrid()) { }
negLogNormConst += gm->choose(discrete)->negLogConstant();
} else if (auto gc = conditional->asGaussian()) {
negLogNormConst += gc->negLogConstant();
} else if (auto dc = conditional->asDiscrete()) {
negLogNormConst += dc->choose(discrete)->negLogConstant();
} else { } else {
throw std::runtime_error( negLogNormConst += conditional->negLogConstant();
"Unknown conditional type when computing negLogConstant");
} }
} }
return negLogNormConst; return negLogNormConst;

View File

@ -237,22 +237,15 @@ class GTSAM_EXPORT HybridBayesNet : public BayesNet<HybridConditional> {
using BayesNet::logProbability; // expose HybridValues version using BayesNet::logProbability; // expose HybridValues version
/**
* @brief Get the negative log of the normalization constant corresponding
* to the joint density represented by this Bayes net.
*
* @return double
*/
double negLogConstant() const;
/** /**
* @brief Get the negative log of the normalization constant * @brief Get the negative log of the normalization constant
* corresponding to the joint Gaussian density represented by * corresponding to the joint density represented by this Bayes net.
* this Bayes net indexed by `discrete`. * Optionally index by `discrete`.
* *
* @param discrete Optional DiscreteValues
* @return double * @return double
*/ */
double negLogConstant(const DiscreteValues &discrete) const; double negLogConstant(const std::optional<DiscreteValues> &discrete) const;
/** /**
* @brief Compute normalized posterior P(M|X=x) and return as a tree. * @brief Compute normalized posterior P(M|X=x) and return as a tree.