gtsam  4.0.0
gtsam
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 
21 #pragma once
22 
23 #include <gtsam/inference/Key.h>
26 #include <gtsam/base/FastSet.h>
27 
28 #include <boost/assign/list_inserter.hpp>
29 #include <algorithm>
30 #include <vector>
31 
32 namespace gtsam {
33 
34 class Ordering: public KeyVector {
35 protected:
36  typedef KeyVector Base;
37 
38 public:
39 
41  enum OrderingType {
42  COLAMD, METIS, NATURAL, CUSTOM
43  };
44 
45  typedef Ordering This;
46  typedef boost::shared_ptr<This> shared_ptr;
47 
49  GTSAM_EXPORT
51  }
52 
54  template<typename KEYS>
55  explicit Ordering(const KEYS& keys) :
56  Base(keys.begin(), keys.end()) {
57  }
58 
60  template<typename ITERATOR>
61  Ordering(ITERATOR firstKey, ITERATOR lastKey) :
62  Base(firstKey, lastKey) {
63  }
64 
67  boost::assign::list_inserter<boost::assign_detail::call_push_back<This> > operator+=(
68  Key key) {
69  return boost::assign::make_list_inserter(
70  boost::assign_detail::call_push_back<This>(*this))(key);
71  }
72 
75 
77 
81  template<class FACTOR_GRAPH>
82  static Ordering Colamd(const FACTOR_GRAPH& graph) {
83  if (graph.empty())
84  return Ordering();
85  else
86  return Colamd(VariableIndex(graph));
87  }
88 
90  static GTSAM_EXPORT Ordering Colamd(const VariableIndex& variableIndex);
91 
100  template<class FACTOR_GRAPH>
101  static Ordering ColamdConstrainedLast(const FACTOR_GRAPH& graph,
102  const KeyVector& constrainLast, bool forceOrder = false) {
103  if (graph.empty())
104  return Ordering();
105  else
106  return ColamdConstrainedLast(VariableIndex(graph), constrainLast, forceOrder);
107  }
108 
115  static GTSAM_EXPORT Ordering ColamdConstrainedLast(
116  const VariableIndex& variableIndex, const KeyVector& constrainLast,
117  bool forceOrder = false);
118 
127  template<class FACTOR_GRAPH>
128  static Ordering ColamdConstrainedFirst(const FACTOR_GRAPH& graph,
129  const KeyVector& constrainFirst, bool forceOrder = false) {
130  if (graph.empty())
131  return Ordering();
132  else
133  return ColamdConstrainedFirst(VariableIndex(graph), constrainFirst, forceOrder);
134  }
135 
143  static GTSAM_EXPORT Ordering ColamdConstrainedFirst(
144  const VariableIndex& variableIndex,
145  const KeyVector& constrainFirst, bool forceOrder = false);
146 
156  template<class FACTOR_GRAPH>
157  static Ordering ColamdConstrained(const FACTOR_GRAPH& graph,
158  const FastMap<Key, int>& groups) {
159  if (graph.empty())
160  return Ordering();
161  else
162  return ColamdConstrained(VariableIndex(graph), groups);
163  }
164 
172  static GTSAM_EXPORT Ordering ColamdConstrained(
173  const VariableIndex& variableIndex, const FastMap<Key, int>& groups);
174 
176  template<class FACTOR_GRAPH>
177  static Ordering Natural(const FACTOR_GRAPH &fg) {
178  KeySet src = fg.keys();
179  KeyVector keys(src.begin(), src.end());
180  std::stable_sort(keys.begin(), keys.end());
181  return Ordering(keys);
182  }
183 
185  template<class FACTOR_GRAPH>
186  static GTSAM_EXPORT void CSRFormat(std::vector<int>& xadj,
187  std::vector<int>& adj, const FACTOR_GRAPH& graph);
188 
190  static GTSAM_EXPORT Ordering Metis(const MetisIndex& met);
191 
192  template<class FACTOR_GRAPH>
193  static Ordering Metis(const FACTOR_GRAPH& graph) {
194  return Metis(MetisIndex(graph));
195  }
196 
198 
200 
201  template<class FACTOR_GRAPH>
202  static Ordering Create(OrderingType orderingType,
203  const FACTOR_GRAPH& graph) {
204  if (graph.empty())
205  return Ordering();
206 
207  switch (orderingType) {
208  case COLAMD:
209  return Colamd(graph);
210  case METIS:
211  return Metis(graph);
212  case NATURAL:
213  return Natural(graph);
214  case CUSTOM:
215  throw std::runtime_error(
216  "Ordering::Create error: called with CUSTOM ordering type.");
217  default:
218  throw std::runtime_error(
219  "Ordering::Create error: called with unknown ordering type.");
220  }
221  }
222 
224 
226 
227  GTSAM_EXPORT
228  void print(const std::string& str = "", const KeyFormatter& keyFormatter =
229  DefaultKeyFormatter) const;
230 
231  GTSAM_EXPORT
232  bool equals(const Ordering& other, double tol = 1e-9) const;
233 
235 
236 private:
238  static GTSAM_EXPORT Ordering ColamdConstrained(
239  const VariableIndex& variableIndex, std::vector<int>& cmember);
240 
243  template<class ARCHIVE>
244  void serialize(ARCHIVE & ar, const unsigned int version) {
245  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
246  }
247 };
248 
250 template<> struct traits<Ordering> : public Testable<Ordering> {
251 };
252 
253 }
254 
static Ordering Natural(const FACTOR_GRAPH &fg)
Return a natural Ordering. Typically used by iterative solvers.
Definition: Ordering.h:177
boost::assign::list_inserter< boost::assign_detail::call_push_back< This > > operator+=(Key key)
Add new variables to the ordering as ordering += key1, key2, ...
Definition: Ordering.h:67
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:157
The MetisIndex class converts a factor graph into the Compressed Sparse Row format for use in METIS a...
Definition: MetisIndex.h:45
GTSAM_EXPORT Ordering()
Create an empty ordering.
Definition: Ordering.h:50
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
static GTSAM_EXPORT void CSRFormat(std::vector< int > &xadj, std::vector< int > &adj, const FACTOR_GRAPH &graph)
METIS Formatting function.
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:43
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
A thin wrapper around std::set that uses boost's fast_pool_allocator.
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
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:82
friend class boost::serialization::access
Serialization function.
Definition: Ordering.h:242
Ordering This
Typedef to this class.
Definition: Ordering.h:45
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: Ordering.h:46
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
FastMap< Key, size_t > invert() const
Invert (not reverse) the ordering - returns a map from key to order position.
Definition: Ordering.cpp:36
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
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:128
Ordering(const KEYS &keys)
Create from a container.
Definition: Ordering.h:55
static GTSAM_EXPORT Ordering Metis(const MetisIndex &met)
Compute an ordering determined by METIS from a VariableIndex.
Definition: Ordering.cpp:212
OrderingType
Type of ordering to use.
Definition: Ordering.h:41
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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:101
Ordering(ITERATOR firstKey, ITERATOR lastKey)
Create an ordering using iterators over keys.
Definition: Ordering.h:61
Definition: Ordering.h:34