gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
18 #pragma once
19 
20 #include <algorithm>
21 #include <vector>
22 #include <boost/assign/list_inserter.hpp>
23 
24 #include <gtsam/base/FastSet.h>
25 #include <gtsam/inference/Key.h>
28 
29 namespace gtsam {
30  class Ordering : public std::vector<Key> {
31  protected:
32  typedef std::vector<Key> Base;
33 
34  public:
35  typedef Ordering This;
36  typedef boost::shared_ptr<This> shared_ptr;
37 
39  GTSAM_EXPORT Ordering() {}
40 
42  template<typename KEYS>
43  explicit Ordering(const KEYS& keys) : Base(keys.begin(), keys.end()) {}
44 
46  template<typename ITERATOR>
47  Ordering(ITERATOR firstKey, ITERATOR lastKey) : Base(firstKey, lastKey) {}
48 
51  boost::assign::list_inserter<boost::assign_detail::call_push_back<This> >
52  operator+=(Key key) {
53  return boost::assign::make_list_inserter(boost::assign_detail::call_push_back<This>(*this))(key);
54  }
55 
58 
60 
64  template<class FACTOR>
65  static Ordering COLAMD(const FactorGraph<FACTOR>& graph) {
66  return COLAMD(VariableIndex(graph)); }
67 
69  static GTSAM_EXPORT Ordering COLAMD(const VariableIndex& variableIndex);
70 
79  template<class FACTOR>
81  const std::vector<Key>& constrainLast, bool forceOrder = false) {
82  return COLAMDConstrainedLast(VariableIndex(graph), constrainLast, forceOrder); }
83 
90  static GTSAM_EXPORT Ordering COLAMDConstrainedLast(const VariableIndex& variableIndex,
91  const std::vector<Key>& constrainLast, bool forceOrder = false);
92 
101  template<class FACTOR>
103  const std::vector<Key>& constrainFirst, bool forceOrder = false) {
104  return COLAMDConstrainedFirst(VariableIndex(graph), constrainFirst, forceOrder); }
105 
113  static GTSAM_EXPORT Ordering COLAMDConstrainedFirst(const VariableIndex& variableIndex,
114  const std::vector<Key>& constrainFirst, bool forceOrder = false);
115 
125  template<class FACTOR>
127  const FastMap<Key, int>& groups) {
128  return COLAMDConstrained(VariableIndex(graph), groups); }
129 
137  static GTSAM_EXPORT Ordering COLAMDConstrained(const VariableIndex& variableIndex,
138  const FastMap<Key, int>& groups);
139 
141  template <class FACTOR>
143  FastSet<Key> src = fg.keys();
144  std::vector<Key> keys(src.begin(), src.end());
145  std::stable_sort(keys.begin(), keys.end());
146  return Ordering(keys);
147  }
148 
150 
152 
153  GTSAM_EXPORT void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
154 
155  GTSAM_EXPORT bool equals(const Ordering& other, double tol = 1e-9) const;
156 
158 
159  private:
161  static GTSAM_EXPORT Ordering COLAMDConstrained(
162  const VariableIndex& variableIndex, std::vector<int>& cmember);
163 
166  template<class ARCHIVE>
167  void serialize(ARCHIVE & ar, const unsigned int version) {
168  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
169  }
170  };
171 }
172 
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:52
static Ordering Natural(const FactorGraph< FACTOR > &fg)
Return a natural Ordering. Typically used by iterative solvers.
Definition: Ordering.h:142
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: BayesTree.h:32
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:65
A thin wrapper around std::set that uses boost's fast_pool_allocator.
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:80
FastMap< Key, size_t > invert() const
Invert (not reverse) the ordering - returns a map from key to order position.
Definition: Ordering.cpp:31
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:102
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:126
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: Ordering.h:36
Ordering(const KEYS &keys)
Create from a container.
Definition: Ordering.h:43
Template to create a binary predicate.
Definition: Testable.h:102
The VariableIndex class computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:42
size_t Key
Integer nonlinear key type.
Definition: types.h:59
Ordering(ITERATOR firstKey, ITERATOR lastKey)
Create an ordering using iterators over keys.
Definition: Ordering.h:47
FastSet< Key > keys() const
Potentially very slow function to return all keys involved.
Definition: FactorGraph-inst.h:75
Definition: Ordering.h:30
Ordering This
Typedef to this class.
Definition: Ordering.h:35
friend class boost::serialization::access
Serialization function.
Definition: Ordering.h:165
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
GTSAM_EXPORT Ordering()
Create an empty ordering.
Definition: Ordering.h:39
Factor Graph Base Class.