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

Discrete Elimination Refactor #1919

Merged
merged 66 commits into from
Jan 7, 2025
Merged

Conversation

varunagrawal
Copy link
Collaborator

Refactor discrete elimination so it is agnostic to the underlying derived type of the DiscreteFactor.

virtual DecisionTreeFactor operator*(const DecisionTreeFactor&) const = 0;
/// Multiply in a DiscreteFactor and return the result as
/// DiscreteFactor
virtual DiscreteFactor::shared_ptr operator*(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should this be a DiscreteFactor::shared_ptr or a DiscreteFactor& (aka a reference?)

@varunagrawal varunagrawal requested a review from dellaert January 5, 2025 18:42
@varunagrawal varunagrawal changed the base branch from develop to discrete-multiply January 5, 2025 23:21
@varunagrawal varunagrawal marked this pull request as ready for review January 6, 2025 01:51
@varunagrawal varunagrawal self-assigned this Jan 6, 2025
@dellaert
Copy link
Member

dellaert commented Jan 6, 2025

Maybe only push when you want a review?

@varunagrawal
Copy link
Collaborator Author

The second push was because of your comments on the multiply PR. Making your life easier.

@varunagrawal varunagrawal changed the base branch from discrete-multiply to develop January 6, 2025 20:22
@varunagrawal
Copy link
Collaborator Author

@dellaert this is ready for review

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.

Awesomesauce. Some minor comments

@@ -45,7 +45,8 @@ template class GTSAM_EXPORT
/* ************************************************************************** */
DiscreteConditional::DiscreteConditional(const size_t nrFrontals,
Copy link
Member

Choose a reason for hiding this comment

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

Can this now be a DiscreetFactor& ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes it can!

@@ -45,7 +45,8 @@ template class GTSAM_EXPORT
/* ************************************************************************** */
DiscreteConditional::DiscreteConditional(const size_t nrFrontals,
const DecisionTreeFactor& f)
: BaseFactor(f / (*f.sum(nrFrontals))), BaseConditional(nrFrontals) {}
: BaseFactor(f / f.sum(nrFrontals)->toDecisionTreeFactor()),
Copy link
Member

Choose a reason for hiding this comment

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

Is that a no-op if already a DecisionTreeFactor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not actually. In the simple case where there are no parents, we'll get a DecisionTreeFactor with a single value as a DiscreteFactor::shared_ptr. We can cast it explicitly, but I used toDecisionTreeFactor to do it instead as a shortcut.

/* ************************************************************************ */
DiscreteFactor::shared_ptr DecisionTreeFactor::operator/(
const DiscreteFactor::shared_ptr& f) const {
if (auto tf = std::dynamic_pointer_cast<TableFactor>(f)) {
Copy link
Member

Choose a reason for hiding this comment

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

Add comments

@@ -111,15 +111,15 @@ TEST(DecisionTreeFactor, sum_max) {
DecisionTreeFactor f1(v0 & v1, "1 2 3 4 5 6");

DecisionTreeFactor expected(v1, "9 12");
DecisionTreeFactor::shared_ptr actual = f1.sum(1);
auto actual = std::dynamic_pointer_cast<DecisionTreeFactor>(f1.sum(1));
CHECK(assert_equal(expected, *actual, 1e-5));
Copy link
Member

Choose a reason for hiding this comment

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

Add CHECK(actual);

CHECK(assert_equal(expected, *actual, 1e-5));

DecisionTreeFactor expected2(v1, "5 6");
DecisionTreeFactor::shared_ptr actual2 = f1.max(1);
auto actual2 = std::dynamic_pointer_cast<DecisionTreeFactor>(f1.max(1));
Copy link
Member

Choose a reason for hiding this comment

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

same

CHECK(assert_equal(expected2, *actual2));

DecisionTreeFactor f2(v1 & v0, "1 2 3 4 5 6");
DecisionTreeFactor::shared_ptr actual22 = f2.sum(1);
auto actual22 = std::dynamic_pointer_cast<DecisionTreeFactor>(f2.sum(1));
Copy link
Member

Choose a reason for hiding this comment

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

same

@@ -303,15 +305,15 @@ TEST(TableFactor, sum_max) {
TableFactor f1(v0 & v1, "1 2 3 4 5 6");

TableFactor expected(v1, "9 12");
TableFactor::shared_ptr actual = f1.sum(1);
auto actual = std::dynamic_pointer_cast<TableFactor>(f1.sum(1));
Copy link
Member

Choose a reason for hiding this comment

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

Check all casts !

@varunagrawal varunagrawal merged commit 82d0ebc into develop Jan 7, 2025
33 checks passed
@varunagrawal varunagrawal deleted the discrete-elimination-refactor branch January 7, 2025 01:34
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