Cheetah Software  1.0
test_osqp.cpp
Go to the documentation of this file.
1 #include "gmock/gmock.h"
2 #include "gtest/gtest.h"
3 
4 #include "Utilities/utilities.h"
5 #include "osqp.h"
6 
7 TEST(OSQP, osqp_c_interface) {
8  // problem:
9  // >> H = [4 1; 1 2]; f = [1;1]; A = [1 1; 1 0; 0 1; -1 -1; -1 0; 0 -1]; b =
10  // [1 .7 .7 -1 0 0]';
11  // >> x = quadprog(H,f,A,b)
12  // x =
13  //
14  // 0.3000
15  // 0.7000
16 
17  // hessian
18  c_float P_x[4] = {
19  4.00,
20  1.00,
21  1.00,
22  2.00,
23  };
24  c_int P_nnz = 4;
25  c_int P_i[4] = {
26  0,
27  1,
28  0,
29  1,
30  };
31  c_int P_p[3] = {
32  0,
33  2,
34  4,
35  };
36 
37  // gradient
38  c_float q[2] = {
39  1.00,
40  1.00,
41  };
42 
43  // constraint grad
44  c_float A_x[4] = {
45  1.00,
46  1.00,
47  1.00,
48  1.00,
49  };
50  c_int A_nnz = 4;
51  c_int A_i[4] = {
52  0,
53  1,
54  0,
55  2,
56  };
57  c_int A_p[3] = {
58  0,
59  2,
60  4,
61  };
62 
63  // bounds
64  c_float l[3] = {
65  1.00,
66  0.00,
67  0.00,
68  };
69  c_float u[3] = {
70  1.00,
71  0.70,
72  0.70,
73  };
74  c_int n = 2; // number of vars
75  c_int m = 3; // number of constraints
76 
77  // osqp things
78  OSQPSettings* settings = (OSQPSettings*)malloc(sizeof(OSQPSettings));
79  OSQPWorkspace* workspace;
80  OSQPData* data = (OSQPData*)malloc(sizeof(OSQPData));
81  data->n = n;
82  data->m = m;
83  data->P = csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p);
84  data->q = q;
85  data->A = csc_matrix(data->m, data->n, A_nnz, A_x, A_i, A_p);
86  data->l = l;
87  data->u = u;
88 
89  // load defaults
90  osqp_set_default_settings(settings);
91  // printf("default alpha: %f\n", settings->alpha);
92  settings->alpha = 1.0;
93 
94  workspace = osqp_setup(data, settings);
95 
96  osqp_solve(workspace);
97 
98  printf("solution: %6.3f, %6.3f\n", workspace->solution->x[0],
99  workspace->solution->x[1]);
100 
101  EXPECT_TRUE(fpEqual(workspace->solution->x[0], .3, .0001));
102  EXPECT_TRUE(fpEqual(workspace->solution->x[1], .7, .0001));
103 
104  osqp_cleanup(workspace);
105  c_free(data->A);
106  c_free(data->P);
107  c_free(data);
108  c_free(settings);
109 }
110 
111 TEST(OSQP, eigenDataOrder) {
112  DMat<double> m(2, 3);
113  m << 1, 2, 3, 4, 5, 6;
114 
115  double* ptr = m.data();
116 
117  double colMajor[6] = {1, 4, 2, 5, 3, 6};
118 
119  for (size_t i = 0; i < 6; i++) {
120  EXPECT_TRUE(colMajor[i] == ptr[i]);
121  }
122 }
bool fpEqual(T a, T b, T tol)
Definition: utilities.h:15
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
TEST(OSQP, osqp_c_interface)
Definition: test_osqp.cpp:7