| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |  * @file    LinearConstraintSQP.cpp | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |  * @author  Duy-Nguyen Ta | 
					
						
							|  |  |  |  * @author  Krunal Chande | 
					
						
							|  |  |  |  * @author  Luca Carlone | 
					
						
							|  |  |  |  * @date    Dec 15, 2014 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  | #include <gtsam/inference/FactorGraph-inst.h>
 | 
					
						
							| 
									
										
										
										
											2014-12-24 03:44:43 +08:00
										 |  |  | #include <gtsam_unstable/linear/QPSolver.h>
 | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  | #include <gtsam_unstable/nonlinear/LinearConstraintSQP.h>
 | 
					
						
							|  |  |  | #include <gtsam_unstable/nonlinear/ConstrainedFactor.h>
 | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2014-12-24 03:44:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | bool LinearConstraintSQP::isStationary(const VectorValues& delta) const { | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   return delta.vector().lpNorm<Eigen::Infinity>() < params_.errorTol; | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | bool LinearConstraintSQP::isPrimalFeasible(const LinearConstraintNLPState& state) const { | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   return lcnlp_.linearEqualities.checkFeasibility(state.values, params_.errorTol); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | bool LinearConstraintSQP::isDualFeasible(const VectorValues& duals) const { | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, lcnlp_.linearInequalities) { | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |     ConstrainedFactor::shared_ptr inequality | 
					
						
							|  |  |  |         = boost::dynamic_pointer_cast<ConstrainedFactor>(factor); | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |     Key dualKey = inequality->dualKey(); | 
					
						
							|  |  |  |     if (!duals.exists(dualKey)) continue; // should be inactive constraint!
 | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  |     double dual = duals.at(dualKey)[0];// because we only support single-valued inequalities
 | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  |     if (dual > 0.0) // See the explanation in QPSolver::identifyLeavingConstraint, we want dual < 0 ?
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |       return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | bool LinearConstraintSQP::isComplementary(const LinearConstraintNLPState& state) const { | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  |   return lcnlp_.linearInequalities.checkFeasibilityAndComplimentary( | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |       state.values, state.duals, params_.errorTol); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | bool LinearConstraintSQP::checkConvergence(const LinearConstraintNLPState& state, | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  |     const VectorValues& delta) const { | 
					
						
							|  |  |  |   return isStationary(delta) && isPrimalFeasible(state) | 
					
						
							|  |  |  |       && isDualFeasible(state.duals) && isComplementary(state); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | VectorValues LinearConstraintSQP::initializeDuals() const { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   VectorValues duals; | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  |   BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, lcnlp_.linearEqualities){ | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |     ConstrainedFactor::shared_ptr constraint | 
					
						
							|  |  |  |         = boost::dynamic_pointer_cast<ConstrainedFactor>(factor); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:23:41 +08:00
										 |  |  |     duals.insert(constraint->dualKey(), Vector::Zero(factor->dim())); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return duals; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  | LinearConstraintNLPState LinearConstraintSQP::iterate( | 
					
						
							|  |  |  |     const LinearConstraintNLPState& state) const { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // construct the qp subproblem
 | 
					
						
							|  |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  |   qp.cost = *lcnlp_.cost.linearize(state.values); | 
					
						
							|  |  |  |   qp.equalities.add(*lcnlp_.linearEqualities.linearize(state.values)); | 
					
						
							|  |  |  |   qp.inequalities.add(*lcnlp_.linearInequalities.linearize(state.values)); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   if(params_.verbosity >= NonlinearOptimizerParams::LINEAR) | 
					
						
							|  |  |  |     qp.print("QP subproblem:"); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // solve the QP subproblem
 | 
					
						
							|  |  |  |   VectorValues delta, duals; | 
					
						
							|  |  |  |   QPSolver qpSolver(qp); | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  |   VectorValues zeroInitialValues; | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  |   BOOST_FOREACH(const Values::ConstKeyValuePair& key_value, state.values) | 
					
						
							| 
									
										
										
										
											2016-05-07 00:23:41 +08:00
										 |  |  |     zeroInitialValues.insert(key_value.key, Vector::Zero(key_value.value.dim())); | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  |   boost::tie(delta, duals) = qpSolver.optimize(zeroInitialValues, state.duals, | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |       params_.warmStart); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   if(params_.verbosity >= NonlinearOptimizerParams::DELTA) | 
					
						
							|  |  |  |     delta.print("Delta"); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // update new state
 | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |   LinearConstraintNLPState newState; | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   newState.values = state.values.retract(delta); | 
					
						
							|  |  |  |   newState.duals = duals; | 
					
						
							|  |  |  |   newState.converged = checkConvergence(newState, delta); | 
					
						
							|  |  |  |   newState.iterations = state.iterations + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-02 21:27:24 +08:00
										 |  |  |   if(params_.verbosity >= NonlinearOptimizerParams::VALUES) | 
					
						
							|  |  |  |     newState.print("Values"); | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   return newState; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | std::pair<Values, VectorValues> LinearConstraintSQP::optimize( | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |     const Values& initialValues) const { | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |   LinearConstraintNLPState state(initialValues); | 
					
						
							|  |  |  |   state.duals = initializeDuals(); | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   while (!state.converged && state.iterations < params_.maxIterations) { | 
					
						
							|  |  |  |     if(params_.verbosity >= NonlinearOptimizerParams::ERROR) | 
					
						
							|  |  |  |       std::cout << "Iteration # " << state.iterations << std::endl; | 
					
						
							|  |  |  |     state = iterate(state); | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-25 21:07:40 +08:00
										 |  |  |   if(params_.verbosity >= NonlinearOptimizerParams::TERMINATION) | 
					
						
							| 
									
										
										
										
											2015-02-25 12:40:53 +08:00
										 |  |  |     std::cout << "Number of iterations: " << state.iterations << std::endl; | 
					
						
							|  |  |  |   return std::make_pair(state.values, state.duals); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-19 21:51:54 +08:00
										 |  |  | } // namespace gtsam
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 |