gtsam
Loading...
Searching...
No Matches
ClusterTree.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <gtsam/base/Testable.h>
13#include <gtsam/base/FastMap.h>
14#include <gtsam/base/FastSet.h>
17
18namespace gtsam {
19
26template <class GRAPH>
28 public:
29 typedef GRAPH FactorGraphType;
31 typedef std::shared_ptr<This> shared_ptr;
32
33 typedef typename GRAPH::FactorType FactorType;
34 typedef std::shared_ptr<FactorType> sharedFactor;
35
37 // TODO(frank): re-factor JunctionTree so we can make members private
38 struct Cluster {
39 typedef FastVector<std::shared_ptr<Cluster> > Children;
40 Children children;
41
42 typedef Ordering Keys;
44
46
47 int problemSize_;
48
49 Cluster() : problemSize_(0) {}
50
51 virtual ~Cluster() {}
52
53 const Cluster& operator[](size_t i) const {
54 return *(children.at(i));
55 }
56
58 template <class CONTAINER>
59 Cluster(Key key, const CONTAINER& factorsToAdd)
60 : problemSize_(0) {
61 addFactors(key, factorsToAdd);
62 }
63
65 template <class CONTAINER>
66 void addFactors(Key key, const CONTAINER& factorsToAdd) {
67 orderedFrontalKeys.push_back(key);
68 factors.push_back(factorsToAdd);
69 problemSize_ += factors.size();
70 }
71
73 void addChild(const std::shared_ptr<Cluster>& cluster) {
74 children.push_back(cluster);
75 problemSize_ = std::max(problemSize_, cluster->problemSize_);
76 }
77
78 size_t nrChildren() const {
79 return children.size();
80 }
81
82 size_t nrFactors() const {
83 return factors.size();
84 }
85
86 size_t nrFrontals() const {
87 return orderedFrontalKeys.size();
88 }
89
90 int problemSize() const {
91 return problemSize_;
92 }
93
95 virtual void print(const std::string& s = "",
96 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
97
99 std::vector<size_t> nrFrontalsOfChildren() const;
100
101 using KeySetMap = FastMap<const Cluster*, KeySet>;
102
104 KeySet separatorKeys(KeySetMap* cache = nullptr) const;
105
107 void merge(const std::shared_ptr<Cluster>& cluster);
108
110 void mergeChildren(const std::vector<bool>& merge);
111
113 void mergeChildrenSiblings(const std::vector<bool>& merge);
114
116 void mergeChildren(const Children& selected);
117
119 void mergeChildrenSiblings(const Children& selected);
120
122 Children childrenFromMask(const std::vector<bool>& merge) const;
123 };
124
125 typedef std::shared_ptr<Cluster> sharedCluster;
126
127 // Define Node=Cluster for compatibility with tree traversal functions
128 typedef Cluster Node;
129 typedef sharedCluster sharedNode;
130
132 GTSAM_CONCEPT_TESTABLE_TYPE(FactorType)
133
134 protected:
136
139
142 ClusterTree(const This& other) {
143 *this = other;
144 }
145
147
148 public:
149
152
155
157 void print(const std::string& s = "",
158 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
159
163
164 void addRoot(const std::shared_ptr<Cluster>& cluster) {
165 roots_.push_back(cluster);
166 }
167
168 void addChildrenAsRoots(const std::shared_ptr<Cluster>& cluster) {
169 for (auto child : cluster->children)
170 this->addRoot(child);
171 }
172
173 size_t nrRoots() const {
174 return roots_.size();
175 }
176
179 return roots_;
180 }
181
182 const Cluster& operator[](size_t i) const {
183 return *(roots_.at(i));
184 }
185
187
188 protected:
189 ~ClusterTree();
190
193
196 This& operator=(const This& other);
197
199};
200
204template <class BAYESTREE, class GRAPH>
206 public:
207 typedef BAYESTREE BayesTreeType;
208 typedef GRAPH FactorGraphType;
210 typedef std::shared_ptr<This> shared_ptr;
211
212 typedef typename BAYESTREE::ConditionalType ConditionalType;
213 typedef std::shared_ptr<ConditionalType>
215
216 typedef typename GRAPH::Eliminate Eliminate;
217 typedef typename GRAPH::FactorType FactorType;
218 typedef std::shared_ptr<FactorType> sharedFactor;
219
220 protected:
221 FastVector<sharedFactor> remainingFactors_;
222
225
228 EliminatableClusterTree(const This& other) : ClusterTree<GRAPH>(other) {
229 *this = other;
230 }
231
233
234 public:
237
243 std::pair<std::shared_ptr<BayesTreeType>, std::shared_ptr<FactorGraphType> > eliminate(
244 const Eliminate& function) const;
245
247
250
253 return remainingFactors_;
254 }
255
257
258 protected:
261
264 This& operator=(const This& other);
265
268
270};
271}
272
A thin wrapper around std::vector that uses a custom allocator.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
A thin wrapper around std::set that uses boost's fast_pool_allocator.
Concept check for values that can be used in unit tests.
Variable ordering for the elimination algorithm.
Collects factorgraph fragments defined on variable clusters, arranged in a tree.
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
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
EliminatableClusterTree< BAYESTREE, GRAPH > This
This class.
Definition ClusterTree.h:209
EliminatableClusterTree()
Default constructor to be used in derived classes.
Definition ClusterTree.h:267
std::shared_ptr< ConditionalType > sharedConditional
Shared pointer to a conditional.
Definition ClusterTree.h:214
BAYESTREE::ConditionalType ConditionalType
The type of conditionals.
Definition ClusterTree.h:212
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:378
const FastVector< sharedFactor > & remainingFactors() const
Return the remaining factors that are not pulled into elimination.
Definition ClusterTree.h:252
EliminatableClusterTree(const This &other)
Copy constructor - makes a deep copy of the tree structure, but only pointers to factors are copied,...
Definition ClusterTree.h:228
BAYESTREE BayesTreeType
The BayesTree type produced by elimination.
Definition ClusterTree.h:207
GRAPH::FactorType FactorType
The type of factors.
Definition ClusterTree.h:217
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition ClusterTree.h:218
std::pair< std::shared_ptr< BayesTreeType >, std::shared_ptr< FactorGraphType > > eliminate(const Eliminate &function) const
Eliminate the factors to a Bayes tree and remaining factor graph.
Definition ClusterTree-inst.h:392
GRAPH FactorGraphType
The factor graph type.
Definition ClusterTree.h:208
std::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition ClusterTree.h:210
GRAPH::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition ClusterTree.h:216
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:247
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition ClusterTree.h:34
std::shared_ptr< This > shared_ptr
Shared pointer to this class.
Definition ClusterTree.h:31
ClusterTree< GRAPH > This
This class.
Definition ClusterTree.h:30
FastVector< sharedNode > roots_
concept check
Definition ClusterTree.h:135
GRAPH::FactorType FactorType
The type of factors.
Definition ClusterTree.h:33
GRAPH FactorGraphType
The factor graph type.
Definition ClusterTree.h:29
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print the cluster tree.
Definition ClusterTree-inst.h:206
const FastVector< sharedNode > & roots() const
Return the set of roots (one for a tree, multiple for a forest).
Definition ClusterTree.h:178
ClusterTree(const This &other)
Copy constructor - makes a deep copy of the tree structure, but only pointers to factors are copied,...
Definition ClusterTree.h:142
ClusterTree()
Default constructor.
Definition ClusterTree.h:151
std::shared_ptr< Cluster > sharedCluster
Shared pointer to Cluster.
Definition ClusterTree.h:125
A Cluster is just a collection of factors.
Definition ClusterTree.h:38
Cluster(Key key, const CONTAINER &factorsToAdd)
Construct from factors associated with a single key.
Definition ClusterTree.h:59
Children children
sub-trees
Definition ClusterTree.h:40
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition ClusterTree-inst.h:27
void merge(const std::shared_ptr< Cluster > &cluster)
Merge in given cluster.
Definition ClusterTree-inst.h:73
KeySet separatorKeys(KeySetMap *cache=nullptr) const
Return the separator keys (subtree keys minus frontals), optionally cached.
Definition ClusterTree-inst.h:45
void mergeChildrenSiblings(const std::vector< bool > &merge)
Merge selected siblings into a new child cluster.
Definition ClusterTree-inst.h:139
Keys orderedFrontalKeys
Frontal keys of this node.
Definition ClusterTree.h:43
void mergeChildren(const std::vector< bool > &merge)
Merge all children for which bit is set into this node.
Definition ClusterTree-inst.h:85
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
Definition ClusterTree-inst.h:35
FactorGraphType factors
Factors associated with this node.
Definition ClusterTree.h:45
void addChild(const std::shared_ptr< Cluster > &cluster)
Add a child cluster.
Definition ClusterTree.h:73
Children childrenFromMask(const std::vector< bool > &merge) const
Convert a child-selection mask into the selected child pointers.
Definition ClusterTree-inst.h:191
void addFactors(Key key, const CONTAINER &factorsToAdd)
Add factors associated with a single key.
Definition ClusterTree.h:66
Definition Ordering.h:33