test_example4.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;
51 
52  /* Setup data of first QP... */
53  real_t H[7*7];
54  real_t A[50*7];
55  real_t g[7];
56  real_t lbA[50];
57 
58  /* ( 1.0 0.5 | )
59  * ( 0.5 2.0 | )
60  * ( --------+------------------- )
61  * H = ( | 1e-6 )
62  * ( | 1e-6 )
63  * ( | ... )
64  * ( | 1e-6 ) */
65  for( i=0; i<7*7; ++i )
66  H[i] = 0.0;
67  for( i=2; i<7; ++i )
68  H[i*7+i] = 1.0e-6;
69  H[0] = 1.0;
70  H[1] = 0.5;
71  H[7] = 0.5;
72  H[8] = 2.0;
73 
74  /* ( x.x x.x | 1.0 )
75  * ( x.x x.x | ... )
76  * ( x.x x.x | 1.0 )
77  * ( x.x x.x | 1.0 )
78  * A = ( x.x x.x | ... )
79  * ( x.x x.x | 1.0 )
80  * ( x.x x.x | ... )
81  * ( x.x x.x | 1.0 )
82  * ( x.x x.x | ... )
83  * ( x.x x.x | 1.0 ) */
84  for( i=0; i<50*7; ++i )
85  A[i] = 0.0;
86  for( i=0; i<50; ++i )
87  {
88  for( j=0; j<2; ++j )
89  A[i*7+j] = (real_t)rand() / (real_t)RAND_MAX;
90 
91  A[i*7 + (i/10)+2] = 1.0;
92  }
93 
94  /* ( -1.0 )
95  * ( -0.5 )
96  * ( ---- )
97  * g = ( )
98  * ( )
99  * ( )
100  * ( ) */
101  for( i=0; i<7; ++i )
102  g[i] = 0.0;
103  g[0] = -1.0;
104  g[1] = -0.5;
105 
106  for( i=0; i<50; ++i )
107  lbA[i] = 1.0;
108 
109  /* ... and setting up user-defined constraint product function. */
110  MyConstraintProduct myCP( 7,50,A );
111 
112 
113  /* Setting up QProblem object and set construct product function. */
114  QProblem exampleCP( 7,50 );
115  exampleCP.setPrintLevel( PL_NONE );
116 
117  exampleCP.setConstraintProduct( &myCP );
118 
119 
120  /* Solve first QP. */
121  real_t cputime = 1.0;
122  int_t nWSR = 100;
123  exampleCP.init( H,g,A,0,0,lbA,0, nWSR,&cputime );
124 
125 
126  /* Solve second QP using a modified gradient. */
127  g[0] = -2.0;
128  g[1] = 0.5;
129 
130  cputime = 1.0;
131  nWSR = 100;
132  exampleCP.hotstart( g,0,0,lbA,0, nWSR,&cputime );
133 
134  /* Get and print solution of second QP. */
135  real_t xOptCP[7];
136  real_t yOptCP[7+50];
137  exampleCP.getPrimalSolution( xOptCP );
138  exampleCP.getDualSolution( yOptCP );
139  printf( "\nxOpt = [ %e, %e, %e ... ]; objVal = %e\n", xOptCP[0],xOptCP[1],xOptCP[2],exampleCP.getObjVal() );
140  printf( "CPU time: %.3f microseconds\n\n", cputime*1.0e6 );
141 
142  /* Compute KKT tolerances */
143  real_t stat, feas, cmpl;
144  SolutionAnalysis analyzerCP;
145 
146  analyzerCP.getKktViolation( &exampleCP, &stat,&feas,&cmpl );
147  printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl );
148 
149  QPOASES_TEST_FOR_TOL( stat,1e-15 );
150  QPOASES_TEST_FOR_TOL( feas,1e-15 );
151  QPOASES_TEST_FOR_TOL( cmpl,1e-15 );
152 
153 
154  /* Do the same without specifying constraint product. */
155  QProblem example( 7,50 );
156  example.setPrintLevel( PL_NONE );
157 
158  /* Solve first QP. */
159  g[0] = -1.0;
160  g[1] = -0.5;
161 
162  cputime = 1.0;
163  nWSR = 100;
164  example.init( H,g,A,0,0,lbA,0, nWSR,&cputime );
165 
166  /* Solve second QP using a modified gradient. */
167  g[0] = -2.0;
168  g[1] = 0.5;
169 
170  cputime = 1.0;
171  nWSR = 100;
172  example.hotstart( g,0,0,lbA,0, nWSR,&cputime );
173 
174  /* Get and print solution of second QP. */
175  real_t xOpt[7];
176  real_t yOpt[7+50];
177  example.getPrimalSolution( xOpt );
178  example.getDualSolution( yOpt );
179  printf( "\nxOpt = [ %e, %e, %e ... ]; objVal = %e\n", xOpt[0],xOpt[1],xOpt[2],example.getObjVal() );
180  printf( "CPU time: %.3f microseconds\n\n", cputime*1.0e6 );
181 
182  /* Compute KKT tolerances */
183  SolutionAnalysis analyzer;
184 
185  analyzer.getKktViolation( &example, &stat,&feas,&cmpl );
186  printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl );
187 
188  QPOASES_TEST_FOR_TOL( stat,1e-15 );
189  QPOASES_TEST_FOR_TOL( feas,1e-15 );
190  QPOASES_TEST_FOR_TOL( cmpl,1e-15 );
191 
192  for( int_t ii=0; ii<7; ++ii )
193  QPOASES_TEST_FOR_NEAR( xOptCP[ii],xOpt[ii] );
194 
195  for( int_t ii=0; ii<7+50; ++ii )
196  QPOASES_TEST_FOR_NEAR( yOptCP[ii],yOpt[ii] );
197 
198  QPOASES_TEST_FOR_NEAR( exampleCP.getObjVal(),example.getObjVal() );
199 
200  return TEST_PASSED;
201 }
202 
203 
204 /*
205  * end of file
206  */
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)
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)
#define PL_NONE
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
real_t getKktViolation(QProblemB *const qp, real_t *const maxStat=0, real_t *const maxFeas=0, real_t *const maxCmpl=0) const
#define QPOASES_TEST_FOR_NEAR(x, y)
Definition: UnitTesting.hpp:58
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