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 
19 #pragma once
20 
21 #include <gtsam/inference/Key.h>
25 #include <gtsam/base/FastSet.h>
26 
27 #include <boost/assign/list_inserter.hpp>
28 #include <algorithm>
29 #include <vector>
30 
31 namespace gtsam {
32 
33 class Ordering: public std::vector<Key> {
34 protected:
35  typedef std::vector<Key> Base;
36 
37 public:
38 
40  enum OrderingType {
41  COLAMD, METIS, NATURAL, CUSTOM
42  };
43 
44  typedef Ordering This;
45  typedef boost::shared_ptr<This> shared_ptr;
46 
48  GTSAM_EXPORT
50  }
51 
53  template<typename KEYS>
54  explicit Ordering(const KEYS& keys) :
55  Base(keys.begin(), keys.end()) {
56  }
57 
59  template<typename ITERATOR>
60  Ordering(ITERATOR firstKey, ITERATOR lastKey) :
61  Base(firstKey, lastKey) {
62  }
63 
66  boost::assign::list_inserter<boost::assign_detail::call_push_back<This> > operator+=(
67  Key key) {
68  return boost::assign::make_list_inserter(
69  boost::assign_detail::call_push_back<This>(*this))(key);
70  }
71 
74 
76 
80  template<class FACTOR>
81  static Ordering Colamd(const FactorGraph<FACTOR>& graph) {
82  if (graph.empty())
83  return Ordering();
84  else
85  return Colamd(VariableIndex(graph));
86  }
87 
89  static GTSAM_EXPORT Ordering Colamd(const VariableIndex& variableIndex);
90 
99  template<class FACTOR>
101  const std::vector<Key>& constrainLast, bool forceOrder = false) {
102  if (graph.empty())
103  return Ordering();
104  else
105  return ColamdConstrainedLast(VariableIndex(graph), constrainLast, forceOrder);
106  }
107 
114  static GTSAM_EXPORT Ordering ColamdConstrainedLast(
115  const VariableIndex& variableIndex, const std::vector<Key>& constrainLast,
116  bool forceOrder = false);
117 
126  template<class FACTOR>
128  const std::vector<Key>& constrainFirst, bool forceOrder = false) {
129  if (graph.empty())
130  return Ordering();
131  else
132  return ColamdConstrainedFirst(VariableIndex(graph), constrainFirst, forceOrder);
133  }
134 
142  static GTSAM_EXPORT Ordering ColamdConstrainedFirst(
143  const VariableIndex& variableIndex,
144  const std::vector<Key>& constrainFirst, bool forceOrder = false);
145 
155  template<class FACTOR>
157  const FastMap<Key, int>& groups) {
158  if (graph.empty())
159  return Ordering();
160  else
161  return ColamdConstrained(VariableIndex(graph), groups);
162  }
163 
171  static GTSAM_EXPORT Ordering ColamdConstrained(
172  const VariableIndex& variableIndex, const FastMap<Key, int>& groups);
173 
175  template<class FACTOR>
177  KeySet src = fg.keys();
178  std::vector<Key> keys(src.begin(), src.end());
179  std::stable_sort(keys.begin(), keys.end());
180  return Ordering(keys);
181  }
182 
184  template<class FACTOR>
185  static GTSAM_EXPORT void CSRFormat(std::vector<int>& xadj,
186  std::vector<int>& adj, const FactorGraph<FACTOR>& graph);
187 
189  static GTSAM_EXPORT Ordering Metis(const MetisIndex& met);
190 
191  template<class FACTOR>
192  static Ordering Metis(const FactorGraph<FACTOR>& graph) {
193  return Metis(MetisIndex(graph));
194  }
195 
197 
199 
200  template<class FACTOR>
201  static Ordering Create(OrderingType orderingType,
202  const FactorGraph<FACTOR>& graph) {
203  if (graph.empty())
204  return Ordering();
205 
206  switch (orderingType) {
207  case COLAMD:
208  return Colamd(graph);
209  case METIS:
210  return Metis(graph);
211  case NATURAL:
212  return Natural(graph);
213  case CUSTOM:
214  throw std::runtime_error(
215  "Ordering::Create error: called with CUSTOM ordering type.");
216  default:
217  throw std::runtime_error(
218  "Ordering::Create error: called with unknown ordering type.");
219  }
220  }
221 
223 
225 
226  GTSAM_EXPORT
227  void print(const std::string& str = "", const KeyFormatter& keyFormatter =
228  DefaultKeyFormatter) const;
229 
230  GTSAM_EXPORT
231  bool equals(const Ordering& other, double tol = 1e-9) const;
232 
234 
235 private:
237  static GTSAM_EXPORT Ordering ColamdConstrained(
238  const VariableIndex& variableIndex, std::vector<int>& cmember);
239 
242  template<class ARCHIVE>
243  void serialize(ARCHIVE & ar, const unsigned int version) {
244  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
245  }
246 };
247 
249 template<> struct traits<Ordering> : public Testable<Ordering> {
250 };
251 
252 }
253 
static GTSAM_EXPORT Ordering Metis(const MetisIndex &met)
Compute an ordering determined by METIS from a VariableIndex.
Definition: Ordering.cpp:201
static Ordering ColamdConstrained(const FactorGraph< FACTOR > &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:156
Ordering This
Typedef to this class.
Definition: Ordering.h:44
GTSAM_EXPORT Ordering()
Create an empty ordering.
Definition: Ordering.h:49
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:66
Factor Graph Base Class.
OrderingType
Type of ordering to use.
Definition: Ordering.h:40
static GTSAM_EXPORT void CSRFormat(std::vector< int > &xadj, std::vector< int > &adj, const FactorGraph< FACTOR > &graph)
METIS Formatting function.
friend class boost::serialization::access
Serialization function.
Definition: Ordering.h:241
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
static Ordering ColamdConstrainedLast(const FactorGraph< FACTOR > &graph, const std::vector< Key > &constrainLast, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition: Ordering.h:100
The VariableIndex class computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:42
static Ordering Natural(const FactorGraph< FACTOR > &fg)
Return a natural Ordering. Typically used by iterative solvers.
Definition: Ordering.h:176
Template to create a binary predicate.
Definition: Testable.h:110
FastMap< Key, size_t > invert() const
Invert (not reverse) the ordering - returns a map from key to order position.
Definition: Ordering.cpp:36
bool empty() const
Check if the graph is empty (null factors set by remove() will cause this to return false)...
Definition: FactorGraph.h:265
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Definition: Ordering.h:33
The MetisIndex class converts a factor graph into the Compressed Sparse Row format for use in METIS a...
Definition: MetisIndex.h:46
A thin wrapper around std::set that uses boost&#39;s fast_pool_allocator.
Ordering(ITERATOR firstKey, ITERATOR lastKey)
Create an ordering using iterators over keys.
Definition: Ordering.h:60
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition: FactorGraph-inst.h:75
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: BayesTree.h:32
static Ordering ColamdConstrainedFirst(const FactorGraph< FACTOR > &graph, const std::vector< Key > &constrainFirst, bool forceOrder=false)
Compute a fill-reducing ordering using constrained COLAMD from a factor graph (see details for note o...
Definition: Ordering.h:127
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: Ordering.h:45
Ordering(const KEYS &keys)
Create from a container.
Definition: Ordering.h:54
static Ordering Colamd(const FactorGraph< FACTOR > &graph)
Compute a fill-reducing ordering using COLAMD from a factor graph (see details for note on performanc...
Definition: Ordering.h:81
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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