example1.cpp
Go to the documentation of this file.
1 // qpOASES testing
2 
3 // calculate time
4 #include <cstdio>
5 #include <sys/time.h>
6 #include <unistd.h>
7 #include <stdarg.h>
8 
9 struct __mtimer__ {
10  double start;
11  char msg[100];
13  __mtimer__(const char* format, ...)
14  __attribute__((format(printf, 2, 3)))
15  {
16  va_list args;
17  va_start(args, format);
18  vsnprintf(msg, sizeof(msg), format, args);
19  va_end(args);
20 
21  start = sec();
22  }
24  fprintf(stderr, "[%s] => %.6f [s]\n", msg, sec() - start);
25  }
26  double sec() {
27  struct timeval tv;
28  gettimeofday(&tv, NULL);
29  return tv.tv_sec + tv.tv_usec * 1e-6;
30  }
31  operator bool() { return false; }
32 };
33 
34 #define mtimer(...) if(__mtimer__ __b__ = __mtimer__(__VA_ARGS__));else
35 
36 
37 // testing codes
38 #include <qpOASES.hpp>
39 
40 int main( )
41 {
42  USING_NAMESPACE_QPOASES
43  {
44  printf( "demo-qp1\n");
45  /* Setup data of first QP. */
46  real_t H[2*2] = {2,1,1,4};
47  real_t A[2*1] = {1};
48  real_t g[2] = {-8,-12};
49  real_t ub[1*2] = {1e10, 1e10};
50  real_t lb[1*2] = {-1e10, -1e10};
51  real_t ubA[1*1] = {1e10};
52  real_t lbA[1*1] = {-1e10};
53  /* Setting up QProblem object. */
54  QProblem example( 2,1 );
55  Options options;
56  options.printLevel = PL_NONE;
57  example.setOptions( options );
58  /* Solve first QP. */
59  int nWSR = 10;
60  getSimpleStatus(example.init( H,g,A,lb,ub,lbA,ubA, nWSR ), BT_FALSE);
61  /* Get and print solution of second QP. */
62  real_t xOpt[2];
63  example.getPrimalSolution( xOpt );
64  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
65  xOpt[0],xOpt[1],example.getObjVal() );
66  // example.printOptions();
67  }
68  {
69  printf( "demo-qp2\n");
70  /* Setup data of first QP. */
71  real_t H[2*2] = {2,1,1,4};
72  real_t A[2*2] = { 2.0, 1.0};
73  real_t g[2] = {-8,-12};
74  real_t ub[1*2] = {1e10, 1e10};
75  real_t lb[1*2] = {-1e10, -1e10};
76  real_t ubA[1*2] = {2.0, 2.0};
77  real_t lbA[1*2] = {2.0,2.0};
78  /* Setting up QProblem object. */
79  QProblem example( 2,1 );
80  Options options;
81  options.printLevel = PL_NONE;
82  example.setOptions( options );
83  /* Solve first QP. */
84  int nWSR = 10;
85  getSimpleStatus(example.init( H,g,A,lb,ub,lbA,ubA, nWSR ), BT_FALSE);
86  /* Get and print solution of second QP. */
87  real_t xOpt[2];
88  example.getPrimalSolution( xOpt );
89  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
90  xOpt[0],xOpt[1],example.getObjVal() );
91  // example.printOptions();
92  }
93  {
94  printf( "demo-qp3\n");
95  /* Setup data of first QP. */
96  real_t H[2*2] = {4,1,1,2};
97  real_t A[2*1] = {1, 2};
98  real_t g[2] = {-3, -4};
99  real_t ub[1*2] = {1e10, 1e10};
100  real_t lb[1*2] = {0,0};
101  real_t ubA[1*1] = {1};
102  real_t lbA[1*1] = {1};
103  /* Setting up QProblem object. */
104  QProblem example( 2,1 );
105  Options options;
106  options.printLevel = PL_NONE;
107  example.setOptions( options );
108  /* Solve first QP. */
109  int nWSR = 10;
110  example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
111  /* Get and print solution of second QP. */
112  real_t xOpt[2];
113  example.getPrimalSolution( xOpt );
114  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
115  xOpt[0],xOpt[1],example.getObjVal() );
116  // example.printOptions();
117  }
118 
119  {
120  printf( "demo-qp3, use hotstart\n");
121  real_t H[2*2] = {4,1,1,2};
122  real_t A[2*1] = {1, 2};
123  real_t g[2] = {-3, -4};
124  real_t ub[1*2] = {1e10, 1e10};
125  real_t lb[1*2] = {0,0};
126  real_t ubA[1*1] = {1};
127  real_t lbA[1*1] = {1};
128  printf( " init\n");
129  mtimer("init x 10 time") {
130  for (size_t i = 0; i < 10; i++) {
131  QProblem example( 2,1 );
132  Options options;
133  options.printLevel = PL_NONE;
134  example.setOptions( options );
135  int nWSR = 10;
136  example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
137  real_t xOpt[2];
138  example.getPrimalSolution( xOpt );
139  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
140  xOpt[0],xOpt[1],example.getObjVal() );
141  }
142  }
143  printf( " hotstart\n");
144  QProblem example( 2,1 );
145  Options options;
146  options.printLevel = PL_NONE;
147  example.setOptions( options );
148  int nWSR = 10;
149  example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
150  real_t xOpt[2];
151  example.getPrimalSolution( xOpt );
152  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
153  xOpt[0],xOpt[1],example.getObjVal() );
154  mtimer("hotstart x 10 time") {
155  for (size_t i = 0; i < 10; i++) {
156  int nWSR2 = 10;
157  example.hotstart(g,lb,ub,lbA,ubA, nWSR2 );
158  example.getPrimalSolution( xOpt );
159  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
160  xOpt[0],xOpt[1],example.getObjVal() );
161  }
162  }
163  }
164 
165  {
166  printf( "demo-qp3, use SQP init and hotstart\n");
167  real_t H[2*2] = {4,1,1,2};
168  real_t A[2*1] = {1, 2};
169  real_t g[2] = {-3, -4};
170  real_t ub[1*2] = {1e10, 1e10};
171  real_t lb[1*2] = {0,0};
172  real_t ubA[1*1] = {1};
173  real_t lbA[1*1] = {1};
174 
175  SQProblem example( 2,1 );
176  Options options;
177  options.printLevel = PL_NONE;
178  example.setOptions( options );
179  mtimer("init x 10 time") {
180  for (size_t i = 0; i < 10; i++) {
181  int nWSR = 10;
182  example.init( H,g,A,lb,ub,lbA,ubA, nWSR);
183  real_t xOpt[2];
184  example.getPrimalSolution( xOpt );
185  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
186  xOpt[0],xOpt[1],example.getObjVal() );
187  }
188  }
189  mtimer("hotstart x 10 time") {
190  for (size_t i = 0; i < 10; i++) {
191  int nWSR = 10;
192  example.hotstart( H,g,A,lb,ub,lbA,ubA, nWSR);
193  real_t xOpt[2];
194  example.getPrimalSolution( xOpt );
195  printf( " xOpt = [ %f, %f ]; objVal = %f\n\n",
196  xOpt[0],xOpt[1],example.getObjVal() );
197  }
198  }
199  }
200  return 0;
201 }
202 
203 
204 /*
205  * end of file
206  */
int main()
Definition: example1.cpp:40
__mtimer__(const char *format,...) __attribute__((format(printf
double sec()
Definition: example1.cpp:26
A
#define NULL
va_start(args, format)
double start
Definition: example1.cpp:10
bool is_sec_print
Definition: example1.cpp:12
char msg[100]
Definition: example1.cpp:11
vsnprintf(msg, sizeof(msg), format, args)
va_end(args)
#define mtimer(...)
Definition: example1.cpp:34


eus_qpoases
Author(s):
autogenerated on Fri May 14 2021 02:51:42