| 
									
										
										
										
											2014-04-15 10:57:55 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file testQPSolver.cpp | 
					
						
							|  |  |  |  * @brief Test simple QP solver for a linear inequality constraint | 
					
						
							|  |  |  |  * @date Apr 10, 2014 | 
					
						
							| 
									
										
										
										
											2018-11-05 05:16:13 +08:00
										 |  |  |  * @author Duy-Nguyen Ta | 
					
						
							|  |  |  |  * @author Ivan Dario Jimenez | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 02:44:14 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/Symbol.h>
 | 
					
						
							| 
									
										
										
										
											2014-11-22 05:04:12 +08:00
										 |  |  | #include <gtsam_unstable/linear/QPSolver.h>
 | 
					
						
							| 
									
										
										
										
											2016-03-07 23:29:43 +08:00
										 |  |  | #include <gtsam_unstable/linear/QPSParser.h>
 | 
					
						
							| 
									
										
										
										
											2014-11-26 16:04:34 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | using namespace gtsam::symbol_shorthand; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  | static const Vector kOne = Vector::Ones(1), kZero = Vector::Zero(1); | 
					
						
							| 
									
										
										
										
											2016-02-09 23:45:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Create test graph according to Forst10book_pg171Ex5
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | QP createTestCase() { | 
					
						
							|  |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 05:40:48 +08:00
										 |  |  |   // Objective functions x1^2 - x1*x2 + x2^2 - 3*x1 + 5
 | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  |   // Note the Hessian encodes:
 | 
					
						
							|  |  |  |   //        0.5*x1'*G11*x1 + x1'*G12*x2 + 0.5*x2'*G22*x2 - x1'*g1 - x2'*g2 + 0.5*f
 | 
					
						
							| 
									
										
										
										
											2014-11-27 05:40:48 +08:00
										 |  |  |   // Hence, we have G11=2, G12 = -1, g1 = +3, G22 = 2, g2 = 0, f = 10
 | 
					
						
							| 
									
										
										
										
											2016-02-09 23:45:55 +08:00
										 |  |  |   //TODO:  THIS TEST MIGHT BE WRONG : the last parameter  might be 5 instead of 10 because the form of the equation
 | 
					
						
							|  |  |  |   // Should be 0.5x'Gx + gx + f : Nocedal 449
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   qp.cost.push_back( | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |       HessianFactor(X(1), X(2), 2.0 * I_1x1, -I_1x1, 3.0 * I_1x1, 2.0 * I_1x1, | 
					
						
							|  |  |  |           Z_1x1, 10.0)); | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Inequality constraints
 | 
					
						
							| 
									
										
										
										
											2016-04-17 00:32:48 +08:00
										 |  |  |   qp.inequalities.push_back(LinearInequality(X(1), I_1x1, X(2), I_1x1, 2, 0)); // x1 + x2 <= 2 --> x1 + x2 -2 <= 0, --> b=2
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   qp.inequalities.push_back(LinearInequality(X(1), -I_1x1, 0, 1)); // -x1     <= 0
 | 
					
						
							|  |  |  |   qp.inequalities.push_back(LinearInequality(X(2), -I_1x1, 0, 2)); //    -x2  <= 0
 | 
					
						
							|  |  |  |   qp.inequalities.push_back(LinearInequality(X(1), I_1x1, 1.5, 3)); // x1      <= 3/2
 | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   return qp; | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-27 05:40:48 +08:00
										 |  |  | TEST(QPSolver, TestCase) { | 
					
						
							|  |  |  |   VectorValues values; | 
					
						
							|  |  |  |   double x1 = 5, x2 = 7; | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   values.insert(X(1), x1 * I_1x1); | 
					
						
							|  |  |  |   values.insert(X(2), x2 * I_1x1); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp = createTestCase(); | 
					
						
							|  |  |  |   DOUBLES_EQUAL(29, x1 * x1 - x1 * x2 + x2 * x2 - 3 * x1 + 5, 1e-9); | 
					
						
							|  |  |  |   DOUBLES_EQUAL(29, qp.cost[0]->error(values), 1e-9); | 
					
						
							| 
									
										
										
										
											2014-11-27 05:40:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 04:27:19 +08:00
										 |  |  | TEST(QPSolver, constraintsAux) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp = createTestCase(); | 
					
						
							| 
									
										
										
										
											2014-11-27 05:40:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QPSolver solver(qp); | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VectorValues lambdas; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   lambdas.insert(0, (Vector(1) << -0.5).finished()); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   lambdas.insert(1, kZero); | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   lambdas.insert(2, (Vector(1) << 0.3).finished()); | 
					
						
							|  |  |  |   lambdas.insert(3, (Vector(1) << 0.1).finished()); | 
					
						
							| 
									
										
										
										
											2014-12-13 01:03:00 +08:00
										 |  |  |   int factorIx = solver.identifyLeavingConstraint(qp.inequalities, lambdas); | 
					
						
							|  |  |  |   LONGS_EQUAL(2, factorIx); | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VectorValues lambdas2; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   lambdas2.insert(0, (Vector(1) << -0.5).finished()); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   lambdas2.insert(1, kZero); | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   lambdas2.insert(2, (Vector(1) << -0.3).finished()); | 
					
						
							|  |  |  |   lambdas2.insert(3, (Vector(1) << -0.1).finished()); | 
					
						
							| 
									
										
										
										
											2014-12-13 01:03:00 +08:00
										 |  |  |   int factorIx2 = solver.identifyLeavingConstraint(qp.inequalities, lambdas2); | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  |   LONGS_EQUAL(-1, factorIx2); | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-04-16 01:55:24 +08:00
										 |  |  | // Create a simple test graph with one equality constraint
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | QP createEqualityConstrainedTest() { | 
					
						
							|  |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Objective functions x1^2 + x2^2
 | 
					
						
							|  |  |  |   // Note the Hessian encodes:
 | 
					
						
							|  |  |  |   //        0.5*x1'*G11*x1 + x1'*G12*x2 + 0.5*x2'*G22*x2 - x1'*g1 - x2'*g2 + 0.5*f
 | 
					
						
							|  |  |  |   // Hence, we have G11=2, G12 = 0, g1 = 0, G22 = 2, g2 = 0, f = 0
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   qp.cost.push_back( | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |       HessianFactor(X(1), X(2), 2.0 * I_1x1, Z_1x1, Z_1x1, 2.0 * I_1x1, Z_1x1, | 
					
						
							|  |  |  |           0.0)); | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Equality constraints
 | 
					
						
							|  |  |  |   // x1 + x2 = 1 --> x1 + x2 -1 = 0, hence we negate the b vector
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   Matrix A1 = I_1x1; | 
					
						
							|  |  |  |   Matrix A2 = I_1x1; | 
					
						
							|  |  |  |   Vector b = -kOne; | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   qp.equalities.push_back(LinearEquality(X(1), A1, X(2), A2, b, 0)); | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   return qp; | 
					
						
							| 
									
										
										
										
											2014-04-16 01:55:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(QPSolver, dual) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp = createEqualityConstrainedTest(); | 
					
						
							| 
									
										
										
										
											2014-04-16 01:55:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  |   // Initials values
 | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   VectorValues initialValues; | 
					
						
							| 
									
										
										
										
											2016-04-17 00:32:48 +08:00
										 |  |  |   initialValues.insert(X(1), I_1x1); | 
					
						
							|  |  |  |   initialValues.insert(X(2), I_1x1); | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QPSolver solver(qp); | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   GaussianFactorGraph::shared_ptr dualGraph = solver.buildDualGraph( | 
					
						
							|  |  |  |       qp.inequalities, initialValues); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   VectorValues dual = dualGraph->optimize(); | 
					
						
							| 
									
										
										
										
											2014-04-15 18:07:41 +08:00
										 |  |  |   VectorValues expectedDual; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedDual.insert(0, (Vector(1) << 2.0).finished()); | 
					
						
							| 
									
										
										
										
											2014-04-18 00:01:29 +08:00
										 |  |  |   CHECK(assert_equal(expectedDual, dual, 1e-10)); | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 01:55:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | TEST(QPSolver, indentifyActiveConstraints) { | 
					
						
							|  |  |  |   QP qp = createTestCase(); | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   VectorValues currentSolution; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   currentSolution.insert(X(1), Z_1x1); | 
					
						
							|  |  |  |   currentSolution.insert(X(2), Z_1x1); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   InequalityFactorGraph workingSet = solver.identifyActiveConstraints( | 
					
						
							|  |  |  |       qp.inequalities, currentSolution); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   CHECK(!workingSet.at(0)->active()); // inactive
 | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |   CHECK(workingSet.at(1)->active());// active
 | 
					
						
							|  |  |  |   CHECK(workingSet.at(2)->active());// active
 | 
					
						
							|  |  |  |   CHECK(!workingSet.at(3)->active());// inactive
 | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 22:48:06 +08:00
										 |  |  |   VectorValues solution = solver.buildWorkingGraph(workingSet).optimize(); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  |   VectorValues expectedSolution; | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedSolution.insert(X(1), kZero); | 
					
						
							|  |  |  |   expectedSolution.insert(X(2), kZero); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  |   CHECK(assert_equal(expectedSolution, solution, 1e-100)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(QPSolver, iterate) { | 
					
						
							|  |  |  |   QP qp = createTestCase(); | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   VectorValues currentSolution; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   currentSolution.insert(X(1), Z_1x1); | 
					
						
							|  |  |  |   currentSolution.insert(X(2), Z_1x1); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   std::vector<VectorValues> expectedSolutions(4), expectedDuals(4); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedSolutions[0].insert(X(1), kZero); | 
					
						
							|  |  |  |   expectedSolutions[0].insert(X(2), kZero); | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedDuals[0].insert(1, (Vector(1) << 3).finished()); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedDuals[0].insert(2, kZero); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedSolutions[1].insert(X(1), (Vector(1) << 1.5).finished()); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedSolutions[1].insert(X(2), kZero); | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedDuals[1].insert(3, (Vector(1) << 1.5).finished()); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedSolutions[2].insert(X(1), (Vector(1) << 1.5).finished()); | 
					
						
							|  |  |  |   expectedSolutions[2].insert(X(2), (Vector(1) << 0.75).finished()); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedSolutions[3].insert(X(1), (Vector(1) << 1.5).finished()); | 
					
						
							|  |  |  |   expectedSolutions[3].insert(X(2), (Vector(1) << 0.5).finished()); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   InequalityFactorGraph workingSet = solver.identifyActiveConstraints( | 
					
						
							|  |  |  |       qp.inequalities, currentSolution); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-17 11:49:14 +08:00
										 |  |  |   QPSolver::State state(currentSolution, VectorValues(), workingSet, false, | 
					
						
							|  |  |  |                         100); | 
					
						
							| 
									
										
										
										
											2014-12-10 05:27:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   int it = 0; | 
					
						
							|  |  |  |   while (!state.converged) { | 
					
						
							|  |  |  |     state = solver.iterate(state); | 
					
						
							|  |  |  |     // These checks will fail because the expected solutions obtained from
 | 
					
						
							|  |  |  |     // Forst10book do not follow exactly what we implemented from Nocedal06book.
 | 
					
						
							|  |  |  |     // Specifically, we do not re-identify active constraints and
 | 
					
						
							|  |  |  |     // do not recompute dual variables after every step!!!
 | 
					
						
							|  |  |  | //    CHECK(assert_equal(expectedSolutions[it], state.values, 1e-10));
 | 
					
						
							|  |  |  | //    CHECK(assert_equal(expectedDuals[it], state.duals, 1e-10));
 | 
					
						
							|  |  |  |     it++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHECK(assert_equal(expectedSolutions[3], state.values, 1e-10)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-04-16 01:55:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | TEST(QPSolver, optimizeForst10book_pg171Ex5) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp = createTestCase(); | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   VectorValues initialValues; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   initialValues.insert(X(1), Z_1x1); | 
					
						
							|  |  |  |   initialValues.insert(X(2), Z_1x1); | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  |   VectorValues solution; | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   boost::tie(solution, boost::tuples::ignore) = solver.optimize(initialValues); | 
					
						
							| 
									
										
										
										
											2014-04-16 03:14:10 +08:00
										 |  |  |   VectorValues expectedSolution; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedSolution.insert(X(1), (Vector(1) << 1.5).finished()); | 
					
						
							|  |  |  |   expectedSolution.insert(X(2), (Vector(1) << 0.5).finished()); | 
					
						
							| 
									
										
										
										
											2014-04-16 03:14:10 +08:00
										 |  |  |   CHECK(assert_equal(expectedSolution, solution, 1e-100)); | 
					
						
							| 
									
										
										
										
											2014-04-16 01:55:24 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 07:00:22 +08:00
										 |  |  | pair<QP, QP> testParser(QPSParser parser) { | 
					
						
							| 
									
										
										
										
											2016-03-07 23:29:43 +08:00
										 |  |  |   QP exampleqp = parser.Parse(); | 
					
						
							|  |  |  |   QP expectedqp; | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   Key X1(Symbol('X', 1)), X2(Symbol('X', 2)); | 
					
						
							| 
									
										
										
										
											2016-03-07 23:29:43 +08:00
										 |  |  |   // min f(x,y) = 4 + 1.5x -y + 0.58x^2 + 2xy + 2yx + 10y^2
 | 
					
						
							|  |  |  |   expectedqp.cost.push_back( | 
					
						
							| 
									
										
										
										
											2016-06-18 22:39:59 +08:00
										 |  |  |       HessianFactor(X1, X2, 8.0 * I_1x1, 2.0 * I_1x1, -1.5 * kOne, 10.0 * I_1x1, | 
					
						
							| 
									
										
										
										
											2016-06-29 07:51:51 +08:00
										 |  |  |           2.0 * kOne, 8.0)); | 
					
						
							| 
									
										
										
										
											2016-02-28 08:21:42 +08:00
										 |  |  |   // 2x + y >= 2
 | 
					
						
							|  |  |  |   // -x + 2y <= 6
 | 
					
						
							| 
									
										
										
										
											2016-03-07 23:29:43 +08:00
										 |  |  |   expectedqp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-06-18 12:40:23 +08:00
										 |  |  |       LinearInequality(X1, -2.0 * I_1x1, X2, -I_1x1, -2, 0)); | 
					
						
							| 
									
										
										
										
											2016-05-03 07:54:58 +08:00
										 |  |  |   expectedqp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-06-18 12:40:23 +08:00
										 |  |  |       LinearInequality(X1, -I_1x1, X2, 2.0 * I_1x1, 6, 1)); | 
					
						
							| 
									
										
										
										
											2016-05-03 07:54:58 +08:00
										 |  |  |   // x<= 20
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedqp.inequalities.push_back(LinearInequality(X1, I_1x1, 20, 4)); | 
					
						
							| 
									
										
										
										
											2016-03-07 23:29:43 +08:00
										 |  |  |   //x >= 0
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedqp.inequalities.push_back(LinearInequality(X1, -I_1x1, 0, 2)); | 
					
						
							| 
									
										
										
										
											2016-02-28 08:21:42 +08:00
										 |  |  |   // y > = 0
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   expectedqp.inequalities.push_back(LinearInequality(X2, -I_1x1, 0, 3)); | 
					
						
							| 
									
										
										
										
											2016-04-26 07:00:22 +08:00
										 |  |  |   return std::make_pair(expectedqp, exampleqp); | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | ; | 
					
						
							| 
									
										
										
										
											2016-03-08 23:34:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  | TEST(QPSolver, ParserSyntaticTest) { | 
					
						
							| 
									
										
										
										
											2016-05-03 07:54:58 +08:00
										 |  |  |   auto expectedActual = testParser(QPSParser("QPExample.QPS")); | 
					
						
							| 
									
										
										
										
											2016-06-18 12:28:49 +08:00
										 |  |  |   CHECK(assert_equal(expectedActual.first.cost, expectedActual.second.cost, | 
					
						
							|  |  |  |                      1e-7)); | 
					
						
							|  |  |  |   CHECK(assert_equal(expectedActual.first.inequalities, | 
					
						
							|  |  |  |                      expectedActual.second.inequalities, 1e-7)); | 
					
						
							|  |  |  |   CHECK(assert_equal(expectedActual.first.equalities, | 
					
						
							|  |  |  |                      expectedActual.second.equalities, 1e-7)); | 
					
						
							| 
									
										
										
										
											2016-05-03 07:54:58 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-08 23:34:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 07:54:58 +08:00
										 |  |  | TEST(QPSolver, ParserSemanticTest) { | 
					
						
							| 
									
										
										
										
											2016-04-26 07:00:22 +08:00
										 |  |  |   auto expected_actual = testParser(QPSParser("QPExample.QPS")); | 
					
						
							|  |  |  |   VectorValues actualSolution, expectedSolution; | 
					
						
							| 
									
										
										
										
											2016-06-18 12:28:49 +08:00
										 |  |  |   boost::tie(expectedSolution, boost::tuples::ignore) = | 
					
						
							|  |  |  |       QPSolver(expected_actual.first).optimize(); | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = | 
					
						
							|  |  |  |       QPSolver(expected_actual.second).optimize(); | 
					
						
							| 
									
										
										
										
											2016-05-03 07:54:58 +08:00
										 |  |  |   CHECK(assert_equal(actualSolution, expectedSolution, 1e-7)); | 
					
						
							| 
									
										
										
										
											2016-02-28 08:21:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-18 21:14:03 +08:00
										 |  |  | TEST(QPSolver, QPExampleTest){ | 
					
						
							|  |  |  |   QP problem = QPSParser("QPExample.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   auto solver = QPSolver(problem); | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = solver.optimize(); | 
					
						
							|  |  |  |   VectorValues expectedSolution; | 
					
						
							|  |  |  |   expectedSolution.insert(Symbol('X',1),0.7625*I_1x1); | 
					
						
							|  |  |  |   expectedSolution.insert(Symbol('X',2),0.4750*I_1x1); | 
					
						
							| 
									
										
										
										
											2016-06-18 22:39:59 +08:00
										 |  |  |   double error_expected = problem.cost.error(expectedSolution); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							| 
									
										
										
										
											2016-06-18 21:14:03 +08:00
										 |  |  |   CHECK(assert_equal(expectedSolution, actualSolution, 1e-7)) | 
					
						
							| 
									
										
										
										
											2016-06-18 22:39:59 +08:00
										 |  |  |   CHECK(assert_equal(error_expected, error_actual)) | 
					
						
							| 
									
										
										
										
											2016-06-18 21:14:03 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-18 22:39:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-29 07:51:51 +08:00
										 |  |  | TEST(QPSolver, HS21) { | 
					
						
							|  |  |  |   QP problem = QPSParser("HS21.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   VectorValues expectedSolution; | 
					
						
							|  |  |  |   expectedSolution.insert(Symbol('X',1), 2.0*I_1x1); | 
					
						
							|  |  |  |   expectedSolution.insert(Symbol('X',2), 0.0*I_1x1); | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							|  |  |  |   CHECK(assert_equal(-99.9599999, error_actual, 1e-7)) | 
					
						
							|  |  |  |   CHECK(assert_equal(expectedSolution, actualSolution)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-30 03:24:27 +08:00
										 |  |  | TEST(QPSolver, HS35) { | 
					
						
							|  |  |  |   QP problem = QPSParser("HS35.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							|  |  |  |   CHECK(assert_equal(1.11111111e-01,error_actual, 1e-7)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-30 03:38:00 +08:00
										 |  |  | TEST(QPSolver, HS35MOD) { | 
					
						
							|  |  |  |   QP problem = QPSParser("HS35MOD.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							|  |  |  |   CHECK(assert_equal(2.50000001e-01,error_actual, 1e-7)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-02 01:02:59 +08:00
										 |  |  | TEST(QPSolver, HS51) { | 
					
						
							|  |  |  |   QP problem = QPSParser("HS51.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							|  |  |  |   CHECK(assert_equal(8.88178420e-16,error_actual, 1e-7)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 02:10:14 +08:00
										 |  |  | TEST(QPSolver, HS52) { | 
					
						
							| 
									
										
										
										
											2016-07-02 01:02:59 +08:00
										 |  |  |   QP problem = QPSParser("HS52.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							|  |  |  |   CHECK(assert_equal(5.32664756,error_actual, 1e-7)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 03:18:53 +08:00
										 |  |  | TEST(QPSolver, HS268) { // This test needs an extra order of magnitude of tolerance than the rest
 | 
					
						
							| 
									
										
										
										
											2016-07-02 01:02:59 +08:00
										 |  |  |   QP problem = QPSParser("HS268.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							| 
									
										
										
										
											2016-09-05 03:18:53 +08:00
										 |  |  |   CHECK(assert_equal(5.73107049e-07,error_actual, 1e-6)) | 
					
						
							| 
									
										
										
										
											2016-07-02 01:02:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 06:52:55 +08:00
										 |  |  | TEST(QPSolver, QPTEST) { // REQUIRES Jacobian Fix
 | 
					
						
							| 
									
										
										
										
											2016-07-02 03:42:23 +08:00
										 |  |  |   QP problem = QPSParser("QPTEST.QPS").Parse(); | 
					
						
							|  |  |  |   VectorValues actualSolution; | 
					
						
							|  |  |  |   boost::tie(actualSolution, boost::tuples::ignore) = QPSolver(problem).optimize(); | 
					
						
							|  |  |  |   double error_actual = problem.cost.error(actualSolution); | 
					
						
							|  |  |  |   CHECK(assert_equal(0.437187500e01,error_actual, 1e-7)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  | // Create Matlab's test graph as in http://www.mathworks.com/help/optim/ug/quadprog.html
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | QP createTestMatlabQPEx() { | 
					
						
							|  |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Objective functions 0.5*x1^2 + x2^2 - x1*x2 - 2*x1 -6*x2
 | 
					
						
							|  |  |  |   // Note the Hessian encodes:
 | 
					
						
							|  |  |  |   //        0.5*x1'*G11*x1 + x1'*G12*x2 + 0.5*x2'*G22*x2 - x1'*g1 - x2'*g2 + 0.5*f
 | 
					
						
							|  |  |  |   // Hence, we have G11=1, G12 = -1, g1 = +2, G22 = 2, g2 = +6, f = 0
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   qp.cost.push_back( | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |       HessianFactor(X(1), X(2), 1.0 * I_1x1, -I_1x1, 2.0 * I_1x1, 2.0 * I_1x1, | 
					
						
							|  |  |  |           6 * I_1x1, 1000.0)); | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Inequality constraints
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   qp.inequalities.push_back(LinearInequality(X(1), I_1x1, X(2), I_1x1, 2, 0)); // x1 + x2 <= 2
 | 
					
						
							| 
									
										
										
										
											2016-06-14 10:58:36 +08:00
										 |  |  |   qp.inequalities.push_back( | 
					
						
							|  |  |  |       LinearInequality(X(1), -I_1x1, X(2), 2 * I_1x1, 2, 1)); //-x1 + 2*x2 <=2
 | 
					
						
							|  |  |  |   qp.inequalities.push_back( | 
					
						
							|  |  |  |       LinearInequality(X(1), 2 * I_1x1, X(2), I_1x1, 3, 2)); // 2*x1 + x2 <=3
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   qp.inequalities.push_back(LinearInequality(X(1), -I_1x1, 0, 3)); // -x1      <= 0
 | 
					
						
							|  |  |  |   qp.inequalities.push_back(LinearInequality(X(2), -I_1x1, 0, 4)); //      -x2 <= 0
 | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   return qp; | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  | ///* ************************************************************************* */
 | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  | TEST(QPSolver, optimizeMatlabEx) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp = createTestMatlabQPEx(); | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   VectorValues initialValues; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   initialValues.insert(X(1), Z_1x1); | 
					
						
							|  |  |  |   initialValues.insert(X(2), Z_1x1); | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  |   VectorValues solution; | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   boost::tie(solution, boost::tuples::ignore) = solver.optimize(initialValues); | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  |   VectorValues expectedSolution; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedSolution.insert(X(1), (Vector(1) << 2.0 / 3.0).finished()); | 
					
						
							|  |  |  |   expectedSolution.insert(X(2), (Vector(1) << 4.0 / 3.0).finished()); | 
					
						
							| 
									
										
										
										
											2014-04-16 04:47:07 +08:00
										 |  |  |   CHECK(assert_equal(expectedSolution, solution, 1e-7)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-12 13:57:37 +08:00
										 |  |  | ///* ************************************************************************* */
 | 
					
						
							| 
									
										
										
										
											2016-02-09 23:45:55 +08:00
										 |  |  | TEST(QPSolver, optimizeMatlabExNoinitials) { | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   QP qp = createTestMatlabQPEx(); | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							|  |  |  |   VectorValues solution; | 
					
						
							|  |  |  |   boost::tie(solution, boost::tuples::ignore) = solver.optimize(); | 
					
						
							|  |  |  |   VectorValues expectedSolution; | 
					
						
							|  |  |  |   expectedSolution.insert(X(1), (Vector(1) << 2.0 / 3.0).finished()); | 
					
						
							|  |  |  |   expectedSolution.insert(X(2), (Vector(1) << 4.0 / 3.0).finished()); | 
					
						
							|  |  |  |   CHECK(assert_equal(expectedSolution, solution, 1e-7)); | 
					
						
							| 
									
										
										
										
											2016-02-09 23:45:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Create test graph as in Nocedal06book, Ex 16.4, pg. 475
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | QP createTestNocedal06bookEx16_4() { | 
					
						
							|  |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   qp.cost.push_back(JacobianFactor(X(1), I_1x1, I_1x1)); | 
					
						
							|  |  |  |   qp.cost.push_back(JacobianFactor(X(2), I_1x1, 2.5 * I_1x1)); | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Inequality constraints
 | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   qp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |       LinearInequality(X(1), -I_1x1, X(2), 2 * I_1x1, 2, 0)); | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   qp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |       LinearInequality(X(1), I_1x1, X(2), 2 * I_1x1, 6, 1)); | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   qp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |       LinearInequality(X(1), I_1x1, X(2), -2 * I_1x1, 2, 2)); | 
					
						
							|  |  |  |   qp.inequalities.push_back(LinearInequality(X(1), -I_1x1, 0.0, 3)); | 
					
						
							|  |  |  |   qp.inequalities.push_back(LinearInequality(X(2), -I_1x1, 0.0, 4)); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return qp; | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(QPSolver, optimizeNocedal06bookEx16_4) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp = createTestNocedal06bookEx16_4(); | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   VectorValues initialValues; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   initialValues.insert(X(1), (Vector(1) << 2.0).finished()); | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   initialValues.insert(X(2), Z_1x1); | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  |   VectorValues solution; | 
					
						
							| 
									
										
										
										
											2014-11-27 17:52:25 +08:00
										 |  |  |   boost::tie(solution, boost::tuples::ignore) = solver.optimize(initialValues); | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  |   VectorValues expectedSolution; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expectedSolution.insert(X(1), (Vector(1) << 1.4).finished()); | 
					
						
							|  |  |  |   expectedSolution.insert(X(2), (Vector(1) << 1.7).finished()); | 
					
						
							| 
									
										
										
										
											2014-04-16 05:28:23 +08:00
										 |  |  |   CHECK(assert_equal(expectedSolution, solution, 1e-7)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(QPSolver, failedSubproblem) { | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2016-04-16 04:54:46 +08:00
										 |  |  |   qp.cost.push_back(JacobianFactor(X(1), I_2x2, Z_2x1)); | 
					
						
							|  |  |  |   qp.cost.push_back(HessianFactor(X(1), Z_2x2, Z_2x1, 100.0)); | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   qp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |       LinearInequality(X(1), (Matrix(1, 2) << -1.0, 0.0).finished(), -1.0, 0)); | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VectorValues expected; | 
					
						
							| 
									
										
										
										
											2014-12-13 06:23:31 +08:00
										 |  |  |   expected.insert(X(1), (Vector(2) << 1.0, 0.0).finished()); | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-17 00:27:20 +08:00
										 |  |  |   VectorValues initialValues; | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   initialValues.insert(X(1), (Vector(2) << 10.0, 100.0).finished()); | 
					
						
							| 
									
										
										
										
											2014-12-17 00:27:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 19:13:57 +08:00
										 |  |  |   QPSolver solver(qp); | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  |   VectorValues solution; | 
					
						
							| 
									
										
										
										
											2014-12-17 00:27:20 +08:00
										 |  |  |   boost::tie(solution, boost::tuples::ignore) = solver.optimize(initialValues); | 
					
						
							| 
									
										
										
										
											2015-02-25 11:10:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Support non positive definite Hessian factors while doing EliminatePreferCholesky with some constrained factors.
Currently, when eliminating a constrained variable, EliminatePreferCholesky converts every other factors to JacobianFactor before doing the special QR factorization for constrained variables. Unfortunately, after a constrained nonlinear graph is linearized, new hessian factors from constraints, multiplied with the dual variable  (-lambda*\hessian{c} terms in the Lagrangian objective function), might become negative definite, thus cannot be converted to JacobianFactors.
Following EliminateCholesky, this version of EliminatePreferCholesky for constrained var gathers all unconstrained factors into a big joint HessianFactor before converting it into a JacobianFactor to be eliminiated by QR together with the other constrained factors.
Of course, this might not solve the non-positive-definite problem entirely, because (1) the original hessian factors might be non-positive definite and (2) large strange value of lambdas might cause the joint factor non-positive definite [is this true?]. But at least, this will help in typical cases.
											
										 
											2014-05-04 06:04:37 +08:00
										 |  |  |   CHECK(assert_equal(expected, solution, 1e-7)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 11:10:07 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(QPSolver, infeasibleInitial) { | 
					
						
							|  |  |  |   QP qp; | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   qp.cost.push_back(JacobianFactor(X(1), I_2x2, Vector::Zero(2))); | 
					
						
							|  |  |  |   qp.cost.push_back(HessianFactor(X(1), Z_2x2, Vector::Zero(2), 100.0)); | 
					
						
							| 
									
										
										
										
											2015-02-25 11:10:07 +08:00
										 |  |  |   qp.inequalities.push_back( | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |       LinearInequality(X(1), (Matrix(1, 2) << -1.0, 0.0).finished(), -1.0, 0)); | 
					
						
							| 
									
										
										
										
											2015-02-25 11:10:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VectorValues expected; | 
					
						
							|  |  |  |   expected.insert(X(1), (Vector(2) << 1.0, 0.0).finished()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   VectorValues initialValues; | 
					
						
							| 
									
										
										
										
											2016-02-16 03:44:00 +08:00
										 |  |  |   initialValues.insert(X(1), (Vector(2) << -10.0, 100.0).finished()); | 
					
						
							| 
									
										
										
										
											2015-02-25 11:10:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   QPSolver solver(qp); | 
					
						
							|  |  |  |   VectorValues solution; | 
					
						
							| 
									
										
										
										
											2016-05-07 00:40:08 +08:00
										 |  |  |   CHECK_EXCEPTION(solver.optimize(initialValues), InfeasibleInitialValues); | 
					
						
							| 
									
										
										
										
											2015-02-25 11:10:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 10:57:55 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  |   TestResult tr; | 
					
						
							|  |  |  |   return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 |