qpOASES-3.2.0/examples/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 "example4CP.cpp"
41 
42 
45 int main( )
46 {
48 
49  int_t i,j;
50 
51  /* Setup data of first QP... */
52  real_t H[7*7];
53  real_t A[50*7];
54  real_t g[7];
55  real_t lbA[50];
56 
57  /* ( 1.0 0.5 | )
58  * ( 0.5 2.0 | )
59  * ( --------+------------------- )
60  * H = ( | 1e-6 )
61  * ( | 1e-6 )
62  * ( | ... )
63  * ( | 1e-6 ) */
64  for( i=0; i<7*7; ++i )
65  H[i] = 0.0;
66  for( i=2; i<7; ++i )
67  H[i*7+i] = 1.0e-6;
68  H[0] = 1.0;
69  H[1] = 0.5;
70  H[7] = 0.5;
71  H[8] = 2.0;
72 
73  /* ( x.x x.x | 1.0 )
74  * ( x.x x.x | ... )
75  * ( x.x x.x | 1.0 )
76  * ( x.x x.x | 1.0 )
77  * A = ( x.x x.x | ... )
78  * ( x.x x.x | 1.0 )
79  * ( x.x x.x | ... )
80  * ( x.x x.x | 1.0 )
81  * ( x.x x.x | ... )
82  * ( x.x x.x | 1.0 ) */
83  for( i=0; i<50*7; ++i )
84  A[i] = 0.0;
85  for( i=0; i<50; ++i )
86  {
87  for( j=0; j<2; ++j )
88  A[i*7+j] = (real_t)rand() / (real_t)RAND_MAX;
89 
90  A[i*7 + (i/10)+2] = 1.0;
91  }
92 
93  /* ( -1.0 )
94  * ( -0.5 )
95  * ( ---- )
96  * g = ( )
97  * ( )
98  * ( )
99  * ( ) */
100  for( i=0; i<7; ++i )
101  g[i] = 0.0;
102  g[0] = -1.0;
103  g[1] = -0.5;
104 
105  for( i=0; i<50; ++i )
106  lbA[i] = 1.0;
107 
108  /* ... and setting up user-defined constraint product function. */
109  MyConstraintProduct myCP( 7,50,A );
110 
111 
112  /* Setting up QProblem object and set construct product function. */
113  QProblem exampleCP( 7,50 );
114  exampleCP.setPrintLevel( PL_NONE );
115 
116  exampleCP.setConstraintProduct( &myCP );
117 
118 
119  /* Solve first QP. */
120  real_t cputime = 1.0;
121  int_t nWSR = 100;
122  exampleCP.init( H,g,A,0,0,lbA,0, nWSR,&cputime );
123 
124 
125  /* Solve second QP using a modified gradient. */
126  g[0] = -2.0;
127  g[1] = 0.5;
128 
129  cputime = 1.0;
130  nWSR = 100;
131  exampleCP.hotstart( g,0,0,lbA,0, nWSR,&cputime );
132 
133  /* Get and print solution of second QP. */
134  real_t xOpt[7];
135  exampleCP.getPrimalSolution( xOpt );
136  printf( "\nxOpt = [ %e, %e, %e ... ]; objVal = %e\n", xOpt[0],xOpt[1],xOpt[2],exampleCP.getObjVal() );
137  printf( "CPU time: %.3f microseconds\n\n", cputime*1.0e6 );
138 
139 
140 
141  /* Do the same without specifying constraint product. */
142  QProblem example( 7,50 );
143  example.setPrintLevel( PL_NONE );
144 
145  /* Solve first QP. */
146  g[0] = -1.0;
147  g[1] = -0.5;
148 
149  cputime = 1.0;
150  nWSR = 100;
151  example.init( H,g,A,0,0,lbA,0, nWSR,&cputime );
152 
153  /* Solve second QP using a modified gradient. */
154  g[0] = -2.0;
155  g[1] = 0.5;
156 
157  cputime = 1.0;
158  nWSR = 100;
159  example.hotstart( g,0,0,lbA,0, nWSR,&cputime );
160 
161  /* Get and print solution of second QP. */
162  example.getPrimalSolution( xOpt );
163  printf( "\nxOpt = [ %e, %e, %e ... ]; objVal = %e\n", xOpt[0],xOpt[1],xOpt[2],example.getObjVal() );
164  printf( "CPU time: %.3f microseconds\n\n", cputime*1.0e6 );
165 
166  return 0;
167 }
168 
169 
170 /*
171  * end of file
172  */
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)
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)
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:34:33