new helper method in DiscreteBayesNet to compute joint conditional

release/4.3a0
Varun Agrawal 2025-05-14 06:26:56 -04:00
parent 1514a0d62e
commit 4d97136f5c
2 changed files with 21 additions and 4 deletions

View File

@ -71,16 +71,14 @@ DiscreteValues DiscreteBayesNet::sample(DiscreteValues result) const {
/* ************************************************************************* */
// The implementation is: build the entire joint into one factor and then prune.
// TODO(Frank): This can be quite expensive *unless* the factors have already
// NOTE: This can be quite expensive *unless* the factors have already
// been pruned before. Another, possibly faster approach is branch and bound
// search to find the K-best leaves and then create a single pruned conditional.
DiscreteBayesNet DiscreteBayesNet::prune(
size_t maxNrLeaves, const std::optional<double>& marginalThreshold,
DiscreteValues* fixedValues) const {
// Multiply into one big conditional. NOTE: possibly quite expensive.
DiscreteConditional joint;
for (const DiscreteConditional::shared_ptr& conditional : *this)
joint = joint * (*conditional);
DiscreteConditional joint = this->joint();
// Prune the joint. NOTE: imperative and, again, possibly quite expensive.
DiscreteConditional pruned = joint;
@ -122,6 +120,15 @@ DiscreteBayesNet DiscreteBayesNet::prune(
return result;
}
/* *********************************************************************** */
DiscreteConditional DiscreteBayesNet::joint() const {
DiscreteConditional joint;
for (const DiscreteConditional::shared_ptr& conditional : *this)
joint = joint * (*conditional);
return joint;
}
/* *********************************************************************** */
std::string DiscreteBayesNet::markdown(
const KeyFormatter& keyFormatter,

View File

@ -136,6 +136,16 @@ class GTSAM_EXPORT DiscreteBayesNet: public BayesNet<DiscreteConditional> {
const std::optional<double>& marginalThreshold = {},
DiscreteValues* fixedValues = nullptr) const;
/**
* @brief Multiply all conditionals into one big joint conditional
* and return it.
*
* NOTE: possibly quite expensive.
*
* @return DiscreteConditional
*/
DiscreteConditional joint() const;
///@}
/// @name Wrapper support
/// @{