-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constraints.cpp
499 lines (385 loc) · 10.5 KB
/
Constraints.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* This file is part of qpOASES.
*
* qpOASES -- An Implementation of the Online Active Set Strategy.
* Copyright (C) 2007-2015 by Hans Joachim Ferreau, Andreas Potschka,
* Christian Kirches et al. All rights reserved.
*
* qpOASES is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* qpOASES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with qpOASES; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/**
* \file src/Constraints.cpp
* \author Hans Joachim Ferreau, Andreas Potschka, Christian Kirches
* \version 3.2
* \date 2007-2015
*
* Implementation of the Constraints class designed to manage working sets of
* constraints within a QProblem.
*/
#include "Constraints.hpp"
BEGIN_NAMESPACE_QPOASES
/*****************************************************************************
* P U B L I C *
*****************************************************************************/
/*
* C o n s t r a i n t s
*/
Constraints::Constraints( ) : SubjectTo( )
{
}
/*
* C o n s t r a i n t s
*/
Constraints::Constraints( int_t _n ) : SubjectTo( _n )
{
init( _n );
}
/*
* C o n s t r a i n t s
*/
Constraints::Constraints( const Constraints& rhs ) : SubjectTo( rhs )
{
copy( rhs );
}
/*
* ~ C o n s t r a i n t s
*/
Constraints::~Constraints( )
{
clear( );
}
/*
* o p e r a t o r =
*/
Constraints& Constraints::operator=( const Constraints& rhs )
{
if ( this != &rhs )
{
clear( );
SubjectTo::operator=( rhs );
copy( rhs );
}
return *this;
}
/*
* i n i t
*/
returnValue Constraints::init( int_t _n
)
{
if ( _n < 0 )
return THROWERROR( RET_INVALID_ARGUMENTS );
clear( );
if ( _n >= 0 )
{
active.init( _n );
inactive.init( _n );
}
return SubjectTo::init( _n );
}
/*
* s e t u p C o n s t r a i n t
*/
returnValue Constraints::setupConstraint( int_t number, SubjectToStatus _status
)
{
/* consistency check */
if ( ( number < 0 ) || ( number >= n ) )
return THROWERROR( RET_INDEX_OUT_OF_BOUNDS );
/* Add constraint index to respective index list. */
switch ( _status )
{
case ST_INACTIVE:
if ( this->addIndex( this->getInactive( ),number,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
break;
case ST_LOWER:
if ( this->addIndex( this->getActive( ),number,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
break;
case ST_UPPER:
if ( this->addIndex( this->getActive( ),number,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
break;
default:
return THROWERROR( RET_INVALID_ARGUMENTS );
}
return SUCCESSFUL_RETURN;
}
/*
* s e t u p A l l I n a c t i v e
*/
returnValue Constraints::setupAllInactive( )
{
return setupAll( ST_INACTIVE );
}
/*
* s e t u p A l l L o w e r
*/
returnValue Constraints::setupAllLower( )
{
return setupAll( ST_LOWER );
}
/*
* s e t u p A l l U p p e r
*/
returnValue Constraints::setupAllUpper( )
{
return setupAll( ST_UPPER );
}
/*
* m o v e A c t i v e T o I n a c t i v e
*/
returnValue Constraints::moveActiveToInactive( int_t number )
{
/* consistency check */
if ( ( number < 0 ) || ( number >= n ) )
return THROWERROR( RET_INDEX_OUT_OF_BOUNDS );
/* Move index from indexlist of active constraints to that of inactive ones. */
if ( this->removeIndex( this->getActive( ),number ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_MOVING_BOUND_FAILED );
if ( this->addIndex( this->getInactive( ),number,ST_INACTIVE ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_MOVING_BOUND_FAILED );
return SUCCESSFUL_RETURN;
}
/*
* m o v e I n a c t i v e T o A c t i v e
*/
returnValue Constraints::moveInactiveToActive( int_t number, SubjectToStatus _status
)
{
/* consistency check */
if ( ( number < 0 ) || ( number >= n ) )
return THROWERROR( RET_INDEX_OUT_OF_BOUNDS );
/* Move index from indexlist of inactive constraints to that of active ones. */
if ( this->removeIndex( this->getInactive( ),number ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_MOVING_BOUND_FAILED );
if ( this->addIndex( this->getActive( ),number,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_MOVING_BOUND_FAILED );
return SUCCESSFUL_RETURN;
}
/*
* f l i p F i x e d
*/
returnValue Constraints::flipFixed( int_t number )
{
/* consistency check */
if ( ( number < 0 ) || ( number >= n ) )
return THROWERROR( RET_INDEX_OUT_OF_BOUNDS );
if ( status != 0 )
switch (status[number])
{
case ST_LOWER: status[number] = ST_UPPER; break;
case ST_UPPER: status[number] = ST_LOWER; break;
default: return THROWERROR( RET_MOVING_CONSTRAINT_FAILED );
}
return SUCCESSFUL_RETURN;
}
/*
* s h i f t
*/
returnValue Constraints::shift( int_t offset )
{
int_t i;
/* consistency check */
if ( ( offset == 0 ) || ( n <= 1 ) )
return SUCCESSFUL_RETURN;
if ( ( offset < 0 ) || ( offset > n/2 ) )
return THROWERROR( RET_INDEX_OUT_OF_BOUNDS );
if ( ( n % offset ) != 0 )
return THROWERROR( RET_INVALID_ARGUMENTS );
/* 1) Shift types and status. */
for( i=0; i<n-offset; ++i )
{
setType( i,getType( i+offset ) );
setStatus( i,getStatus( i+offset ) );
}
/* 2) Construct shifted index lists of free and fixed variables. */
Indexlist shiftedActive( n );
Indexlist shiftedInactive( n );
for( i=0; i<n; ++i )
{
switch ( getStatus( i ) )
{
case ST_INACTIVE:
if ( shiftedInactive.addNumber( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SHIFTING_FAILED );
break;
case ST_LOWER:
if ( shiftedActive.addNumber( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SHIFTING_FAILED );
break;
case ST_UPPER:
if ( shiftedActive.addNumber( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SHIFTING_FAILED );
break;
default:
return THROWERROR( RET_SHIFTING_FAILED );
}
}
/* 3) Assign shifted index list. */
active = shiftedActive;
inactive = shiftedInactive;
return SUCCESSFUL_RETURN;
}
/*
* r o t a t e
*/
returnValue Constraints::rotate( int_t offset )
{
int_t i;
/* consistency check */
if ( ( offset == 0 ) || ( offset == n ) || ( n <= 1 ) )
return SUCCESSFUL_RETURN;
if ( ( offset < 0 ) || ( offset > n ) )
return THROWERROR( RET_INDEX_OUT_OF_BOUNDS );
/* 1) Rotate types and status. */
SubjectToType* typeTmp = new SubjectToType[offset];
SubjectToStatus* statusTmp = new SubjectToStatus[offset];
for( i=0; i<offset; ++i )
{
typeTmp[i] = getType( i );
statusTmp[i] = getStatus( i );
}
for( i=0; i<n-offset; ++i )
{
setType( i,getType( i+offset ) );
setStatus( i,getStatus( i+offset ) );
}
for( i=n-offset; i<n; ++i )
{
setType( i,typeTmp[i-n+offset] );
setStatus( i,statusTmp[i-n+offset] );
}
delete[] statusTmp; delete[] typeTmp;
/* 2) Construct shifted index lists of free and fixed variables. */
Indexlist rotatedActive( n );
Indexlist rotatedInactive( n );
for( i=0; i<n; ++i )
{
switch ( getStatus( i ) )
{
case ST_INACTIVE:
if ( rotatedInactive.addNumber( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_ROTATING_FAILED );
break;
case ST_LOWER:
if ( rotatedActive.addNumber( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_ROTATING_FAILED );
break;
case ST_UPPER:
if ( rotatedActive.addNumber( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_ROTATING_FAILED );
break;
default:
return THROWERROR( RET_ROTATING_FAILED );
}
}
/* 3) Assign shifted index list. */
active = rotatedActive;
inactive = rotatedInactive;
return SUCCESSFUL_RETURN;
}
/*
* p r i n t
*/
returnValue Constraints::print( )
{
if ( n == 0 )
return SUCCESSFUL_RETURN;
#ifndef __SUPPRESSANYOUTPUT__
char myPrintfString[MAX_STRING_LENGTH];
int_t nIAC = getNIAC( );
int_t nAC = getNAC( );
int_t* IAC_idx;
getInactive( )->getNumberArray( &IAC_idx );
int_t* AC_idx;
getActive( )->getNumberArray( &AC_idx );
snprintf( myPrintfString,MAX_STRING_LENGTH,"Constraints object comprising %d constraints (%d inactive, %d active):\n",(int)n,(int)nIAC,(int)nAC );
myPrintf( myPrintfString );
REFER_NAMESPACE_QPOASES print( IAC_idx,nIAC,"inactive" );
REFER_NAMESPACE_QPOASES print( AC_idx, nAC, "active " );
#endif /* __SUPPRESSANYOUTPUT__ */
return SUCCESSFUL_RETURN;
}
/*****************************************************************************
* P R O T E C T E D *
*****************************************************************************/
/*
* c l e a r
*/
returnValue Constraints::clear( )
{
return SUCCESSFUL_RETURN;
}
/*
* c o p y
*/
returnValue Constraints::copy( const Constraints& rhs
)
{
active = rhs.active;
inactive = rhs.inactive;
return SUCCESSFUL_RETURN;
}
/*
* s e t u p A l l
*/
returnValue Constraints::setupAll( SubjectToStatus _status )
{
int_t i;
/* 1) Place unbounded constraints at the beginning of the index list of inactive constraints. */
for( i=0; i<n; ++i )
{
if ( getType( i ) == ST_UNBOUNDED )
{
if ( setupConstraint( i,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
}
}
/* 2) Add remaining (i.e. "real" inequality) constraints to the index list of inactive constraints. */
for( i=0; i<n; ++i )
{
if ( getType( i ) == ST_BOUNDED )
{
if ( setupConstraint( i,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
}
}
/* 3) Place implicit equality constraints at the end of the index list of inactive constraints. */
for( i=0; i<n; ++i )
{
if ( getType( i ) == ST_EQUALITY )
{
if ( setupConstraint( i,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
}
}
/* 4) Moreover, add all constraints of unknown type. */
for( i=0; i<n; ++i )
{
if ( getType( i ) == ST_UNKNOWN || getType( i ) == ST_DISABLED )
{
if ( setupConstraint( i,_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_CONSTRAINT_FAILED );
}
}
return SUCCESSFUL_RETURN;
}
END_NAMESPACE_QPOASES
/*
* end of file
*/