-
Notifications
You must be signed in to change notification settings - Fork 803
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 solutions #2005
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4027e86
stream operator
dellaert 2808982
Best-K search, v0
dellaert 74b7a96
Methods in Solutions
dellaert 1f4d9bb
Separate class
dellaert d879b15
Asia example
dellaert 455554c
DiscreteSearch
dellaert 54f4933
BayesTree WIP
dellaert 14eeaf9
Works for Bayes trees now as well
dellaert 70089a0
Move to cpp file
dellaert b10ea06
Clean up, MPE tests
dellaert f174a38
Made run const, take K
dellaert cee7769
Moved data structures to cpp
dellaert 9dcb52b
Made queue a class
dellaert 908e4dc
Check MPE
dellaert 1594a25
Fix assert issue
dellaert d53b48c
Don't use c99 feature :-/
dellaert 2da5d76
Pre-compute asia things
dellaert cf44418
Get rid of extra ;
dellaert 16131fe
Remove spurious typedef
dellaert 41fdeb6
Export
dellaert 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* 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 | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/* | ||
* DiscreteSearch.cpp | ||
* | ||
* @date January, 2025 | ||
* @author Frank Dellaert | ||
*/ | ||
|
||
#include <gtsam/discrete/DiscreteSearch.h> | ||
|
||
namespace gtsam { | ||
|
||
using Solution = DiscreteSearch::Solution; | ||
|
||
/** | ||
* @brief Represents a node in the search tree for discrete search algorithms. | ||
* | ||
* @details Each SearchNode contains a partial assignment of discrete variables, | ||
* the current error, a bound on the final error, and the index of the next | ||
* conditional to be assigned. | ||
*/ | ||
struct SearchNode { | ||
DiscreteValues assignment; ///< Partial assignment of discrete variables. | ||
double error; ///< Current error for the partial assignment. | ||
double bound; ///< Lower bound on the final error for unassigned variables. | ||
int nextConditional; ///< Index of the next conditional to be assigned. | ||
|
||
/** | ||
* @brief Construct the root node for the search. | ||
*/ | ||
static SearchNode Root(size_t numConditionals, double bound) { | ||
return {DiscreteValues(), 0.0, bound, | ||
static_cast<int>(numConditionals) - 1}; | ||
} | ||
|
||
struct Compare { | ||
bool operator()(const SearchNode& a, const SearchNode& b) const { | ||
return a.bound > b.bound; // smallest bound -> highest priority | ||
} | ||
}; | ||
|
||
/** | ||
* @brief Checks if the node represents a complete assignment. | ||
* | ||
* @return True if all variables have been assigned, false otherwise. | ||
*/ | ||
inline bool isComplete() const { return nextConditional < 0; } | ||
|
||
/** | ||
* @brief Expands the node by assigning the next variable. | ||
* | ||
* @param conditional The discrete conditional representing the next variable | ||
* to be assigned. | ||
* @param fa The frontal assignment for the next variable. | ||
* @return A new SearchNode representing the expanded state. | ||
*/ | ||
SearchNode expand(const DiscreteConditional& conditional, | ||
const DiscreteValues& fa) const { | ||
// Combine the new frontal assignment with the current partial assignment | ||
DiscreteValues newAssignment = assignment; | ||
for (auto& [key, value] : fa) { | ||
newAssignment[key] = value; | ||
} | ||
|
||
return {newAssignment, error + conditional.error(newAssignment), 0.0, | ||
nextConditional - 1}; | ||
} | ||
|
||
/** | ||
* @brief Prints the SearchNode to an output stream. | ||
* | ||
* @param os The output stream. | ||
* @param node The SearchNode to be printed. | ||
* @return The output stream. | ||
*/ | ||
friend std::ostream& operator<<(std::ostream& os, const SearchNode& node) { | ||
os << "SearchNode(error=" << node.error << ", bound=" << node.bound << ")"; | ||
return os; | ||
} | ||
}; | ||
|
||
struct CompareSolution { | ||
bool operator()(const Solution& a, const Solution& b) const { | ||
return a.error < b.error; | ||
} | ||
}; | ||
|
||
// Define the Solutions class | ||
class Solutions { | ||
private: | ||
size_t maxSize_; | ||
std::priority_queue<Solution, std::vector<Solution>, CompareSolution> pq_; | ||
|
||
public: | ||
Solutions(size_t maxSize) : maxSize_(maxSize) {} | ||
|
||
/// Add a solution to the priority queue, possibly evicting the worst one. | ||
/// Return true if we added the solution. | ||
bool maybeAdd(double error, const DiscreteValues& assignment) { | ||
const bool full = pq_.size() == maxSize_; | ||
if (full && error >= pq_.top().error) return false; | ||
if (full) pq_.pop(); | ||
pq_.emplace(error, assignment); | ||
return true; | ||
} | ||
|
||
/// Check if we have any solutions | ||
bool empty() const { return pq_.empty(); } | ||
|
||
// Method to print all solutions | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
friend std::ostream& operator<<(std::ostream& os, const Solutions& sn) { | ||
os << "Solutions (top " << sn.pq_.size() << "):\n"; | ||
auto pq = sn.pq_; | ||
while (!pq.empty()) { | ||
os << pq.top() << "\n"; | ||
pq.pop(); | ||
} | ||
return os; | ||
} | ||
|
||
/// Check if (partial) solution with given bound can be pruned. If we have | ||
/// room, we never prune. Otherwise, prune if lower bound on error is worse | ||
/// than our current worst error. | ||
bool prune(double bound) const { | ||
if (pq_.size() < maxSize_) return false; | ||
return bound >= pq_.top().error; | ||
} | ||
|
||
// Method to extract solutions in ascending order of error | ||
dellaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::vector<Solution> extractSolutions() { | ||
std::vector<Solution> result; | ||
while (!pq_.empty()) { | ||
result.push_back(pq_.top()); | ||
pq_.pop(); | ||
} | ||
std::sort( | ||
result.begin(), result.end(), | ||
[](const Solution& a, const Solution& b) { return a.error < b.error; }); | ||
return result; | ||
} | ||
}; | ||
|
||
DiscreteSearch::DiscreteSearch(const DiscreteBayesNet& bayesNet) { | ||
std::vector<DiscreteConditional::shared_ptr> conditionals; | ||
for (auto& factor : bayesNet) conditionals_.push_back(factor); | ||
costToGo_ = computeCostToGo(conditionals_); | ||
} | ||
|
||
DiscreteSearch::DiscreteSearch(const DiscreteBayesTree& bayesTree) { | ||
std::function<void(const DiscreteBayesTree::sharedClique&)> | ||
collectConditionals = [&](const auto& clique) { | ||
if (!clique) return; | ||
for (const auto& child : clique->children) collectConditionals(child); | ||
conditionals_.push_back(clique->conditional()); | ||
}; | ||
for (const auto& root : bayesTree.roots()) collectConditionals(root); | ||
costToGo_ = computeCostToGo(conditionals_); | ||
} | ||
|
||
struct SearchNodeQueue | ||
: public std::priority_queue<SearchNode, std::vector<SearchNode>, | ||
SearchNode::Compare> { | ||
void expandNextNode( | ||
const std::vector<DiscreteConditional::shared_ptr>& conditionals, | ||
const std::vector<double>& costToGo, Solutions* solutions) { | ||
// Pop the partial assignment with the smallest bound | ||
SearchNode current = top(); | ||
pop(); | ||
|
||
// If we already have K solutions, prune if we cannot beat the worst one. | ||
if (solutions->prune(current.bound)) { | ||
return; | ||
} | ||
|
||
// Check if we have a complete assignment | ||
if (current.isComplete()) { | ||
solutions->maybeAdd(current.error, current.assignment); | ||
return; | ||
} | ||
|
||
// Expand on the next factor | ||
const auto& conditional = conditionals[current.nextConditional]; | ||
|
||
for (auto& fa : conditional->frontalAssignments()) { | ||
auto childNode = current.expand(*conditional, fa); | ||
if (childNode.nextConditional >= 0) | ||
childNode.bound = childNode.error + costToGo[childNode.nextConditional]; | ||
|
||
// Again, prune if we cannot beat the worst solution | ||
if (!solutions->prune(childNode.bound)) { | ||
emplace(childNode); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
std::vector<Solution> DiscreteSearch::run(size_t K) const { | ||
Solutions solutions(K); | ||
SearchNodeQueue expansions; | ||
expansions.push(SearchNode::Root(conditionals_.size(), | ||
costToGo_.empty() ? 0.0 : costToGo_.back())); | ||
|
||
#ifdef DISCRETE_SEARCH_DEBUG | ||
size_t numExpansions = 0; | ||
#endif | ||
|
||
// Perform the search | ||
while (!expansions.empty()) { | ||
expansions.expandNextNode(conditionals_, costToGo_, &solutions); | ||
#ifdef DISCRETE_SEARCH_DEBUG | ||
++numExpansions; | ||
#endif | ||
} | ||
|
||
#ifdef DISCRETE_SEARCH_DEBUG | ||
std::cout << "Number of expansions: " << numExpansions << std::endl; | ||
#endif | ||
|
||
// Extract solutions from bestSolutions in ascending order of error | ||
return solutions.extractSolutions(); | ||
} | ||
|
||
std::vector<double> DiscreteSearch::computeCostToGo( | ||
const std::vector<DiscreteConditional::shared_ptr>& conditionals) { | ||
std::vector<double> costToGo; | ||
double error = 0.0; | ||
for (const auto& conditional : conditionals) { | ||
Ordering ordering(conditional->begin(), conditional->end()); | ||
auto maxx = conditional->max(ordering); | ||
error -= std::log(maxx->evaluate({})); | ||
costToGo.push_back(error); | ||
} | ||
return costToGo; | ||
} | ||
|
||
} // 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,78 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* 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 | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/* | ||
* DiscreteSearch.cpp | ||
* | ||
* @date January, 2025 | ||
* @author Frank Dellaert | ||
*/ | ||
|
||
#include <gtsam/discrete/DiscreteBayesNet.h> | ||
#include <gtsam/discrete/DiscreteBayesTree.h> | ||
|
||
#include <queue> | ||
|
||
namespace gtsam { | ||
|
||
/** | ||
* DiscreteSearch: Search for the K best solutions. | ||
*/ | ||
class GTSAM_EXPORT DiscreteSearch { | ||
public: | ||
/** | ||
* @brief A solution to a discrete search problem. | ||
*/ | ||
struct Solution { | ||
double error; | ||
DiscreteValues assignment; | ||
Solution(double err, const DiscreteValues& assign) | ||
: error(err), assignment(assign) {} | ||
friend std::ostream& operator<<(std::ostream& os, const Solution& sn) { | ||
os << "[ error=" << sn.error << " assignment={" << sn.assignment << "}]"; | ||
return os; | ||
} | ||
}; | ||
|
||
/** | ||
* Construct from a DiscreteBayesNet and K. | ||
*/ | ||
DiscreteSearch(const DiscreteBayesNet& bayesNet); | ||
|
||
/** | ||
* Construct from a DiscreteBayesTree and K. | ||
*/ | ||
DiscreteSearch(const DiscreteBayesTree& bayesTree); | ||
|
||
/** | ||
* @brief Search for the K best solutions. | ||
* | ||
* This method performs a search to find the K best solutions for the given | ||
* DiscreteBayesNet. It uses a priority queue to manage the search nodes, | ||
* expanding nodes with the smallest bound first. The search continues until | ||
* all possible nodes have been expanded or pruned. | ||
* | ||
* @return A vector of the K best solutions found during the search. | ||
*/ | ||
std::vector<Solution> run(size_t K = 1) const; | ||
|
||
private: | ||
/// Compute the cumulative cost-to-go for each conditional slot. | ||
static std::vector<double> computeCostToGo( | ||
const std::vector<DiscreteConditional::shared_ptr>& conditionals); | ||
|
||
/// Expand the next node in the search tree. | ||
void expandNextNode() const; | ||
|
||
std::vector<DiscreteConditional::shared_ptr> conditionals_; | ||
std::vector<double> costToGo_; | ||
}; | ||
} // 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
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
Oops, something went wrong.
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.
Maybe more information on why and where this is used so one doesn't have to go hunting through the source code?