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

Re-linearization #2017

Merged
merged 10 commits into from
Feb 3, 2025
Merged

Re-linearization #2017

merged 10 commits into from
Feb 3, 2025

Conversation

dellaert
Copy link
Member

@dellaert dellaert commented Feb 2, 2025

  • extensive implementation of restrict and re-linearization
  • some are not implemented, which is ok for now.

@dellaert dellaert requested a review from varunagrawal February 2, 2025 17:08
@dellaert
Copy link
Member Author

dellaert commented Feb 3, 2025

Yay, @varunagrawal , tests finally pass. Had to fix some stragglers.

Copy link
Collaborator

@varunagrawal varunagrawal left a comment

Choose a reason for hiding this comment

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

LGTM. I can address the comments in my own PR or a follow up.

@@ -153,7 +153,8 @@ class GTSAM_EXPORT HybridConditional
* @return HybridGaussianConditional::shared_ptr otherwise
*/
HybridGaussianConditional::shared_ptr asHybrid() const {
return std::dynamic_pointer_cast<HybridGaussianConditional>(inner_);
if (!isHybrid()) return nullptr;
return std::static_pointer_cast<HybridGaussianConditional>(inner_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wow we've been doing it wrong for a while now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually do we want this? If we call asHybrid on a non-hybrid factor, it should error out. I recommend updating the (!isHybrid()) check to throw an exception perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, disagree. I did not change the semantics here, just the performance. I like these semantics.

@@ -162,7 +163,8 @@ class GTSAM_EXPORT HybridConditional
* @return GaussianConditional::shared_ptr otherwise
*/
GaussianConditional::shared_ptr asGaussian() const {
return std::dynamic_pointer_cast<GaussianConditional>(inner_);
if (!isContinuous()) return nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Preferably throw exception instead of returning a nullptr since this can be a silent error/failure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope

@@ -172,7 +174,8 @@ class GTSAM_EXPORT HybridConditional
*/
template <typename T = DiscreteConditional>
typename T::shared_ptr asDiscrete() const {
return std::dynamic_pointer_cast<T>(inner_);
if (!isDiscrete()) return nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Preferably throw exception instead of returning a nullptr since this can be a silent error/failure.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, @varunagrawal , this is used to find out whether it is of this type. I don’t think we should mess with that semantics

gtsam/hybrid/HybridNonlinearFactor.h Show resolved Hide resolved
} else if (auto df = dynamic_pointer_cast<DiscreteFactor>(f)) {
result.push_back(df->restrict(discreteValues));
} else {
result.push_back(f); // Everything else is just added as is
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a TODO here. Potentially lots of performance left on the table here, particularly in HybridNonlinearISAM where we keep track of the nonlinear factors and relinearize.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe. These are shared pointers so not costly here

/* ************************************************************************ */
DiscreteFactor::shared_ptr DecisionTreeFactor::restrict(
const DiscreteValues& assignment) const {
throw std::runtime_error("DecisionTreeFactor::restrict not implemented");
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should just be Base::restrict and convert to a factor, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, is already a factor, so it should restrict using the fact that it is a decision tree, and then return a restricted decision tree. Base::restrict can’t do that.

@varunagrawal
Copy link
Collaborator

Merging so I can merge into my own PR.

@varunagrawal varunagrawal merged commit 814a734 into develop Feb 3, 2025
35 checks passed
@dellaert dellaert deleted the feature/city_relin branch February 3, 2025 20:22
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