Skip to content

Commit

Permalink
Revert "simplify to remove DiscreteProduct static function"
Browse files Browse the repository at this point in the history
This reverts commit 453059b.
  • Loading branch information
varunagrawal committed Jan 5, 2025
1 parent a02baec commit 13bafb0
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions gtsam/discrete/DiscreteFactorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,11 @@ namespace gtsam {

/* ************************************************************************ */
DecisionTreeFactor DiscreteFactorGraph::product() const {
// PRODUCT: multiply all factors
gttic(product);
DiscreteFactor::shared_ptr product = *this->begin();
DiscreteFactor::shared_ptr result = *this->begin();
for (auto it = this->begin() + 1; it != this->end(); ++it) {
if (*it) product = product->multiply(*it);
if (*it) result = result->multiply(*it);
}
gttoc(product);

DecisionTreeFactor = result->toDecisionTreeFactor();

// Max over all the potentials by pretending all keys are frontal:
auto denominator = product.max(product.size());

// Normalize the product factor to prevent underflow.
product = product / (*denominator);

return product;
return result->toDecisionTreeFactor();
}

/* ************************************************************************ */
Expand Down Expand Up @@ -123,12 +111,34 @@ namespace gtsam {
// }
// }

/**
* @brief Multiply all the `factors`.
*
* @param factors The factors to multiply as a DiscreteFactorGraph.
* @return DecisionTreeFactor
*/
static DecisionTreeFactor DiscreteProduct(
const DiscreteFactorGraph& factors) {
// PRODUCT: multiply all factors
gttic(product);
DecisionTreeFactor product = factors.product();
gttoc(product);

// Max over all the potentials by pretending all keys are frontal:
auto denominator = product.max(product.size());

// Normalize the product factor to prevent underflow.
product = product / (*denominator);

return product;
}

/* ************************************************************************ */
// Alternate eliminate function for MPE
std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
EliminateForMPE(const DiscreteFactorGraph& factors,
const Ordering& frontalKeys) {
DecisionTreeFactor product = factors.product();
DecisionTreeFactor product = DiscreteProduct(factors);

// max out frontals, this is the factor on the separator
gttic(max);
Expand Down Expand Up @@ -206,7 +216,7 @@ namespace gtsam {
std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
EliminateDiscrete(const DiscreteFactorGraph& factors,
const Ordering& frontalKeys) {
DecisionTreeFactor product = factors.product();
DecisionTreeFactor product = DiscreteProduct(factors);

// sum out frontals, this is the factor on the separator
gttic(sum);
Expand Down

0 comments on commit 13bafb0

Please sign in to comment.