Constraints.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/Constraints.h>
37 
38 
40 
41 
42 /*****************************************************************************
43  * P U B L I C *
44  *****************************************************************************/
45 
46 
47 /*
48  * C o n s t r a i n t s
49  */
51  int _n
52  )
53 {
54  Constraints_init( _THIS,_n );
55 }
56 
57 
58 /*
59  * c o p y
60  */
62  Constraints* TO
63  )
64 {
65  int i;
66 
67  TO->n = FROM->n;
68  TO->noLower = FROM->noLower;
69  TO->noUpper = FROM->noUpper;
70 
71  if ( FROM->n != 0 )
72  {
73  for( i=0; i<TO->n; ++i )
74  {
75  TO->type[i] = FROM->type[i];
76  TO->status[i] = FROM->status[i];
77  }
78  }
79 
80  IndexlistCPY( &(FROM->active),&(TO->active) );
81  IndexlistCPY( &(FROM->inactive),&(TO->inactive) );
82 }
83 
84 
85 
86 /*
87  * i n i t
88  */
90  int _n
91  )
92 {
93  int i;
94 
95  if ( _n < 0 )
97 
98  if ( _n >= 0 )
99  {
100  Indexlist_init( &(_THIS->active),_n );
101  Indexlist_init( &(_THIS->inactive),_n );
102  }
103 
104  _THIS->n = _n;
105  _THIS->noLower = BT_TRUE;
106  _THIS->noUpper = BT_TRUE;
107 
108  assert( _THIS->n <= NCMAX );
109 
110  if ( _THIS->n > 0 )
111  {
112  for( i=0; i<_THIS->n; ++i )
113  {
114  _THIS->type[i] = ST_UNKNOWN;
115  _THIS->status[i] = ST_UNDEFINED;
116  }
117  }
118 
119  return SUCCESSFUL_RETURN;
120 }
121 
122 
123 
124 /*
125  * s e t u p C o n s t r a i n t
126  */
128  int number, SubjectToStatus _status
129  )
130 {
131  /* consistency check */
132  if ( ( number < 0 ) || ( number >= _THIS->n ) )
134 
135  /* Add constraint index to respective index list. */
136  switch ( _status )
137  {
138  case ST_INACTIVE:
139  if ( Constraints_addIndex( _THIS,Constraints_getInactive( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
141  break;
142 
143  case ST_LOWER:
144  if ( Constraints_addIndex( _THIS,Constraints_getActive( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
146  break;
147 
148  case ST_UPPER:
149  if ( Constraints_addIndex( _THIS,Constraints_getActive( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
151  break;
152 
153  default:
155  }
156 
157  return SUCCESSFUL_RETURN;
158 }
159 
160 
161 /*
162  * s e t u p A l l I n a c t i v e
163  */
165  )
166 {
167  return Constraints_setupAll( _THIS,ST_INACTIVE );
168 }
169 
170 
171 /*
172  * s e t u p A l l L o w e r
173  */
175  )
176 {
177  return Constraints_setupAll( _THIS,ST_LOWER );
178 }
179 
180 
181 /*
182  * s e t u p A l l U p p e r
183  */
185  )
186 {
187  return Constraints_setupAll( _THIS,ST_UPPER );
188 }
189 
190 
191 /*
192  * m o v e A c t i v e T o I n a c t i v e
193  */
195  int number
196  )
197 {
198  /* consistency check */
199  if ( ( number < 0 ) || ( number >= _THIS->n ) )
201 
202  /* Move index from indexlist of active constraints to that of inactive ones. */
203  if ( Constraints_removeIndex( _THIS,Constraints_getActive( _THIS ),number ) != SUCCESSFUL_RETURN )
205 
208 
209  return SUCCESSFUL_RETURN;
210 }
211 
212 
213 /*
214  * m o v e I n a c t i v e T o A c t i v e
215  */
217  int number, SubjectToStatus _status
218  )
219 {
220  /* consistency check */
221  if ( ( number < 0 ) || ( number >= _THIS->n ) )
223 
224  /* Move index from indexlist of inactive constraints to that of active ones. */
225  if ( Constraints_removeIndex( _THIS,Constraints_getInactive( _THIS ),number ) != SUCCESSFUL_RETURN )
227 
228  if ( Constraints_addIndex( _THIS,Constraints_getActive( _THIS ),number,_status ) != SUCCESSFUL_RETURN )
230 
231  return SUCCESSFUL_RETURN;
232 }
233 
234 
235 /*
236  * f l i p F i x e d
237  */
239 {
240  /* consistency check */
241  if ( ( number < 0 ) || ( number >= _THIS->n ) )
243 
244  if ( _THIS->status != 0 )
245  switch (_THIS->status[number])
246  {
247  case ST_LOWER: _THIS->status[number] = ST_UPPER; break;
248  case ST_UPPER: _THIS->status[number] = ST_LOWER; break;
249  default: return THROWERROR( RET_MOVING_CONSTRAINT_FAILED );
250  }
251 
252  return SUCCESSFUL_RETURN;
253 }
254 
255 
256 /*
257  * s h i f t
258  */
260 {
261  int i;
262  myStatic Indexlist shiftedActive;
263  myStatic Indexlist shiftedInactive;
264 
265  Indexlist_init( &shiftedActive,_THIS->n );
266  Indexlist_init( &shiftedInactive,_THIS->n );
267 
268  /* consistency check */
269  if ( ( offset == 0 ) || ( _THIS->n <= 1 ) )
270  return SUCCESSFUL_RETURN;
271 
272  if ( ( offset < 0 ) || ( offset > _THIS->n/2 ) )
274 
275  if ( ( _THIS->n % offset ) != 0 )
277 
278 
279  /* 1) Shift types and status. */
280  for( i=0; i<_THIS->n-offset; ++i )
281  {
282  Constraints_setType( _THIS,i,Constraints_getType( _THIS,i+offset ) );
283  Constraints_setStatus( _THIS,i,Constraints_getStatus( _THIS,i+offset ) );
284  }
285 
286  /* 2) Construct shifted index lists of free and fixed variables. */
287  for( i=0; i<_THIS->n; ++i )
288  {
289  switch ( Constraints_getStatus( _THIS,i ) )
290  {
291  case ST_INACTIVE:
292  if ( Indexlist_addNumber( &shiftedInactive,i ) != SUCCESSFUL_RETURN )
294  break;
295 
296  case ST_LOWER:
297  if ( Indexlist_addNumber( &shiftedActive,i ) != SUCCESSFUL_RETURN )
299  break;
300 
301  case ST_UPPER:
302  if ( Indexlist_addNumber( &shiftedActive,i ) != SUCCESSFUL_RETURN )
304  break;
305 
306  default:
308  }
309  }
310 
311  /* 3) Assign shifted index list. */
312  IndexlistCPY( &shiftedActive,&(_THIS->active) );
313  IndexlistCPY( &shiftedInactive,&(_THIS->inactive) );
314 
315  return SUCCESSFUL_RETURN;
316 }
317 
318 
319 /*
320  * r o t a t e
321  */
323 {
324  int i;
325  myStatic SubjectToType typeTmp[NCMAX];
326  myStatic SubjectToStatus statusTmp[NCMAX];
327 
328  myStatic Indexlist rotatedActive;
329  myStatic Indexlist rotatedInactive;
330 
331  Indexlist_init( &rotatedActive,_THIS->n );
332  Indexlist_init( &rotatedInactive,_THIS->n );
333 
334  /* consistency check */
335  if ( ( offset == 0 ) || ( offset == _THIS->n ) || ( _THIS->n <= 1 ) )
336  return SUCCESSFUL_RETURN;
337 
338  if ( ( offset < 0 ) || ( offset > _THIS->n ) )
340 
341 
342  /* 1) Rotate types and status. */
343  for( i=0; i<offset; ++i )
344  {
345  typeTmp[i] = Constraints_getType( _THIS,i );
346  statusTmp[i] = Constraints_getStatus( _THIS,i );
347  }
348 
349  for( i=0; i<_THIS->n-offset; ++i )
350  {
351  Constraints_setType( _THIS,i,Constraints_getType( _THIS,i+offset ) );
352  Constraints_setStatus( _THIS,i,Constraints_getStatus( _THIS,i+offset ) );
353  }
354 
355  for( i=_THIS->n-offset; i<_THIS->n; ++i )
356  {
357  Constraints_setType( _THIS,i,typeTmp[i-_THIS->n+offset] );
358  Constraints_setStatus( _THIS,i,statusTmp[i-_THIS->n+offset] );
359  }
360 
361  /* 2) Construct shifted index lists of free and fixed variables. */
362  for( i=0; i<_THIS->n; ++i )
363  {
364  switch ( Constraints_getStatus( _THIS,i ) )
365  {
366  case ST_INACTIVE:
367  if ( Indexlist_addNumber( &rotatedInactive,i ) != SUCCESSFUL_RETURN )
369  break;
370 
371  case ST_LOWER:
372  if ( Indexlist_addNumber( &rotatedActive,i ) != SUCCESSFUL_RETURN )
374  break;
375 
376  case ST_UPPER:
377  if ( Indexlist_addNumber( &rotatedActive,i ) != SUCCESSFUL_RETURN )
379  break;
380 
381  default:
383  }
384  }
385 
386  /* 3) Assign shifted index list. */
387  IndexlistCPY( &rotatedActive,&(_THIS->active) );
388  IndexlistCPY( &rotatedInactive,&(_THIS->inactive) );
389 
390  return SUCCESSFUL_RETURN;
391 }
392 
393 
394 /*
395  * p r i n t
396  */
398 {
399  #ifndef __SUPPRESSANYOUTPUT__
400 
401  myStatic char myPrintfString[QPOASES_MAX_STRING_LENGTH];
402 
403  int nIAC = Constraints_getNIAC( _THIS );
404  int nAC = Constraints_getNAC( _THIS );
405 
406  int *IAC_idx, *AC_idx;
407 
408  if ( _THIS->n == 0 )
409  return SUCCESSFUL_RETURN;
410 
413 
414  snprintf( myPrintfString,QPOASES_MAX_STRING_LENGTH,"Constraints object comprising %d constraints (%d inactive, %d active):\n",_THIS->n,nIAC,nAC );
415  qpOASES_myPrintf( myPrintfString );
416 
417  REFER_NAMESPACE_QPOASES qpOASES_printNI( IAC_idx,nIAC,"inactive" );
418  REFER_NAMESPACE_QPOASES qpOASES_printNI( AC_idx, nAC, "active " );
419 
420  #endif /* __SUPPRESSANYOUTPUT__ */
421 
422  return SUCCESSFUL_RETURN;
423 }
424 
425 
426 
427 /*****************************************************************************
428  * P R O T E C T E D *
429  *****************************************************************************/
430 
431 /*
432  * s e t u p A l l
433  */
435 {
436  int i;
437 
438  /* 1) Place unbounded constraints at the beginning of the index list of inactive constraints. */
439  for( i=0; i<_THIS->n; ++i )
440  {
441  if ( Constraints_getType( _THIS,i ) == ST_UNBOUNDED )
442  {
443  if ( Constraints_setupConstraint( _THIS,i,_status ) != SUCCESSFUL_RETURN )
445  }
446  }
447 
448  /* 2) Add remaining (i.e. "real" inequality) constraints to the index list of inactive constraints. */
449  for( i=0; i<_THIS->n; ++i )
450  {
451  if ( Constraints_getType( _THIS,i ) == ST_BOUNDED )
452  {
453  if ( Constraints_setupConstraint( _THIS,i,_status ) != SUCCESSFUL_RETURN )
455  }
456  }
457 
458  /* 3) Place implicit equality constraints at the end of the index list of inactive constraints. */
459  for( i=0; i<_THIS->n; ++i )
460  {
461  if ( Constraints_getType( _THIS,i ) == ST_EQUALITY )
462  {
463  if ( Constraints_setupConstraint( _THIS,i,_status ) != SUCCESSFUL_RETURN )
465  }
466  }
467 
468  /* 4) Moreover, add all constraints of unknown type. */
469  for( i=0; i<_THIS->n; ++i )
470  {
471  if ( Constraints_getType( _THIS,i ) == ST_UNKNOWN || Constraints_getType( _THIS,i ) == ST_DISABLED )
472  {
473  if ( Constraints_setupConstraint( _THIS,i,_status ) != SUCCESSFUL_RETURN )
475  }
476  }
477 
478 
479  return SUCCESSFUL_RETURN;
480 }
481 
482 
483 /*
484  * a d d I n d e x
485  */
487  Indexlist* const indexlist,
488  int newnumber, SubjectToStatus newstatus
489  )
490 {
491  if ( _THIS->status != 0 )
492  {
493  /* consistency check */
494  if ( _THIS->status[newnumber] == newstatus )
496 
497  _THIS->status[newnumber] = newstatus;
498  }
499  else
501 
502  if ( indexlist != 0 )
503  {
504  if ( Indexlist_addNumber( indexlist,newnumber ) == RET_INDEXLIST_EXCEEDS_MAX_LENGTH )
506  }
507  else
509 
510  return SUCCESSFUL_RETURN;
511 }
512 
513 
514 /*
515  * r e m o v e I n d e x
516  */
518  Indexlist* const indexlist,
519  int removenumber
520  )
521 {
522  if ( _THIS->status != 0 )
523  _THIS->status[removenumber] = ST_UNDEFINED;
524  else
526 
527  if ( indexlist != 0 )
528  {
529  if ( Indexlist_removeNumber( indexlist,removenumber ) != SUCCESSFUL_RETURN )
531  }
532  else
534 
535  return SUCCESSFUL_RETURN;
536 }
537 
538 
539 /*
540  * s w a p I n d e x
541  */
543  int number1, int number2
544  )
545 {
546  /* consistency checks */
547  if ( _THIS->status != 0 )
548  {
549  if ( _THIS->status[number1] != _THIS->status[number2] )
551  }
552  else
554 
555  if ( number1 == number2 )
556  {
558  return SUCCESSFUL_RETURN;
559  }
560 
561  if ( indexlist != 0 )
562  {
563  if ( Indexlist_swapNumbers( indexlist,number1,number2 ) != SUCCESSFUL_RETURN )
565  }
566  else
568 
569  return SUCCESSFUL_RETURN;
570 }
571 
572 
573 
575 
576 
577 /*
578  * end of file
579  */
SubjectToType type[NCMAX]
Definition: Constraints.h:60
returnValue Constraints_setupAllInactive(Constraints *_THIS)
Definition: Constraints.c:164
returnValue Constraints_flipFixed(Constraints *_THIS, int number)
Definition: Constraints.c:238
#define ST_LOWER
returnValue Constraints_moveInactiveToActive(Constraints *_THIS, int number, SubjectToStatus _status)
Definition: Constraints.c:216
BEGIN_NAMESPACE_QPOASES void ConstraintsCON(Constraints *_THIS, int _n)
Definition: Constraints.c:50
returnValue Indexlist_removeNumber(Indexlist *_THIS, int removenumber)
Definition: Indexlist.c:160
returnValue Constraints_rotate(Constraints *_THIS, int offset)
Definition: Constraints.c:322
void ConstraintsCPY(Constraints *FROM, Constraints *TO)
Definition: Constraints.c:61
static SubjectToStatus Constraints_getStatus(Constraints *_THIS, int i)
Definition: Constraints.h:365
static Indexlist * Constraints_getInactive(Constraints *_THIS)
Definition: Constraints.h:508
returnValue Constraints_shift(Constraints *_THIS, int offset)
Definition: Constraints.c:259
Allows to pass back messages to the calling function.
returnValue Constraints_setupAllUpper(Constraints *_THIS)
Definition: Constraints.c:184
returnValue Constraints_moveActiveToInactive(Constraints *_THIS, int number)
Definition: Constraints.c:194
#define ST_UNDEFINED
#define ST_INACTIVE
BooleanType noLower
Definition: Constraints.h:63
static Indexlist * Constraints_getActive(Constraints *_THIS)
Definition: Constraints.h:499
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 SubjectToType Constraints_getType(Constraints *_THIS, int i)
Definition: Constraints.h:353
returnValue Constraints_setupAllLower(Constraints *_THIS)
Definition: Constraints.c:174
returnValue qpOASES_myPrintf(const char *s)
Definition: Utils.c:269
BooleanType noUpper
Definition: Constraints.h:64
returnValue Constraints_removeIndex(Constraints *_THIS, Indexlist *const indexlist, int removenumber)
Definition: Constraints.c:517
SubjectToStatus status[NCMAX]
Definition: Constraints.h:61
#define assert(ignore)
returnValue Constraints_init(Constraints *_THIS, int _n)
Definition: Constraints.c:89
returnValue Indexlist_init(Indexlist *_THIS, int n)
Definition: Indexlist.c:86
returnValue Constraints_print(Constraints *_THIS)
Definition: Constraints.c:397
void IndexlistCPY(Indexlist *FROM, Indexlist *TO)
Definition: Indexlist.c:60
#define BT_TRUE
Definition: acado_types.hpp:47
static returnValue Constraints_setStatus(Constraints *_THIS, int i, SubjectToStatus value)
Definition: Constraints.h:392
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 Constraints_setupAll(Constraints *_THIS, SubjectToStatus _status)
Definition: Constraints.c:434
#define QPOASES_MAX_STRING_LENGTH
returnValue Constraints_addIndex(Constraints *_THIS, Indexlist *const indexlist, int newnumber, SubjectToStatus newstatus)
Definition: Constraints.c:486
static returnValue Constraints_setType(Constraints *_THIS, int i, SubjectToType value)
Definition: Constraints.h:377
returnValue Constraints_swapIndex(Constraints *_THIS, Indexlist *const indexlist, int number1, int number2)
Definition: Constraints.c:542
static int Constraints_getNIAC(Constraints *_THIS)
Definition: Constraints.h:489
#define ST_UPPER
#define myStatic
Definition: Types.h:103
returnValue Constraints_setupConstraint(Constraints *_THIS, int number, SubjectToStatus _status)
Definition: Constraints.c:127
static int Constraints_getNAC(Constraints *_THIS)
Definition: Constraints.h:480


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