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

Improved HybridBayesNet API #1823

Merged
merged 10 commits into from
Sep 6, 2024
Merged

Improved HybridBayesNet API #1823

merged 10 commits into from
Sep 6, 2024

Conversation

varunagrawal
Copy link
Collaborator

  1. Support adding both pointer and non-pointer types to the HybridBayesNet.
  2. Break up HybridValues into .h and .cpp.
  3. Update tests.

@varunagrawal varunagrawal self-assigned this Sep 5, 2024
@dellaert
Copy link
Member

dellaert commented Sep 5, 2024

Hmmm. If anything I want to get away from adding non-pointer types to FG/BN. There is just a useless copy and another allocation. The preferred style to promote is push_pack_shared<T> or whatever it is called in the base factor graph class. The only reason const& versions still exist right now is backwards compatibility- I’d rather change all examples and tests to use the different style.

@varunagrawal
Copy link
Collaborator Author

I don't see a push_back_shared method, just an emplace_shared.

My main motivation for this was to be able to add shared pointers to the hybrid Bayes net. Currently we can only add raw pointers.

Adding support for non-pointer types is syntactic sugar, which makes tests less verbose. My hope is that the user is cognizant of the difference.

I'll remove the const& on the shared pointer version and attempt to add emplace_shared as an additional method. Would that work? Or do you want me to get rid of the non pointer version altogether?

@dellaert
Copy link
Member

dellaert commented Sep 5, 2024

The only things that should be possible going forward are

  • push_back a shared pointer
  • emplace_shared which creates and pushes back

KeyVector frontalKeys{z}, continuousParents;
DiscreteKeys discreteParents{m};
std::vector<GaussianConditional::shared_ptr> conditionals = {c0, c1};
auto gm = make_shared<GaussianMixture>(frontalKeys, continuousParents,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dellaert clang is having a hard time figuring out the constructor for the shared pointer if I use ({z}, {}, {m}, {c0, c1}). It's so weird.

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 seems like it is an issue regarding lvalues.

@varunagrawal
Copy link
Collaborator Author

The only things that should be possible going forward are

  • push_back a shared pointer
  • emplace_shared which creates and pushes back

Okay done.

}

/**
* Preferred: add a conditional directly using a pointer.
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm, no, emplace_shared should be preferred. I don’t think this raw pointer call fits anything we have ever done in GTSAM.

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 was pre-existing and confusing to me as well. I'm going to remove it.

@dellaert
Copy link
Member

dellaert commented Sep 6, 2024

The only things that should be possible going forward are

  • push_back a shared pointer
  • emplace_shared which creates and pushes back

Okay done.

I don’t see that. Can you check emplace_shared in FactorGraph.h ?

* @param conditional The conditional as a shared pointer.
*/
template <class CONDITIONAL>
void push_back(std::shared_ptr<CONDITIONAL> conditional) {
Copy link
Member

Choose a reason for hiding this comment

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

This is good. Although check what we do in GaussianBayesNet. I think we use const& to a shared pointer there. My comment before about const& was about a ref to the underlying object, not the shared pointer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

GaussianBayesNet inherits from FactorGraph which has this method (which I followed), so no const& for the shared pointer:

/// Add a factor directly using a shared_ptr.
template <class DERIVEDFACTOR>
IsDerived<DERIVEDFACTOR> push_back(std::shared_ptr<DERIVEDFACTOR> factor) {
  factors_.push_back(std::shared_ptr<FACTOR>(factor));
}

I should add const& since it provides a small performance benefit.

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.

Great!!!

@varunagrawal varunagrawal merged commit caf85c2 into develop Sep 6, 2024
34 checks passed
@varunagrawal varunagrawal deleted the improved-hybrid-api branch September 6, 2024 16:23
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