gtsam
Loading...
Searching...
No Matches
Ordering.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
20
21#pragma once
22
23#include <gtsam/inference/Key.h>
26#include <gtsam/base/FastSet.h>
27
28#include <algorithm>
29#include <vector>
30
31namespace gtsam {
32
33class GTSAM_EXPORT Ordering: public KeyVector {
34protected:
35 typedef KeyVector Base;
36
37public:
38
41 COLAMD, METIS, NATURAL, CUSTOM
42 };
43
44 typedef Ordering This;
45 typedef std::shared_ptr<This> shared_ptr;
46
49 }
50
51 using KeyVector::KeyVector; // Inherit the KeyVector's constructors
52
54 template<typename KEYS>
55 explicit Ordering(const KEYS& keys) :
56 Base(keys.begin(), keys.end()) {
57 }
58
61 This& operator+=(Key key);
62
64 // e.g. keys += key1, key2
65 This& operator,(Key key);
66
73 This& operator+=(KeyVector& keys);
74
76 bool contains(const Key& key) const;
77
84 FastMap<Key, size_t> invert() const;
85
88
92 template<class FACTOR_GRAPH>
93 static Ordering Colamd(const FACTOR_GRAPH& graph) {
94 if (graph.empty())
95 return Ordering();
96 else
97 return Colamd(VariableIndex(graph));
98 }
99
101 static Ordering Colamd(const VariableIndex& variableIndex);
102
111 template<class FACTOR_GRAPH>
112 static Ordering ColamdConstrainedLast(const FACTOR_GRAPH& graph,
113 const KeyVector& constrainLast, bool forceOrder = false) {
114 if (graph.empty())
115 return Ordering();
116 else
117 return ColamdConstrainedLast(VariableIndex(graph), constrainLast, forceOrder);
118 }
119
126 static Ordering ColamdConstrainedLast(
127 const VariableIndex& variableIndex, const KeyVector& constrainLast,
128 bool forceOrder = false);
129
138 template<class FACTOR_GRAPH>
139 static Ordering ColamdConstrainedFirst(const FACTOR_GRAPH& graph,
140 const KeyVector& constrainFirst, bool forceOrder = false) {
141 if (graph.empty())
142 return Ordering();
143 else
144 return ColamdConstrainedFirst(VariableIndex(graph), constrainFirst, forceOrder);
145 }
146
154 static Ordering ColamdConstrainedFirst(
155 const VariableIndex& variableIndex,
156 const KeyVector& constrainFirst, bool forceOrder = false);
157
167 template<class FACTOR_GRAPH>
168 static Ordering ColamdConstrained(const FACTOR_GRAPH& graph,
169 const FastMap<Key, int>& groups) {
170 if (graph.empty())
171 return Ordering();
172 else
173 return ColamdConstrained(VariableIndex(graph), groups);
174 }
175
183 static Ordering ColamdConstrained(
184 const VariableIndex& variableIndex, const FastMap<Key, int>& groups);
185
187 template<class FACTOR_GRAPH>
188 static Ordering Natural(const FACTOR_GRAPH &fg) {
189 KeySet src = fg.keys();
190 KeyVector keys(src.begin(), src.end());
191 std::stable_sort(keys.begin(), keys.end());
192 return Ordering(keys.begin(), keys.end());
193 }
194
196 template<class FACTOR_GRAPH>
197 static void CSRFormat(std::vector<int>& xadj,
198 std::vector<int>& adj, const FACTOR_GRAPH& graph);
199
207 static Ordering Metis(const MetisIndex& met, int seed = 4321);
208
209 template<class FACTOR_GRAPH>
210 static Ordering Metis(const FACTOR_GRAPH& graph, int seed = 4321) {
211 if (graph.empty())
212 return Ordering();
213 else
214 return Metis(MetisIndex(graph), seed);
215 }
216
218
221
222 template<class FACTOR_GRAPH>
223 static Ordering Create(OrderingType orderingType,
224 const FACTOR_GRAPH& graph) {
225 if (graph.empty())
226 return Ordering();
227
228 switch (orderingType) {
229 case COLAMD:
230 return Colamd(graph);
231 case METIS:
232 return Metis(graph);
233 case NATURAL:
234 return Natural(graph);
235 case CUSTOM:
236 throw std::runtime_error(
237 "Ordering::Create error: called with CUSTOM ordering type.");
238 default:
239 throw std::runtime_error(
240 "Ordering::Create error: called with unknown ordering type.");
241 }
242 }
243
245
248
249 void print(const std::string& str = "", const KeyFormatter& keyFormatter =
250 DefaultKeyFormatter) const;
251
252 bool equals(const Ordering& other, double tol = 1e-9) const;
253
255
256private:
258 static Ordering ColamdConstrained(
259 const VariableIndex& variableIndex, std::vector<int>& cmember);
260
261#if GTSAM_ENABLE_BOOST_SERIALIZATION
263 friend class boost::serialization::access;
264 template<class ARCHIVE>
265 void serialize(ARCHIVE & ar, const unsigned int version) {
266 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
267 }
268#endif
269};
270
272template<> struct traits<Ordering> : public Testable<Ordering> {
273};
274
275}
276
A thin wrapper around std::set that uses boost's fast_pool_allocator.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition FastMap.h:40
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
The MetisIndex class converts a factor graph into the Compressed Sparse Row format for use in METIS a...
Definition MetisIndex.h:37
Definition Ordering.h:33
static Ordering Natural(const FACTOR_GRAPH &fg)
Return a natural Ordering. Typically used by iterative solvers.
Definition Ordering.h:188
static Ordering ColamdConstrained(const FACTOR_GRAPH &graph, const FastMap< Key, int > &groups)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition Ordering.h:168
Ordering(const KEYS &keys)
Create from a container.
Definition Ordering.h:55
static Ordering Colamd(const FACTOR_GRAPH &graph)
Compute a fill-reducing ordering using COLAMD from a factor graph (see details for note on performanc...
Definition Ordering.h:93
OrderingType
Type of ordering to use.
Definition Ordering.h:40
static Ordering ColamdConstrainedLast(const FACTOR_GRAPH &graph, const KeyVector &constrainLast, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition Ordering.h:112
static Ordering Metis(const MetisIndex &met, int seed=4321)
Compute an ordering determined by METIS from a VariableIndex.
Definition Ordering.cpp:256
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition Ordering.h:45
static void CSRFormat(std::vector< int > &xadj, std::vector< int > &adj, const FACTOR_GRAPH &graph)
METIS Formatting function.
static Ordering ColamdConstrainedFirst(const FACTOR_GRAPH &graph, const KeyVector &constrainFirst, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition Ordering.h:139
Ordering()
Create an empty ordering.
Definition Ordering.h:48
Ordering This
Typedef to this class.
Definition Ordering.h:44
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition VariableIndex.h:41