| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  |  * @file    LCNLPSolver.cpp | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |  * @author  Duy-Nguyen Ta | 
					
						
							|  |  |  |  * @author  Krunal Chande | 
					
						
							|  |  |  |  * @author  Luca Carlone | 
					
						
							|  |  |  |  * @date    Dec 15, 2014 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | #include <gtsam_unstable/nonlinear/LCNLPSolver.h>
 | 
					
						
							| 
									
										
										
										
											2014-12-24 03:44:43 +08:00
										 |  |  | #include <gtsam_unstable/linear/QPSolver.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 { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | bool LCNLPSolver::isStationary(const VectorValues& delta) const { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   return delta.vector().lpNorm<Eigen::Infinity>() < errorTol; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | bool LCNLPSolver::isPrimalFeasible(const LCNLPState& state) const { | 
					
						
							|  |  |  |   return lcnlp_.linearEqualities.checkFeasibility(state.values, errorTol); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | bool LCNLPSolver::isDualFeasible(const VectorValues& duals) const { | 
					
						
							|  |  |  |   BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, lcnlp_.linearInequalities) { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |     NonlinearConstraint::shared_ptr inequality = boost::dynamic_pointer_cast<NonlinearConstraint>(factor); | 
					
						
							|  |  |  |     Key dualKey = inequality->dualKey(); | 
					
						
							|  |  |  |     if (!duals.exists(dualKey)) continue; // should be inactive constraint!
 | 
					
						
							|  |  |  |     double dual = duals.at(dualKey)[0];   // because we only support single-valued inequalities
 | 
					
						
							| 
									
										
										
										
											2014-12-24 05:14:50 +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; | 
					
						
							| 
									
										
										
										
											2014-12-24 05:14:50 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | bool LCNLPSolver::isComplementary(const LCNLPState& state) const { | 
					
						
							|  |  |  |   return lcnlp_.linearInequalities.checkFeasibilityAndComplimentary(state.values, state.duals, errorTol); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | bool LCNLPSolver::checkConvergence(const LCNLPState& state, const VectorValues& delta) const { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   return isStationary(delta) && isPrimalFeasible(state) && isDualFeasible(state.duals) && isComplementary(state); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  | VectorValues LCNLPSolver::initializeDuals() const { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   VectorValues duals; | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  |   BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, lcnlp_.linearEqualities) { | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |     NonlinearConstraint::shared_ptr constraint = boost::dynamic_pointer_cast<NonlinearConstraint>(factor); | 
					
						
							|  |  |  |     duals.insert(constraint->dualKey(), zero(factor->dim())); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return duals; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-01-04 01:32:49 +08:00
										 |  |  | std::pair<Values, VectorValues> LCNLPSolver::optimize(const Values& initialValues, bool useWarmStart, bool debug) const { | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  |   LCNLPState state(initialValues); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   state.duals = initializeDuals(); | 
					
						
							|  |  |  |   while (!state.converged && state.iterations < 100) { | 
					
						
							| 
									
										
										
										
											2014-12-25 13:00:22 +08:00
										 |  |  |     if (debug) | 
					
						
							|  |  |  |     std::cout << "state: iteration " << state.iterations << std::endl; | 
					
						
							| 
									
										
										
										
											2015-01-04 01:32:49 +08:00
										 |  |  |     state = iterate(state, useWarmStart, debug); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-12-25 13:00:22 +08:00
										 |  |  |   if (debug) | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  |   std::cout << "Number of iterations: " << state.iterations << std::endl; | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   return std::make_pair(state.values, state.duals); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-01-04 01:32:49 +08:00
										 |  |  | LCNLPState LCNLPSolver::iterate(const LCNLPState& state, bool useWarmStart, bool debug) 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  | //  if (debug)
 | 
					
						
							|  |  |  | //    qp.print("QP subproblem:");
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // solve the QP subproblem
 | 
					
						
							|  |  |  |   VectorValues delta, duals; | 
					
						
							|  |  |  |   QPSolver qpSolver(qp); | 
					
						
							| 
									
										
										
										
											2015-01-04 01:32:49 +08:00
										 |  |  |   if (useWarmStart == false || state.iterations == 0) | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  |     boost::tie(delta, duals) = qpSolver.optimize(); | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     VectorValues zeroInitialValues; | 
					
						
							|  |  |  |     BOOST_FOREACH(const Values::ConstKeyValuePair& key_value, state.values) { | 
					
						
							|  |  |  |       zeroInitialValues.insert(key_value.key, zero(key_value.value.dim())); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-04 01:32:49 +08:00
										 |  |  |     boost::tie(delta, duals) = qpSolver.optimize(zeroInitialValues, state.duals, useWarmStart); | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  |   if (debug) | 
					
						
							|  |  |  |     state.values.print("state.values: "); | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   if (debug) | 
					
						
							|  |  |  |     delta.print("delta = "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  | //  if (debug)
 | 
					
						
							|  |  |  | //    duals.print("duals = ");
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // update new state
 | 
					
						
							| 
									
										
										
										
											2014-12-24 04:12:53 +08:00
										 |  |  |   LCNLPState 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-24 07:16:22 +08:00
										 |  |  |   if (debug) | 
					
						
							|  |  |  |     newState.print("newState: "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 08:49:32 +08:00
										 |  |  |   return newState; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |