test_constraintProduct2.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;
120  int_t nC;
121  int_t diagOffset;
122  real_t* A;
124 };
125 
126 
129 int main( )
130 {
131  int_t nQP, nV, nC, nEC;
132  real_t *H, *g, *A, *lb, *ub, *lbA, *ubA;
133  real_t cputime;
134 
135  real_t xOpt[1000];
136  real_t yOpt[1000];
137  real_t xOptCP[1000+1000];
138  real_t yOptCP[1000+1000];
139 
140  const char* path = "./cpp/data/oqp/diesel/";
141  int_t k = 200; //th problem
142 
143 
144  if ( readOqpDimensions( path, nQP,nV,nC,nEC ) != SUCCESSFUL_RETURN )
145  return TEST_DATA_NOT_FOUND;
146 
147  readOqpData( path, nQP,nV,nC,nEC,
148  &H,&g,&A,&lb,&ub,&lbA,&ubA,
149  0,0,0
150  );
151 
152  Options myOptions;
153  myOptions.setToMPC();
154  myOptions.printLevel = PL_LOW;
155 
156  int_t nWSR = 500;
157  cputime = 10.0;
158  QProblem qp( nV,nC );
159  qp.setOptions( myOptions );
160  qp.init( H,&(g[k*nV]),A,&(lb[k*nV]),&(ub[k*nV]),&(lbA[k*nC]),&(ubA[k*nC]),nWSR,&cputime );
161  qp.getPrimalSolution( xOpt );
162  qp.getDualSolution( yOpt );
163  printf( "cputime without constraintProduct: %.3ems\n", cputime*1000.0 );
164 
165 
166  nWSR = 500;
167  cputime = 10.0;
168  MpcConstraintProduct myCP( nV,nC,1,A );
169  QProblem qpCP( nV,nC );
170  qpCP.setOptions( myOptions );
171  qpCP.setConstraintProduct( &myCP );
172  qpCP.init( H,&(g[k*nV]),A,&(lb[k*nV]),&(ub[k*nV]),&(lbA[k*nC]),&(ubA[k*nC]),nWSR,&cputime );
173  qpCP.getPrimalSolution( xOptCP );
174  qpCP.getDualSolution( yOptCP );
175  printf( "cputime without constraintProduct: %.3ems\n", cputime*1000.0 );
176 
177  delete[] ubA;
178  delete[] lbA;
179  delete[] ub;
180  delete[] lb;
181  delete[] A;
182  delete[] g;
183  delete[] H;
184 
185  for( int_t ii=0; ii<nV; ++ii )
186  QPOASES_TEST_FOR_NEAR( xOptCP[ii],xOpt[ii] );
187 
188  for( int_t ii=0; ii<nV+nC; ++ii )
189  QPOASES_TEST_FOR_NEAR( yOptCP[ii],yOpt[ii] );
190 
191  return TEST_PASSED;
192 }
193 
194 
195 /*
196  * end of file
197  */
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)
#define TEST_PASSED
Definition: UnitTesting.hpp:45
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)
returnValue setToMPC()
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