external_packages/qpOASES-3.0beta/src/Utils.cpp
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-2011 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 
35 #include <math.h>
36 
37 #if defined(__WIN32__) || defined(WIN32)
38  #include <windows.h>
39 #elif defined(LINUX)
40  #include <sys/stat.h>
41  #include <sys/time.h>
42 #endif
43 
44 #ifdef __MATLAB__
45  #include "mex.h"
46 #endif
47 
48 
49 #include <qpOASES/Utils.hpp>
50 
51 
53 
54 
55 /*
56  * p r i n t
57  */
58 returnValue print( const real_t* const v, int n )
59 {
60  #ifndef __SUPPRESSANYOUTPUT__
61  #ifndef __XPCTARGET__
62  int i;
63  char myPrintfString[160];
64 
65  /* Print a vector. */
66  myPrintf( "[\t" );
67  for( i=0; i<n; ++i )
68  {
69  snprintf( myPrintfString,160," %.16e\t", v[i] );
70  myPrintf( myPrintfString );
71  }
72  myPrintf( "]\n" );
73  #endif
74  #endif
75 
76  return SUCCESSFUL_RETURN;
77 }
78 
79 
80 /*
81  * p r i n t
82  */
83 returnValue print( const real_t* const v, int n,
84  const int* const V_idx
85  )
86 {
87  #ifndef __SUPPRESSANYOUTPUT__
88  #ifndef __XPCTARGET__
89  int i;
90  char myPrintfString[160];
91 
92  /* Print a permuted vector. */
93  myPrintf( "[\t" );
94  for( i=0; i<n; ++i )
95  {
96  snprintf( myPrintfString,160," %.16e\t", v[ V_idx[i] ] );
97  myPrintf( myPrintfString );
98  }
99  myPrintf( "]\n" );
100  #endif
101  #endif
102 
103  return SUCCESSFUL_RETURN;
104 }
105 
106 
107 /*
108  * p r i n t
109  */
110 returnValue print( const real_t* const v, int n,
111  const char* name
112  )
113 {
114  #ifndef __SUPPRESSANYOUTPUT__
115  #ifndef __XPCTARGET__
116  char myPrintfString[160];
117 
118  /* Print vector name ... */
119  snprintf( myPrintfString,160,"%s = ", name );
120  myPrintf( myPrintfString );
121  #endif
122  #endif
123 
124  /* ... and the vector itself. */
125  return print( v, n );
126 }
127 
128 /*
129  * p r i n t
130  */
131 returnValue print( const real_t* const M, int nrow, int ncol )
132 {
133  #ifndef __SUPPRESSANYOUTPUT__
134  #ifndef __XPCTARGET__
135  int i;
136 
137  /* Print a matrix as a collection of row vectors. */
138  for( i=0; i<nrow; ++i )
139  print( &(M[i*ncol]), ncol );
140  myPrintf( "\n" );
141  #endif
142  #endif
143 
144  return SUCCESSFUL_RETURN;
145 }
146 
147 
148 /*
149  * p r i n t
150  */
151 returnValue print( const real_t* const M, int nrow, int ncol,
152  const int* const ROW_idx, const int* const COL_idx
153  )
154 {
155  #ifndef __SUPPRESSANYOUTPUT__
156  #ifndef __XPCTARGET__
157  int i;
158 
159  /* Print a permuted matrix as a collection of permuted row vectors. */
160  for( i=0; i<nrow; ++i )
161  print( &( M[ ROW_idx[i]*ncol ] ), ncol, COL_idx );
162  myPrintf( "\n" );
163  #endif
164  #endif
165 
166  return SUCCESSFUL_RETURN;
167 }
168 
169 
170 /*
171  * p r i n t
172  */
173 returnValue print( const real_t* const M, int nrow, int ncol,
174  const char* name
175  )
176 {
177  #ifndef __SUPPRESSANYOUTPUT__
178  #ifndef __XPCTARGET__
179  char myPrintfString[160];
180 
181  /* Print matrix name ... */
182  snprintf( myPrintfString,160,"%s = ", name );
183  myPrintf( myPrintfString );
184  #endif
185  #endif
186 
187  /* ... and the matrix itself. */
188  return print( M, nrow, ncol );
189 }
190 
191 
192 /*
193  * p r i n t
194  */
195 returnValue print( const int* const index, int n )
196 {
197  #ifndef __SUPPRESSANYOUTPUT__
198  #ifndef __XPCTARGET__
199  int i;
200  char myPrintfString[160];
201 
202  /* Print a indexlist. */
203  myPrintf( "[\t" );
204  for( i=0; i<n; ++i )
205  {
206  snprintf( myPrintfString,160," %d\t", index[i] );
207  myPrintf( myPrintfString );
208  }
209  myPrintf( "]\n" );
210  #endif
211  #endif
212 
213  return SUCCESSFUL_RETURN;
214 }
215 
216 
217 /*
218  * p r i n t
219  */
220 returnValue print( const int* const index, int n,
221  const char* name
222  )
223 {
224  #ifndef __SUPPRESSANYOUTPUT__
225  #ifndef __XPCTARGET__
226  char myPrintfString[160];
227 
228  /* Print indexlist name ... */
229  snprintf( myPrintfString,160,"%s = ", name );
230  myPrintf( myPrintfString );
231  #endif
232  #endif
233 
234  /* ... and the indexlist itself. */
235  return print( index, n );
236 }
237 
238 
239 /*
240  * m y P r i n t f
241  */
242 returnValue myPrintf( const char* s )
243 {
244  #ifndef __SUPPRESSANYOUTPUT__
245  #ifndef __XPCTARGET__
246  #ifdef __MATLAB__
247  mexPrintf( s );
248  #else
249  FILE* outputfile = getGlobalMessageHandler( )->getOutputFile( );
250  if ( outputfile == 0 )
252 
253  fprintf( outputfile, "%s", s );
254  #endif
255  #endif
256  #endif
257 
258  return SUCCESSFUL_RETURN;
259 }
260 
261 
262 /*
263  * p r i n t C o p y r i g h t N o t i c e
264  */
266 {
267  #ifndef __SUPPRESSANYOUTPUT__
268  #ifndef __XPCTARGET__
269  #ifndef __DSPACE__
270  #ifndef __NO_COPYRIGHT__
271  myPrintf( "\nqpOASES -- An Implementation of the Online Active Set Strategy.\nCopyright (C) 2007-2011 by Hans Joachim Ferreau, Andreas Potschka,\nChristian Kirches et al. All rights reserved.\n\nqpOASES is distributed under the terms of the \nGNU Lesser General Public License 2.1 in the hope that it will be \nuseful, but WITHOUT ANY WARRANTY; without even the implied warranty \nof MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \nSee the GNU Lesser General Public License for more details.\n\n" );
272  #endif
273  #endif
274  #endif
275  #endif
276  return SUCCESSFUL_RETURN;
277 }
278 
279 
280 /*
281  * r e a d F r o m F i l e
282  */
283 returnValue readFromFile( real_t* data, int nrow, int ncol,
284  const char* datafilename
285  )
286 {
287  #ifndef __XPCTARGET__
288  int i, j;
289  real_t float_data;
290  FILE* datafile;
291 
292  /* 1) Open file. */
293  if ( ( datafile = fopen( datafilename, "r" ) ) == 0 )
294  {
295  char errstr[80];
296  snprintf( errstr,80,"(%s)",datafilename );
298  }
299 
300  /* 2) Read data from file. */
301  for( i=0; i<nrow; ++i )
302  {
303  for( j=0; j<ncol; ++j )
304  {
305  #ifdef __USE_SINGLE_PRECISION__
306  if ( fscanf( datafile, "%f ", &float_data ) == 0 )
307  #else
308  if ( fscanf( datafile, "%lf ", &float_data ) == 0 )
309  #endif /* __USE_SINGLE_PRECISION__ */
310  {
311  fclose( datafile );
312  char errstr[80];
313  snprintf( errstr,80,"(%s)",datafilename );
315  }
316  data[i*ncol + j] = ( (real_t) float_data );
317  }
318  }
319 
320  /* 3) Close file. */
321  fclose( datafile );
322 
323  return SUCCESSFUL_RETURN;
324  #else
325 
327 
328  #endif
329 }
330 
331 
332 /*
333  * r e a d F r o m F i l e
334  */
336  const char* datafilename
337  )
338 {
339  return readFromFile( data, n, 1, datafilename );
340 }
341 
342 
343 
344 /*
345  * r e a d F r o m F i l e
346  */
347 returnValue readFromFile( int* data, int n,
348  const char* datafilename
349  )
350 {
351  #ifndef __XPCTARGET__
352  int i;
353  FILE* datafile;
354 
355  /* 1) Open file. */
356  if ( ( datafile = fopen( datafilename, "r" ) ) == 0 )
357  {
358  char errstr[80];
359  snprintf( errstr,80,"(%s)",datafilename );
361  }
362 
363  /* 2) Read data from file. */
364  for( i=0; i<n; ++i )
365  {
366  if ( fscanf( datafile, "%d\n", &(data[i]) ) == 0 )
367  {
368  fclose( datafile );
369  char errstr[80];
370  snprintf( errstr,80,"(%s)",datafilename );
372  }
373  }
374 
375  /* 3) Close file. */
376  fclose( datafile );
377 
378  return SUCCESSFUL_RETURN;
379  #else
380 
382 
383  #endif
384 }
385 
386 
387 /*
388  * w r i t e I n t o F i l e
389  */
390 returnValue writeIntoFile( const real_t* const data, int nrow, int ncol,
391  const char* datafilename, BooleanType append
392  )
393 {
394  #ifndef __XPCTARGET__
395  int i, j;
396  FILE* datafile;
397 
398  /* 1) Open file. */
399  if ( append == BT_TRUE )
400  {
401  /* append data */
402  if ( ( datafile = fopen( datafilename, "a" ) ) == 0 )
403  {
404  char errstr[80];
405  snprintf( errstr,80,"(%s)",datafilename );
407  }
408  }
409  else
410  {
411  /* do not append data */
412  if ( ( datafile = fopen( datafilename, "w" ) ) == 0 )
413  {
414  char errstr[80];
415  snprintf( errstr,80,"(%s)",datafilename );
417  }
418  }
419 
420  /* 2) Write data into file. */
421  for( i=0; i<nrow; ++i )
422  {
423  for( j=0; j<ncol; ++j )
424  fprintf( datafile, "%.16e ", data[i*ncol+j] );
425 
426  fprintf( datafile, "\n" );
427  }
428 
429  /* 3) Close file. */
430  fclose( datafile );
431 
432  return SUCCESSFUL_RETURN;
433  #else
434 
436 
437  #endif
438 }
439 
440 
441 /*
442  * w r i t e I n t o F i l e
443  */
444 returnValue writeIntoFile( const real_t* const data, int n,
445  const char* datafilename, BooleanType append
446  )
447 {
448  return writeIntoFile( data,1,n,datafilename,append );
449 }
450 
451 
452 /*
453  * w r i t e I n t o F i l e
454  */
455 returnValue writeIntoFile( const int* const integer, int n,
456  const char* datafilename, BooleanType append
457  )
458 {
459  #ifndef __XPCTARGET__
460  int i;
461 
462  FILE* datafile;
463 
464  /* 1) Open file. */
465  if ( append == BT_TRUE )
466  {
467  /* append data */
468  if ( ( datafile = fopen( datafilename, "a" ) ) == 0 )
469  {
470  char errstr[80];
471  snprintf( errstr,80,"(%s)",datafilename );
473  }
474  }
475  else
476  {
477  /* do not append data */
478  if ( ( datafile = fopen( datafilename, "w" ) ) == 0 )
479  {
480  char errstr[80];
481  snprintf( errstr,80,"(%s)",datafilename );
483  }
484  }
485 
486  /* 2) Write data into file. */
487  for( i=0; i<n; ++i )
488  fprintf( datafile, "%d\n", integer[i] );
489 
490  /* 3) Close file. */
491  fclose( datafile );
492 
493  return SUCCESSFUL_RETURN;
494  #else
495 
497 
498  #endif
499 }
500 
501 
502 /*
503  * g e t C P U t i m e
504  */
506 {
507  real_t current_time = -1.0;
508 
509  #if defined(__WIN32__) || defined(WIN32)
510  LARGE_INTEGER counter, frequency;
511  QueryPerformanceFrequency(&frequency);
512  QueryPerformanceCounter(&counter);
513  current_time = ((real_t) counter.QuadPart) / ((real_t) frequency.QuadPart);
514  #elif defined(LINUX)
515  struct timeval theclock;
516  gettimeofday( &theclock,0 );
517  current_time = 1.0*theclock.tv_sec + 1.0e-6*theclock.tv_usec;
518  #endif
519 
520  return current_time;
521 }
522 
523 
524 /*
525  * g e t N o r m
526  */
527 real_t getNorm( const real_t* const v, int n )
528 {
529  int i;
530 
531  real_t norm = 0.0;
532 
533  for( i=0; i<n; ++i )
534  norm += v[i]*v[i];
535 
536  return sqrt( norm );
537 }
538 
539 
540 
541 /*
542  * g e t K K T R e s i d u a l
543  */
544 void getKKTResidual( int nV, int nC,
545  const real_t* const H, const real_t* const g,
546  const real_t* const A, const real_t* const lb, const real_t* const ub,
547  const real_t* const lbA, const real_t* const ubA,
548  const real_t* const x, const real_t* const y,
549  real_t& stat, real_t& feas, real_t& cmpl)
550 {
551  /* Tolerance for dual variables considered zero. */
552  const real_t dualActiveTolerance = 1.0e3 * EPS;
553 
554  int i, j;
555  real_t sum, prod;
556 
557  /* Initialize residuals */
558  stat = feas = cmpl = 0.0;
559 
560  /* Pointers to data and solution of current QP */
561  const real_t* gCur = g;
562  const real_t* lbCur = lb;
563  const real_t* ubCur = ub;
564  const real_t* lbACur = lbA ? lbA : 0;
565  const real_t* ubACur = ubA ? ubA : 0;
566  const real_t* xCur = x;
567  const real_t* yCur = y;
568 
569  /* check stationarity */
570  for (i = 0; i < nV; i++)
571  {
572  /* g term and variable bounds dual term */
573  sum = gCur[i] - yCur[i];
574 
575  /* H*x term */
576  for (j = 0; j < nV; j++) sum += H[i*nV+j] * xCur[j];
577  /* A'*y term */
578  for (j = 0; j < nC; j++) sum -= A[j*nV+i] * yCur[j+nV];
579 
580  /* update stat */
581  if (fabs(sum) > stat) stat = fabs(sum);
582  }
583 
584  /* check primal feasibility and complementarity */
585  /* variable bounds */
586  for (i = 0; i < nV; i++)
587  {
588  /* feasibility */
589  if (lbCur[i] - xCur[i] > feas)
590  feas = lbCur[i] - xCur[i];
591  if (xCur[i] - ubCur[i] > feas)
592  feas = xCur[i] - ubCur[i];
593 
594  /* complementarity */
595  prod = 0.0;
596  if (yCur[i] > dualActiveTolerance) /* lower bound */
597  prod = (xCur[i] - lbCur[i]) * yCur[i];
598  if (yCur[i] < -dualActiveTolerance) /* upper bound */
599  prod = (xCur[i] - ubCur[i]) * yCur[i];
600  if (fabs(prod) > cmpl) cmpl = fabs(prod);
601  }
602  /* A*x bounds */
603  for (i = 0; i < nC; i++)
604  {
605  /* compute sum = (A*x)_i */
606  sum = 0.0;
607  for (j = 0; j < nV; j++)
608  sum += A[i*nV+j] * xCur[j];
609 
610  /* feasibility */
611  if (lbACur[i] - sum > feas)
612  feas = lbACur[i] - sum;
613  if (sum - ubACur[i] > feas)
614  feas = sum - ubACur[i];
615 
616  /* complementarity */
617  prod = 0.0;
618  if (yCur[nV+i] > dualActiveTolerance) /* lower bound */
619  prod = (sum - lbACur[i]) * yCur[nV+i];
620  if (yCur[nV+i] < -dualActiveTolerance) /* upper bound */
621  prod = (sum - ubACur[i]) * yCur[nV+i];
622  if (fabs(prod) > cmpl) cmpl = fabs(prod);
623  }
624 }
625 
626 
628 {
629  if ( value == BT_TRUE )
630  snprintf( string,20,"BT_TRUE" );
631  else
632  snprintf( string,20,"BT_FALSE" );
633 
634  return SUCCESSFUL_RETURN;
635 }
636 
637 
639 {
640  switch( value )
641  {
642  case ST_INACTIVE:
643  snprintf( string,20,"ST_INACTIVE" );
644  break;
645 
646  case ST_LOWER:
647  snprintf( string,20,"ST_LOWER" );
648  break;
649 
650  case ST_UPPER:
651  snprintf( string,20,"ST_UPPER" );
652  break;
653 
654  case ST_UNDEFINED:
655  snprintf( string,20,"ST_UNDEFINED" );
656  break;
657  }
658 
659  return SUCCESSFUL_RETURN;
660 }
661 
662 
664 {
665  switch( value )
666  {
667  case PL_NONE:
668  snprintf( string,20,"PL_NONE" );
669  break;
670 
671  case PL_LOW:
672  snprintf( string,20,"PL_LOW" );
673  break;
674 
675  case PL_MEDIUM:
676  snprintf( string,20,"PL_MEDIUM" );
677  break;
678 
679  case PL_HIGH:
680  snprintf( string,20,"PL_HIGH" );
681  break;
682 
683  case PL_TABULAR:
684  snprintf( string,20,"PL_TABULAR" );
685  break;
686  }
687 
688  return SUCCESSFUL_RETURN;
689 }
690 
691 
692 
693 #ifdef __DEBUG__
694 extern "C" void gdb_printmat(const char *fname, real_t *M, int n, int m, int ldim)
695 {
696  int i, j;
697  FILE *fid;
698 
699  fid = fopen(fname, "wt");
700  if (!fid)
701  {
702  perror("Error opening file: ");
703  return;
704  }
705 
706  for (i = 0; i < n; i++)
707  {
708  for (j = 0; j < m; j++)
709  fprintf(fid, " %23.16e", M[j*ldim+i]);
710  fprintf(fid, "\n");
711  }
712  fclose(fid);
713 }
714 #endif /* __DEBUG__ */
715 
716 
717 
719 
720 
721 /*
722  * end of file
723  */
int counter
Definition: powerkite_c.cpp:40
IntermediateState sqrt(const Expression &arg)
#define ST_LOWER
#define __LINE__
returnValue throwError(returnValue Enumber, const char *additionaltext, const char *functionname, const char *filename, const unsigned long linenumber, VisibilityStatus localVisibilityStatus)
BEGIN_NAMESPACE_ACADO const double EPS
returnValue readFromFile(real_t *data, int nrow, int ncol, const char *datafilename)
Allows to pass back messages to the calling function.
returnValue convertSubjectToStatusToString(SubjectToStatus value, char *const string)
#define PL_MEDIUM
#define __FUNCTION__
#define PL_NONE
returnValue convertPrintLevelToString(PrintLevel value, char *const string)
#define ST_UNDEFINED
real_t getNorm(const real_t *const v, int n)
#define ST_INACTIVE
returnValue convertBooleanTypeToString(BooleanType value, char *const string)
#define PL_HIGH
#define PL_LOW
#define v
PrintLevel
returnValue myPrintf(const char *s)
#define BT_TRUE
Definition: acado_types.hpp:47
BEGIN_NAMESPACE_QPOASES returnValue print(const real_t *const v, int n)
myFILE * getOutputFile() const
#define __FILE__
#define PL_TABULAR
#define ST_UPPER
double real_t
Definition: AD_test.c:10
returnValue writeIntoFile(const real_t *const data, int nrow, int ncol, const char *datafilename, BooleanType append)
void getKKTResidual(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 &stat, real_t &feas, real_t &cmpl)


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