Skip to content

Commit

Permalink
major improvement: continuousSeparator no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
dellaert committed Sep 27, 2024
1 parent 7d51e1c commit bc25fce
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions gtsam/hybrid/HybridGaussianFactorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ static std::shared_ptr<Factor> createHybridGaussianFactor(
static std::pair<HybridConditional::shared_ptr, std::shared_ptr<Factor>>
hybridElimination(const HybridGaussianFactorGraph &factors,
const Ordering &frontalKeys,
const KeyVector &continuousSeparator,
const std::set<DiscreteKey> &discreteSeparatorSet) {
// NOTE: since we use the special JunctionTree,
// only possibility is continuous conditioned on discrete.
Expand All @@ -386,13 +385,18 @@ hybridElimination(const HybridGaussianFactorGraph &factors,
factorGraphTree = removeEmpty(factorGraphTree);

// This is the elimination method on the leaf nodes
bool someContinuousLeft = false;
auto eliminate = [&](const GaussianFactorGraph &graph) -> Result {
if (graph.empty()) {
return {nullptr, nullptr};
}

// Expensive elimination of product factor.
auto result = EliminatePreferCholesky(graph, frontalKeys);

// Record whether there any continuous variables left
someContinuousLeft |= !result.second->empty();

return result;
};

Expand All @@ -403,9 +407,9 @@ hybridElimination(const HybridGaussianFactorGraph &factors,
// error for each discrete choice. Otherwise, create a HybridGaussianFactor
// on the separator, taking care to correct for conditional constants.
auto newFactor =
continuousSeparator.empty()
? createDiscreteFactor(eliminationResults, discreteSeparator)
: createHybridGaussianFactor(eliminationResults, discreteSeparator);
someContinuousLeft
? createHybridGaussianFactor(eliminationResults, discreteSeparator)
: createDiscreteFactor(eliminationResults, discreteSeparator);

// Create the HybridGaussianConditional from the conditionals
HybridGaussianConditional::Conditionals conditionals(
Expand Down Expand Up @@ -514,22 +518,12 @@ EliminateHybrid(const HybridGaussianFactorGraph &factors,
// Case 3: We are now in the hybrid land!
KeySet frontalKeysSet(frontalKeys.begin(), frontalKeys.end());

// Find all the keys in the set of continuous keys
// which are not in the frontal keys. This is our continuous separator.
KeyVector continuousSeparator;
auto continuousKeySet = factors.continuousKeySet();
std::set_difference(
continuousKeySet.begin(), continuousKeySet.end(),
frontalKeysSet.begin(), frontalKeysSet.end(),
std::inserter(continuousSeparator, continuousSeparator.begin()));

// Similarly for the discrete separator.
// Find all discrete keys.
// Since we eliminate all continuous variables first,
// the discrete separator will be *all* the discrete keys.
std::set<DiscreteKey> discreteSeparator = factors.discreteKeys();

return hybridElimination(factors, frontalKeys, continuousSeparator,
discreteSeparator);
return hybridElimination(factors, frontalKeys, discreteSeparator);
}
}

Expand Down

0 comments on commit bc25fce

Please sign in to comment.