test_example5.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-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 
37 #include <stdlib.h>
38 
39 #include <qpOASES.hpp>
40 #include "../../examples/example4CP.cpp"
41 #include <qpOASES/UnitTesting.hpp>
42 
43 
46 int main( )
47 {
49 
50  int_t i,j,jj;
51  real_t d = 0.0;
52 
53  /* Setup data of first QP... */
54  real_t H[7*7];
55  real_t A[50*7];
56  real_t g[7];
57  real_t lbA[50];
58 
59  /* ( 1.0 0.5 | )
60  * ( 0.5 2.0 | )
61  * ( --------+------------------- )
62  * H = ( | 1e-6 )
63  * ( | 1e-6 )
64  * ( | ... )
65  * ( | 1e-6 ) */
66  for( i=0; i<7*7; ++i )
67  H[i] = 0.0;
68  for( i=2; i<7; ++i )
69  H[i*7+i] = 1.0e-6;
70  H[0] = 1.0;
71  H[1] = 0.5;
72  H[7] = 0.5;
73  H[8] = 2.0;
74 
75  /* ( x.x x.x | 1.0 )
76  * ( x.x x.x | ... )
77  * ( x.x x.x | 1.0 )
78  * ( x.x x.x | 1.0 )
79  * A = ( x.x x.x | ... )
80  * ( x.x x.x | 1.0 )
81  * ( x.x x.x | ... )
82  * ( x.x x.x | 1.0 )
83  * ( x.x x.x | ... )
84  * ( x.x x.x | 1.0 ) */
85  for( i=0; i<50*7; ++i )
86  A[i] = 0.0;
87  for( i=0; i<50; ++i )
88  {
89  for( j=0; j<2; ++j )
90  A[i*7+j] = (real_t)rand() / (real_t)RAND_MAX;
91 
92  A[i*7 + (i/10)+2] = 1.0;
93  }
94 
95  /* ( -1.0 )
96  * ( -0.5 )
97  * ( ---- )
98  * g = ( )
99  * ( )
100  * ( )
101  * ( ) */
102  for( i=0; i<7; ++i )
103  g[i] = 0.0;
104  g[0] = -1.0;
105  g[1] = -0.5;
106 
107  for( i=0; i<50; ++i )
108  lbA[i] = 1.0;
109 
110  /* ... and setting up user-defined constraint product function. */
111  MyConstraintProduct myCP( 7,50,A );
112 
113 
114  /* Setting up QProblem object and set construct product function. */
115  QProblem example( 7,50 );
116  example.setConstraintProduct( &myCP );
117 
118 
119  /* Solve first QP. */
120  real_t cputime = 1.0;
121  int_t nWSR = 100;
122  example.init( H,g,A,0,0,lbA,0, nWSR,&cputime );
123 
124  /* Get and print solution of QP. */
125  real_t xOpt[7], yOpt[7+50];
126  example.getPrimalSolution( xOpt );
127  example.getDualSolution( yOpt );
128 
129 
130  /* Compute local linear feedback law */
131  const int_t n_rhs = 7+7+50;
132  real_t g_in[7*n_rhs];
133  real_t b_in[7*n_rhs];
134  real_t bA_in[50*n_rhs];
135  real_t x_out[7*n_rhs];
136  real_t y_out[(7+50)*n_rhs];
137 
138  int_t ii;
139  memset (g_in, 0, sizeof (g_in));
140  memset (b_in, 0, sizeof (b_in));
141  memset (bA_in, 0, sizeof (bA_in));
142 
143  for ( ii = 0; ii < 7; ++ii )
144  g_in[ii*7 + ii] = 1.0;
145  for ( ii = 0; ii < 7; ++ii )
146  b_in[(ii+7)*7 + ii] = 1.0;
147  for ( ii = 0; ii < 50; ++ii )
148  bA_in[(ii+14)*50 + ii] = 1.0;
149 
150  example.solveCurrentEQP ( n_rhs, g_in, b_in, b_in, bA_in, bA_in, x_out, y_out );
151 
152  /* Verify validity of local feedback law by perturbation and hot starts */
153  real_t perturb = 1.0e-6;
154  real_t nrm = 0.0;
155  for ( ii = 0; ii < n_rhs; ++ii )
156  {
157  for ( jj = 0; jj < 7; ++jj )
158  g_in[ii*7 + jj] = g[jj] + g_in[ii*7+jj]*perturb;
159  for ( jj = 0; jj < 50; ++jj )
160  bA_in[ii*50 + jj] = lbA[jj] + bA_in[ii*50+jj]*perturb;
161 
162  nWSR = 100;
163  example.hotstart( &g_in[ii*7],0,0,&bA_in[ii*50],0, nWSR, 0 );
164 
165  real_t xPer[7], yPer[7+50];
166  example.getPrimalSolution( xPer );
167  example.getDualSolution( yPer );
168 
169  for ( jj = 0; jj < 7; ++jj )
170  {
171  d = getAbs (x_out[ii*7+jj]*perturb - (xPer[jj]-xOpt[jj]) );
172  if (nrm < d) nrm=d;
173  }
174  for ( jj = 0; jj < 7+50; ++jj )
175  {
176  d = getAbs (y_out[ii*(7+50)+jj]*perturb - (yPer[jj]-yOpt[jj]) );
177  if (nrm < d) nrm=d;
178  }
179  }
180  printf ("Maximum perturbation over all directions: %e\n", nrm);
181 
182  QPOASES_TEST_FOR_TOL( nrm,1e-15 );
183 
184 
185  /* // print feedback matrix
186  for (ii = 0; ii < n_rhs; ++ii)
187  {
188  printf ("x: ");
189  for (jj = 0; jj < 7; ++jj )
190  printf ("%8.2e ", x_out[ii*7+jj]);
191  printf (" y: ");
192  for (jj = 0; jj < 7+50; ++jj )
193  printf ("%8.2e ", y_out[ii*(7+50)+jj]);
194  printf("\n");
195  }
196 */
197 
198  return TEST_PASSED;
199 }
200 
201 
202 /*
203  * end of file
204  */
returnValue solveCurrentEQP(const int n_rhs, const real_t *g_in, const real_t *lb_in, const real_t *ub_in, const real_t *lbA_in, const real_t *ubA_in, real_t *x_out, real_t *y_out)
returnValue init(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, int &nWSR, const real_t *const yOpt=0, real_t *const cputime=0)
real_t getAbs(real_t x)
int main()
returnValue hotstart(const real_t *const g_new, const real_t *const lb_new, const real_t *const ub_new, const real_t *const lbA_new, const real_t *const ubA_new, int &nWSR, real_t *const cputime)
Example illustrating the use of the ConstraintProduct class.
returnValue setConstraintProduct(ConstraintProduct *const _constraintProduct)
#define QPOASES_TEST_FOR_TOL(x, tol)
Definition: UnitTesting.hpp:61
#define TEST_PASSED
Definition: UnitTesting.hpp:45
double real_t
Definition: AD_test.c:10
Implements the online active set strategy for QPs with general constraints.


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