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

Big Re-"Factor" of Hybrid #1374

Merged
merged 16 commits into from
Jan 8, 2023
Merged

Big Re-"Factor" of Hybrid #1374

merged 16 commits into from
Jan 8, 2023

Conversation

dellaert
Copy link
Member

@dellaert dellaert commented Jan 7, 2023

  • made Factor the common base class
  • this allowed removing the (cumbersome) wrapper classes

Most changes are in HybridGaussianFactorGraph, and the rest is mainly removing files and boilerplate. We also make good use now of emplace_shared.

The dispatch is ugly and possibly slower than vtables (although probably negligible?), so I am also thinking about streamlining even more

@varunagrawal
Copy link
Collaborator

I'm going to need some time to look into this in detail. A bit lost since I thought we need those wrapper classes for type erasure so whatever sorcery happened to eliminate that needs to be well understood for me.

@dellaert
Copy link
Member Author

dellaert commented Jan 7, 2023

I'm going to need some time to look into this in detail. A bit lost since I thought we need those wrapper classes for type erasure so whatever sorcery happened to eliminate that needs to be well understood for me.

It was actually very simple: a HybridFactorGraph is now simply a FG of Factors. You can stick whatever Factor type in there - the idea being that in GTSAM 5 the most general FG is “hybrid”. In the new scheme, the HybridFactor class is just the base class for factors that actually have both discrete and continuous arguments.

The (annoying, boilerplate generating) sorcery was only needed for our older factors to “pretend” to be Hybrid.

Please don’t take too much time? I want to finish this and try to add an error function in the Factor base class in a next PR. And, I also know how we can get rid of the GraphAndConstant thing.

@dellaert
Copy link
Member Author

dellaert commented Jan 7, 2023

Just removed remaining emplace_discrete and emplace_hybrid, which are now redundant: emplace_shared can be used for all factor types.

gtsam/hybrid/GaussianMixture.cpp Show resolved Hide resolved
gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
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.

To be honest, I am not a fan of this update. We're sacrificing a lot of readability and code-convenience just for the sake of removing a few "add/insert" methods which seems like a skewed pros-to-cons ratio.

I think the correct way forward that helps maintain all the readability is to make HybridFactor a superclass of DiscreteFactor, GaussianFactor and NonlinearFactor (basically every factor that the HybridFactorGraph is capable of handling currently). This will result in clean code but with the desired API design.

This is important going forward since if Hybrid is to be the de-facto mechanism, we don't want contributors balking at the current complexity and difficulty in parsing the code (especially if they're not proficient in C++ at your level, which is pretty darn high).

gtsam/hybrid/HybridFactorGraph.h Show resolved Hide resolved
std::string("factor is discrete in continuous elimination ") +
demangle(typeid(fr).name()));
}
// TODO(dellaert): there was an unattributed comment here: We need to
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unattributed comment?
I believe the comment is misleading. It should be

// Encountered a BayesTreeOrphanWrapper in continuous elimination, so throw error.

Unless I am missing something.

Copy link
Member Author

Choose a reason for hiding this comment

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

Normally, TODOs come with a username so we can ask who wrote the comment. I have no idea whether something needs to happen here or not, or it should be ignored.

gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
gtsam/hybrid/HybridGaussianFactorGraph.cpp Show resolved Hide resolved
gtsam/hybrid/HybridNonlinearFactorGraph.cpp Show resolved Hide resolved
@dellaert
Copy link
Member Author

dellaert commented Jan 8, 2023

To be honest, I am not a fan of this update. We're sacrificing a lot of readability and code-convenience just for the sake of removing a few "add/insert" methods which seems like a skewed pros-to-cons ratio.

I disagree. I think everything become much simpler, and simpler to understand. The new style: just use emplace_shared. It names the thing it is inserting, and avoids a copy. Examples abound in the unit tests, which is what people copy, anyway. The previous code was baroque.

@varunagrawal
Copy link
Collaborator

I disagree. I think everything become much simpler, and simpler to understand. The new style: just use emplace_shared. It names the thing it is inserting, and avoids a copy. Examples abound in the unit tests, which is what people copy, anyway. The previous code was baroque.

I agree there was a lot of extra fat in the previous code, and this PR is a wonderful for addressing that! But all the pointer up- and down-casting is what I am referring to as being really confusing and less readable, while there exists an alternative that gives us the best of both worlds.

@dellaert
Copy link
Member Author

dellaert commented Jan 8, 2023

I disagree. I think everything become much simpler, and simpler to understand. The new style: just use emplace_shared. It names the thing it is inserting, and avoids a copy. Examples abound in the unit tests, which is what people copy, anyway. The previous code was baroque.

I agree there was a lot of extra fat in the previous code, and this PR is a wonderful for addressing that! But all the pointer up- and down-casting is what I am referring to as being really confusing and less readable, while there exists an alternative that gives us the best of both worlds.

I hear you. But (a) that code is mostly internal and pretty localized, (b) I have a follow-up PR that at least removes some of the up-casting. Note virtual methods are just cleaner upcasting.

# Conflicts:
#	gtsam/hybrid/HybridGaussianFactorGraph.cpp
#	gtsam/hybrid/tests/testHybridGaussianFactorGraph.cpp
#	python/gtsam/tests/test_HybridNonlinearFactorGraph.py
@dellaert dellaert merged commit 00dc32d into develop Jan 8, 2023
@dellaert dellaert deleted the hybrid/refactor branch January 8, 2023 17:23
@dellaert dellaert added this to the Hybrid Inference milestone Feb 7, 2023
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.

3 participants