-
Notifications
You must be signed in to change notification settings - Fork 779
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
Hybrid Pruning #1309
Merged
Merged
Hybrid Pruning #1309
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d387d2c
update switching example to accept measurements in constructor
varunagrawal 40d3865
rename discreteFactor to decisionTree
varunagrawal 82f328b
expose DiscreteKeysAsSet as a function
varunagrawal 949958d
new updateDiscreteConditionals method for after we prune
varunagrawal cc78a14
HybridSmoother based on BayesNet
varunagrawal 453870e
unit test for end-2-end hybrid estimation
varunagrawal 58a15c3
remove extraneous tictoc print
varunagrawal 74d6f05
add assertions for HybridEstimation example
varunagrawal 0b79399
simplify conversion of root discrete conditional
varunagrawal ebb29ef
Make Switching test fixture 0-indexed to match iMHS paper
varunagrawal 7f8bd8a
Merge pull request #1311 from borglab/hybrid/switching-0-index
varunagrawal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
|
||
* See LICENSE for the license information | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file HybridSmoother.cpp | ||
* @brief An incremental smoother for hybrid factor graphs | ||
* @author Varun Agrawal | ||
* @date October 2022 | ||
*/ | ||
|
||
#include <gtsam/hybrid/HybridSmoother.h> | ||
|
||
#include <algorithm> | ||
#include <unordered_set> | ||
|
||
namespace gtsam { | ||
|
||
/* ************************************************************************* */ | ||
void HybridSmoother::update(HybridGaussianFactorGraph graph, | ||
const Ordering &ordering, | ||
boost::optional<size_t> maxNrLeaves) { | ||
// Add the necessary conditionals from the previous timestep(s). | ||
std::tie(graph, hybridBayesNet_) = | ||
addConditionals(graph, hybridBayesNet_, ordering); | ||
|
||
// Eliminate. | ||
auto bayesNetFragment = graph.eliminateSequential(ordering); | ||
|
||
/// Prune | ||
if (maxNrLeaves) { | ||
// `pruneBayesNet` sets the leaves with 0 in discreteFactor to nullptr in | ||
// all the conditionals with the same keys in bayesNetFragment. | ||
HybridBayesNet prunedBayesNetFragment = | ||
bayesNetFragment->prune(*maxNrLeaves); | ||
// Set the bayes net fragment to the pruned version | ||
bayesNetFragment = | ||
boost::make_shared<HybridBayesNet>(prunedBayesNetFragment); | ||
} | ||
|
||
// Add the partial bayes net to the posterior bayes net. | ||
hybridBayesNet_.push_back<HybridBayesNet>(*bayesNetFragment); | ||
} | ||
|
||
/* ************************************************************************* */ | ||
std::pair<HybridGaussianFactorGraph, HybridBayesNet> | ||
HybridSmoother::addConditionals(const HybridGaussianFactorGraph &originalGraph, | ||
const HybridBayesNet &originalHybridBayesNet, | ||
const Ordering &ordering) const { | ||
HybridGaussianFactorGraph graph(originalGraph); | ||
HybridBayesNet hybridBayesNet(originalHybridBayesNet); | ||
|
||
// If we are not at the first iteration, means we have conditionals to add. | ||
if (!hybridBayesNet.empty()) { | ||
// We add all relevant conditional mixtures on the last continuous variable | ||
// in the previous `hybridBayesNet` to the graph | ||
|
||
// Conditionals to remove from the bayes net | ||
// since the conditional will be updated. | ||
std::vector<HybridConditional::shared_ptr> conditionals_to_erase; | ||
|
||
// New conditionals to add to the graph | ||
gtsam::HybridBayesNet newConditionals; | ||
|
||
// NOTE(Varun) Using a for-range loop doesn't work since some of the | ||
// conditionals are invalid pointers | ||
for (size_t i = 0; i < hybridBayesNet.size(); i++) { | ||
auto conditional = hybridBayesNet.at(i); | ||
|
||
for (auto &key : conditional->frontals()) { | ||
if (std::find(ordering.begin(), ordering.end(), key) != | ||
ordering.end()) { | ||
newConditionals.push_back(conditional); | ||
conditionals_to_erase.push_back(conditional); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
// Remove conditionals at the end so we don't affect the order in the | ||
// original bayes net. | ||
for (auto &&conditional : conditionals_to_erase) { | ||
auto it = find(hybridBayesNet.begin(), hybridBayesNet.end(), conditional); | ||
hybridBayesNet.erase(it); | ||
} | ||
|
||
graph.push_back(newConditionals); | ||
// newConditionals.print("\n\n\nNew Conditionals to add back"); | ||
} | ||
return {graph, hybridBayesNet}; | ||
} | ||
|
||
/* ************************************************************************* */ | ||
GaussianMixture::shared_ptr HybridSmoother::gaussianMixture( | ||
size_t index) const { | ||
return boost::dynamic_pointer_cast<GaussianMixture>( | ||
hybridBayesNet_.at(index)); | ||
} | ||
|
||
/* ************************************************************************* */ | ||
const HybridBayesNet &HybridSmoother::hybridBayesNet() const { | ||
return hybridBayesNet_; | ||
} | ||
|
||
} // namespace gtsam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
|
||
* See LICENSE for the license information | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file HybridSmoother.h | ||
* @brief An incremental smoother for hybrid factor graphs | ||
* @author Varun Agrawal | ||
* @date October 2022 | ||
*/ | ||
|
||
#include <gtsam/discrete/DiscreteFactorGraph.h> | ||
#include <gtsam/hybrid/HybridBayesNet.h> | ||
#include <gtsam/hybrid/HybridGaussianFactorGraph.h> | ||
|
||
namespace gtsam { | ||
|
||
class HybridSmoother { | ||
private: | ||
HybridBayesNet hybridBayesNet_; | ||
HybridGaussianFactorGraph remainingFactorGraph_; | ||
|
||
public: | ||
/** | ||
* Given new factors, perform an incremental update. | ||
* The relevant densities in the `hybridBayesNet` will be added to the input | ||
* graph (fragment), and then eliminated according to the `ordering` | ||
* presented. The remaining factor graph contains Gaussian mixture factors | ||
* that are not connected to the variables in the ordering, or a single | ||
* discrete factor on all discrete keys, plus all discrete factors in the | ||
* original graph. | ||
* | ||
* \note If maxComponents is given, we look at the discrete factor resulting | ||
* from this elimination, and prune it and the Gaussian components | ||
* corresponding to the pruned choices. | ||
* | ||
* @param graph The new factors, should be linear only | ||
* @param ordering The ordering for elimination, only continuous vars are | ||
* allowed | ||
* @param maxNrLeaves The maximum number of leaves in the new discrete factor, | ||
* if applicable | ||
*/ | ||
void update(HybridGaussianFactorGraph graph, const Ordering& ordering, | ||
boost::optional<size_t> maxNrLeaves = boost::none); | ||
|
||
/** | ||
* @brief Add conditionals from previous timestep as part of liquefication. | ||
* | ||
* @param graph The new factor graph for the current time step. | ||
* @param hybridBayesNet The hybrid bayes net containing all conditionals so | ||
* far. | ||
* @param ordering The elimination ordering. | ||
* @return std::pair<HybridGaussianFactorGraph, HybridBayesNet> | ||
*/ | ||
std::pair<HybridGaussianFactorGraph, HybridBayesNet> addConditionals( | ||
const HybridGaussianFactorGraph& graph, | ||
const HybridBayesNet& hybridBayesNet, const Ordering& ordering) const; | ||
|
||
/// Get the Gaussian Mixture from the Bayes Net posterior at `index`. | ||
GaussianMixture::shared_ptr gaussianMixture(size_t index) const; | ||
|
||
/// Return the Bayes Net posterior. | ||
const HybridBayesNet& hybridBayesNet() const; | ||
}; | ||
|
||
}; // namespace gtsam |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I don't think this is possible in a Hybrid Bayes Net?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is! I was able to get a simple example up and running with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no other explanation why a for-range loop doesn't work, unfortunately...