qp_solver_qpoases.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
36 
38 
39 
40 //
41 // PUBLIC MEMBER FUNCTIONS:
42 //
43 
45 {
46  qp = 0;
47 }
48 
49 
50 QPsolver_qpOASES::QPsolver_qpOASES( UserInteraction* _userInteraction ) : DenseQPsolver( _userInteraction )
51 {
52  qp = 0;
53 }
54 
55 
57 {
58  if ( rhs.qp != 0 )
59  qp = new qpOASES::SQProblem( *(rhs.qp) );
60  else
61  qp = 0;
62 }
63 
64 
66 {
67  if ( qp != 0 )
68  delete qp;
69 }
70 
71 
73 {
74  if ( this != &rhs )
75  {
77 
78  if ( qp != 0 )
79  delete qp;
80 
81 
82  if ( rhs.qp != 0 )
83  qp = new qpOASES::SQProblem( *(rhs.qp) );
84  else
85  qp = 0;
86 
87  }
88 
89  return *this;
90 }
91 
92 
94 {
95  return new QPsolver_qpOASES(*this);
96 }
97 
98 
100 {
101  return new QPsolver_qpOASES(*this);
102 }
103 
104 
106 {
107  return DenseQPsolver::solve( cp_ );
108 }
109 
110 
112  double* A,
113  double* g,
114  double* lb,
115  double* ub,
116  double* lbA,
117  double* ubA,
118  uint maxIter
119  )
120 {
121  if ( qp == 0 )
123 
124  /* call to qpOASES, using hotstart if possible and desired */
125  numberOfSteps = maxIter;
126  qpOASES::returnValue returnvalue;
128 
129  //printf( "nV: %d, nC: %d \n",qp->getNV(),qp->getNC() );
130 
131  if ( (bool)qp->isInitialised( ) == false )
132  {
133  returnvalue = qp->init( H,g,A,lb,ub,lbA,ubA,numberOfSteps,0 );
134  }
135  else
136  {
137  int performHotstart = 0;
138  get( HOTSTART_QP,performHotstart );
139 
140  if ( (bool)performHotstart == true )
141  {
142  returnvalue = qp->hotstart( H,g,A,lb,ub,lbA,ubA,numberOfSteps,0 );
143  }
144  else
145  {
146  /* if no hotstart is desired, reset QP and use cold start */
147  qp->reset( );
148  returnvalue = qp->init( H,g,A,lb,ub,lbA,ubA,numberOfSteps,0 );
149  }
150  }
152 
153  /* update QP status and determine return value */
154  return updateQPstatus( returnvalue );
155 }
156 
157 
159  DMatrix *A,
160  DVector *g,
161  DVector *lb,
162  DVector *ub,
163  DVector *lbA,
164  DVector *ubA,
165  uint maxIter )
166 {
167  return solve( H->data(),
168  A->data(),
169  g->data(),
170  lb->data(),
171  ub->data(),
172  lbA->data(),
173  ubA->data(),
174  maxIter
175  );
176 }
177 
178 
179 
181  double* A,
182  double* g,
183  double* lb,
184  double* ub,
185  double* lbA,
186  double* ubA
187  )
188 {
189  /* perform a single QP iteration */
190  return solve( H,A,g,lb,ub,lbA,ubA,1 );
191 }
192 
193 
195  DMatrix *A,
196  DVector *g,
197  DVector *lb,
198  DVector *ub,
199  DVector *lbA,
200  DVector *ubA
201  )
202 {
203  /* perform a single QP iteration */
204  return solve( H,A,g,lb,ub,lbA,ubA,1 );
205 }
206 
207 
209 {
210  if ( qp == 0 )
212 
213  uint dim = qp->getNV( );
214  double* xOptTmp = new double[dim];
215 
216  if ( qp->getPrimalSolution( xOptTmp ) == qpOASES::SUCCESSFUL_RETURN )
217  {
218  xOpt = DVector(dim, xOptTmp);
219  delete[] xOptTmp;
220  return SUCCESSFUL_RETURN;
221  }
222  else
223  {
224  delete[] xOptTmp;
225  return ACADOERROR( RET_QP_NOT_SOLVED );
226  }
227 }
228 
229 
231 {
232  if ( qp == 0 )
234 
235  uint dim = qp->getNV( ) + qp->getNC( );
236  double* yOptTmp = new double[dim];
237 
238  if ( qp->getDualSolution( yOptTmp ) == qpOASES::SUCCESSFUL_RETURN )
239  {
240  yOpt = DVector(dim, yOptTmp);
241  delete[] yOptTmp;
242  return SUCCESSFUL_RETURN;
243  }
244  else
245  {
246  delete[] yOptTmp;
247  return ACADOERROR( RET_QP_NOT_SOLVED );
248  }
249 }
250 
251 
253 {
254  if ( isUnbounded( ) == true )
255  return -INFTY;
256 
257  if ( ( isSolved( ) == false ) || ( qp == 0 ) )
258  return INFTY;
259 
260  return qp->getObjVal( );
261 }
262 
263 
265 
267 }
268 
269 
271 {
272  if ( qp != 0 )
273  return qp->getNV( );
274  else
275  return 0;
276 }
277 
279 {
280  if ( qp != 0 )
281  return qp->getNC( );
282  else
283  return 0;
284 }
285 
286 
288 
289  if ( qp == 0 )
291 
292  if ( (bool)isSolved( ) == false ) return ACADOERROR( RET_QP_NOT_SOLVED );
293 
294  qpOASES::returnValue returnvalue;
295  qpOASES::SolutionAnalysis analyser ;
296 
297  uint NV, NC ;
298  uint run1, run2 ;
299 
300  NV = qp->getNV();
301  NC = qp->getNC();
302 
303  double *Var = new double[(2*NV+NC)*(2*NV+NC)];
304  double *PrimalDualVar = new double[(2*NV+NC)*(2*NV+NC)];
305 
306  for( run1 = 0; run1 < (2*NV+NC)*(2*NV+NC); run1++ )
307  Var[run1] = 0.0;
308 
309  for( run1 = 0; run1 < NV; run1++ )
310  for( run2 = 0; run2 < NV; run2++ )
311  Var[run1*(2*NV+NC)+run2] = H(run1,run2);
312 
313  returnvalue = analyser.getVarianceCovariance( qp, Var, PrimalDualVar );
314 
315  if( returnvalue != qpOASES::SUCCESSFUL_RETURN ){
316  delete[] Var ;
317  delete[] PrimalDualVar;
319  }
320 
321  var.init( NV, NV );
322 
323  for( run1 = 0; run1 < NV; run1++ )
324  for( run2 = 0; run2 < NV; run2++ )
325  var( run1, run2 ) = PrimalDualVar[run1*(2*NV+NC)+run2];
326 
327  delete[] Var ;
328  delete[] PrimalDualVar;
329  return SUCCESSFUL_RETURN;
330 }
331 
332 
333 
334 //
335 // PROTECTED MEMBER FUNCTIONS:
336 //
337 
338 
340 {
341  if ( qp != 0 )
342  delete qp;
343 
344  /* create new qpOASES QP object... */
345  qp = new qpOASES::SQProblem( nV,nC );
346 
347  qpOASES::Options options;
348  options.setToFast();
349 
350  qp->setOptions( options );
351 
352  /* ... and define its printLevel */
353  int printLevel = 0;
354  //get( PRINTLEVEL,printLevel );
355 
356  switch( (PrintLevel) printLevel )
357  {
358  case HIGH:
359  qp->setPrintLevel( qpOASES::PL_MEDIUM );
360  break;
361 
362  case DEBUG:
363  qp->setPrintLevel( qpOASES::PL_HIGH );
364  break;
365 
366  // PL_NONE, PL_LOW, PL_MEDIUM
367  default:
368  qp->setPrintLevel( qpOASES::PL_NONE );
369  }
370 
372 
373  return SUCCESSFUL_RETURN;
374 }
375 
376 
378 {
379  switch ( (qpOASES::returnValue)ret )
380  {
383  return SUCCESSFUL_RETURN;
384 
388 
389  default:
391 
392  /* check for infeasibility */
393  if ( (bool)qp->isInfeasible( ) == true )
394  {
396  return RET_QP_INFEASIBLE;
397  }
398 
399  /* check for unboundedness */
400  if ( (bool)qp->isUnbounded( ) == true )
401  {
403  return RET_QP_UNBOUNDED;
404  }
405 
406  return RET_QP_SOLUTION_FAILED;
407  }
408 }
409 
410 
412 
413 // end of file.
returnValue setLast(LogName _name, int lastValue, double time=-INFTY)
void init(unsigned _nRows=0, unsigned _nCols=0)
Definition: matrix.hpp:135
QPsolver_qpOASES & operator=(const QPsolver_qpOASES &rhs)
virtual returnValue solve(DenseCP *cp_)
DenseQPsolver & operator=(const DenseQPsolver &rhs)
const double INFTY
Allows to pass back messages to the calling function.
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
#define PL_MEDIUM
virtual returnValue getVarianceCovariance(DMatrix &var)
BooleanType isUnbounded() const
virtual returnValue getDualSolution(DVector &yOpt) const
#define CLOSE_NAMESPACE_ACADO
#define PL_NONE
(not yet documented)
virtual returnValue getPrimalSolution(DVector &xOpt) const
Abstract base class for algorithms solving quadratic programs.
BooleanType isSolved() const
#define PL_HIGH
virtual returnValue setupQPobject(uint nV, uint nC)
virtual DenseQPsolver * cloneDenseQPsolver() const
Data class for storing generic conic programs.
Definition: dense_cp.hpp:55
printLevel
Definition: example1.py:55
returnValue updateQPstatus(int ret)
virtual double getObjVal() const
Encapsulates all user interaction for setting options, logging data and plotting results.
qpOASES::SQProblem * qp
#define returnValue
void rhs(const real_t *x, real_t *f)
PrintLevel
EIGEN_STRONG_INLINE const Scalar * data() const
virtual returnValue step(double *H, double *A, double *g, double *lb, double *ub, double *lbA, double *ubA)
GenericVector< double > DVector
Definition: vector.hpp:329
virtual returnValue solve(DenseCP *cp_)
#define BEGIN_NAMESPACE_ACADO
Base class for algorithms solving conic programs.
virtual uint getNumberOfVariables() const
virtual DenseCPsolver * clone() const
#define ACADOERROR(retval)
virtual uint getNumberOfConstraints() const


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