gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianFactorGraph.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 
22 #pragma once
23 
29 #include <gtsam/linear/Errors.h> // Included here instead of fw-declared so we can use Errors::iterator
30 
31 namespace gtsam {
32 
33  // Forward declarations
34  class GaussianFactorGraph;
35  class GaussianFactor;
36  class GaussianConditional;
37  class GaussianBayesNet;
38  class GaussianEliminationTree;
39  class GaussianBayesTree;
40  class GaussianJunctionTree;
41 
42  /* ************************************************************************* */
44  {
52  static std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> >
54  DefaultEliminate(const FactorGraphType& factors, const Ordering& keys) {
55  return EliminatePreferCholesky(factors, keys); }
56  };
57 
58  /* ************************************************************************* */
65  class GTSAM_EXPORT GaussianFactorGraph :
66  public FactorGraph<GaussianFactor>,
67  public EliminateableFactorGraph<GaussianFactorGraph>
68  {
69  public:
70 
74  typedef boost::shared_ptr<This> shared_ptr;
75 
78 
80  template<typename ITERATOR>
81  GaussianFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) : Base(firstFactor, lastFactor) {}
82 
84  template<class CONTAINER>
85  explicit GaussianFactorGraph(const CONTAINER& factors) : Base(factors) {}
86 
88  template<class DERIVEDFACTOR>
90 
92  virtual ~GaussianFactorGraph() {}
93 
96 
97  bool equals(const This& fg, double tol = 1e-9) const;
98 
100 
102  void add(const GaussianFactor& factor) { push_back(factor.clone()); }
103 
105  void add(const sharedFactor& factor) { push_back(factor); }
106 
108  void add(const Vector& b) {
109  add(JacobianFactor(b)); }
110 
112  void add(Key key1, const Matrix& A1,
113  const Vector& b, const SharedDiagonal& model = SharedDiagonal()) {
114  add(JacobianFactor(key1,A1,b,model)); }
115 
117  void add(Key key1, const Matrix& A1,
118  Key key2, const Matrix& A2,
119  const Vector& b, const SharedDiagonal& model = SharedDiagonal()) {
120  add(JacobianFactor(key1,A1,key2,A2,b,model)); }
121 
123  void add(Key key1, const Matrix& A1,
124  Key key2, const Matrix& A2,
125  Key key3, const Matrix& A3,
126  const Vector& b, const SharedDiagonal& model = SharedDiagonal()) {
127  add(JacobianFactor(key1,A1,key2,A2,key3,A3,b,model)); }
128 
130  template<class TERMS>
131  void add(const TERMS& terms, const Vector &b, const SharedDiagonal& model = SharedDiagonal()) {
132  add(JacobianFactor(terms,b,model)); }
133 
139  Keys keys() const;
140 
141  /* return a map of (Key, dimension) */
142  std::map<Key, size_t> getKeyDimMap() const;
143 
144  std::vector<size_t> getkeydim() const;
145 
147  double error(const VectorValues& x) const {
148  double total_error = 0.;
149  BOOST_FOREACH(const sharedFactor& factor, *this){
150  if(factor)
151  total_error += factor->error(x);
152  }
153  return total_error;
154  }
155 
157  double probPrime(const VectorValues& c) const {
158  return exp(-0.5 * error(c));
159  }
160 
166  virtual GaussianFactorGraph clone() const;
167 
172  virtual GaussianFactorGraph::shared_ptr cloneToPtr() const;
173 
180  GaussianFactorGraph negate() const;
181 
184 
190  std::vector<boost::tuple<size_t, size_t, double> > sparseJacobian() const;
191 
197  Matrix sparseJacobian_() const;
198 
206  Matrix augmentedJacobian(boost::optional<const Ordering&> optionalOrdering = boost::none) const;
207 
215  std::pair<Matrix,Vector> jacobian(boost::optional<const Ordering&> optionalOrdering = boost::none) const;
216 
228  Matrix augmentedHessian(boost::optional<const Ordering&> optionalOrdering = boost::none) const;
229 
236  std::pair<Matrix,Vector> hessian(boost::optional<const Ordering&> optionalOrdering = boost::none) const;
237 
239  virtual VectorValues hessianDiagonal() const;
240 
242  virtual std::map<Key,Matrix> hessianBlockDiagonal() const;
243 
248  VectorValues optimize(OptionalOrdering ordering = boost::none,
249  const Eliminate& function = EliminationTraitsType::DefaultEliminate) const;
250 
260  VectorValues gradient(const VectorValues& x0) const;
261 
269  virtual VectorValues gradientAtZero() const;
270 
295  VectorValues optimizeGradientSearch() const;
296 
298  VectorValues transposeMultiply(const Errors& e) const;
299 
301  void transposeMultiplyAdd(double alpha, const Errors& e, VectorValues& x) const;
302 
304  Errors gaussianErrors(const VectorValues& x) const;
305 
307  Errors operator*(const VectorValues& x) const;
308 
310  void multiplyHessianAdd(double alpha, const VectorValues& x,
311  VectorValues& y) const;
312 
314  void multiplyHessianAdd(double alpha, const double* x,
315  double* y) const;
316 
318  void multiplyInPlace(const VectorValues& x, Errors& e) const;
319 
321  void multiplyInPlace(const VectorValues& x, const Errors::iterator& e) const;
322 
324 
325  private:
327  friend class boost::serialization::access;
328  template<class ARCHIVE>
329  void serialize(ARCHIVE & ar, const unsigned int version) {
330  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
331  }
332 
333  };
334 
339  GTSAM_EXPORT bool hasConstraints(const GaussianFactorGraph& factors);
340 
341  /****** Linear Algebra Opeations ******/
342 
344  //GTSAM_EXPORT void residual(const GaussianFactorGraph& fg, const VectorValues &x, VectorValues &r);
345  //GTSAM_EXPORT void multiply(const GaussianFactorGraph& fg, const VectorValues &x, VectorValues &r);
346 
347 } // namespace gtsam
void add(const sharedFactor &factor)
Add a factor by pointer - stores pointer without copying the factor.
Definition: GaussianFactorGraph.h:105
void transposeMultiplyAdd(double alpha, const Matrix &A, const Vector &e, Vector &x)
BLAS Level-2 style x <- x + alpha*A'*e.
Definition: Matrix.cpp:168
GaussianEliminationTree EliminationTreeType
Type of elimination tree.
Definition: GaussianFactorGraph.h:49
GaussianFactorGraph(const FactorGraph< DERIVEDFACTOR > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition: GaussianFactorGraph.h:89
virtual ~GaussianFactorGraph()
Virtual destructor.
Definition: GaussianFactorGraph.h:92
Variable elimination algorithms for factor graphs.
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: BayesTree.h:32
double error(const VectorValues &x) const
unnormalized error
Definition: GaussianFactorGraph.h:147
A Bayes net made from linear-Gaussian densities.
Definition: GaussianBayesNet.h:30
GaussianBayesNet BayesNetType
Type of Bayes net from sequential elimination.
Definition: GaussianFactorGraph.h:48
GaussianFactor FactorType
Type of factors in factor graph.
Definition: GaussianFactorGraph.h:45
bool hasConstraints(const GaussianFactorGraph &factors)
Evaluates whether linear factors have any constrained noise models.
Definition: GaussianFactorGraph.cpp:383
void add(const GaussianFactor &factor)
Add a factor by value - makes a copy.
Definition: GaussianFactorGraph.h:102
Contains the HessianFactor class, a general quadratic factor.
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:82
Definition: GaussianEliminationTree.h:27
void add(Key key1, const Matrix &A1, Key key2, const Matrix &A2, const Vector &b, const SharedDiagonal &model=SharedDiagonal())
Add a binary factor.
Definition: GaussianFactorGraph.h:117
GaussianFactorGraph FactorGraphType
Type of the factor graph (e.g. GaussianFactorGraph)
Definition: GaussianFactorGraph.h:46
GaussianFactorGraph()
Default constructor.
Definition: GaussianFactorGraph.h:77
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:249
boost::shared_ptr< GaussianFactor > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:84
static std::pair< boost::shared_ptr< ConditionalType >, boost::shared_ptr< FactorType > > DefaultEliminate(const FactorGraphType &factors, const Ordering &keys)
The default dense elimination function.
Definition: GaussianFactorGraph.h:54
A factor with a quadratic error function - a Gaussian.
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactorGraph.h:74
GaussianBayesTree BayesTreeType
Type of Bayes tree.
Definition: GaussianFactorGraph.h:50
GaussianFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Construct from iterator over factors.
Definition: GaussianFactorGraph.h:81
Template to create a binary predicate.
Definition: Testable.h:102
void add(Key key1, const Matrix &A1, Key key2, const Matrix &A2, Key key3, const Matrix &A3, const Vector &b, const SharedDiagonal &model=SharedDiagonal())
Add a ternary factor.
Definition: GaussianFactorGraph.h:123
GaussianConditional ConditionalType
Type of conditionals from elimination.
Definition: GaussianFactorGraph.h:47
size_t Key
Integer nonlinear key type.
Definition: types.h:59
virtual GaussianFactor::shared_ptr clone() const =0
Clone a factor (make a deep copy)
Point3 optimize(const NonlinearFactorGraph &graph, const Values &values, Key landmarkKey)
Optimize for triangulation.
Definition: triangulation.cpp:72
double probPrime(const VectorValues &c) const
Unnormalized probability.
Definition: GaussianFactorGraph.h:157
Definition: GaussianJunctionTree.h:48
GaussianFactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition: GaussianFactorGraph.h:85
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
void add(Key key1, const Matrix &A1, const Vector &b, const SharedDiagonal &model=SharedDiagonal())
Add a unary factor.
Definition: GaussianFactorGraph.h:112
Traits class for eliminateable factor graphs, specifies the types that result from elimination...
Definition: BayesTreeCliqueBase.h:28
void add(const Vector &b)
Add a null factor.
Definition: GaussianFactorGraph.h:108
EliminateableFactorGraph< This > BaseEliminateable
Typedef to base elimination class.
Definition: GaussianFactorGraph.h:73
GaussianFactorGraph This
Typedef to this class.
Definition: GaussianFactorGraph.h:71
vector of errors
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:65
Definition: Ordering.h:30
vector of errors
Definition: Errors.h:33
FactorGraph< GaussianFactor > Base
Typedef to base factor graph type.
Definition: GaussianFactorGraph.h:72
void add(const TERMS &terms, const Vector &b, const SharedDiagonal &model=SharedDiagonal())
Add an n-ary factor.
Definition: GaussianFactorGraph.h:131
A conditional Gaussian functions as the node in a Bayes network It has a set of parents y...
Definition: GaussianConditional.h:36
A Bayes tree representing a Gaussian density.
Definition: GaussianBayesTree.h:49
GaussianJunctionTree JunctionTreeType
Type of Junction tree.
Definition: GaussianFactorGraph.h:51
EliminateableFactorGraph is a base class for factor graphs that contains elimination algorithms...
Definition: EliminateableFactorGraph.h:56
FastSet< Key > Keys
Return the set of variables involved in the factors (computes a set union).
Definition: GaussianFactorGraph.h:138
Factor Graph Base Class.
An abstract virtual base class for JacobianFactor and HessianFactor.
Definition: GaussianFactor.h:35