test_constraintProduct1.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 <qpOASES.hpp>
38 #include <qpOASES/UnitTesting.hpp>
39 
40 
41 
43 
44 
55 {
56  public:
59 
62  int_t _nC,
63  int_t _diagOffset,
64  real_t* _A
65  )
66  {
67  nV = _nV;
68  nC = _nC;
69  diagOffset = _diagOffset;
70  A = _A;
71  };
72 
75  )
76  {
77  nV = rhs.nV;
78  nC = rhs.nC;
79  diagOffset = rhs.diagOffset;
80  A = rhs.A;
81  };
82 
84  virtual ~MpcConstraintProduct( ) {};
85 
88  )
89  {
90  if ( this != &rhs )
91  {
92  nV = rhs.nV;
93  nC = rhs.nC;
94  diagOffset = rhs.diagOffset;
95  A = rhs.A;
96  }
97  else
98  return *this;
99  };
100 
101  virtual int_t operator() ( int_t constrIndex,
102  const real_t* const x,
103  real_t* const constrValue
104  ) const
105  {
106  int_t i;
107  int_t maxI = (int_t)(((real_t)constrIndex) * ((real_t)nV) / ((real_t)nC)) + diagOffset;
108  maxI = getMin( maxI,nV );
109 
110  constrValue[0] = 0.0;
111 
112  for( i=0; i<maxI; ++i )
113  constrValue[0] += A[constrIndex*nV + i] * x[i];
114 
115  return 0;
116  };
117 
118  protected:
119  int_t nV;
124 };
125 
126 
127 
130 int main( )
131 {
133 
134  int_t nQP, nV, nC, nEC;
135  real_t *H, *g, *A, *lb, *ub, *lbA, *ubA;
136  real_t cputime;
137 
138  real_t xOpt[1000];
139  real_t yOpt[1000];
140  real_t xOptCP[1000+1000];
141  real_t yOptCP[1000+1000];
142 
143  const char* path = "./cpp/data/oqp/chain80w/";
144  int_t k = 10; //th problem
145 
146 
147  if ( readOqpDimensions( path, nQP,nV,nC,nEC ) != SUCCESSFUL_RETURN )
148  return TEST_DATA_NOT_FOUND;
149 
150  readOqpData( path, nQP,nV,nC,nEC,
151  &H,&g,&A,&lb,&ub,&lbA,&ubA,
152  0,0,0
153  );
154 
155  Options myOptions;
156  //myOptions.setToMPC();
157  myOptions.printLevel = PL_LOW;
158 
159  int_t nWSR = 500;
160  cputime = 20.0;
161  QProblem qp( nV,nC );
162  qp.setOptions( myOptions );
163  qp.init( H,&(g[k*nV]),A,&(lb[k*nV]),&(ub[k*nV]),&(lbA[k*nC]),&(ubA[k*nC]),nWSR,&cputime );
164  qp.getPrimalSolution( xOpt );
165  qp.getDualSolution( yOpt );
166  printf( "cputime without constraintProduct: %.3ems\n", cputime*1000.0 );
167 
168 
169  nWSR = 500;
170  cputime = 20.0;
171  MpcConstraintProduct myCP( nV,nC,2,A );
172  QProblem qpCP( nV,nC );
173  qpCP.setOptions( myOptions );
174  qpCP.setConstraintProduct( &myCP );
175  qpCP.init( H,&(g[k*nV]),A,&(lb[k*nV]),&(ub[k*nV]),&(lbA[k*nC]),&(ubA[k*nC]),nWSR,&cputime );
176  qpCP.getPrimalSolution( xOptCP );
177  qpCP.getDualSolution( yOptCP );
178  printf( "cputime with constraintProduct: %.3ems\n", cputime*1000.0 );
179 
180  delete[] ubA;
181  delete[] lbA;
182  delete[] ub;
183  delete[] lb;
184  delete[] A;
185  delete[] g;
186  delete[] H;
187 
188  for( int ii=0; ii<nV; ++ii )
189  QPOASES_TEST_FOR_NEAR( xOptCP[ii],xOpt[ii] );
190 
191  for( int ii=0; ii<nV+nC; ++ii )
192  QPOASES_TEST_FOR_NEAR( yOptCP[ii],yOpt[ii] );
193 
194  return 0;
195 }
196 
197 
198 /*
199  * end of file
200  */
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)
virtual int_t operator()(int_t constrIndex, const real_t *const x, real_t *const constrValue) const
Interface for specifying user-defined evaluations of constraint products.
#define TEST_DATA_NOT_FOUND
Definition: UnitTesting.hpp:51
real_t getMin(real_t x, real_t y)
returnValue setOptions(const Options &_options)
returnValue readOqpData(const char *path, int_t &nQP, int_t &nV, int_t &nC, int_t &nEC, real_t **H, real_t **g, real_t **A, real_t **lb, real_t **ub, real_t **lbA, real_t **ubA, real_t **xOpt, real_t **yOpt, real_t **objOpt)
returnValue setConstraintProduct(ConstraintProduct *const _constraintProduct)
Provides a generic way to set and pass user-specified options.
Definition: options.hpp:65
#define PL_LOW
MpcConstraintProduct & operator=(const MpcConstraintProduct &rhs)
void rhs(const real_t *x, real_t *f)
Example illustrating the use of the ConstraintProduct class.
MpcConstraintProduct(int_t _nV, int_t _nC, int_t _diagOffset, real_t *_A)
BEGIN_NAMESPACE_QPOASES returnValue readOqpDimensions(const char *path, int_t &nQP, int_t &nV, int_t &nC, int_t &nEC)
#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.
MpcConstraintProduct(const MpcConstraintProduct &rhs)


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