Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TableFactor and TableDistribution #1953

Merged
merged 94 commits into from
Jan 8, 2025

Conversation

varunagrawal
Copy link
Collaborator

@varunagrawal varunagrawal commented Dec 31, 2024

Use the TableFactor for discrete elimination and use a new TableDistribution which maintains an underlying representation of the distribution using the same SparseVector mechanism.

@varunagrawal varunagrawal self-assigned this Dec 31, 2024
gtsam/discrete/DiscreteTableConditional.cpp Outdated Show resolved Hide resolved
gtsam/discrete/DiscreteFactorGraph.cpp Outdated Show resolved Hide resolved
@varunagrawal varunagrawal changed the base branch from hybrid-timing to hybrid-custom-discrete December 31, 2024 20:05
@varunagrawal varunagrawal changed the title TableFactor and DiscreteTableConditional TableFactor and TableDistribution Jan 4, 2025
@varunagrawal varunagrawal requested a review from dellaert January 7, 2025 03:18
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still have some concerns, and several smaller comments. MAybe we should chat about the joint calculation in particular. I did not expect that to break.

gtsam/discrete/DiscreteFactorGraph.cpp Show resolved Hide resolved
@@ -450,11 +450,18 @@ TEST(HybridBayesNet, UpdateDiscreteConditionals) {

DiscreteConditional joint;
for (auto&& conditional : posterior->discreteMarginal()) {
joint = joint * (*conditional);
// The last discrete conditional may be a TableDistribution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code should still work as is.

@@ -20,6 +20,7 @@

#include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/discrete/DiscreteKey.h>
#include <gtsam/discrete/TableDistribution.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Move to cpp to remove header bloat

@@ -41,6 +42,22 @@ bool HybridBayesTree::equals(const This& other, double tol) const {
return Base::equals(other, tol);
}

/* ************************************************************************* */
DiscreteValues HybridBayesTree::discreteMaxProduct(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in DiscreteFactorGraph?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is special behavior leveraging the TableFactor. I don't think we should mess with DiscreteFactorGraph wrt this.

@@ -52,15 +52,24 @@ HybridBayesNet HybridBayesNet::prune(size_t maxNrLeaves) const {
// Multiply into one big conditional. NOTE: possibly quite expensive.
DiscreteConditional joint;
for (auto &&conditional : marginal) {
joint = joint * (*conditional);
// The last discrete conditional may be a TableDistribution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does code no longer work as is?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must have been from before we refactored discrete multiplication.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about how it can still work this way, please? Seems it should be possible. The new code is too complex

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is not working as is because we enabled multiplication for DiscreteFactor::shared_ptr so the result is a DiscreteFactor::shared_ptr which has no concept of frontal variables.

We can either convert this to DiscreteFactors and then convert to a DiscreteConditional at the end, or use a TableDistribution as the type for joint.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just seeing this comment. This will work with the fix we discussed, right? Noticed the comments is older than that...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am starting to wonder if this has been a distraction. The previous HybridBayesNet::TableProduct method worked great for us.

@varunagrawal
Copy link
Collaborator Author

NOTE: I had to override sample in TableDistribution since setting the ADT to null revealed a bug in sampling.

@varunagrawal varunagrawal requested a review from dellaert January 7, 2025 20:50
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dellaert
Copy link
Member

dellaert commented Jan 8, 2025

I’ll let you merge

@varunagrawal varunagrawal merged commit 8cf2123 into hybrid-timing Jan 8, 2025
33 checks passed
@varunagrawal varunagrawal deleted the discrete-table-conditional branch January 8, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants