Print factors

release/4.3a0
Frank Dellaert 2025-01-27 13:05:20 -05:00
parent 41fdeb6c04
commit 35e7acbf16
2 changed files with 90 additions and 50 deletions

View File

@ -16,19 +16,42 @@
* @author Richard Roberts * @author Richard Roberts
*/ */
#include <gtsam/inference/JunctionTree-inst.h>
#include <gtsam/discrete/DiscreteJunctionTree.h>
#include <gtsam/discrete/DiscreteEliminationTree.h> #include <gtsam/discrete/DiscreteEliminationTree.h>
#include <gtsam/discrete/DiscreteJunctionTree.h>
#include <gtsam/inference/JunctionTree-inst.h>
namespace gtsam { namespace gtsam {
// Instantiate base classes // Instantiate base classes
template class EliminatableClusterTree<DiscreteBayesTree, DiscreteFactorGraph>; template class EliminatableClusterTree<DiscreteBayesTree, DiscreteFactorGraph>;
template class JunctionTree<DiscreteBayesTree, DiscreteFactorGraph>; template class JunctionTree<DiscreteBayesTree, DiscreteFactorGraph>;
/* ************************************************************************* */ /* ************************************************************************* */
DiscreteJunctionTree::DiscreteJunctionTree( DiscreteJunctionTree::DiscreteJunctionTree(
const DiscreteEliminationTree& eliminationTree) : const DiscreteEliminationTree& eliminationTree)
Base(eliminationTree) {} : Base(eliminationTree) {}
/* ************************************************************************* */
namespace {
struct PrintForestVisitorPre {
const KeyFormatter& formatter;
PrintForestVisitorPre(const KeyFormatter& formatter) : formatter(formatter) {}
std::string operator()(
const std::shared_ptr<DiscreteJunctionTree::Cluster>& node,
const std::string& parentString) {
// Print the current node
node->print(parentString + "-", formatter);
node->factors.print(parentString + "-", formatter);
std::cout << std::endl;
// Increment the indentation
return parentString + "| ";
}
};
} // namespace
void DiscreteJunctionTree::print(const std::string& s,
const KeyFormatter& keyFormatter) const {
PrintForestVisitorPre visitor(keyFormatter);
treeTraversal::DepthFirstForest(*this, s, visitor);
} }
} // namespace gtsam

View File

@ -18,54 +18,71 @@
#pragma once #pragma once
#include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/discrete/DiscreteBayesTree.h> #include <gtsam/discrete/DiscreteBayesTree.h>
#include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/inference/JunctionTree.h> #include <gtsam/inference/JunctionTree.h>
namespace gtsam { namespace gtsam {
// Forward declarations // Forward declarations
class DiscreteEliminationTree; class DiscreteEliminationTree;
/** /**
* An EliminatableClusterTree, i.e., a set of variable clusters with factors, arranged in a tree, * An EliminatableClusterTree, i.e., a set of variable clusters with factors,
* with the additional property that it represents the clique tree associated with a Bayes net. * arranged in a tree, with the additional property that it represents the
* clique tree associated with a Bayes net.
* *
* In GTSAM a junction tree is an intermediate data structure in multifrontal * In GTSAM a junction tree is an intermediate data structure in multifrontal
* variable elimination. Each node is a cluster of factors, along with a * variable elimination. Each node is a cluster of factors, along with a
* clique of variables that are eliminated all at once. In detail, every node k represents * clique of variables that are eliminated all at once. In detail, every node k
* a clique (maximal fully connected subset) of an associated chordal graph, such as a * represents a clique (maximal fully connected subset) of an associated chordal
* chordal Bayes net resulting from elimination. * graph, such as a chordal Bayes net resulting from elimination.
* *
* The difference with the BayesTree is that a JunctionTree stores factors, whereas a * The difference with the BayesTree is that a JunctionTree stores factors,
* BayesTree stores conditionals, that are the product of eliminating the factors in the * whereas a BayesTree stores conditionals, that are the product of eliminating
* corresponding JunctionTree cliques. * the factors in the corresponding JunctionTree cliques.
* *
* The tree structure and elimination method are exactly analogous to the EliminationTree, * The tree structure and elimination method are exactly analogous to the
* except that in the JunctionTree, at each node multiple variables are eliminated at a time. * EliminationTree, except that in the JunctionTree, at each node multiple
* variables are eliminated at a time.
* *
* \ingroup Multifrontal * \ingroup Multifrontal
* @ingroup discrete * @ingroup discrete
* \nosubgrouping * \nosubgrouping
*/ */
class GTSAM_EXPORT DiscreteJunctionTree : class GTSAM_EXPORT DiscreteJunctionTree
public JunctionTree<DiscreteBayesTree, DiscreteFactorGraph> { : public JunctionTree<DiscreteBayesTree, DiscreteFactorGraph> {
public: public:
typedef JunctionTree<DiscreteBayesTree, DiscreteFactorGraph> Base; ///< Base class typedef JunctionTree<DiscreteBayesTree, DiscreteFactorGraph>
Base; ///< Base class
typedef DiscreteJunctionTree This; ///< This class typedef DiscreteJunctionTree This; ///< This class
typedef std::shared_ptr<This> shared_ptr; ///< Shared pointer to this class typedef std::shared_ptr<This> shared_ptr; ///< Shared pointer to this class
/// @name Constructors
/// @{
/** /**
* Build the elimination tree of a factor graph using precomputed column structure. * Build the elimination tree of a factor graph using precomputed column
* structure.
* @param factorGraph The factor graph for which to build the elimination tree * @param factorGraph The factor graph for which to build the elimination tree
* @param structure The set of factors involving each variable. If this is not * @param structure The set of factors involving each variable. If this is
* precomputed, you can call the Create(const FactorGraph<DERIVEDFACTOR>&) * not precomputed, you can call the Create(const FactorGraph<DERIVEDFACTOR>&)
* named constructor instead. * named constructor instead.
* @return The elimination tree * @return The elimination tree
*/ */
DiscreteJunctionTree(const DiscreteEliminationTree& eliminationTree); DiscreteJunctionTree(const DiscreteEliminationTree& eliminationTree);
};
/// typedef for wrapper: /// @}
using DiscreteCluster = DiscreteJunctionTree::Cluster; /// @name Testable
} /// @{
/** Print the tree to cout */
void print(const std::string& name = "DiscreteJunctionTree: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const;
/// @}
};
/// typedef for wrapper:
using DiscreteCluster = DiscreteJunctionTree::Cluster;
} // namespace gtsam