ClusterTree.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <gtsam/base/Testable.h>
13 #include <gtsam/base/FastVector.h>
15 
16 namespace gtsam {
17 
24 template <class GRAPH>
25 class ClusterTree {
26  public:
27  typedef GRAPH FactorGraphType;
29  typedef std::shared_ptr<This> shared_ptr;
30 
31  typedef typename GRAPH::FactorType FactorType;
32  typedef std::shared_ptr<FactorType> sharedFactor;
33 
35  // TODO(frank): re-factor JunctionTree so we can make members private
36  struct Cluster {
39 
40  typedef Ordering Keys;
42 
44 
46 
48 
49  virtual ~Cluster() {}
50 
51  const Cluster& operator[](size_t i) const {
52  return *(children.at(i));
53  }
54 
56  template <class CONTAINER>
57  Cluster(Key key, const CONTAINER& factorsToAdd)
58  : problemSize_(0) {
59  addFactors(key, factorsToAdd);
60  }
61 
63  template <class CONTAINER>
64  void addFactors(Key key, const CONTAINER& factorsToAdd) {
65  orderedFrontalKeys.push_back(key);
66  factors.push_back(factorsToAdd);
67  problemSize_ += factors.size();
68  }
69 
71  void addChild(const std::shared_ptr<Cluster>& cluster) {
72  children.push_back(cluster);
73  problemSize_ = std::max(problemSize_, cluster->problemSize_);
74  }
75 
76  size_t nrChildren() const {
77  return children.size();
78  }
79 
80  size_t nrFactors() const {
81  return factors.size();
82  }
83 
84  size_t nrFrontals() const {
85  return orderedFrontalKeys.size();
86  }
87 
88  int problemSize() const {
89  return problemSize_;
90  }
91 
93  virtual void print(const std::string& s = "",
94  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
95 
97  std::vector<size_t> nrFrontalsOfChildren() const;
98 
100  void merge(const std::shared_ptr<Cluster>& cluster);
101 
103  void mergeChildren(const std::vector<bool>& merge);
104  };
105 
106  typedef std::shared_ptr<Cluster> sharedCluster;
107 
108  // Define Node=Cluster for compatibility with tree traversal functions
109  typedef Cluster Node;
111 
114 
115  protected:
117 
120 
124  *this = other;
125  }
126 
128 
129  public:
130 
133 
136 
138  void print(const std::string& s = "",
139  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
140 
144 
145  void addRoot(const std::shared_ptr<Cluster>& cluster) {
146  roots_.push_back(cluster);
147  }
148 
149  void addChildrenAsRoots(const std::shared_ptr<Cluster>& cluster) {
150  for (auto child : cluster->children)
151  this->addRoot(child);
152  }
153 
154  size_t nrRoots() const {
155  return roots_.size();
156  }
157 
159  const FastVector<sharedNode>& roots() const {
160  return roots_;
161  }
162 
163  const Cluster& operator[](size_t i) const {
164  return *(roots_.at(i));
165  }
166 
168 
169  ~ClusterTree();
170 
171  protected:
174 
177  This& operator=(const This& other);
178 
180 };
181 
185 template <class BAYESTREE, class GRAPH>
186 class EliminatableClusterTree : public ClusterTree<GRAPH> {
187  public:
188  typedef BAYESTREE BayesTreeType;
189  typedef GRAPH FactorGraphType;
191  typedef std::shared_ptr<This> shared_ptr;
192 
193  typedef typename BAYESTREE::ConditionalType ConditionalType;
194  typedef std::shared_ptr<ConditionalType>
196 
197  typedef typename GRAPH::Eliminate Eliminate;
198  typedef typename GRAPH::FactorType FactorType;
199  typedef std::shared_ptr<FactorType> sharedFactor;
200 
201  protected:
203 
206 
210  *this = other;
211  }
212 
214 
215  public:
218 
224  std::pair<std::shared_ptr<BayesTreeType>, std::shared_ptr<FactorGraphType> > eliminate(
225  const Eliminate& function) const;
226 
228 
231 
234  return remainingFactors_;
235  }
236 
238 
239  protected:
242 
245  This& operator=(const This& other);
246 
249 
251 };
252 }
253 
gtsam::ClusterTree::Cluster::nrFrontalsOfChildren
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
Definition: ClusterTree-inst.h:35
gtsam::ClusterTree::Cluster::nrFactors
size_t nrFactors() const
Definition: ClusterTree.h:80
gtsam::EliminatableClusterTree::shared_ptr
std::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition: ClusterTree.h:191
gtsam::ClusterTree::sharedFactor
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:32
gtsam::ClusterTree::Cluster::nrFrontals
size_t nrFrontals() const
Definition: ClusterTree.h:84
gtsam::ClusterTree::roots
const FastVector< sharedNode > & roots() const
Definition: ClusterTree.h:159
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
s
RealScalar s
Definition: level1_cplx_impl.h:126
Testable.h
Concept check for values that can be used in unit tests.
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:57
gtsam::EliminatableClusterTree::sharedFactor
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:199
gtsam::EliminatableClusterTree::FactorGraphType
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:189
gtsam::ClusterTree::Node
Cluster Node
Definition: ClusterTree.h:109
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
gtsam::ClusterTree::This
ClusterTree< GRAPH > This
This class.
Definition: ClusterTree.h:28
gtsam::ClusterTree::Cluster::factors
FactorGraphType factors
Factors associated with this node.
Definition: ClusterTree.h:43
gtsam::ClusterTree::Cluster::orderedFrontalKeys
Keys orderedFrontalKeys
Frontal keys of this node.
Definition: ClusterTree.h:41
gtsam::EliminatableClusterTree::EliminatableClusterTree
EliminatableClusterTree(const This &other)
Definition: ClusterTree.h:209
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::EliminatableClusterTree::FactorType
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:198
gtsam::ClusterTree::Cluster::addFactors
void addFactors(Key key, const CONTAINER &factorsToAdd)
Add factors associated with a single key.
Definition: ClusterTree.h:64
gtsam::EliminatableClusterTree::sharedConditional
std::shared_ptr< ConditionalType > sharedConditional
Shared pointer to a conditional.
Definition: ClusterTree.h:195
gtsam::EliminatableClusterTree::This
EliminatableClusterTree< BAYESTREE, GRAPH > This
This class.
Definition: ClusterTree.h:190
gtsam::ClusterTree::ClusterTree
ClusterTree()
Default constructor.
Definition: ClusterTree.h:132
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::ClusterTree::Cluster::children
Children children
sub-trees
Definition: ClusterTree.h:38
gtsam::ClusterTree::addRoot
void addRoot(const std::shared_ptr< Cluster > &cluster)
Definition: ClusterTree.h:145
gtsam::EliminatableClusterTree
Definition: BayesTree.h:35
gtsam::EliminatableClusterTree::BayesTreeType
BAYESTREE BayesTreeType
The BayesTree type produced by elimination.
Definition: ClusterTree.h:188
gtsam::ClusterTree::FactorGraphType
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:27
gtsam::ClusterTree::Cluster::print
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: ClusterTree-inst.h:27
gtsam::ClusterTree::Cluster::~Cluster
virtual ~Cluster()
Definition: ClusterTree.h:49
gtsam::EliminatableClusterTree::EliminatableClusterTree
EliminatableClusterTree()
Default constructor to be used in derived classes.
Definition: ClusterTree.h:248
gtsam::ClusterTree::roots_
FastVector< sharedNode > roots_
Definition: ClusterTree.h:116
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::ClusterTree::Cluster::problemSize
int problemSize() const
Definition: ClusterTree.h:88
gtsam::ClusterTree::operator[]
const Cluster & operator[](size_t i) const
Definition: ClusterTree.h:163
gtsam::ClusterTree::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: ClusterTree-inst.h:99
gtsam::ClusterTree::Cluster::Cluster
Cluster(Key key, const CONTAINER &factorsToAdd)
Construct from factors associated with a single key.
Definition: ClusterTree.h:57
gtsam::EliminatableClusterTree::remainingFactors
const FastVector< sharedFactor > & remainingFactors() const
Definition: ClusterTree.h:233
gtsam::ClusterTree::sharedNode
sharedCluster sharedNode
Definition: ClusterTree.h:110
gtsam::ClusterTree::Cluster::Cluster
Cluster()
Definition: ClusterTree.h:47
key
const gtsam::Symbol key('X', 0)
gtsam::ClusterTree::Cluster::problemSize_
int problemSize_
Definition: ClusterTree.h:45
gtsam::ClusterTree::Cluster::operator[]
const Cluster & operator[](size_t i) const
Definition: ClusterTree.h:51
gtsam::ClusterTree::Cluster::Keys
Ordering Keys
Definition: ClusterTree.h:40
gtsam::EliminatableClusterTree::Eliminate
GRAPH::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition: ClusterTree.h:197
gtsam::ClusterTree::operator=
This & operator=(const This &other)
Definition: ClusterTree-inst.h:140
gtsam::ClusterTree::~ClusterTree
~ClusterTree()
Definition: ClusterTree-inst.h:111
gtsam
traits
Definition: SFMdata.h:40
gtsam::ClusterTree::Cluster::nrChildren
size_t nrChildren() const
Definition: ClusterTree.h:76
gtsam::ClusterTree::Cluster
A Cluster is just a collection of factors.
Definition: ClusterTree.h:36
GTSAM_CONCEPT_TESTABLE_TYPE
#define GTSAM_CONCEPT_TESTABLE_TYPE(T)
Definition: Testable.h:177
gtsam::EliminatableClusterTree::remainingFactors_
FastVector< sharedFactor > remainingFactors_
Definition: ClusterTree.h:202
gtsam::ClusterTree::sharedCluster
std::shared_ptr< Cluster > sharedCluster
Shared pointer to Cluster.
Definition: ClusterTree.h:106
gtsam::ClusterTree::Cluster::merge
void merge(const std::shared_ptr< Cluster > &cluster)
Merge in given cluster.
Definition: ClusterTree-inst.h:45
gtsam::ClusterTree::Cluster::Children
FastVector< std::shared_ptr< Cluster > > Children
Definition: ClusterTree.h:37
gtsam::ClusterTree::addChildrenAsRoots
void addChildrenAsRoots(const std::shared_ptr< Cluster > &cluster)
Definition: ClusterTree.h:149
gtsam::ClusterTree
Definition: ClusterTree.h:25
gtsam::EliminatableClusterTree::eliminate
std::pair< std::shared_ptr< BayesTreeType >, std::shared_ptr< FactorGraphType > > eliminate(const Eliminate &function) const
Definition: ClusterTree-inst.h:285
gtsam::ClusterTree::FactorType
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:31
ClusterTree-inst.h
Collects factorgraph fragments defined on variable clusters, arranged in a tree.
gtsam::ClusterTree::shared_ptr
std::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition: ClusterTree.h:29
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
max
#define max(a, b)
Definition: datatypes.h:20
gtsam::Ordering
Definition: inference/Ordering.h:33
gtsam::EliminatableClusterTree::operator=
This & operator=(const This &other)
Definition: ClusterTree-inst.h:271
gtsam::EliminatableClusterTree::ConditionalType
BAYESTREE::ConditionalType ConditionalType
The type of conditionals.
Definition: ClusterTree.h:193
gtsam::ClusterTree::ClusterTree
ClusterTree(const This &other)
Definition: ClusterTree.h:123
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::ClusterTree::Cluster::addChild
void addChild(const std::shared_ptr< Cluster > &cluster)
Add a child cluster.
Definition: ClusterTree.h:71
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::ClusterTree::nrRoots
size_t nrRoots() const
Definition: ClusterTree.h:154


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:06