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 boost::shared_ptr<This> shared_ptr;
30 
31  typedef typename GRAPH::FactorType FactorType;
32  typedef boost::shared_ptr<FactorType> sharedFactor;
33 
35  // TODO(frank): re-factor JunctionTree so we can make members private
36  struct Cluster {
38  Children children;
39 
40  typedef Ordering Keys;
42 
43  FactorGraphType factors;
44 
46 
47  Cluster() : problemSize_(0) {}
48 
49  virtual ~Cluster() {}
50 
51  const Cluster& operator[](size_t i) const {
52  return *(children[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 boost::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 boost::shared_ptr<Cluster>& cluster);
101 
103  void mergeChildren(const std::vector<bool>& merge);
104  };
105 
106  typedef boost::shared_ptr<Cluster> sharedCluster;
107 
108  // Define Node=Cluster for compatibility with tree traversal functions
109  typedef Cluster Node;
110  typedef sharedCluster sharedNode;
111 
113  GTSAM_CONCEPT_TESTABLE_TYPE(FactorType);
114 
115  protected:
117 
120 
123  ClusterTree(const This& other) {
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 
142 
145 
146  void addRoot(const boost::shared_ptr<Cluster>& cluster) {
147  roots_.push_back(cluster);
148  }
149 
150  void addChildrenAsRoots(const boost::shared_ptr<Cluster>& cluster) {
151  for (auto child : cluster->children)
152  this->addRoot(child);
153  }
154 
155  size_t nrRoots() const {
156  return roots_.size();
157  }
158 
160  const FastVector<sharedNode>& roots() const {
161  return roots_;
162  }
163 
164  const Cluster& operator[](size_t i) const {
165  return *(roots_[i]);
166  }
167 
169 
170  protected:
172 
175  This& operator=(const This& other);
176 
178 };
179 
183 template <class BAYESTREE, class GRAPH>
184 class EliminatableClusterTree : public ClusterTree<GRAPH> {
185  public:
186  typedef BAYESTREE BayesTreeType;
187  typedef GRAPH FactorGraphType;
189  typedef boost::shared_ptr<This> shared_ptr;
190 
191  typedef typename BAYESTREE::ConditionalType ConditionalType;
192  typedef boost::shared_ptr<ConditionalType>
194 
195  typedef typename GRAPH::Eliminate Eliminate;
196  typedef typename GRAPH::FactorType FactorType;
197  typedef boost::shared_ptr<FactorType> sharedFactor;
198 
199  protected:
201 
204 
207  EliminatableClusterTree(const This& other) : ClusterTree<GRAPH>(other) {
208  *this = other;
209  }
210 
212 
213  public:
216 
222  std::pair<boost::shared_ptr<BayesTreeType>, boost::shared_ptr<FactorGraphType> > eliminate(
223  const Eliminate& function) const;
224 
226 
229 
232  return remainingFactors_;
233  }
234 
236 
237  protected:
239 
242  This& operator=(const This& other);
243 
246 
248 };
249 }
250 
EliminatableClusterTree(const This &other)
Definition: ClusterTree.h:207
ClusterTree()
Default constructor.
Definition: ClusterTree.h:132
#define max(a, b)
Definition: datatypes.h:20
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:187
FactorGraphType factors
Factors associated with this node.
Definition: ClusterTree.h:43
Concept check for values that can be used in unit tests.
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
FastVector< sharedNode > roots_
Definition: ClusterTree.h:116
GTSAM_CONCEPT_TESTABLE_TYPE(FactorType)
BAYESTREE::ConditionalType ConditionalType
The type of conditionals.
Definition: ClusterTree.h:191
boost::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition: ClusterTree.h:29
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:196
sharedCluster sharedNode
Definition: ClusterTree.h:110
void addChildrenAsRoots(const boost::shared_ptr< Cluster > &cluster)
Definition: ClusterTree.h:150
size_t nrFactors() const
Definition: ClusterTree.h:80
EliminatableClusterTree< BAYESTREE, GRAPH > This
This class.
Definition: ClusterTree.h:188
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:31
This & operator=(const This &other)
boost::shared_ptr< Cluster > sharedCluster
Shared pointer to Cluster.
Definition: ClusterTree.h:106
const Cluster & operator[](size_t i) const
Definition: ClusterTree.h:164
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:32
boost::shared_ptr< ConditionalType > sharedConditional
Shared pointer to a conditional.
Definition: ClusterTree.h:193
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
EliminatableClusterTree()
Default constructor to be used in derived classes.
Definition: ClusterTree.h:245
FastVector< boost::shared_ptr< Cluster > > Children
Definition: ClusterTree.h:37
size_t nrRoots() const
Definition: ClusterTree.h:155
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
boost::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition: ClusterTree.h:189
ClusterTree(const This &other)
Definition: ClusterTree.h:123
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
RealScalar s
BAYESTREE BayesTreeType
The BayesTree type produced by elimination.
Definition: ClusterTree.h:186
size_t nrChildren() const
Definition: ClusterTree.h:76
A thin wrapper around std::vector that uses a custom allocator.
Keys orderedFrontalKeys
Frontal keys of this node.
Definition: ClusterTree.h:41
traits
Definition: chartTesting.h:28
const FastVector< sharedFactor > & remainingFactors() const
Definition: ClusterTree.h:231
size_t nrFrontals() const
Definition: ClusterTree.h:84
A Cluster is just a collection of factors.
Definition: ClusterTree.h:36
void addFactors(Key key, const CONTAINER &factorsToAdd)
Add factors associated with a single key.
Definition: ClusterTree.h:64
Children children
sub-trees
Definition: ClusterTree.h:38
const Cluster & operator[](size_t i) const
Definition: ClusterTree.h:51
void addChild(const boost::shared_ptr< Cluster > &cluster)
Add a child cluster.
Definition: ClusterTree.h:71
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
void merge(const boost::shared_ptr< Cluster > &cluster)
Merge in given cluster.
GRAPH::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition: ClusterTree.h:195
FastVector< sharedFactor > remainingFactors_
Definition: ClusterTree.h:200
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:197
ClusterTree< GRAPH > This
This class.
Definition: ClusterTree.h:28
void mergeChildren(const std::vector< bool > &merge)
Merge all children for which bit is set into this node.
const FastVector< sharedNode > & roots() const
Definition: ClusterTree.h:160
void addRoot(const boost::shared_ptr< Cluster > &cluster)
Definition: ClusterTree.h:146
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:27
Cluster(Key key, const CONTAINER &factorsToAdd)
Construct from factors associated with a single key.
Definition: ClusterTree.h:57


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:47