Loading...
Searching...
No Matches
ClusterTree.h
Go to the documentation of this file.
#pragma once
#include <gtsam/base/Testable.h>
#include <gtsam/base/FastVector.h>
#include <gtsam/inference/Ordering.h>
namespace gtsam {
template <class GRAPH>
class ClusterTree {
public:
typedef GRAPH FactorGraphType;
typedef ClusterTree<GRAPH> This;
typedef boost::shared_ptr<This> shared_ptr;
typedef typename GRAPH::FactorType FactorType;
typedef boost::shared_ptr<FactorType> sharedFactor;
// TODO(frank): re-factor JunctionTree so we can make members private
struct Cluster {
typedef FastVector<boost::shared_ptr<Cluster> > Children;
Children children;
typedef Ordering Keys;
Keys orderedFrontalKeys;
FactorGraphType factors;
int problemSize_;
Cluster() : problemSize_(0) {}
virtual ~Cluster() {}
const Cluster& operator[](size_t i) const {
return *(children.at(i));
}
template <class CONTAINER>
Cluster(Key key, const CONTAINER& factorsToAdd)
: problemSize_(0) {
addFactors(key, factorsToAdd);
}
template <class CONTAINER>
void addFactors(Key key, const CONTAINER& factorsToAdd) {
orderedFrontalKeys.push_back(key);
factors.push_back(factorsToAdd);
problemSize_ += factors.size();
}
void addChild(const boost::shared_ptr<Cluster>& cluster) {
children.push_back(cluster);
problemSize_ = std::max(problemSize_, cluster->problemSize_);
}
size_t nrChildren() const {
return children.size();
}
size_t nrFactors() const {
return factors.size();
}
size_t nrFrontals() const {
return orderedFrontalKeys.size();
}
int problemSize() const {
return problemSize_;
}
virtual void print(const std::string& s = "",
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
std::vector<size_t> nrFrontalsOfChildren() const;
void merge(const boost::shared_ptr<Cluster>& cluster);
void mergeChildren(const std::vector<bool>& merge);
};
typedef boost::shared_ptr<Cluster> sharedCluster;
// Define Node=Cluster for compatibility with tree traversal functions
typedef Cluster Node;
typedef sharedCluster sharedNode;
GTSAM_CONCEPT_TESTABLE_TYPE(FactorType)
protected:
FastVector<sharedNode> roots_;
ClusterTree(const This& other) {
*this = other;
}
public:
ClusterTree() {}
void print(const std::string& s = "",
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
void addRoot(const boost::shared_ptr<Cluster>& cluster) {
roots_.push_back(cluster);
}
void addChildrenAsRoots(const boost::shared_ptr<Cluster>& cluster) {
for (auto child : cluster->children)
this->addRoot(child);
}
size_t nrRoots() const {
return roots_.size();
}
const FastVector<sharedNode>& roots() const {
return roots_;
}
const Cluster& operator[](size_t i) const {
return *(roots_.at(i));
}
protected:
This& operator=(const This& other);
};
template <class BAYESTREE, class GRAPH>
class EliminatableClusterTree : public ClusterTree<GRAPH> {
public:
typedef BAYESTREE BayesTreeType;
typedef GRAPH FactorGraphType;
typedef EliminatableClusterTree<BAYESTREE, GRAPH> This;
typedef boost::shared_ptr<This> shared_ptr;
typedef typename BAYESTREE::ConditionalType ConditionalType;
typedef boost::shared_ptr<ConditionalType>
sharedConditional;
typedef typename GRAPH::Eliminate Eliminate;
typedef typename GRAPH::FactorType FactorType;
typedef boost::shared_ptr<FactorType> sharedFactor;
protected:
FastVector<sharedFactor> remainingFactors_;
EliminatableClusterTree(const This& other) : ClusterTree<GRAPH>(other) {
*this = other;
}
public:
std::pair<boost::shared_ptr<BayesTreeType>, boost::shared_ptr<FactorGraphType> > eliminate(
const Eliminate& function) const;
const FastVector<sharedFactor>& remainingFactors() const {
return remainingFactors_;
}
protected:
This& operator=(const This& other);
EliminatableClusterTree() {}
};
}
#include <gtsam/inference/ClusterTree-inst.h>
Testable.h
Concept check for values that can be used in unit tests.
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
Ordering.h
Variable ordering for the elimination algorithm.
ClusterTree-inst.h
Collects factorgraph fragments defined on variable clusters, arranged in a tree.
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
gtsam::EliminatableClusterTree::This
EliminatableClusterTree< BAYESTREE, GRAPH > This
This class.
Definition ClusterTree.h:188
gtsam::EliminatableClusterTree::EliminatableClusterTree
EliminatableClusterTree()
Default constructor to be used in derived classes.
Definition ClusterTree.h:246
gtsam::EliminatableClusterTree::ConditionalType
BAYESTREE::ConditionalType ConditionalType
The type of conditionals.
Definition ClusterTree.h:191
gtsam::EliminatableClusterTree::operator=
This & operator=(const This &other)
Assignment operator - makes a deep copy of the tree structure, but only pointers to factors are copie...
Definition ClusterTree-inst.h:231
gtsam::EliminatableClusterTree::eliminate
std::pair< boost::shared_ptr< BayesTreeType >, boost::shared_ptr< FactorGraphType > > eliminate(const Eliminate &function) const
Eliminate the factors to a Bayes tree and remaining factor graph.
Definition ClusterTree-inst.h:245
gtsam::EliminatableClusterTree::sharedConditional
boost::shared_ptr< ConditionalType > sharedConditional
Shared pointer to a conditional.
Definition ClusterTree.h:193
gtsam::EliminatableClusterTree::sharedFactor
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition ClusterTree.h:197
gtsam::EliminatableClusterTree::remainingFactors
const FastVector< sharedFactor > & remainingFactors() const
Return the remaining factors that are not pulled into elimination.
Definition ClusterTree.h:231
gtsam::EliminatableClusterTree::EliminatableClusterTree
EliminatableClusterTree(const This &other)
Copy constructor - makes a deep copy of the tree structure, but only pointers to factors are copied,...
Definition ClusterTree.h:207
gtsam::EliminatableClusterTree::BayesTreeType
BAYESTREE BayesTreeType
The BayesTree type produced by elimination.
Definition ClusterTree.h:186
gtsam::EliminatableClusterTree::FactorType
GRAPH::FactorType FactorType
The type of factors.
Definition ClusterTree.h:196
gtsam::EliminatableClusterTree::FactorGraphType
GRAPH FactorGraphType
The factor graph type.
Definition ClusterTree.h:187
gtsam::EliminatableClusterTree::shared_ptr
boost::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition ClusterTree.h:189
gtsam::EliminatableClusterTree::Eliminate
GRAPH::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition ClusterTree.h:195
gtsam::ClusterTree::operator=
This & operator=(const This &other)
Assignment operator - makes a deep copy of the tree structure, but only pointers to factors are copie...
Definition ClusterTree-inst.h:104
gtsam::ClusterTree::This
ClusterTree< GRAPH > This
This class.
Definition ClusterTree.h:28
gtsam::ClusterTree::shared_ptr
boost::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition ClusterTree.h:29
gtsam::ClusterTree::roots_
FastVector< sharedNode > roots_
concept check
Definition ClusterTree.h:116
gtsam::ClusterTree::FactorType
GRAPH::FactorType FactorType
The type of factors.
Definition ClusterTree.h:31
gtsam::ClusterTree::FactorGraphType
GRAPH FactorGraphType
The factor graph type.
Definition ClusterTree.h:27
gtsam::ClusterTree::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print the cluster tree.
Definition ClusterTree-inst.h:98
gtsam::ClusterTree::sharedFactor
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition ClusterTree.h:32
gtsam::ClusterTree::roots
const FastVector< sharedNode > & roots() const
Return the set of roots (one for a tree, multiple for a forest).
Definition ClusterTree.h:159
gtsam::ClusterTree::ClusterTree
ClusterTree(const This &other)
Copy constructor - makes a deep copy of the tree structure, but only pointers to factors are copied,...
Definition ClusterTree.h:123
gtsam::ClusterTree::ClusterTree
ClusterTree()
Default constructor.
Definition ClusterTree.h:132
gtsam::ClusterTree::sharedCluster
boost::shared_ptr< Cluster > sharedCluster
Shared pointer to Cluster.
Definition ClusterTree.h:106
gtsam::ClusterTree::Cluster
A Cluster is just a collection of factors.
Definition ClusterTree.h:36
gtsam::ClusterTree::Cluster::Cluster
Cluster(Key key, const CONTAINER &factorsToAdd)
Construct from factors associated with a single key.
Definition ClusterTree.h:57
gtsam::ClusterTree::Cluster::children
Children children
sub-trees
Definition ClusterTree.h:38
gtsam::ClusterTree::Cluster::print
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition ClusterTree-inst.h:26
gtsam::ClusterTree::Cluster::orderedFrontalKeys
Keys orderedFrontalKeys
Frontal keys of this node.
Definition ClusterTree.h:41
gtsam::ClusterTree::Cluster::mergeChildren
void mergeChildren(const std::vector< bool > &merge)
Merge all children for which bit is set into this node.
Definition ClusterTree-inst.h:56
gtsam::ClusterTree::Cluster::merge
void merge(const boost::shared_ptr< Cluster > &cluster)
Merge in given cluster.
Definition ClusterTree-inst.h:44
gtsam::ClusterTree::Cluster::nrFrontalsOfChildren
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
Definition ClusterTree-inst.h:34
gtsam::ClusterTree::Cluster::factors
FactorGraphType factors
Factors associated with this node.
Definition ClusterTree.h:43
gtsam::ClusterTree::Cluster::addChild
void addChild(const boost::shared_ptr< Cluster > &cluster)
Add a child cluster.
Definition ClusterTree.h:71
gtsam::ClusterTree::Cluster::addFactors
void addFactors(Key key, const CONTAINER &factorsToAdd)
Add factors associated with a single key.
Definition ClusterTree.h:64
gtsam::Ordering
Definition Ordering.h:34