example5.cpp
Go to the documentation of this file.
00001 /*
00002  *      This file is part of qpOASES.
00003  *
00004  *      qpOASES -- An Implementation of the Online Active Set Strategy.
00005  *      Copyright (C) 2007-2011 by Hans Joachim Ferreau, Andreas Potschka,
00006  *      Christian Kirches et al. All rights reserved.
00007  *
00008  *      qpOASES is free software; you can redistribute it and/or
00009  *      modify it under the terms of the GNU Lesser General Public
00010  *      License as published by the Free Software Foundation; either
00011  *      version 2.1 of the License, or (at your option) any later version.
00012  *
00013  *      qpOASES is distributed in the hope that it will be useful,
00014  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  *      See the GNU Lesser General Public License for more details.
00017  *
00018  *      You should have received a copy of the GNU Lesser General Public
00019  *      License along with qpOASES; if not, write to the Free Software
00020  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  *
00022  */
00023 
00024 
00037 #include <stdlib.h>
00038 
00039 #include <qpOASES.hpp>
00040 #include "example4CP.cpp"
00041 
00042 
00045 int main( )
00046 {
00047         USING_NAMESPACE_QPOASES
00048 
00049         int i,j,jj;
00050         real_t d = 0.0;
00051 
00052         /* Setup data of first QP... */
00053         real_t H[7*7];
00054         real_t A[50*7];
00055         real_t g[7];
00056         real_t lbA[50];
00057 
00058         /*          ( 1.0 0.5 |                    )
00059          *          ( 0.5 2.0 |                    )
00060          *          ( --------+------------------- )
00061          *      H = (         | 1e-6               )
00062          *          (         |      1e-6          )
00063          *          (         |           ...      )
00064          *          (         |               1e-6 ) */
00065         for( i=0; i<7*7; ++i )
00066                 H[i] = 0.0;
00067         for( i=2; i<7; ++i )
00068                 H[i*7+i] = 1.0e-6;
00069         H[0] = 1.0;
00070         H[1] = 0.5;
00071         H[7] = 0.5;
00072         H[8] = 2.0;
00073 
00074         /*          ( x.x x.x | 1.0             )
00075          *          ( x.x x.x | ...             )
00076          *          ( x.x x.x | 1.0             )
00077          *          ( x.x x.x |     1.0         )
00078          *      A = ( x.x x.x |     ...         )
00079          *          ( x.x x.x |     1.0         )
00080          *          ( x.x x.x |         ...     )
00081          *          ( x.x x.x |             1.0 )
00082          *          ( x.x x.x |             ... )
00083          *          ( x.x x.x |             1.0 ) */
00084         for( i=0; i<50*7; ++i )
00085                 A[i] = 0.0;
00086         for( i=0; i<50; ++i )
00087         {
00088                 for( j=0; j<2; ++j )
00089                         A[i*7+j] = (real_t)rand() / (real_t)RAND_MAX;
00090 
00091                 A[i*7 + (i/10)+2] = 1.0;
00092         }
00093 
00094         /*          ( -1.0 )
00095          *          ( -0.5 )
00096          *          ( ---- )
00097          *      g = (      )
00098          *          (      )
00099          *          (      )
00100          *          (      ) */
00101         for( i=0; i<7; ++i )
00102                 g[i] = 0.0;
00103         g[0] = -1.0;
00104         g[1] = -0.5;
00105 
00106         for( i=0; i<50; ++i )
00107                 lbA[i] = 1.0;
00108 
00109         /* ... and setting up user-defined constraint product function. */
00110         MyConstraintProduct myCP( 7,50,A );
00111 
00112 
00113         /* Setting up QProblem object and set construct product function. */
00114         QProblem example( 7,50 );
00115         example.setConstraintProduct( &myCP );
00116 
00117 
00118         /* Solve first QP. */
00119         real_t cputime = 1.0;
00120         int nWSR = 100;
00121         example.init( H,g,A,0,0,lbA,0, nWSR,&cputime );
00122 
00123         /* Get and print solution of QP. */
00124         real_t xOpt[7], yOpt[7+50];
00125         example.getPrimalSolution( xOpt );
00126         example.getDualSolution( yOpt );
00127 
00128 
00129         /* Compute local linear feedback law */
00130         const int n_rhs = 7+7+50;
00131         real_t g_in[7*n_rhs];
00132         real_t b_in[7*n_rhs];
00133         real_t bA_in[50*n_rhs];
00134         real_t x_out[7*n_rhs];
00135         real_t y_out[(7+50)*n_rhs];
00136 
00137         int ii;
00138         memset (g_in, 0, sizeof (g_in));
00139         memset (b_in, 0, sizeof (b_in));
00140         memset (bA_in, 0, sizeof (bA_in));
00141 
00142         for ( ii = 0; ii < 7; ++ii )
00143                 g_in[ii*7 + ii] = 1.0;
00144         for ( ii = 0; ii < 7; ++ii )
00145                 b_in[(ii+7)*7 + ii] = 1.0;
00146         for ( ii = 0; ii < 50; ++ii )
00147                 bA_in[(ii+14)*50 + ii] = 1.0;
00148 
00149         example.solveCurrentEQP ( n_rhs, g_in, b_in, b_in, bA_in, bA_in, x_out, y_out );
00150 
00151         /* Verify validity of local feedback law by perturbation and hot starts */
00152         real_t perturb = 1.0e-6;
00153         real_t nrm = 0.0;
00154         for ( ii = 0; ii < n_rhs; ++ii )
00155         {
00156                 for ( jj = 0; jj < 7; ++jj )
00157                         g_in[ii*7 + jj] = g[jj] + g_in[ii*7+jj]*perturb;
00158                 for ( jj = 0; jj < 50; ++jj )
00159                         bA_in[ii*50 + jj] = lbA[jj] + bA_in[ii*50+jj]*perturb;
00160 
00161                 nWSR = 100;
00162                 example.hotstart( &g_in[ii*7],0,0,&bA_in[ii*50],0, nWSR, 0 );
00163 
00164                 real_t xPer[7], yPer[7+50];
00165                 example.getPrimalSolution( xPer );
00166                 example.getDualSolution( yPer );
00167 
00168                 for ( jj = 0; jj < 7; ++jj )
00169                 {
00170                         d = fabs (x_out[ii*7+jj]*perturb - (xPer[jj]-xOpt[jj]) );
00171                         if (nrm < d) nrm=d;
00172                 }
00173                 for ( jj = 0; jj < 7+50; ++jj )
00174                 {
00175                         d = fabs (y_out[ii*(7+50)+jj]*perturb - (yPer[jj]-yOpt[jj]) );
00176                         if (nrm < d) nrm=d;
00177                 }
00178         }
00179         printf ("Maximum perturbation over all directions: %e\n", nrm);
00180 
00181         /* // print feedback matrix
00182         for (ii = 0; ii < n_rhs; ++ii)
00183         {
00184                 printf ("x: ");
00185                 for (jj = 0; jj < 7; ++jj )
00186                         printf ("%8.2e ", x_out[ii*7+jj]);
00187                 printf (" y: ");
00188                 for (jj = 0; jj < 7+50; ++jj )
00189                         printf ("%8.2e ", y_out[ii*(7+50)+jj]);
00190                 printf("\n");
00191         }
00192 */
00193 
00194         return 0;
00195 }
00196 
00197 
00198 /*
00199  *      end of file
00200  */


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:58:09