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

Fix visitWith for Pruned Tree #1135

Merged
merged 3 commits into from
Mar 18, 2022
Merged

Fix visitWith for Pruned Tree #1135

merged 3 commits into from
Mar 18, 2022

Conversation

varunagrawal
Copy link
Collaborator

@varunagrawal varunagrawal commented Mar 18, 2022

Added a unit test demonstrating a bug in partial assignments of visitWith and added a fix for it.

For example, if we have a pruned tree like below:

 Choice(C)
 0 Choice(B)
 0 0 Leaf 0
 0 1 Choice(A)
 0 1 0 Leaf 2
 0 1 1 Leaf 3
 1 Choice(B)
 1 0 Leaf 4
 1 1 Choice(A)
 1 1 0 Leaf 6
 1 1 1 Leaf 7

if we look at all the assignments that get generated by visitWith, we get

>>: (B, 0)(C, 0)
>>: (A, 0)(B, 1)(C, 0)
>>: (A, 1)(B, 1)(C, 0)
>>: (A, 1)(B, 0)(C, 1)  // This is WRONG!
>>: (A, 0)(B, 1)(C, 1)
>>: (A, 1)(B, 1)(C, 1)

and we should instead get

>>: (B, 0)(C, 0)
>>: (A, 0)(B, 1)(C, 0)
>>: (A, 1)(B, 1)(C, 0)
>>: (B, 0)(C, 1)  // This is CORRECT.
>>: (A, 0)(B, 1)(C, 1)
>>: (A, 1)(B, 1)(C, 1)

@varunagrawal varunagrawal added the bugfix Fixes an issue or bug label Mar 18, 2022
@varunagrawal varunagrawal self-assigned this Mar 18, 2022
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.

Good find! Please see whether my suggested fix works, as I think that will not touch the complexity.

@@ -20,13 +20,11 @@
// #define DT_DEBUG_MEMORY
// #define DT_NO_PRUNING
#define DISABLE_DOT
#include <gtsam/discrete/DecisionTree-inl.h>

#include <CppUnitLite/TestHarness.h>
Copy link
Member

Choose a reason for hiding this comment

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

Include order convention in gtsam is near -> far. It was already correct except all discrete includes should precede base includes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Auto-format oopsie

(*this)(choice->branches()[i]); // recurse!
choices[choice->label()] = i; // Set assignment for label to i

VisitWith<L, Y> visit(f);
Copy link
Member

Choose a reason for hiding this comment

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

That seems rather heavy handed. Can we not fix the issue simply by removing the choice after the recursive call? That we way we are not copying binary trees all over the place. Worried that the complexity of this method just became O(n^2) rather than O(n).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Gotcha. The VisitWith struct is a pretty lightweight datastructure so I figured a functional recursive implementation would be alright. Let me update it.

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.

Cool ! Thanks for finding the bug and fixing it!

@varunagrawal varunagrawal merged commit e5fb4cd into develop Mar 18, 2022
@varunagrawal varunagrawal deleted the fix/visitWith branch March 18, 2022 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fixes an issue or bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants