Go to the documentation of this file.
38 std::optional<size_t>
next;
47 return a.bound >
b.bound;
56 std::optional<size_t> nextSlot)
const {
62 double errorSoFar =
error + slot.
factor->error(newAssignment);
63 return {newAssignment, errorSoFar, errorSoFar + slot.
heuristic, nextSlot};
68 os <<
"SearchNode(error=" << node.
error <<
", bound=" << node.
bound <<
")";
75 return a.error <
b.error;
95 if (full &&
error >=
pq_.top().error)
return false;
106 os <<
"Solutions (top " <<
sn.pq_.size() <<
"):\n";
108 while (!pq.empty()) {
109 os << pq.top() <<
"\n";
125 std::vector<Solution>
result;
126 while (!
pq_.empty()) {
138 template <
typename NodeType>
140 const auto&
factors = node->factors;
146 using NodePtr = std::shared_ptr<DiscreteEliminationTree::Node>;
147 auto visitor = [
this](
const NodePtr& node,
int data) {
149 const size_t cardinality =
factor->cardinality(node->key);
150 std::vector<std::pair<Key, size_t>> pairs{{node->key, cardinality}};
152 slots_.emplace_back(std::move(slot));
162 using NodePtr = std::shared_ptr<DiscreteJunctionTree::Cluster>;
163 auto visitor = [
this](
const NodePtr& cluster,
int data) {
165 std::vector<std::pair<Key, size_t>> pairs;
166 for (
Key key : cluster->orderedFrontalKeys) {
170 slots_.emplace_back(std::move(slot));
181 bool buildJunctionTree) {
183 if (buildJunctionTree) {
193 for (
auto& conditional :
bayesNet) {
194 const Slot slot{conditional, conditional->frontalAssignments(), 0.0};
195 slots_.emplace_back(std::move(slot));
203 auto visitor = [
this](
const NodePtr& clique,
int data) {
204 auto conditional = clique->conditional();
205 const Slot slot{conditional, conditional->frontalAssignments(), 0.0};
206 slots_.emplace_back(std::move(slot));
217 std::cout <<
name <<
" with " <<
slots_.size() <<
" slots:\n";
218 for (
size_t i = 0;
i <
slots_.size(); ++
i) {
219 std::cout <<
i <<
": " <<
slots_[
i] << std::endl;
223 using SearchNodeQueue = std::priority_queue<SearchNode, std::vector<SearchNode>,
236 while (!expansions.empty()) {
254 std::optional<size_t> nextSlot = *current.
next + 1;
255 if (nextSlot ==
slots_.size()) nextSlot.reset();
256 for (
auto& fa : slot.assignments) {
257 auto childNode = current.
expand(fa, slot, nextSlot);
260 if (!solutions.
prune(childNode.bound)) {
261 expansions.emplace(childNode);
279 for (
auto it =
slots_.rbegin(); it !=
slots_.rend(); ++it) {
280 it->heuristic =
error;
282 auto maxx = it->factor->max(
ordering);
bool operator()(const SearchNode &a, const SearchNode &b) const
SearchNode expand(const DiscreteValues &fa, const Slot &slot, std::optional< size_t > nextSlot) const
friend std::ostream & operator<<(std::ostream &os, const SearchNode &node)
Annotation for function names.
static const DiscreteBayesNet bayesNet
Solutions(size_t maxSize)
friend std::ostream & operator<<(std::ostream &os, const Solutions &sn)
double bound(double a, double min, double max)
DiscreteSearch::Slot Slot
const GaussianFactorGraph factors
std::vector< Solution > run(size_t K=1) const
Search for the K best solutions.
DiscreteSearch: Search for the K best solutions.
static const DiscreteBayesTree bayesTree
const KeyFormatter & formatter
const EIGEN_DEVICE_FUNC LogReturnType log() const
std::priority_queue< SearchNode, std::vector< SearchNode >, SearchNode::Compare > SearchNodeQueue
DiscreteSearch(const DiscreteEliminationTree &etree)
Construct from a DiscreteEliminationTree.
ofstream os("timeSchurFactors.csv")
static DiscreteSearch FromFactorGraph(const DiscreteFactorGraph &factorGraph, const Ordering &ordering, bool buildJunctionTree=false)
DiscreteValues assignment
std::vector< Solution > extractSolutions()
static const DiscreteFactorGraph factorGraph(bayesNet)
DecisionTreeFactor factor(D &C &B &A, "0.0 0.0 0.0 0.60658897 0.61241912 0.61241969 0.61247685 0.61247742 0.0 " "0.0 0.0 0.99995287 1.0 1.0 1.0 1.0")
double lowerBound_
Lower bound on the cost-to-go for the entire search.
DiscreteSearch::Solution Solution
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
Elimination tree for discrete factors.
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
double computeHeuristic()
bool operator()(const Solution &a, const Solution &b) const
DiscreteFactor::shared_ptr product() const
static enum @1096 ordering
const gtsam::Symbol key('X', 0)
sharedFactor back() const
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
std::shared_ptr< Clique > sharedClique
Shared pointer to a clique.
static DiscreteFactor::shared_ptr getFactor(const NodeType &node)
Defines the DiscreteSearch class for discrete search algorithms.
A Bayes tree representing a Discrete distribution.
std::optional< size_t > next
DiscreteFactor::shared_ptr factor
void reverse(const MatrixType &m)
bool maybeAdd(double error, const DiscreteValues &assignment)
static SearchNode Root(size_t numSlots, double bound)
std::vector< Slot > slots_
The slots to fill in the search.
std::uint64_t Key
Integer nonlinear key type.
void print(const std::string &name="DiscreteSearch: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
std::priority_queue< Solution, std::vector< Solution >, CompareSolution > pq_
bool prune(double bound) const
gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:36