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

Search for k-best in factor graph #2006

Merged
merged 12 commits into from
Jan 28, 2025
Merged

Search for k-best in factor graph #2006

merged 12 commits into from
Jan 28, 2025

Conversation

dellaert
Copy link
Member

This PR builds upon #2005, now enabling constructing a DiscreteSearch instance from a factor graph. This avoids having to do costly inference to transform into a Bayes net or Bayes tree. There are two versions:

  • one that groups factors using an elimination tree, one per variable in the given ordering
  • one that additionally groups the factors according to cliques, multi-frontal style

There is still a bit of compute that happens in the constructor: factors get multiplied together for each "search slot". So, this does the "product" of "sum-product", but not the sum, not the division to get a conditional.

The choice between e-tree or j-tree is documented:

  /**
   * Construct from a DiscreteFactorGraph.
   *
   * Internally creates either an elimination tree or a junction tree. The
   * latter incurs more up-front computation but the search itself might be
   * faster. Then again, for the elimination tree, the heuristic will be more
   * fine-grained (more slots).
   *
   * @param factorGraph The factor graph to search over.
   * @param ordering The ordering used to create etree (and maybe jtree).
   * @param buildJunctionTree Whether to build a junction tree or not.
   */
  static DiscreteSearch FromFactorGraph(const DiscreteFactorGraph& factorGraph,
                                        const Ordering& ordering,
                                        bool buildJunctionTree = false);

Testing:

All unit tests pass, and all variants give the same answer for Asia, for both MPE and 4-best solutions:

  auto fromETree =
      DiscreteSearch::FromFactorGraph(asia::factorGraph, asia::ordering);
  auto fromJunctionTree =
      DiscreteSearch::FromFactorGraph(asia::factorGraph, asia::ordering, true);
  const DiscreteSearch fromBayesNet(asia::bayesNet);
  const DiscreteSearch fromBayesTree(asia::bayesTree);

  for (auto& search :
       {fromETree, fromJunctionTree, fromBayesNet, fromBayesTree}) {
    // Ask for the MPE
    auto mpe = search.run();

    EXPECT_LONGS_EQUAL(1, mpe.size());
    // Regression test: check the MPE solution
    EXPECT_DOUBLES_EQUAL(1.236627, std::fabs(mpe[0].error), 1e-5);

    // Check it is equal to MPE via inference
    EXPECT(assert_equal(asia::mpe, mpe[0].assignment));

    // Ask for top 4 solutions
    auto solutions = search.run(4);

    EXPECT_LONGS_EQUAL(4, solutions.size());
    // Regression test: check the first and last solution
    EXPECT_DOUBLES_EQUAL(1.236627, std::fabs(solutions[0].error), 1e-5);
    EXPECT_DOUBLES_EQUAL(2.201708, std::fabs(solutions[3].error), 1e-5);
  }

@dellaert dellaert requested a review from varunagrawal January 28, 2025 00:16
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 modulo some minor comments.

gtsam/discrete/DiscreteJunctionTree.cpp Outdated Show resolved Hide resolved
gtsam/discrete/DiscreteSearch.cpp Outdated Show resolved Hide resolved
gtsam/discrete/DiscreteSearch.h Outdated Show resolved Hide resolved
Base automatically changed from feature/k-best to develop January 28, 2025 19:09
@dellaert dellaert merged commit 1daca19 into develop Jan 28, 2025
35 checks passed
@dellaert dellaert deleted the feature/k_best_fg branch January 28, 2025 22:06
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