Cheetah Software  1.0
test_JCQP.cpp
Go to the documentation of this file.
1 #include "gmock/gmock.h"
2 #include "gtest/gtest.h"
3 
4 #include "../third-party/JCQP/ProblemGenerator.h"
5 #include "../third-party/JCQP/QpProblem.h"
6 #include "../third-party/JCQP/Timer.h"
7 #include "Utilities/utilities.h"
8 
9 TEST(JCQP, test_example) {
10  QpProblem<double> problem(2, 3);
11  problem.A << 1, 2, 4, 4, 5, 6;
12  problem.P << 12, 5, 5, 8;
13  problem.u << 1, 2, 3;
14  problem.l << -1, -2, -3;
15  problem.q << 1, 2;
16  problem.runFromDense();
17 
18  Vector<double> soln = problem.getSolution();
19  Vector<double> qf = 0.5 * soln.transpose() * problem.P * soln;
20 
21  double objective = qf(0, 0) + problem.q.transpose() * soln;
22  printf("objective: %f\n", objective);
23 
24  Vector<double> Ax = problem.A * soln;
25  for (int i = 0; i < problem.m; i++) {
26  if (Ax[i] < problem.l[i]) {
27  printf("lb error (%.3f %.3f, %.3f)!\n", problem.l[i], problem.u[i],
28  soln[i]);
29  EXPECT_TRUE(false);
30  }
31  if (Ax[i] > problem.u[i]) {
32  printf("ub error (%.3f %.3f, %.3f)!\n", problem.l[i], problem.u[i],
33  soln[i]);
34  EXPECT_TRUE(false);
35  }
36  }
37 }
38 
39 TEST(JCQP, test_result_dense) {
40  QpProblem<double> problem(2, 6);
41  problem.A << 1, 1, 1, 0, 0, 1, -1, -1, -1, 0, 0, -1;
42  problem.P << 4, 1, 1, 2;
43  problem.u << 1, 0.7, 0.7, -1, 0, 0;
44  problem.l << -1000, -1000, -1000, -1000, -1000, -1000;
45  problem.q << 0, 0;
46  problem.runFromDense();
47 
48  EXPECT_TRUE(fpEqual(problem.getSolution()[0], 0.3, .0001));
49  EXPECT_TRUE(fpEqual(problem.getSolution()[1], 0.7, .0001));
50 }
51 
52 TEST(JCQP, test_result_sparse) {
53  QpProblem<double> problem(2, 3);
54  problem.A << 1, 1, 1., 0., 0., 1.;
55  problem.P << 4., 1., 1., 2.;
56  problem.u << 1., 0.7, 0.7;
57  problem.l << 1., 0., 0.;
58  problem.q << 0, 0;
59  problem.runFromDense(-1, true);
60 
61  EXPECT_TRUE(fpEqual(problem.getSolution()[0], 0.3, .0001));
62  EXPECT_TRUE(fpEqual(problem.getSolution()[1], 0.7, .0001));
63 }
64 
65 TEST(JCQP, test_sort_triples) {
66  std::vector<SparseTriple<double>> tris;
67  tris.push_back({1,1,1});
68  tris.push_back({0,1,0});
69  tris.push_back({0,0,1});
70  tris.push_back({0,0,0});
71 
72  sortTriples(tris, true);
73  EXPECT_TRUE(tris[0].r == 0 && tris[0].c == 0);
74  EXPECT_TRUE(tris[1].r == 1 && tris[1].c == 0);
75  EXPECT_TRUE(tris[2].r == 0 && tris[2].c == 1);
76  EXPECT_TRUE(tris[3].r == 1 && tris[3].c == 1);
77 
78  tris.push_back({10,1,1});
79  EXPECT_THROW(sortTriples(tris, true), std::runtime_error);
80 
81  sortAndSumTriples(tris);
82  EXPECT_TRUE(tris[0].value == 10 + 1);
83 
84  tris.clear();
85  tris.push_back({1,1,1});
86  tris.push_back({0,1,0});
87  tris.push_back({0,0,1});
88  tris.push_back({0,0,0});
89  tris.push_back({10,1,1});
90 
91  sortAndSumTriples(tris);
92  EXPECT_TRUE(tris[0].value == 10 + 1);
93 }
94 
95 TEST(JCQP, test_result_sparse_triples) {
96  QpProblem<double> problem(2,3);
97  problem.A_triples = {{1.,0,0}, {1.,0,1}, {1., 1, 0},
98  {1.,2,1}};
99  problem.P_triples = {{4.,0,0}, {1.,0,1},
100  {1.,1,0}, {2.,1,1}};
101  problem.u << 1., 0.7, 0.7;
102  problem.l << 1., 0., 0.;
103  problem.q << 0, 0;
104  problem.runFromTriples(-1, true);
105  EXPECT_TRUE(fpEqual(problem.getSolution()[0], 0.3, .0001));
106  EXPECT_TRUE(fpEqual(problem.getSolution()[1], 0.7, .0001));
107 }
108 
109 TEST(JCQP, test_solver_dense_double) {
110  DenseMatrix<double> A(100, 100);
111  A.setRandom();
112  A = A * A.transpose();
113  Vector<double> x(100);
114  x.setRandom();
115 
116  Vector<double> b = x;
117 
118  CholeskyDenseSolver<double> solver(false);
119  for (s64 i = 0; i < 3; i++) {
120  solver.setup(A);
121  }
122 
123  solver.solve(x);
124 
125  Vector<double> diff = A * x - b;
126  EXPECT_TRUE(fpEqual(diff.minCoeff(), 0.0, 0.001));
127  EXPECT_TRUE(fpEqual(diff.maxCoeff(), 0.0, 0.001));
128 }
129 
130 TEST(JCQP, test_solver_dense_float) {
131  DenseMatrix<float> A(10, 10);
132  A.setRandom();
133  A = A * A.transpose();
134  Vector<float> x(10);
135  x.setRandom();
136 
137  Vector<float> b = x;
138 
139  CholeskyDenseSolver<float> solver(false);
140  for (s64 i = 0; i < 3; i++) {
141  solver.setup(A);
142  }
143 
144  solver.solve(x);
145 
146  Vector<float> diff = A * x - b;
147  EXPECT_TRUE(fpEqual(diff.minCoeff(), 0.0f, 0.001f));
148  EXPECT_TRUE(fpEqual(diff.maxCoeff(), 0.0f, 0.001f));
149 }
150 
151 TEST(JCQP, test_solver_sparse) {
152  int n = 300;
153  Vector<double> x(n);
154  x.setRandom();
155  Vector<double> b = x;
156  DenseMatrix<double> A(n, n);
157  A.setRandom();
158  for (int i = 0; i < n; i++) {
159  for (int j = 0; j < n; j++) {
160  if ((i + 3 * j) % 7 == 1) {
161  A(i, j) = 0;
162  }
163  }
164  }
165 
166  A = A * A.transpose();
167  SparseMatrix<double> As = A.sparseView();
168  CholeskySparseSolver<double> solver;
169  solver.preSetup(As);
170  solver.setup();
171  solver.solve(x);
172  Vector<double> diff = As * x - b;
173  EXPECT_TRUE(fpEqual(diff.minCoeff(), 0.0, 0.001));
174  EXPECT_TRUE(fpEqual(diff.maxCoeff(), 0.0, 0.001));
175 
176  printf("diff %g %g\n", diff.minCoeff(), diff.maxCoeff());
177 }
bool fpEqual(T a, T b, T tol)
Definition: utilities.h:15
int64_t s64
Definition: cTypes.h:24
TEST(JCQP, test_example)
Definition: test_JCQP.cpp:9