qpOASES-3.0beta/src/SQProblem.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-2011 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/SQProblem.hpp>
38 
39 
41 
42 
43 /*****************************************************************************
44  * P U B L I C *
45  *****************************************************************************/
46 
47 
48 /*
49  * S Q P r o b l e m
50  */
52 {
53 }
54 
55 
56 /*
57  * S Q P r o b l e m
58  */
59 SQProblem::SQProblem( int _nV, int _nC, HessianType _hessianType ) : QProblem( _nV,_nC,_hessianType )
60 {
61 }
62 
63 
64 /*
65  * S Q P r o b l e m
66  */
68 {
69 }
70 
71 
72 /*
73  * ~ S Q P r o b l e m
74  */
76 {
77 }
78 
79 
80 /*
81  * o p e r a t o r =
82  */
84 {
85  if ( this != &rhs )
86  {
87  QProblem::operator=( rhs );
88  }
89 
90  return *this;
91 }
92 
93 
94 /*
95  * h o t s t a r t
96  */
97 returnValue SQProblem::hotstart( const real_t* const H_new, const real_t* const g_new, const real_t* const A_new,
98  const real_t* const lb_new, const real_t* const ub_new,
99  const real_t* const lbA_new, const real_t* const ubA_new,
100  int& nWSR, real_t* const cputime )
101 {
102  if ( ( getStatus( ) == QPS_NOTINITIALISED ) ||
105  {
107  }
108 
109  /* start runtime measurement */
110  real_t starttime = 0.0;
111  if ( cputime != 0 )
112  starttime = getCPUtime( );
113 
114 
115  /* I) UPDATE QP MATRICES AND VECTORS */
116  if ( setupAuxiliaryQP( H_new,A_new ) != SUCCESSFUL_RETURN )
118 
119 
120  /* II) PERFORM USUAL HOMOTOPY */
121 
122  /* Allow only remaining CPU time for usual hotstart. */
123  if ( cputime != 0 )
124  *cputime -= getCPUtime( ) - starttime;
125 
126  returnValue returnvalue = QProblem::hotstart( g_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,cputime );
127 
128 
129  /* stop runtime measurement */
130  if ( cputime != 0 )
131  *cputime = getCPUtime( ) - starttime;
132 
133  return returnvalue;
134 }
135 
136 
137 /*
138  * h o t s t a r t
139  */
140 returnValue SQProblem::hotstart( const char* const H_file, const char* const g_file, const char* const A_file,
141  const char* const lb_file, const char* const ub_file,
142  const char* const lbA_file, const char* const ubA_file,
143  int& nWSR, real_t* const cputime
144  )
145 {
146  int nV = getNV( );
147  int nC = getNC( );
148 
149  returnValue returnvalue;
150 
151  /* consistency checks */
152  if ( ( H_file == 0 ) || ( g_file == 0 ) )
154 
155  if ( ( nC > 0 ) && ( A_file == 0 ) )
157 
158 
159  /* 1) Load new QP matrices from files. */
160  real_t* H_new = new real_t[nV*nV];
161  real_t* A_new = new real_t[nC*nV];
162 
163  if ( readFromFile( H_new, nV,nV, H_file ) != SUCCESSFUL_RETURN )
165 
166  if ( readFromFile( A_new, nC,nV, A_file ) != SUCCESSFUL_RETURN )
168 
169  /* 2) Load new QP vectors from files. */
170  real_t* g_new = new real_t[nV];
171  real_t* lb_new = 0;
172  real_t* ub_new = 0;
173  real_t* lbA_new = 0;
174  real_t* ubA_new = 0;
175 
176  if ( lb_file != 0 )
177  lb_new = new real_t[nV];
178  if ( ub_file != 0 )
179  ub_new = new real_t[nV];
180  if ( lbA_file != 0 )
181  lbA_new = new real_t[nC];
182  if ( ubA_file != 0 )
183  ubA_new = new real_t[nC];
184 
185  returnvalue = loadQPvectorsFromFile( g_file,lb_file,ub_file,lbA_file,ubA_file,
186  g_new,lb_new,ub_new,lbA_new,ubA_new
187  );
188  if ( returnvalue != SUCCESSFUL_RETURN )
189  {
190  if ( ubA_file != 0 )
191  delete[] ubA_new;
192  if ( lbA_file != 0 )
193  delete[] lbA_new;
194  if ( ub_file != 0 )
195  delete[] ub_new;
196  if ( lb_file != 0 )
197  delete[] lb_new;
198  delete[] g_new;
199  delete[] A_new;
200  delete[] H_new;
201 
203  }
204 
205  /* 3) Actually perform hotstart. */
206  returnvalue = hotstart( H_new,g_new,A_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,cputime );
207 
208  if ( ubA_file != 0 )
209  delete[] ubA_new;
210  if ( lbA_file != 0 )
211  delete[] lbA_new;
212  if ( ub_file != 0 )
213  delete[] ub_new;
214  if ( lb_file != 0 )
215  delete[] lb_new;
216  delete[] g_new;
217  delete[] A_new;
218  delete[] H_new;
219 
220  return returnvalue;
221 }
222 
223 /*
224  * h o t s t a r t
225  */
227  const real_t* const g_new,
228  Matrix *A_new,
229  const real_t* const lb_new,
230  const real_t* const ub_new,
231  const real_t* const lbA_new,
232  const real_t* const ubA_new,
233  int& nWSR,
234  real_t* const cputime
235  )
236 {
237  if ( ( getStatus( ) == QPS_NOTINITIALISED ) ||
240  {
242  }
243 
244  /* start runtime measurement */
245  real_t starttime = 0.0;
246  if ( cputime != 0 )
247  starttime = getCPUtime( );
248 
249 
250  /* I) UPDATE QP MATRICES AND VECTORS */
251  if ( setupAuxiliaryQP( H_new,A_new ) != SUCCESSFUL_RETURN )
253 
254 
255  /* II) PERFORM USUAL HOMOTOPY */
256 
257  /* Allow only remaining CPU time for usual hotstart. */
258  if ( cputime != 0 )
259  *cputime -= getCPUtime( ) - starttime;
260 
261  returnValue returnvalue = QProblem::hotstart( g_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,cputime );
262 
263 
264  /* stop runtime measurement */
265  if ( cputime != 0 )
266  *cputime = getCPUtime( ) - starttime;
267 
268  return returnvalue;
269 }
270 
271 
272 /*
273  * h o t s t a r t
274  */
276  const real_t* const lb_new, const real_t* const ub_new,
277  const real_t* const lbA_new, const real_t* const ubA_new,
278  int& nWSR, real_t* const cputime
279  )
280 {
281  /* Call to hotstart function for fixed QP matrices. */
282  return QProblem::hotstart( g_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,cputime );
283 }
284 
285 
286 /*
287  * h o t s t a r t
288  */
289 returnValue SQProblem::hotstart( const char* const g_file,
290  const char* const lb_file, const char* const ub_file,
291  const char* const lbA_file, const char* const ubA_file,
292  int& nWSR, real_t* const cputime
293  )
294 {
295  /* Call to hotstart function for fixed QP matrices. */
296  return QProblem::hotstart( g_file,lb_file,ub_file,lbA_file,ubA_file, nWSR,cputime );
297 }
298 
299 
300 /*
301  * h o t s t a r t
302  */
304  const real_t* const lb_new, const real_t* const ub_new,
305  const real_t* const lbA_new, const real_t* const ubA_new,
306  int& nWSR, real_t* const cputime,
307  const Bounds* const guessedBounds, const Constraints* const guessedConstraints
308  )
309 {
310  /* Call to hotstart function for fixed QP matrices. */
311  return QProblem::hotstart( g_new,lb_new,ub_new,lbA_new,ubA_new, nWSR,cputime, guessedBounds,guessedConstraints );
312 }
313 
314 
315 /*
316  * h o t s t a r t
317  */
318 returnValue SQProblem::hotstart( const char* const g_file,
319  const char* const lb_file, const char* const ub_file,
320  const char* const lbA_file, const char* const ubA_file,
321  int& nWSR, real_t* const cputime,
322  const Bounds* const guessedBounds, const Constraints* const guessedConstraints
323  )
324 {
325  /* Call to hotstart function for fixed QP matrices. */
326  return QProblem::hotstart( g_file,lb_file,ub_file,lbA_file,ubA_file, nWSR,cputime, guessedBounds,guessedConstraints );
327 }
328 
329 
330 
331 #ifdef __MATLAB__
332 returnValue SQProblem::resetMatrixPointers( )
333 {
334  H = 0;
335  A = 0;
336 
337  return SUCCESSFUL_RETURN;
338 }
339 #endif
340 
341 
342 
343 /*****************************************************************************
344  * P R O T E C T E D *
345  *****************************************************************************/
346 
347 /*
348  * s e t u p A u x i l i a r y Q P
349  */
351 {
352  int nV = getNV( );
353  int nC = getNC( );
354 
355  DenseMatrix *dA = new DenseMatrix(nC, nV, nV, (real_t*) A_new);
356  SymDenseMat *sH = new SymDenseMat(nV, nV, nV, (real_t*) H_new);
357 
358  returnValue returnvalue = setupAuxiliaryQP ( sH, dA );
359 
360  if ( H_new != 0 )
363 
364  return returnvalue;
365 }
366 
367 
368 /*
369  * s e t u p A u x i l i a r y Q P
370  */
372 {
373 
374  int i;
375  int nV = getNV( );
376  int nC = getNC( );
377  returnValue returnvalue;
378 
379  if ( ( getStatus( ) == QPS_NOTINITIALISED ) ||
382  {
384  }
385 
387 
388 
389  /* I) SETUP NEW QP MATRICES AND VECTORS: */
390  /* 1) Shift constraints' bounds vectors by (A_new - A)'*x_opt to ensure
391  * that old optimal solution remains feasible for new QP data. */
392  /* Firstly, shift by -A'*x_opt and ... */
393  if ( nC > 0 )
394  {
395  if ( A_new == 0 )
397 
398  for ( i=0; i<nC; ++i )
399  {
400  lbA[i] = -Ax_l[i];
401  ubA[i] = Ax_u[i];
402  }
403 
404  /* Set constraint matrix as well as ... */
405  setA( A_new );
406 
407  /* ... secondly, shift by +A_new'*x_opt. */
408  for ( i=0; i<nC; ++i )
409  {
410  lbA[i] += Ax[i];
411  ubA[i] += Ax[i];
412  }
413 
414  /* update constraint products. */
415  for ( i=0; i<nC; ++i )
416  {
417  Ax_u[i] = ubA[i] - Ax[i];
418  Ax_l[i] = Ax[i] - lbA[i];
419  }
420  }
421 
422  /* 2) Set new Hessian matrix,determine Hessian type and
423  * regularise new Hessian matrix if necessary. */
424  /* a) Setup new Hessian matrix and determine its type. */
425  if ( H_new != 0 )
426  {
427  setH( H_new );
428 
432 
433  /* b) Regularise new Hessian if necessary. */
434  if ( ( hessianType == HST_ZERO ) ||
435  ( hessianType == HST_SEMIDEF ) ||
436  ( usingRegularisation( ) == BT_TRUE ) )
437  {
438  isRegularised = BT_FALSE; /* reset previous regularisation */
439 
442  }
443  }
444  else
445  {
446  if ( H != 0 )
448  }
449 
450 
451 
452  /* 3) Setup QP gradient. */
455 
456 
457  /* II) SETUP WORKING SETS AND MATRIX FACTORISATIONS: */
458  /* 1) Make a copy of current bounds/constraints ... */
459  Bounds oldBounds = bounds;
460  Constraints oldConstraints = constraints;
461 
462  /* ... reset them ... */
463  bounds.init( nV );
464  constraints.init( nC );
465 
466  /* ... and set them up afresh. */
469 
472 
475 
476  /* 2) Setup TQ factorisation. */
479 
480  /* 3) Setup old working sets afresh (updating TQ factorisation). */
481  if ( setupAuxiliaryWorkingSet( &oldBounds,&oldConstraints,BT_TRUE ) != SUCCESSFUL_RETURN )
483 
484  /* 4) Calculate Cholesky decomposition. */
485  if ( ( getNAC( ) + getNFX( ) ) == 0 )
486  {
487  /* Factorise full Hessian if no bounds/constraints are active. */
488  returnvalue = setupCholeskyDecomposition( );
489  }
490  else
491  {
492  /* Factorise projected Hessian if there active bounds/constraints. */
493  returnvalue = setupCholeskyDecompositionProjected( );
494  }
495  if ( returnvalue != SUCCESSFUL_RETURN )
496  {
497  /* recede to trivial active set */
498  bounds.init(nV);
501  else
503  constraints.init(nC);
505 
508  }
509 
511 
512  return SUCCESSFUL_RETURN;
513 }
514 
516 
517 
518 /*
519  * end of file
520  */
QProblemStatus status
Definition: QProblem.h:82
#define ST_LOWER
int getNFX()
Implements the online active set strategy for QPs with varying matrices.
Allows to pass back messages to the calling function.
HessianType hessianType
Definition: QProblem.h:87
Interfaces matrix-vector operations tailored to symmetric dense matrices.
int getNV() const
returnValue setH(const real_t *const H_new)
SQProblem & operator=(const SQProblem &rhs)
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)
virtual returnValue setupAuxiliaryQP(const real_t *const H_new, const real_t *const A_new)
returnValue readFromFile(real_t *data, int nrow, int ncol, const char *datafilename)
int getNAC()
BooleanType usingRegularisation() const
Interfaces matrix-vector operations tailored to general dense matrices.
Bounds bounds
Definition: QProblem.h:72
Abstract base class for interfacing tailored matrix-vector operations.
Options options
Definition: QProblem.h:98
returnValue loadQPvectorsFromFile(const char *const g_file, const char *const lb_file, const char *const ub_file, const char *const lbA_file, const char *const ubA_file, real_t *const g_new, real_t *const lb_new, real_t *const ub_new, real_t *const lbA_new, real_t *const ubA_new) const
void rhs(const real_t *x, real_t *f)
int getNC() const
QProblemStatus getStatus() const
#define BT_TRUE
Definition: acado_types.hpp:47
#define HST_ZERO
#define HST_SEMIDEF
#define HST_UNKNOWN
DenseMatrix * H
Definition: QProblem.h:65
#define BT_FALSE
Definition: acado_types.hpp:49
Manages working sets of bounds (= box constraints).
double real_t
Definition: AD_test.c:10
Implements the online active set strategy for QPs with general constraints.
returnValue setupAuxiliaryWorkingSet(const Bounds *const auxiliaryBounds, const Constraints *const auxiliaryConstraints, BooleanType setupAfresh)
returnValue setA(const real_t *const A_new)
Abstract base class for interfacing matrix-vector operations tailored to symmetric matrices...
returnValue hotstart(const real_t *const H_new, const real_t *const g_new, const real_t *const A_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)


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