gtsam
Loading...
Searching...
No Matches
FactorGraph.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// \callgraph
22
23#pragma once
24
26#include <gtsam/inference/Key.h>
28#include <gtsam/base/Testable.h>
29
30#include <Eigen/Core> // for Eigen::aligned_allocator
31
32#if GTSAM_ENABLE_BOOST_SERIALIZATION
33#include <boost/serialization/nvp.hpp>
34#include <boost/serialization/vector.hpp>
35#endif
36
37#include <string>
38#include <type_traits>
39#include <utility>
40#include <iosfwd>
41
42namespace gtsam {
45
46// Forward declarations
47template <class CLIQUE>
48class BayesTree;
49
50class HybridValues;
51
57template <class FACTOR>
59 public:
60 typedef FACTOR FactorType;
61 typedef std::shared_ptr<FACTOR>
63 typedef sharedFactor value_type;
64 typedef typename FastVector<sharedFactor>::iterator iterator;
65 typedef typename FastVector<sharedFactor>::const_iterator const_iterator;
66
67 private:
68 typedef FactorGraph<FACTOR> This;
69 typedef std::shared_ptr<This>
70 shared_ptr;
71
73 template <typename DERIVEDFACTOR>
74 using IsDerived = typename std::enable_if<
75 std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type;
76
78 template <typename T>
79 using HasDerivedValueType = typename std::enable_if<
80 std::is_base_of<FactorType, typename T::value_type>::value>::type;
81
83 template <typename T>
84 using HasDerivedElementType = typename std::enable_if<std::is_base_of<
85 FactorType, typename T::value_type::element_type>::value>::type;
86
87 protected:
89 GTSAM_CONCEPT_TESTABLE_TYPE(FACTOR)
90
91
93
95 bool isEqual(const FactorGraph& other) const {
96 return factors_ == other.factors_;
97 }
98
101
104
106 template <typename ITERATOR>
107 FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) {
108 push_back(firstFactor, lastFactor);
109 }
110
112 template <class CONTAINER>
113 explicit FactorGraph(const CONTAINER& factors) {
114 push_back(factors);
115 }
116
118
119 public:
122
125 virtual ~FactorGraph() = default;
126
131 template <class DERIVEDFACTOR, typename = IsDerived<DERIVEDFACTOR>>
132 FactorGraph(std::initializer_list<std::shared_ptr<DERIVEDFACTOR>> sharedFactors)
133 : factors_(sharedFactors) {}
134
138
143 void reserve(size_t size) { factors_.reserve(size); }
144
146 template <class DERIVEDFACTOR>
147 IsDerived<DERIVEDFACTOR> push_back(std::shared_ptr<DERIVEDFACTOR> factor) {
148 factors_.push_back(std::shared_ptr<FACTOR>(factor));
149 }
150
152 template <class DERIVEDFACTOR, class... Args>
153 IsDerived<DERIVEDFACTOR> emplace_shared(Args&&... args) {
154 factors_.push_back(std::allocate_shared<DERIVEDFACTOR>(
155 Eigen::aligned_allocator<DERIVEDFACTOR>(),
156 std::forward<Args>(args)...));
157 }
158
163 template <class DERIVEDFACTOR>
164 IsDerived<DERIVEDFACTOR> push_back(const DERIVEDFACTOR& factor) {
165 factors_.push_back(std::allocate_shared<DERIVEDFACTOR>(
166 Eigen::aligned_allocator<DERIVEDFACTOR>(), factor));
167 }
168
170 template <class DERIVEDFACTOR>
171 IsDerived<DERIVEDFACTOR> add(std::shared_ptr<DERIVEDFACTOR> factor) {
172 push_back(factor);
173 }
174
176 template <class DERIVEDFACTOR>
177 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value,
178 This>::type&
179 operator+=(std::shared_ptr<DERIVEDFACTOR> factor) {
180 push_back(factor);
181 return *this;
182 }
183
189 template <class DERIVEDFACTOR>
190 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value, This>::type& operator,(
191 std::shared_ptr<DERIVEDFACTOR> factor) {
192 push_back(factor);
193 return *this;
194 }
195
199
204 template <typename ITERATOR>
205 HasDerivedElementType<ITERATOR> push_back(ITERATOR firstFactor,
206 ITERATOR lastFactor) {
207 factors_.insert(end(), firstFactor, lastFactor);
208 }
209
211 template <typename ITERATOR>
212 HasDerivedValueType<ITERATOR> push_back(ITERATOR firstFactor,
213 ITERATOR lastFactor) {
214 for (ITERATOR f = firstFactor; f != lastFactor; ++f) push_back(*f);
215 }
216
220
225 template <typename CONTAINER>
226 HasDerivedElementType<CONTAINER> push_back(const CONTAINER& container) {
227 push_back(container.begin(), container.end());
228 }
229
231 template <typename CONTAINER>
232 HasDerivedValueType<CONTAINER> push_back(const CONTAINER& container) {
233 push_back(container.begin(), container.end());
234 }
235
240 template <class FACTOR_OR_CONTAINER>
241 void add(const FACTOR_OR_CONTAINER& factorOrContainer) {
242 push_back(factorOrContainer);
243 }
244
249 template <class FACTOR_OR_CONTAINER>
250 This& operator+=(const FACTOR_OR_CONTAINER& factorOrContainer) {
251 push_back(factorOrContainer);
252 return *this;
253 }
254
258
264 template <class CLIQUE>
265 typename std::enable_if<
266 std::is_base_of<This, typename CLIQUE::FactorGraphType>::value>::type
267 push_back(const BayesTree<CLIQUE>& bayesTree) {
268 bayesTree.addFactorsToGraph(this);
269 }
270
275 template <typename CONTAINER, typename = HasDerivedElementType<CONTAINER>>
276 FactorIndices add_factors(const CONTAINER& factors,
277 bool useEmptySlots = false);
278
282
284 virtual void print(const std::string& s = "FactorGraph",
285 const KeyFormatter& formatter = DefaultKeyFormatter) const;
286
288 bool equals(const This& fg, double tol = 1e-9) const;
290
291 public:
294
297 size_t size() const { return factors_.size(); }
298
301 bool empty() const { return factors_.empty(); }
302
306 const sharedFactor at(size_t i) const { return factors_.at(i); }
307
311 sharedFactor& at(size_t i) { return factors_.at(i); }
312
317 template <typename F>
318 std::shared_ptr<F> at(size_t i) {
319 return std::dynamic_pointer_cast<F>(factors_.at(i));
320 }
321
323 template <typename F>
324 const std::shared_ptr<F> at(size_t i) const {
325 return std::dynamic_pointer_cast<F>(factors_.at(i));
326 }
327
331 const sharedFactor operator[](size_t i) const { return at(i); }
332
336 sharedFactor& operator[](size_t i) { return at(i); }
337
339 const_iterator begin() const { return factors_.begin(); }
340
342 const_iterator end() const { return factors_.end(); }
343
345 sharedFactor front() const { return factors_.front(); }
346
348 sharedFactor back() const { return factors_.back(); }
349
351 double error(const HybridValues &values) const;
352
356
358 iterator begin() { return factors_.begin(); }
359
361 iterator end() { return factors_.end(); }
362
367 virtual void resize(size_t size) { factors_.resize(size); }
368
371 void remove(size_t i) { factors_.at(i).reset(); }
372
374 void replace(size_t index, sharedFactor factor) { at(index) = factor; }
375
377 iterator erase(iterator item) { return factors_.erase(item); }
378
380 iterator erase(iterator first, iterator last) {
381 return factors_.erase(first, last);
382 }
383
387
389 void dot(std::ostream& os,
390 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
391 const DotWriter& writer = DotWriter()) const;
392
394 std::string dot(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
395 const DotWriter& writer = DotWriter()) const;
396
398 void saveGraph(const std::string& filename,
399 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
400 const DotWriter& writer = DotWriter()) const;
401
405
407 size_t nrFactors() const;
408
411 KeySet keys() const;
412
417
420 inline bool exists(size_t idx) const { return idx < size() && at(idx); }
421
422 private:
423#if GTSAM_ENABLE_BOOST_SERIALIZATION
425 friend class boost::serialization::access;
426 template <class ARCHIVE>
427 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
428 ar& BOOST_SERIALIZATION_NVP(factors_);
429 }
430#endif
431
433}; // FactorGraph
434} // namespace gtsam
435
A thin wrapper around std::vector that uses a custom allocator.
Concept check for values that can be used in unit tests.
Graphviz formatter.
Factor Graph Base Class.
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
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition Factor.h:37
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
virtual void print(const std::string &s="FactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const
Print out graph to std::cout, with optional key formatter.
Definition FactorGraph-inst.h:37
bool isEqual(const FactorGraph &other) const
Definition FactorGraph.h:95
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition FactorGraph-inst.h:85
bool empty() const
Check if the graph is empty (null factors set by remove() will cause this to return false).
Definition FactorGraph.h:301
FactorIndices add_factors(const CONTAINER &factors, bool useEmptySlots=false)
Add new factors to a factor graph and returns a list of new factor indices, optionally finding and re...
Definition FactorGraph-inst.h:109
void dot(std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
Output to graphviz format, stream version.
Definition FactorGraph-inst.h:141
iterator erase(iterator item)
Erase factor and rearrange other factors to take up the empty space.
Definition FactorGraph.h:377
void add(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition FactorGraph.h:241
iterator erase(iterator first, iterator last)
Erase factors and rearrange other factors to take up the empty space.
Definition FactorGraph.h:380
FactorGraph(std::initializer_list< std::shared_ptr< DERIVEDFACTOR > > sharedFactors)
Constructor that takes an initializer list of shared pointers.
Definition FactorGraph.h:132
sharedFactor back() const
Get the last factor.
Definition FactorGraph.h:348
FactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects).
Definition FactorGraph.h:113
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition FactorGraph.h:147
void remove(size_t i)
delete factor without re-arranging indexes by inserting a nullptr pointer
Definition FactorGraph.h:371
KeyVector keyVector() const
Potentially slow function to return all keys involved, sorted, as a vector.
Definition FactorGraph-inst.h:95
HasDerivedValueType< CONTAINER > push_back(const CONTAINER &container)
Push back non-pointer objects in a container (factors are copied).
Definition FactorGraph.h:232
FactorGraph()
Default constructor.
Definition FactorGraph.h:103
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition FactorGraph.h:153
sharedFactor & operator[](size_t i)
Get a specific factor by index (this does not check array bounds, as opposed to at() which does).
Definition FactorGraph.h:336
sharedFactor & at(size_t i)
Get a specific factor by index (this checks array bounds and may throw an exception,...
Definition FactorGraph.h:311
std::enable_if< std::is_base_of< This, typenameCLIQUE::FactorGraphType >::value >::type push_back(const BayesTree< CLIQUE > &bayesTree)
Push back a BayesTree as a collection of factors.
Definition FactorGraph.h:267
double error(const HybridValues &values) const
Add error for all factors.
Definition FactorGraph-inst.h:66
FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Constructor from iterator over factors (shared_ptr or plain objects).
Definition FactorGraph.h:107
std::shared_ptr< F > at(size_t i)
Get a specific factor by index and typecast to factor type F (this checks array bounds and may throw ...
Definition FactorGraph.h:318
std::shared_ptr< DiscreteFactor > sharedFactor
Definition FactorGraph.h:62
virtual void resize(size_t size)
Directly resize the number of factors in the graph.
Definition FactorGraph.h:367
size_t nrFactors() const
return the number of non-null factors
Definition FactorGraph-inst.h:76
size_t size() const
Definition FactorGraph.h:297
HasDerivedValueType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator (factors are copied).
Definition FactorGraph.h:212
sharedFactor front() const
Get the first factor.
Definition FactorGraph.h:345
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, This >::type & operator,(std::shared_ptr< DERIVEDFACTOR > factor)
Overload comma operator to allow for append chaining.
Definition FactorGraph.h:190
const_iterator end() const
Iterator to end of factors.
Definition FactorGraph.h:342
HasDerivedElementType< CONTAINER > push_back(const CONTAINER &container)
Push back many factors as shared_ptr's in a container (factors are not copied).
Definition FactorGraph.h:226
DiscreteFactor FactorType
Definition FactorGraph.h:60
void replace(size_t index, sharedFactor factor)
replace a factor by index
Definition FactorGraph.h:374
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
Definition FactorGraph-inst.h:50
const std::shared_ptr< F > at(size_t i) const
Const version of templated at method.
Definition FactorGraph.h:324
iterator begin()
non-const STL-style begin()
Definition FactorGraph.h:358
This & operator+=(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition FactorGraph.h:250
const_iterator begin() const
Iterator to beginning of factors.
Definition FactorGraph.h:339
HasDerivedElementType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator over shared_ptr (factors are not copied).
Definition FactorGraph.h:205
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, This >::type & operator+=(std::shared_ptr< DERIVEDFACTOR > factor)
Append factor to factor graph.
Definition FactorGraph.h:179
bool exists(size_t idx) const
MATLAB interface utility: Checks whether a factor index idx exists in the graph and is a live pointer...
Definition FactorGraph.h:420
const sharedFactor operator[](size_t i) const
Get a specific factor by index (this does not check array bounds, as opposed to at() which does).
Definition FactorGraph.h:331
std::string dot(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
Output to graphviz format string.
Definition FactorGraph-inst.h:168
IsDerived< DERIVEDFACTOR > push_back(const DERIVEDFACTOR &factor)
Add a factor by value, will be copy-constructed (use push_back with a shared_ptr to avoid the copy).
Definition FactorGraph.h:164
iterator end()
non-const STL-style end()
Definition FactorGraph.h:361
FastVector< sharedFactor > factors_
Definition FactorGraph.h:92
const sharedFactor at(size_t i) const
Get a specific factor by index (this checks array bounds and may throw an exception,...
Definition FactorGraph.h:306
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition FactorGraph.h:171
void saveGraph(const std::string &filename, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
output to file with graphviz format.
Definition FactorGraph-inst.h:177
virtual ~FactorGraph()=default
Default destructor Public and virtual so boost serialization can call it.
void reserve(size_t size)
Reserve space for the specified number of factors if you know in advance how many there will be (work...
Definition FactorGraph.h:143
Bayes tree.
Definition BayesTree.h:77
void addFactorsToGraph(FactorGraph< FactorType > *graph) const
Add all cliques in this BayesTree to the specified factor graph.
Definition BayesTree-inst.h:171
DotWriter is a helper class for writing graphviz .dot files.
Definition DotWriter.h:36