OQPinterface.c
Go to the documentation of this file.
1 /*
2  * This file is part of qpOASES.
3  *
4  * qpOASES -- An Implementation of the Online Active Set Strategy.
5  * Copyright (C) 2007-2015 by Hans Joachim Ferreau, Andreas Potschka,
6  * Christian Kirches et al. All rights reserved.
7  *
8  * qpOASES is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * qpOASES is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with qpOASES; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 
40 #include <qpOASES_e/QProblemB.h>
41 #include <qpOASES_e/QProblem.h>
42 
43 
45 
46 
47 /*
48  * r e a d O Q P d i m e n s i o n s
49  */
50 returnValue readOQPdimensions( const char* path,
51  int* nQP, int* nV, int* nC, int* nEC
52  )
53 {
54  int dims[4];
55 
56  /* 1) Setup file name where dimensions are stored. */
57  char filename[QPOASES_MAX_STRING_LENGTH];
58  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sdims.oqp",path );
59 
60  /* 2) Load dimensions from file. */
61  if ( qpOASES_readFromFileI( dims,4,filename ) != SUCCESSFUL_RETURN )
63 
64  *nQP = dims[0];
65  *nV = dims[1];
66  *nC = dims[2];
67  *nEC = dims[3];
68 
69  /* printf( "nQP = %d, nV = %d, nC = %d, nEC = %d\n",*nQP,*nV,*nC,*nEC ); */
70 
71  /* consistency check */
72  if ( ( *nQP <= 0 ) || ( *nV <= 0 ) || ( *nC < 0 ) || ( *nEC < 0 ) )
74 
75  if ( ( *nV > NVMAX ) || ( *nC > NCMAX ) || ( *nQP > NQPMAX ) )
77 
78  return SUCCESSFUL_RETURN;
79 }
80 
81 
82 /*
83  * r e a d O Q P d a t a
84  */
85 returnValue readOQPdata( const char* path,
86  int* nQP, int* nV, int* nC, int* nEC,
88  real_t* xOpt, real_t* yOpt, real_t* objOpt
89  )
90 {
91  char filename[QPOASES_MAX_STRING_LENGTH];
92 
93  /* consistency check */
94  if ( ( H == 0 ) || ( g == 0 ) || ( lb == 0 ) || ( ub == 0 ) )
96 
97 
98  /* 1) Obtain OQP dimensions. */
99  if ( readOQPdimensions( path, nQP,nV,nC,nEC ) != SUCCESSFUL_RETURN )
101 
102 
103  /* another consistency check */
104  if ( ( *nC > 0 ) && ( ( A == 0 ) || ( lbA == 0 ) || ( ubA == 0 ) ) )
106 
107 
108  /* 2) Allocate memory and load OQP data: */
109  /* Hessian matrix */
110  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sH.oqp",path );
111  if ( qpOASES_readFromFileM( H,(*nV),(*nV),filename ) != SUCCESSFUL_RETURN )
113 
114  /* gradient vector sequence */
115  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sg.oqp",path );
116  if ( qpOASES_readFromFileM( g,(*nQP),(*nV),filename ) != SUCCESSFUL_RETURN )
118 
119  /* lower bound vector sequence */
120  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%slb.oqp",path );
121  if ( qpOASES_readFromFileM( lb,(*nQP),(*nV),filename ) != SUCCESSFUL_RETURN )
123 
124  /* upper bound vector sequence */
125  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sub.oqp",path );
126  if ( qpOASES_readFromFileM( ub,(*nQP),(*nV),filename ) != SUCCESSFUL_RETURN )
128 
129  if ( (*nC) > 0 )
130  {
131  /* Constraint matrix */
132  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sA.oqp",path );
133  if ( qpOASES_readFromFileM( A,(*nC),(*nV),filename ) != SUCCESSFUL_RETURN )
135 
136  /* lower constraints' bound vector sequence */
137  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%slbA.oqp",path );
138  if ( qpOASES_readFromFileM( lbA,(*nQP),(*nC),filename ) != SUCCESSFUL_RETURN )
140 
141  /* upper constraints' bound vector sequence */
142  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%subA.oqp",path );
143  if ( qpOASES_readFromFileM( ubA,(*nQP),(*nC),filename ) != SUCCESSFUL_RETURN )
145  }
146 
147  if ( xOpt != 0 )
148  {
149  /* primal solution vector sequence */
150  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sx_opt.oqp",path );
151  if ( qpOASES_readFromFileM( xOpt,(*nQP),(*nV),filename ) != SUCCESSFUL_RETURN )
153  }
154 
155  if ( yOpt != 0 )
156  {
157  /* dual solution vector sequence */
158  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sy_opt.oqp",path );
159  if ( qpOASES_readFromFileM( yOpt,(*nQP),(*nV)+(*nC),filename ) != SUCCESSFUL_RETURN )
161  }
162 
163  if ( objOpt != 0 )
164  {
165  /* dual solution vector sequence */
166  snprintf( filename,QPOASES_MAX_STRING_LENGTH,"%sobj_opt.oqp",path );
167  if ( qpOASES_readFromFileM( objOpt,(*nQP),1,filename ) != SUCCESSFUL_RETURN )
169  }
170 
171  return SUCCESSFUL_RETURN;
172 }
173 
174 
175 /*
176  * s o l v e O Q P b e n c h m a r k
177  */
178 returnValue solveOQPbenchmark( int nQP, int nV, int nC, int nEC,
179  real_t* _H, const real_t* const g, real_t* _A,
180  const real_t* const lb, const real_t* const ub,
181  const real_t* const lbA, const real_t* const ubA,
182  BooleanType isSparse, BooleanType useHotstarts,
183  const Options* options, int maxAllowedNWSR,
184  real_t* maxNWSR, real_t* avgNWSR, real_t* maxCPUtime, real_t* avgCPUtime,
185  real_t* maxStationarity, real_t* maxFeasibility, real_t* maxComplementarity
186  )
187 {
188  int k;
189 
191  returnValue returnvalue;
192 
193  /* I) SETUP AUXILIARY VARIABLES: */
194  /* 1) Keep nWSR and store current and maximum number of
195  * working set recalculations in temporary variables */
196  int nWSRcur;
197 
198  real_t CPUtimeLimit = *maxCPUtime;
199  real_t CPUtimeCur = CPUtimeLimit;
200  real_t stat, feas, cmpl;
201 
202  /* 2) Pointers to data of current QP ... */
203  const real_t* gCur;
204  const real_t* lbCur;
205  const real_t* ubCur;
206  const real_t* lbACur;
207  const real_t* ubACur;
208 
209  /* 3) Vectors for solution obtained by qpOASES. */
210  myStatic real_t x[NVMAX];
212 
213  /* 4) Prepare matrix objects */
214  DenseMatrix *H, *A;
215  myStatic DenseMatrix HH, AA;
216 
217 
218  DenseMatrixCON( &HH, nV, nV, nV, _H );
219  DenseMatrixCON( &AA, nC, nV, nV, _A );
220 
221  H = &HH;
222  A = &AA;
223 
224  *maxNWSR = 0;
225  *avgNWSR = 0;
226  *maxCPUtime = 0.0;
227  *avgCPUtime = 0.0;
228  *maxStationarity = 0.0;
229  *maxFeasibility = 0.0;
230  *maxComplementarity = 0.0;
231 
232  /*DenseMatrix_print( H );*/
233 
234  /* II) SETUP QPROBLEM OBJECT */
235  QProblemCON( &qp,nV,nC,HST_UNKNOWN );
236  QProblem_setOptions( &qp,*options );
237  /*QProblem_setPrintLevel( &qp,PL_LOW );*/
238 
239  /* QProblem_printOptions( &qp ); */
240 
241  /* III) RUN BENCHMARK SEQUENCE: */
242 
243  for( k=0; k<nQP; ++k )
244  {
245  /* 1) Update pointers to current QP data. */
246  gCur = &( g[k*nV] );
247  lbCur = &( lb[k*nV] );
248  ubCur = &( ub[k*nV] );
249  lbACur = &( lbA[k*nC] );
250  ubACur = &( ubA[k*nC] );
251 
252  /* 2) Set nWSR and maximum CPU time. */
253  nWSRcur = maxAllowedNWSR;
254  CPUtimeCur = CPUtimeLimit;
255 
256  /* 3) Solve current QP. */
257  if ( ( k == 0 ) || ( useHotstarts == BT_FALSE ) )
258  {
259  /* initialise */
260  returnvalue = QProblem_initM( &qp, H,gCur,A,lbCur,ubCur,lbACur,ubACur, &nWSRcur,&CPUtimeCur );
261  if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) )
262  return THROWERROR( returnvalue );
263  }
264  else
265  {
266  /* hotstart */
267  returnvalue = QProblem_hotstart( &qp, gCur,lbCur,ubCur,lbACur,ubACur, &nWSRcur,&CPUtimeCur );
268  if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) )
269  return THROWERROR( returnvalue );
270  }
271 
272  /* 4) Obtain solution vectors and objective function value */
274  QProblem_getDualSolution( &qp,y );
275 
276  /* 5) Compute KKT residuals */
277  qpOASES_getKktViolation( nV,nC, _H,gCur,_A,lbCur,ubCur,lbACur,ubACur, x,y, &stat,&feas,&cmpl );
278 
279  /* 6) Update maximum values. */
280  if ( nWSRcur > *maxNWSR )
281  *maxNWSR = nWSRcur;
282  if (stat > *maxStationarity) *maxStationarity = stat;
283  if (feas > *maxFeasibility) *maxFeasibility = feas;
284  if (cmpl > *maxComplementarity) *maxComplementarity = cmpl;
285 
286  if ( CPUtimeCur > *maxCPUtime )
287  *maxCPUtime = CPUtimeCur;
288 
289  *avgNWSR += nWSRcur;
290  *avgCPUtime += CPUtimeCur;
291  }
292  *avgNWSR /= nQP;
293  *avgCPUtime /= ((double)nQP);
294 
295  return SUCCESSFUL_RETURN;
296 }
297 
298 
299 /*
300  * s o l v e O Q P b e n c h m a r k
301  */
303  real_t* _H, const real_t* const g,
304  const real_t* const lb, const real_t* const ub,
305  BooleanType isSparse, BooleanType useHotstarts,
306  const Options* options, int maxAllowedNWSR,
307  real_t* maxNWSR, real_t* avgNWSR, real_t* maxCPUtime, real_t* avgCPUtime,
308  real_t* maxStationarity, real_t* maxFeasibility, real_t* maxComplementarity
309  )
310 {
311  int k;
312 
314  returnValue returnvalue;
315 
316  /* I) SETUP AUXILIARY VARIABLES: */
317  /* 1) Keep nWSR and store current and maximum number of
318  * working set recalculations in temporary variables */
319  int nWSRcur;
320 
321  real_t CPUtimeLimit = *maxCPUtime;
322  real_t CPUtimeCur = CPUtimeLimit;
323  real_t stat, feas, cmpl;
324 
325  /* 2) Pointers to data of current QP ... */
326  const real_t* gCur;
327  const real_t* lbCur;
328  const real_t* ubCur;
329 
330  /* 3) Vectors for solution obtained by qpOASES. */
331  myStatic real_t x[NVMAX];
333 
334  /* 4) Prepare matrix objects */
335  DenseMatrix *H;
337 
338  DenseMatrixCON( &HH, nV, nV, nV, _H );
339  H = &HH;
340 
341  *maxNWSR = 0;
342  *avgNWSR = 0;
343  *maxCPUtime = 0.0;
344  *avgCPUtime = 0.0;
345  *maxStationarity = 0.0;
346  *maxFeasibility = 0.0;
347  *maxComplementarity = 0.0;
348 
349  /* II) SETUP QPROBLEM OBJECT */
350  QProblemBCON( &qp,nV,HST_UNKNOWN );
351  QProblemB_setOptions( &qp,*options );
352  /*QProblemB_setPrintLevel( &qp,PL_LOW );*/
353 
354 
355  /* III) RUN BENCHMARK SEQUENCE: */
356  for( k=0; k<nQP; ++k )
357  {
358  /* 1) Update pointers to current QP data. */
359  gCur = &( g[k*nV] );
360  lbCur = &( lb[k*nV] );
361  ubCur = &( ub[k*nV] );
362 
363  /* 2) Set nWSR and maximum CPU time. */
364  nWSRcur = maxAllowedNWSR;
365  CPUtimeCur = CPUtimeLimit;
366 
367  /* 3) Solve current QP. */
368  if ( ( k == 0 ) || ( useHotstarts == BT_FALSE ) )
369  {
370  /* initialise */
371  returnvalue = QProblemB_initM( &qp,H,gCur,lbCur,ubCur, &nWSRcur,&CPUtimeCur );
372  if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) )
373  return THROWERROR( returnvalue );
374  }
375  else
376  {
377  /* hotstart */
378  returnvalue = QProblemB_hotstart( &qp,gCur,lbCur,ubCur, &nWSRcur,&CPUtimeCur );
379  if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) )
380  return THROWERROR( returnvalue );
381  }
382 
383  /* 4) Obtain solution vectors and objective function value ... */
385  QProblemB_getDualSolution( &qp,y );
386 
387  /* 5) Compute KKT residuals */
388  qpOASES_getKktViolationSB( nV, _H,gCur,lbCur,ubCur, x,y, &stat,&feas,&cmpl );
389 
390  /* 6) update maximum values. */
391  if ( nWSRcur > *maxNWSR )
392  *maxNWSR = nWSRcur;
393  if (stat > *maxStationarity) *maxStationarity = stat;
394  if (feas > *maxFeasibility) *maxFeasibility = feas;
395  if (cmpl > *maxComplementarity) *maxComplementarity = cmpl;
396 
397  if ( CPUtimeCur > *maxCPUtime )
398  *maxCPUtime = CPUtimeCur;
399 
400  *avgNWSR += nWSRcur;
401  *avgCPUtime += CPUtimeCur;
402  }
403  *avgNWSR /= nQP;
404  *avgCPUtime /= ((double)nQP);
405 
406  return SUCCESSFUL_RETURN;
407 }
408 
409 
410 /*
411  * r u n O Q P b e n c h m a r k
412  */
413 returnValue runOQPbenchmark( const char* path, BooleanType isSparse, BooleanType useHotstarts,
414  const Options* options, int maxAllowedNWSR,
415  real_t* maxNWSR, real_t* avgNWSR, real_t* maxCPUtime, real_t* avgCPUtime,
416  real_t* maxStationarity, real_t* maxFeasibility, real_t* maxComplementarity
417  )
418 {
419  int nQP=0, nV=0, nC=0, nEC=0;
420 
428 
429  returnValue returnvalue;
430 
431  /* I) SETUP BENCHMARK: */
432  /* 1) Obtain QP sequence dimensions. */
433  /*if ( readOQPdimensions( path, &nQP,&nV,&nC,&nEC ) != SUCCESSFUL_RETURN )
434  return THROWERROR( RET_BENCHMARK_ABORTED );*/
435 
436  /* 2) Read OQP benchmark data. */
437  if ( readOQPdata( path,
438  &nQP,&nV,&nC,&nEC,
439  H,g,A,lb,ub,lbA,ubA,
440  0,0,0
441  ) != SUCCESSFUL_RETURN )
442  {
444  }
445 
446  /* II) SOLVE BENCHMARK */
447  if ( nC > 0 )
448  {
449  returnvalue = solveOQPbenchmark( nQP,nV,nC,nEC,
450  H,g,A,lb,ub,lbA,ubA,
451  isSparse,useHotstarts,
452  options,maxAllowedNWSR,
453  maxNWSR,avgNWSR,maxCPUtime,avgCPUtime,
454  maxStationarity,maxFeasibility,maxComplementarity
455  );
456 
457  if ( returnvalue != SUCCESSFUL_RETURN )
458  return THROWERROR( returnvalue );
459  }
460  else
461  {
462  returnvalue = solveOQPbenchmarkB( nQP,nV,
463  H,g,lb,ub,
464  isSparse,useHotstarts,
465  options,maxAllowedNWSR,
466  maxNWSR,avgNWSR,maxCPUtime,avgCPUtime,
467  maxStationarity,maxFeasibility,maxComplementarity
468  );
469 
470  if ( returnvalue != SUCCESSFUL_RETURN )
471  return THROWERROR( returnvalue );
472  }
473 
474  return SUCCESSFUL_RETURN;
475 }
476 
477 
479 
480 
481 /*
482  * end of file
483  */
void QProblemBCON(QProblemB *_THIS, int _nV, HessianType _hessianType)
Definition: QProblemB.c:45
returnValue QProblem_getPrimalSolution(QProblem *_THIS, real_t *const xOpt)
Definition: QProblem.c:1168
returnValue QProblem_initM(QProblem *_THIS, DenseMatrix *_H, const real_t *const _g, DenseMatrix *_A, const real_t *const _lb, const real_t *const _ub, const real_t *const _lbA, const real_t *const _ubA, int *nWSR, real_t *const cputime)
Definition: QProblem.c:256
returnValue solveOQPbenchmark(int nQP, int nV, int nC, int nEC, real_t *_H, const real_t *const g, real_t *_A, const real_t *const lb, const real_t *const ub, const real_t *const lbA, const real_t *const ubA, BooleanType isSparse, BooleanType useHotstarts, const Options *options, int maxAllowedNWSR, real_t *maxNWSR, real_t *avgNWSR, real_t *maxCPUtime, real_t *avgCPUtime, real_t *maxStationarity, real_t *maxFeasibility, real_t *maxComplementarity)
Definition: OQPinterface.c:178
BEGIN_NAMESPACE_QPOASES returnValue readOQPdimensions(const char *path, int *nQP, int *nV, int *nC, int *nEC)
Definition: OQPinterface.c:50
Implements the online active set strategy for box-constrained QPs.
returnValue QProblemB_getDualSolution(QProblemB *_THIS, real_t *const yOpt)
Definition: QProblemB.c:905
returnValue qpOASES_getKktViolation(int nV, int nC, const real_t *const H, const real_t *const g, const real_t *const A, const real_t *const lb, const real_t *const ub, const real_t *const lbA, const real_t *const ubA, const real_t *const x, const real_t *const y, real_t *const _stat, real_t *const feas, real_t *const cmpl)
Definition: Utils.c:658
Allows to pass back messages to the calling function.
returnValue readOQPdata(const char *path, int *nQP, int *nV, int *nC, int *nEC, real_t *H, real_t *g, real_t *A, real_t *lb, real_t *ub, real_t *lbA, real_t *ubA, real_t *xOpt, real_t *yOpt, real_t *objOpt)
Definition: OQPinterface.c:85
returnValue runOQPbenchmark(const char *path, BooleanType isSparse, BooleanType useHotstarts, const Options *options, int maxAllowedNWSR, real_t *maxNWSR, real_t *avgNWSR, real_t *maxCPUtime, real_t *avgCPUtime, real_t *maxStationarity, real_t *maxFeasibility, real_t *maxComplementarity)
Definition: OQPinterface.c:413
returnValue qpOASES_readFromFileM(real_t *data, int nrow, int ncol, const char *datafilename)
Definition: Utils.c:314
void DenseMatrixCON(DenseMatrix *_THIS, int m, int n, int lD, real_t *v)
Definition: Matrices.c:47
Interfaces matrix-vector operations tailored to general dense matrices.
returnValue qpOASES_getKktViolationSB(int nV, const real_t *const H, const real_t *const g, const real_t *const lb, const real_t *const ub, const real_t *const x, const real_t *const y, real_t *const _stat, real_t *const feas, real_t *const cmpl)
Definition: Utils.c:764
int_t QProblemB_hotstart(const real_t *const g, const real_t *const lb, const real_t *const ub, int_t *const nWSR, real_t *const cputime, real_t *const x, real_t *const y, real_t *const obj, int_t *const status)
Provides a generic way to set and pass user-specified options.
Definition: options.hpp:65
returnValue QProblemB_initM(QProblemB *_THIS, DenseMatrix *_H, const real_t *const _g, const real_t *const _lb, const real_t *const _ub, int *nWSR, real_t *const cputime)
Definition: QProblemB.c:192
static returnValue QProblemB_setOptions(QProblemB *_THIS, Options _options)
Definition: QProblemB.h:1299
returnValue QProblemB_getPrimalSolution(QProblemB *_THIS, real_t *const xOpt)
Definition: QProblemB.c:880
returnValue QProblem_getDualSolution(QProblem *_THIS, real_t *const yOpt)
Definition: QProblem.c:1058
#define HST_UNKNOWN
returnValue solveOQPbenchmarkB(int nQP, int nV, real_t *_H, const real_t *const g, const real_t *const lb, const real_t *const ub, BooleanType isSparse, BooleanType useHotstarts, const Options *options, int maxAllowedNWSR, real_t *maxNWSR, real_t *avgNWSR, real_t *maxCPUtime, real_t *avgCPUtime, real_t *maxStationarity, real_t *maxFeasibility, real_t *maxComplementarity)
Definition: OQPinterface.c:302
#define QPOASES_MAX_STRING_LENGTH
#define BT_FALSE
Definition: acado_types.hpp:49
int_t QProblem_hotstart(const real_t *const g, const real_t *const lb, const real_t *const ub, const real_t *const lbA, const real_t *const ubA, int_t *const nWSR, real_t *const cputime, real_t *const x, real_t *const y, real_t *const obj, int_t *const status)
double real_t
Definition: AD_test.c:10
Implements the online active set strategy for QPs with general constraints.
returnValue qpOASES_readFromFileI(int *data, int n, const char *datafilename)
Definition: Utils.c:379
void QProblemCON(QProblem *_THIS, int _nV, int _nC, HessianType _hessianType)
Definition: QProblem.c:51
static returnValue QProblem_setOptions(QProblem *_THIS, Options _options)
Definition: QProblem.h:1818
#define myStatic
Definition: Types.h:103


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:55