Bounds.c
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 
36 #include <qpOASES_e/Bounds.h>
37 
38 
40 
41 
42 /*****************************************************************************
43  * P U B L I C *
44  *****************************************************************************/
45 
46 
47 /*
48  * B o u n d s
49  */
50 void BoundsCON( Bounds* _THIS, int _n )
51 {
52  Bounds_init( _THIS,_n );
53 }
54 
55 
56 /*
57  * c o p y
58  */
59 void BoundsCPY( Bounds* FROM,
60  Bounds* TO
61  )
62 {
63  int i;
64 
65  TO->n = FROM->n;
66  TO->noLower = FROM->noLower;
67  TO->noUpper = FROM->noUpper;
68 
69  if ( FROM->n != 0 )
70  {
71  for( i=0; i<TO->n; ++i )
72  {
73  TO->type[i] = FROM->type[i];
74  TO->status[i] = FROM->status[i];
75  }
76  }
77 
78  IndexlistCPY( &(FROM->freee),&(TO->freee) );
79  IndexlistCPY( &(FROM->fixed),&(TO->fixed) );
80 }
81 
82 
83 
84 /*
85  * i n i t
86  */
88  int _n
89  )
90 {
91  int i;
92 
93  if ( _n < 0 )
95 
96  if ( _n >= 0 )
97  {
98  Indexlist_init( &(_THIS->freee),_n );
99  Indexlist_init( &(_THIS->fixed),_n );
100  }
101 
102  _THIS->n = _n;
103  _THIS->noLower = BT_TRUE;
104  _THIS->noUpper = BT_TRUE;
105 
106  assert( _THIS->n <= NVMAX );
107 
108  if ( _THIS->n > 0 )
109  {
110  for( i=0; i<_THIS->n; ++i )
111  {
112  _THIS->type[i] = ST_UNKNOWN;
113  _THIS->status[i] = ST_UNDEFINED;
114  }
115  }
116 
117  return SUCCESSFUL_RETURN;
118 }
119 
120 
121 
122 /*
123  * s e t u p B o u n d
124  */
126  )
127 {
128  /* consistency check */
129  if ( ( number < 0 ) || ( number >= _THIS->n ) )
131 
132  /* Add bound index to respective index list. */
133  switch ( _status )
134  {
135  case ST_INACTIVE:
136  if ( Bounds_addIndex( _THIS,Bounds_getFree( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
138  break;
139 
140  case ST_LOWER:
141  if ( Bounds_addIndex( _THIS,Bounds_getFixed( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
143  break;
144 
145  case ST_UPPER:
146  if ( Bounds_addIndex( _THIS,Bounds_getFixed( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
148  break;
149 
150  default:
152  }
153 
154  return SUCCESSFUL_RETURN;
155 }
156 
157 
158 /*
159  * s e t u p A l l F r e e
160  */
162 {
163  return Bounds_setupAll( _THIS,ST_INACTIVE );
164 }
165 
166 
167 /*
168  * s e t u p A l l L o w e r
169  */
171 {
172  return Bounds_setupAll( _THIS,ST_LOWER );
173 }
174 
175 
176 /*
177  * s e t u p A l l U p p e r
178  */
180 {
181  return Bounds_setupAll( _THIS,ST_UPPER );
182 }
183 
184 
185 /*
186  * m o v e F i x e d T o F r e e
187  */
189 {
190  /* consistency check */
191  if ( ( number < 0 ) || ( number >= _THIS->n ) )
193 
194  /* Move index from indexlist of fixed variables to that of free ones. */
195  if ( Bounds_removeIndex( _THIS,Bounds_getFixed( _THIS ),number ) != SUCCESSFUL_RETURN )
197 
198  if ( Bounds_addIndex( _THIS,Bounds_getFree( _THIS ),number,ST_INACTIVE ) != SUCCESSFUL_RETURN )
200 
201  return SUCCESSFUL_RETURN;
202 }
203 
204 
205 /*
206  * m o v e F r e e T o F i x e d
207  */
209  )
210 {
211  /* consistency check */
212  if ( ( number < 0 ) || ( number >= _THIS->n ) )
214 
215  /* Move index from indexlist of free variables to that of fixed ones. */
216  if ( Bounds_removeIndex( _THIS,Bounds_getFree( _THIS ),number ) != SUCCESSFUL_RETURN )
218 
219  if ( Bounds_addIndex( _THIS,Bounds_getFixed( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
221 
222  return SUCCESSFUL_RETURN;
223 }
224 
225 
226 /*
227  * f l i p F i x e d
228  */
229 returnValue Bounds_flipFixed( Bounds* _THIS, int number )
230 {
231  /* consistency check */
232  if ( ( number < 0 ) || ( number >= _THIS->n ) )
234 
235  if ( _THIS->status != 0 )
236  switch (_THIS->status[number])
237  {
238  case ST_LOWER: _THIS->status[number] = ST_UPPER; break;
239  case ST_UPPER: _THIS->status[number] = ST_LOWER; break;
240  default: return THROWERROR( RET_MOVING_BOUND_FAILED );
241  }
242 
243  return SUCCESSFUL_RETURN;
244 }
245 
246 
247 /*
248  * s w a p F r e e
249  */
250 returnValue Bounds_swapFree( Bounds* _THIS, int number1, int number2
251  )
252 {
253  /* consistency check */
254  if ( ( number1 < 0 ) || ( number1 >= _THIS->n ) || ( number2 < 0 ) || ( number2 >= _THIS->n ) )
256 
257  /* Swap index within indexlist of free variables. */
258  return Bounds_swapIndex( _THIS,Bounds_getFree( _THIS ),number1,number2 );
259 }
260 
261 
262 /*
263  * s h i f t
264  */
265 returnValue Bounds_shift( Bounds* _THIS, int offset )
266 {
267  int i;
268 
269  myStatic Indexlist shiftedFreee;
270  myStatic Indexlist shiftedFixed;
271 
272  Indexlist_init( &shiftedFreee,_THIS->n );
273  Indexlist_init( &shiftedFixed,_THIS->n );
274 
275  /* consistency check */
276  if ( ( offset == 0 ) || ( _THIS->n <= 1 ) )
277  return SUCCESSFUL_RETURN;
278 
279  if ( ( offset < 0 ) || ( offset > _THIS->n/2 ) )
281 
282  if ( ( _THIS->n % offset ) != 0 )
284 
285 
286  /* 1) Shift types and _THIS->status. */
287  for( i=0; i<_THIS->n-offset; ++i )
288  {
289  Bounds_setType( _THIS,i,Bounds_getType( _THIS,i+offset ) );
290  Bounds_setStatus( _THIS,i,Bounds_getStatus( _THIS,i+offset ) );
291  }
292 
293  /* 2) Construct shifted index lists of free and fixed variables. */
294  for( i=0; i<_THIS->n; ++i )
295  {
296  switch ( Bounds_getStatus( _THIS,i ) )
297  {
298  case ST_INACTIVE:
299  if ( Indexlist_addNumber( &shiftedFreee,i ) != SUCCESSFUL_RETURN )
301  break;
302 
303  case ST_LOWER:
304  if ( Indexlist_addNumber( &shiftedFixed,i ) != SUCCESSFUL_RETURN )
306  break;
307 
308  case ST_UPPER:
309  if ( Indexlist_addNumber( &shiftedFixed,i ) != SUCCESSFUL_RETURN )
311  break;
312 
313  default:
315  }
316  }
317 
318  /* 3) Assign shifted index list. */
319  IndexlistCPY( &shiftedFreee,&(_THIS->freee) );
320  IndexlistCPY( &shiftedFixed,&(_THIS->fixed) );
321 
322  return SUCCESSFUL_RETURN;
323 }
324 
325 
326 /*
327  * r o t a t e
328  */
329 returnValue Bounds_rotate( Bounds* _THIS, int offset )
330 {
331  int i;
332  myStatic SubjectToType typeTmp[NVMAX];
333  myStatic SubjectToStatus statusTmp[NVMAX];
334 
335  myStatic Indexlist rotatedFreee;
336  myStatic Indexlist rotatedFixed;
337 
338  Indexlist_init( &rotatedFreee,_THIS->n );
339  Indexlist_init( &rotatedFixed,_THIS->n );
340 
341  /* consistency check */
342  if ( ( offset == 0 ) || ( offset == _THIS->n ) || ( _THIS->n <= 1 ) )
343  return SUCCESSFUL_RETURN;
344 
345  if ( ( offset < 0 ) || ( offset > _THIS->n ) )
347 
348 
349  /* 1) Rotate types and status. */
350  for( i=0; i<offset; ++i )
351  {
352  typeTmp[i] = Bounds_getType( _THIS,i );
353  statusTmp[i] = Bounds_getStatus( _THIS,i );
354  }
355 
356  for( i=0; i<_THIS->n-offset; ++i )
357  {
358  Bounds_setType( _THIS,i,Bounds_getType( _THIS,i+offset ) );
359  Bounds_setStatus( _THIS,i,Bounds_getStatus( _THIS,i+offset ) );
360  }
361 
362  for( i=_THIS->n-offset; i<_THIS->n; ++i )
363  {
364  Bounds_setType( _THIS,i,typeTmp[i-_THIS->n+offset] );
365  Bounds_setStatus( _THIS,i,statusTmp[i-_THIS->n+offset] );
366  }
367 
368  /* 2) Construct shifted index lists of free and fixed variables. */
369  for( i=0; i<_THIS->n; ++i )
370  {
371  switch ( Bounds_getStatus( _THIS,i ) )
372  {
373  case ST_INACTIVE:
374  if ( Indexlist_addNumber( &rotatedFreee,i ) != SUCCESSFUL_RETURN )
376  break;
377 
378  case ST_LOWER:
379  if ( Indexlist_addNumber( &rotatedFixed,i ) != SUCCESSFUL_RETURN )
381  break;
382 
383  case ST_UPPER:
384  if ( Indexlist_addNumber( &rotatedFixed,i ) != SUCCESSFUL_RETURN )
386  break;
387 
388  default:
390  }
391  }
392 
393  /* 3) Assign shifted index list. */
394  IndexlistCPY( &rotatedFreee,&(_THIS->freee) );
395  IndexlistCPY( &rotatedFixed,&(_THIS->fixed) );
396 
397  return SUCCESSFUL_RETURN;
398 }
399 
400 
401 /*
402  * p r i n t
403  */
405 {
406  #ifndef __SUPPRESSANYOUTPUT__
407 
408  myStatic char myPrintfString[QPOASES_MAX_STRING_LENGTH];
409 
410  int nFR = Bounds_getNFR( _THIS );
411  int nFX = Bounds_getNFX( _THIS );
412 
413  int *FR_idx, *FX_idx;
414 
415  if ( _THIS->n == 0 )
416  return SUCCESSFUL_RETURN;
417 
418  Indexlist_getNumberArray( Bounds_getFree( _THIS ),&FR_idx );
419  Indexlist_getNumberArray( Bounds_getFixed( _THIS ),&FX_idx );
420 
421  snprintf( myPrintfString,QPOASES_MAX_STRING_LENGTH,"Bounds object comprising %d variables (%d free, %d fixed):\n",_THIS->n,nFR,nFX );
422  qpOASES_myPrintf( myPrintfString );
423 
424  REFER_NAMESPACE_QPOASES qpOASES_printNI( FR_idx,nFR,"free " );
425  REFER_NAMESPACE_QPOASES qpOASES_printNI( FX_idx,nFX,"fixed" );
426 
427  #endif /* __SUPPRESSANYOUTPUT__ */
428 
429  return SUCCESSFUL_RETURN;
430 }
431 
432 
433 
434 /*****************************************************************************
435  * P R O T E C T E D *
436  *****************************************************************************/
437 
438 /*
439  * s e t u p A l l
440  */
442 {
443  int i;
444 
445  /* 1) Place unbounded variables at the beginning of the index list of free variables. */
446  for( i=0; i<_THIS->n; ++i )
447  {
448  if ( Bounds_getType( _THIS,i ) == ST_UNBOUNDED )
449  {
450  if ( Bounds_setupBound( _THIS,i,_status ) != SUCCESSFUL_RETURN )
452  }
453  }
454 
455  /* 2) Add remaining (i.e. bounded but possibly free) variables to the index list of free variables. */
456  for( i=0; i<_THIS->n; ++i )
457  {
458  if ( Bounds_getType( _THIS,i ) == ST_BOUNDED )
459  {
460  if ( Bounds_setupBound( _THIS,i,_status ) != SUCCESSFUL_RETURN )
462  }
463  }
464 
465  /* 3) Place implicitly fixed variables at the end of the index list of free variables. */
466  for( i=0; i<_THIS->n; ++i )
467  {
468  if ( Bounds_getType( _THIS,i ) == ST_EQUALITY )
469  {
470  if ( Bounds_setupBound( _THIS,i,_status ) != SUCCESSFUL_RETURN )
472  }
473  }
474 
475  /* 4) Moreover, add all bounds of unknown type. */
476  for( i=0; i<_THIS->n; ++i )
477  {
478  if ( Bounds_getType( _THIS,i ) == ST_UNKNOWN || Bounds_getType( _THIS,i ) == ST_DISABLED )
479  {
480  if ( Bounds_setupBound( _THIS,i,_status ) != SUCCESSFUL_RETURN )
482  }
483  }
484 
485  return SUCCESSFUL_RETURN;
486 }
487 
488 
489 /*
490  * a d d I n d e x
491  */
492 returnValue Bounds_addIndex( Bounds* _THIS, Indexlist* const indexlist,
493  int newnumber, SubjectToStatus newstatus
494  )
495 {
496  if ( _THIS->status != 0 )
497  {
498  /* consistency check */
499  if ( _THIS->status[newnumber] == newstatus )
501 
502  _THIS->status[newnumber] = newstatus;
503  }
504  else
506 
507  if ( indexlist != 0 )
508  {
509  if ( Indexlist_addNumber( indexlist,newnumber ) == RET_INDEXLIST_EXCEEDS_MAX_LENGTH )
511  }
512  else
514 
515  return SUCCESSFUL_RETURN;
516 }
517 
518 
519 /*
520  * r e m o v e I n d e x
521  */
522 returnValue Bounds_removeIndex( Bounds* _THIS, Indexlist* const indexlist,
523  int removenumber
524  )
525 {
526  if ( _THIS->status != 0 )
527  _THIS->status[removenumber] = ST_UNDEFINED;
528  else
530 
531  if ( indexlist != 0 )
532  {
533  if ( Indexlist_removeNumber( indexlist,removenumber ) != SUCCESSFUL_RETURN )
535  }
536  else
538 
539  return SUCCESSFUL_RETURN;
540 }
541 
542 
543 /*
544  * s w a p I n d e x
545  */
546 returnValue Bounds_swapIndex( Bounds* _THIS, Indexlist* const indexlist,
547  int number1, int number2
548  )
549 {
550  /* consistency checks */
551  if ( _THIS->status != 0 )
552  {
553  if ( _THIS->status[number1] != _THIS->status[number2] )
555  }
556  else
558 
559  if ( number1 == number2 )
560  {
562  return SUCCESSFUL_RETURN;
563  }
564 
565  if ( indexlist != 0 )
566  {
567  if ( Indexlist_swapNumbers( indexlist,number1,number2 ) != SUCCESSFUL_RETURN )
569  }
570  else
572 
573  return SUCCESSFUL_RETURN;
574 }
575 
576 
577 
579 
580 
581 /*
582  * end of file
583  */
returnValue Bounds_print(Bounds *_THIS)
Definition: Bounds.c:404
static Indexlist * Bounds_getFree(Bounds *_THIS)
Definition: Bounds.h:508
SubjectToStatus status[NVMAX]
Definition: Bounds.h:61
returnValue Bounds_shift(Bounds *_THIS, int offset)
Definition: Bounds.c:265
#define ST_LOWER
int n
Definition: Bounds.h:58
BEGIN_NAMESPACE_QPOASES void BoundsCON(Bounds *_THIS, int _n)
Definition: Bounds.c:50
returnValue Indexlist_removeNumber(Indexlist *_THIS, int removenumber)
Definition: Indexlist.c:160
returnValue Bounds_swapFree(Bounds *_THIS, int number1, int number2)
Definition: Bounds.c:250
returnValue Bounds_rotate(Bounds *_THIS, int offset)
Definition: Bounds.c:329
static SubjectToStatus Bounds_getStatus(Bounds *_THIS, int i)
Definition: Bounds.h:375
Allows to pass back messages to the calling function.
returnValue Bounds_swapIndex(Bounds *_THIS, Indexlist *const indexlist, int number1, int number2)
Definition: Bounds.c:546
returnValue Bounds_setupAllLower(Bounds *_THIS)
Definition: Bounds.c:170
returnValue Bounds_setupAllUpper(Bounds *_THIS)
Definition: Bounds.c:179
returnValue Bounds_setupAllFree(Bounds *_THIS)
Definition: Bounds.c:161
#define ST_UNDEFINED
#define ST_INACTIVE
returnValue Bounds_addIndex(Bounds *_THIS, Indexlist *const indexlist, int newnumber, SubjectToStatus newstatus)
Definition: Bounds.c:492
static returnValue Bounds_setType(Bounds *_THIS, int i, SubjectToType value)
Definition: Bounds.h:387
returnValue Bounds_setupAll(Bounds *_THIS, SubjectToStatus _status)
Definition: Bounds.c:441
returnValue Indexlist_swapNumbers(Indexlist *_THIS, int number1, int number2)
Definition: Indexlist.c:190
returnValue Indexlist_addNumber(Indexlist *_THIS, int addnumber)
Definition: Indexlist.c:139
static int Bounds_getNFX(Bounds *_THIS)
Definition: Bounds.h:499
returnValue Bounds_moveFreeToFixed(Bounds *_THIS, int number, SubjectToStatus _status)
Definition: Bounds.c:208
void BoundsCPY(Bounds *FROM, Bounds *TO)
Definition: Bounds.c:59
returnValue Bounds_removeIndex(Bounds *_THIS, Indexlist *const indexlist, int removenumber)
Definition: Bounds.c:522
returnValue qpOASES_myPrintf(const char *s)
Definition: Utils.c:269
BooleanType noUpper
Definition: Bounds.h:64
#define assert(ignore)
BooleanType noLower
Definition: Bounds.h:63
returnValue Indexlist_init(Indexlist *_THIS, int n)
Definition: Indexlist.c:86
static int Bounds_getNFR(Bounds *_THIS)
Definition: Bounds.h:490
returnValue Bounds_init(Bounds *_THIS, int _n)
Definition: Bounds.c:87
void IndexlistCPY(Indexlist *FROM, Indexlist *TO)
Definition: Indexlist.c:60
#define BT_TRUE
Definition: acado_types.hpp:47
returnValue qpOASES_printNI(const int *const _index, int n, const char *name)
Definition: Utils.c:244
returnValue Indexlist_getNumberArray(Indexlist *_THIS, int **const numberarray)
Definition: Indexlist.c:105
returnValue Bounds_moveFixedToFree(Bounds *_THIS, int number)
Definition: Bounds.c:188
#define QPOASES_MAX_STRING_LENGTH
SubjectToType type[NVMAX]
Definition: Bounds.h:60
returnValue Bounds_flipFixed(Bounds *_THIS, int number)
Definition: Bounds.c:229
Manages working sets of bounds (= box constraints).
#define ST_UPPER
static SubjectToType Bounds_getType(Bounds *_THIS, int i)
Definition: Bounds.h:363
static returnValue Bounds_setStatus(Bounds *_THIS, int i, SubjectToStatus value)
Definition: Bounds.h:402
static Indexlist * Bounds_getFixed(Bounds *_THIS)
Definition: Bounds.h:517
returnValue Bounds_setupBound(Bounds *_THIS, int number, SubjectToStatus _status)
Definition: Bounds.c:125
#define myStatic
Definition: Types.h:103


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