| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file LinearizedFactor.cpp | 
					
						
							|  |  |  |  * @brief A dummy factor that allows a linear factor to act as a nonlinear factor | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/nonlinear/LinearizedFactor.h>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2024-12-05 00:43:51 +08:00
										 |  |  | #include <cassert>
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | LinearizedGaussianFactor::LinearizedGaussianFactor( | 
					
						
							|  |  |  |     const GaussianFactor::shared_ptr& gaussian, const Values& lin_points) | 
					
						
							|  |  |  | : NonlinearFactor(gaussian->keys()) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   // Extract the keys and linearization points
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(const Key& key: gaussian->keys()) { | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  |     // extract linearization point
 | 
					
						
							|  |  |  |     assert(lin_points.exists(key)); | 
					
						
							|  |  |  |     this->lin_points_.insert(key, lin_points.at(key)); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // LinearizedJacobianFactor
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-13 05:41:05 +08:00
										 |  |  | LinearizedJacobianFactor::LinearizedJacobianFactor() { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | LinearizedJacobianFactor::LinearizedJacobianFactor( | 
					
						
							|  |  |  |     const JacobianFactor::shared_ptr& jacobian, const Values& lin_points) | 
					
						
							| 
									
										
										
										
											2013-08-13 05:41:05 +08:00
										 |  |  | : Base(jacobian, lin_points) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the dims array
 | 
					
						
							| 
									
										
										
										
											2013-02-25 03:09:52 +08:00
										 |  |  |   size_t *dims = (size_t *)alloca(sizeof(size_t) * (jacobian->size() + 1)); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   size_t index = 0; | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   for(JacobianFactor::const_iterator iter = jacobian->begin(); iter != jacobian->end(); ++iter) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     dims[index++] = jacobian->getDim(iter); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   dims[index] = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Update the BlockInfo accessor
 | 
					
						
							| 
									
										
										
										
											2013-08-13 05:41:05 +08:00
										 |  |  |   Ab_ = VerticalBlockMatrix(dims, dims+jacobian->size()+1, jacobian->rows()); | 
					
						
							|  |  |  |   // Get the Ab matrix from the Jacobian factor, with any covariance baked in
 | 
					
						
							|  |  |  |   Ab_.matrix() = jacobian->augmentedJacobian(); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void LinearizedJacobianFactor::print(const std::string& s, const KeyFormatter& keyFormatter) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::cout << s << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::cout << "Nonlinear Keys: "; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(const Key& key: this->keys()) | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     std::cout << keyFormatter(key) << " "; | 
					
						
							|  |  |  |   std::cout << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 05:04:12 +08:00
										 |  |  |   for(const_iterator key=begin(); key!=end(); ++key) { | 
					
						
							|  |  |  |     std::cout << "A[" << keyFormatter(*key) << "]=\n" << A(*key) << std::endl; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   std::cout << "b=\n" << b() << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   lin_points_.print("Linearization Point: "); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool LinearizedJacobianFactor::equals(const NonlinearFactor& expected, double tol) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const This *e = dynamic_cast<const This*> (&expected); | 
					
						
							|  |  |  |   if (e) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix thisMatrix = this->Ab_.range(0, Ab_.nBlocks()); | 
					
						
							|  |  |  |     Matrix rhsMatrix = e->Ab_.range(0, Ab_.nBlocks()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Base::equals(expected, tol) | 
					
						
							|  |  |  |         && lin_points_.equals(e->lin_points_, tol) | 
					
						
							|  |  |  |         && equal_with_abs_tol(thisMatrix, rhsMatrix, tol); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | double LinearizedJacobianFactor::error(const Values& c) const { | 
					
						
							|  |  |  |   Vector errorVector = error_vector(c); | 
					
						
							|  |  |  |   return 0.5 * errorVector.dot(errorVector); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  | std::shared_ptr<GaussianFactor> | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | LinearizedJacobianFactor::linearize(const Values& c) const { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the 'terms' data structure for the Jacobian constructor
 | 
					
						
							| 
									
										
										
										
											2013-11-09 00:35:28 +08:00
										 |  |  |   std::vector<std::pair<Key, Matrix> > terms; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(Key key: keys()) { | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  |     terms.push_back(std::make_pair(key, this->A(key))); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // compute rhs
 | 
					
						
							|  |  |  |   Vector b = -error_vector(c); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |   return std::shared_ptr<GaussianFactor>(new JacobianFactor(terms, b, noiseModel::Unit::Create(dim()))); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Vector LinearizedJacobianFactor::error_vector(const Values& c) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Vector errorVector = -b(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(Key key: this->keys()) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     const Value& newPt = c.at(key); | 
					
						
							|  |  |  |     const Value& linPt = lin_points_.at(key); | 
					
						
							|  |  |  |     Vector d = linPt.localCoordinates_(newPt); | 
					
						
							|  |  |  |     const constABlock A = this->A(key); | 
					
						
							|  |  |  |     errorVector += A * d; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return errorVector; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // LinearizedHessianFactor
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-13 05:41:05 +08:00
										 |  |  | LinearizedHessianFactor::LinearizedHessianFactor() { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | LinearizedHessianFactor::LinearizedHessianFactor( | 
					
						
							|  |  |  |     const HessianFactor::shared_ptr& hessian, const Values& lin_points) | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     : Base(hessian, lin_points), info_(hessian->info()) {} | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void LinearizedHessianFactor::print(const std::string& s, const KeyFormatter& keyFormatter) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::cout << s << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::cout << "Nonlinear Keys: "; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(const Key& key: this->keys()) | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     std::cout << keyFormatter(key) << " "; | 
					
						
							|  |  |  |   std::cout << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   gtsam::print(Matrix(info_.selfadjointView()), "Ab^T * Ab: "); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   lin_points_.print("Linearization Point: "); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool LinearizedHessianFactor::equals(const NonlinearFactor& expected, double tol) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const This *e = dynamic_cast<const This*> (&expected); | 
					
						
							|  |  |  |   if (e) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     Matrix thisMatrix = this->info_.selfadjointView(); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     thisMatrix(thisMatrix.rows()-1, thisMatrix.cols()-1) = 0.0; | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     Matrix rhsMatrix = e->info_.selfadjointView(); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     rhsMatrix(rhsMatrix.rows()-1, rhsMatrix.cols()-1) = 0.0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Base::equals(expected, tol) | 
					
						
							|  |  |  |         && lin_points_.equals(e->lin_points_, tol) | 
					
						
							|  |  |  |         && equal_with_abs_tol(thisMatrix, rhsMatrix, tol); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | double LinearizedHessianFactor::error(const Values& c) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Construct an error vector in key-order from the Values
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   Vector dx = Vector::Zero(dim()); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   size_t index = 0; | 
					
						
							|  |  |  |   for(unsigned int i = 0; i < this->size(); ++i){ | 
					
						
							|  |  |  |     Key key = this->keys()[i]; | 
					
						
							|  |  |  |     const Value& newPt = c.at(key); | 
					
						
							|  |  |  |     const Value& linPt = lin_points_.at(key); | 
					
						
							|  |  |  |     dx.segment(index, linPt.dim()) = linPt.localCoordinates_(newPt); | 
					
						
							|  |  |  |     index += linPt.dim(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // error 0.5*(f - 2*x'*g + x'*G*x)
 | 
					
						
							|  |  |  |   double f = constantTerm(); | 
					
						
							|  |  |  |   double xtg = dx.dot(linearTerm()); | 
					
						
							| 
									
										
										
										
											2013-11-27 07:49:53 +08:00
										 |  |  |   double xGx = dx.transpose() * squaredTerm() * dx; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return 0.5 * (f - 2.0 * xtg +  xGx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  | std::shared_ptr<GaussianFactor> | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:52 +08:00
										 |  |  | LinearizedHessianFactor::linearize(const Values& c) const { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Construct an error vector in key-order from the Values
 | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   Vector dx = Vector::Zero(dim()); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   size_t index = 0; | 
					
						
							|  |  |  |   for(unsigned int i = 0; i < this->size(); ++i){ | 
					
						
							|  |  |  |     Key key = this->keys()[i]; | 
					
						
							|  |  |  |     const Value& newPt = c.at(key); | 
					
						
							|  |  |  |     const Value& linPt = lin_points_.at(key); | 
					
						
							|  |  |  |     dx.segment(index, linPt.dim()) = linPt.localCoordinates_(newPt); | 
					
						
							|  |  |  |     index += linPt.dim(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // f2 = f1 - 2*dx'*g1 + dx'*G1*dx
 | 
					
						
							|  |  |  |   //newInfo(this->size(), this->size())(0,0) += -2*dx.dot(linearTerm()) + dx.transpose() * squaredTerm().selfadjointView<Eigen::Upper>() * dx;
 | 
					
						
							| 
									
										
										
										
											2013-11-27 07:49:53 +08:00
										 |  |  |   double f = constantTerm() - 2*dx.dot(linearTerm()) + dx.transpose() * squaredTerm() * dx; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // g2 = g1 - G1*dx
 | 
					
						
							|  |  |  |   //newInfo.rangeColumn(0, this->size(), this->size(), 0) -= squaredTerm().selfadjointView<Eigen::Upper>() * dx;
 | 
					
						
							| 
									
										
										
										
											2013-11-27 07:49:53 +08:00
										 |  |  |   Vector g = linearTerm() - squaredTerm() * dx; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   std::vector<Vector> gs; | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   std::size_t offset = 0; | 
					
						
							| 
									
										
										
										
											2013-08-13 05:41:05 +08:00
										 |  |  |   for(DenseIndex i = 0; i < info_.nBlocks()-1; ++i) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     const std::size_t dim = info_.getDim(i); | 
					
						
							|  |  |  |     gs.push_back(g.segment(offset, dim)); | 
					
						
							|  |  |  |     offset += dim; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // G2 = G1
 | 
					
						
							|  |  |  |   // Do Nothing
 | 
					
						
							|  |  |  |   std::vector<Matrix> Gs; | 
					
						
							| 
									
										
										
										
											2013-08-13 05:41:05 +08:00
										 |  |  |   for(DenseIndex i = 0; i < info_.nBlocks()-1; ++i) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     Gs.push_back(info_.diagonalBlock(i)); | 
					
						
							|  |  |  |     for(DenseIndex j = i + 1; j < info_.nBlocks()-1; ++j) { | 
					
						
							|  |  |  |       Gs.push_back(info_.aboveDiagonalBlock(i, j)); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Create a Hessian Factor from the modified info matrix
 | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |   //return std::shared_ptr<GaussianFactor>(new HessianFactor(js, newInfo));
 | 
					
						
							|  |  |  |   return std::shared_ptr<GaussianFactor>(new HessianFactor(keys(), Gs, gs, f)); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // \namespace aspn
 |