undo changes to DiscreteFactorGraph

release/4.3a0
Varun Agrawal 2024-12-31 13:27:55 -05:00
parent 9f85d4cc2d
commit 9cacb9876e
2 changed files with 15 additions and 23 deletions

View File

@ -19,7 +19,6 @@
#include <gtsam/discrete/DiscreteBayesTree.h> #include <gtsam/discrete/DiscreteBayesTree.h>
#include <gtsam/discrete/DiscreteConditional.h> #include <gtsam/discrete/DiscreteConditional.h>
#include <gtsam/discrete/DiscreteTableConditional.h>
#include <gtsam/discrete/DiscreteEliminationTree.h> #include <gtsam/discrete/DiscreteEliminationTree.h>
#include <gtsam/discrete/DiscreteFactorGraph.h> #include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/discrete/DiscreteJunctionTree.h> #include <gtsam/discrete/DiscreteJunctionTree.h>
@ -65,17 +64,10 @@ namespace gtsam {
} }
/* ************************************************************************ */ /* ************************************************************************ */
TableFactor DiscreteFactorGraph::product() const { DecisionTreeFactor DiscreteFactorGraph::product() const {
TableFactor result; DecisionTreeFactor result;
for (const sharedFactor& factor : *this) { for (const sharedFactor& factor : *this) {
if (factor) { if (factor) result = (*factor) * result;
if (auto f = std::dynamic_pointer_cast<TableFactor>(factor)) {
result = result * (*f);
} else if (auto dtf =
std::dynamic_pointer_cast<DecisionTreeFactor>(factor)) {
result = TableFactor(result * (*dtf));
}
}
} }
return result; return result;
} }
@ -124,14 +116,15 @@ namespace gtsam {
* product to prevent underflow. * product to prevent underflow.
* *
* @param factors The factors to multiply as a DiscreteFactorGraph. * @param factors The factors to multiply as a DiscreteFactorGraph.
* @return TableFactor * @return DecisionTreeFactor
*/ */
static TableFactor ProductAndNormalize(const DiscreteFactorGraph& factors) { static DecisionTreeFactor ProductAndNormalize(
const DiscreteFactorGraph& factors) {
// PRODUCT: multiply all factors // PRODUCT: multiply all factors
#if GTSAM_HYBRID_TIMING #if GTSAM_HYBRID_TIMING
gttic_(DiscreteProduct); gttic_(DiscreteProduct);
#endif #endif
TableFactor product = factors.product(); DecisionTreeFactor product = factors.product();
#if GTSAM_HYBRID_TIMING #if GTSAM_HYBRID_TIMING
gttoc_(DiscreteProduct); gttoc_(DiscreteProduct);
#endif #endif
@ -156,11 +149,11 @@ namespace gtsam {
std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> // std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
EliminateForMPE(const DiscreteFactorGraph& factors, EliminateForMPE(const DiscreteFactorGraph& factors,
const Ordering& frontalKeys) { const Ordering& frontalKeys) {
TableFactor product = ProductAndNormalize(factors); DecisionTreeFactor product = ProductAndNormalize(factors);
// max out frontals, this is the factor on the separator // max out frontals, this is the factor on the separator
gttic(max); gttic(max);
TableFactor::shared_ptr max = product.max(frontalKeys); DecisionTreeFactor::shared_ptr max = product.max(frontalKeys);
gttoc(max); gttoc(max);
// Ordering keys for the conditional so that frontalKeys are really in front // Ordering keys for the conditional so that frontalKeys are really in front
@ -173,8 +166,8 @@ namespace gtsam {
// Make lookup with product // Make lookup with product
gttic(lookup); gttic(lookup);
size_t nrFrontals = frontalKeys.size(); size_t nrFrontals = frontalKeys.size();
auto lookup = std::make_shared<DiscreteLookupTable>( auto lookup =
nrFrontals, orderedKeys, product.toDecisionTreeFactor()); std::make_shared<DiscreteLookupTable>(nrFrontals, orderedKeys, product);
gttoc(lookup); gttoc(lookup);
return {std::dynamic_pointer_cast<DiscreteConditional>(lookup), max}; return {std::dynamic_pointer_cast<DiscreteConditional>(lookup), max};
@ -234,13 +227,13 @@ namespace gtsam {
std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> // std::pair<DiscreteConditional::shared_ptr, DiscreteFactor::shared_ptr> //
EliminateDiscrete(const DiscreteFactorGraph& factors, EliminateDiscrete(const DiscreteFactorGraph& factors,
const Ordering& frontalKeys) { const Ordering& frontalKeys) {
TableFactor product = ProductAndNormalize(factors); DecisionTreeFactor product = ProductAndNormalize(factors);
// sum out frontals, this is the factor on the separator // sum out frontals, this is the factor on the separator
#if GTSAM_HYBRID_TIMING #if GTSAM_HYBRID_TIMING
gttic_(EliminateDiscreteSum); gttic_(EliminateDiscreteSum);
#endif #endif
TableFactor::shared_ptr sum = product.sum(frontalKeys); DecisionTreeFactor::shared_ptr sum = product.sum(frontalKeys);
#if GTSAM_HYBRID_TIMING #if GTSAM_HYBRID_TIMING
gttoc_(EliminateDiscreteSum); gttoc_(EliminateDiscreteSum);
#endif #endif
@ -257,7 +250,7 @@ namespace gtsam {
gttic_(EliminateDiscreteToDiscreteConditional); gttic_(EliminateDiscreteToDiscreteConditional);
#endif #endif
auto conditional = auto conditional =
std::make_shared<DiscreteTableConditional>(product, *sum, orderedKeys); std::make_shared<DiscreteConditional>(product, *sum, orderedKeys);
#if GTSAM_HYBRID_TIMING #if GTSAM_HYBRID_TIMING
gttoc_(EliminateDiscreteToDiscreteConditional); gttoc_(EliminateDiscreteToDiscreteConditional);
#endif #endif

View File

@ -25,7 +25,6 @@
#include <gtsam/inference/FactorGraph.h> #include <gtsam/inference/FactorGraph.h>
#include <gtsam/inference/Ordering.h> #include <gtsam/inference/Ordering.h>
#include <gtsam/base/FastSet.h> #include <gtsam/base/FastSet.h>
#include <gtsam/discrete/TableFactor.h>
#include <string> #include <string>
#include <utility> #include <utility>
@ -148,7 +147,7 @@ class GTSAM_EXPORT DiscreteFactorGraph
DiscreteKeys discreteKeys() const; DiscreteKeys discreteKeys() const;
/** return product of all factors as a single factor */ /** return product of all factors as a single factor */
TableFactor product() const; DecisionTreeFactor product() const;
/** /**
* Evaluates the factor graph given values, returns the joint probability of * Evaluates the factor graph given values, returns the joint probability of