Skip to content

Commit

Permalink
use optional DiscreteValues
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal committed Nov 3, 2024
1 parent 8aacfa9 commit 5c63ac8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
33 changes: 14 additions & 19 deletions gtsam/hybrid/HybridBayesNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// Iterate over each conditional.
for (auto &&conditional : *this) {
negLogNormConst += conditional->negLogConstant();
}
return negLogNormConst;
}

/* ************************************************************************* */
double HybridBayesNet::negLogConstant(const DiscreteValues &discrete) const {
double negLogNormConst = 0.0;
// Iterate over each conditional.
for (auto &&conditional : *this) {
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();
if (discrete.has_value()) {
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 {
throw std::runtime_error(
"Unknown conditional type when computing negLogConstant");
}
} else {
throw std::runtime_error(
"Unknown conditional type when computing negLogConstant");
negLogNormConst += conditional->negLogConstant();
}
}
return negLogNormConst;
Expand Down
15 changes: 4 additions & 11 deletions gtsam/hybrid/HybridBayesNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,15 @@ class GTSAM_EXPORT HybridBayesNet : public BayesNet<HybridConditional> {

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
* corresponding to the joint Gaussian density represented by
* this Bayes net indexed by `discrete`.
* corresponding to the joint density represented by this Bayes net.
* Optionally index by `discrete`.
*
* @param discrete Optional DiscreteValues
* @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.
Expand Down

0 comments on commit 5c63ac8

Please sign in to comment.