-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQProblemSchur.cpp
3626 lines (3062 loc) · 101 KB
/
SQProblemSchur.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of qpOASES.
*
* qpOASES -- An Implementation of the Online Active Set Strategy.
* Copyright (C) 2007-2014 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/SQProblemSchur.cpp
* \author Andreas Waechter and Dennis Janka, based on QProblem.cpp by Hans Joachim Ferreau, Andreas Potschka, Christian Kirches
* \version 3.2
* \date 2012-2015
*
* Implementation of the SQProblemSchur class which is able to use the newly
* developed online active set strategy for parametric quadratic programming.
* This implementation uses a Schur complement approach to solve the linear
* systems.
*/
#include "SQProblemSchur.hpp"
#ifndef __MATLAB__
# include <cstdarg>
void MyPrintf(const char* pformat, ... )
{
va_list ap;
va_start(ap, pformat);
vfprintf(stdout, pformat, ap);
va_end(ap);
}
#else
# include <mex.h>
# define MyPrintf mexPrintf
#endif
BEGIN_NAMESPACE_QPOASES
/*****************************************************************************
* P U B L I C *
*****************************************************************************/
/*
* Q P r o b l e m
*/
SQProblemSchur::SQProblemSchur( ) : SQProblem( )
{
#ifdef SOLVER_MA57
sparseSolver = new Ma57SparseSolver();
#elif defined SOLVER_MA27
sparseSolver = new Ma27SparseSolver();
#elif defined SOLVER_NONE
sparseSolver = new DummySparseSolver();
#endif
nSmax = 0;
nS = -1;
S = 0;
Q_ = 0;
R_ = 0;
detS = 0.0;
rcondS = 0.0;
schurUpdateIndex = 0;
schurUpdate = 0;
numFactorizations = 0;
M_physicallength = 0;
M_vals = 0;
M_ir = 0;
M_jc = 0;
}
/*
* Q P r o b l e m
*/
SQProblemSchur::SQProblemSchur( int_t _nV, int_t _nC, HessianType _hessianType, int_t maxSchurUpdates ) : SQProblem( _nV,_nC,_hessianType )
{
/* We use the variables Q and R to store the QR factorization of S.
* T is not required. */
delete [] R; R = 0;
delete [] Q; Q = 0;
delete [] T; T = 0;
/* The interface to the sparse linear solver. In the long run,
different linear solvers might be optionally chosen. */
#ifdef SOLVER_MA57
sparseSolver = new Ma57SparseSolver();
#elif defined SOLVER_MA27
sparseSolver = new Ma27SparseSolver();
#elif defined SOLVER_NONE
sparseSolver = new DummySparseSolver();
#endif
nSmax = maxSchurUpdates;
nS = -1;
if ( nSmax > 0 )
{
S = new real_t[nSmax*nSmax];
schurUpdateIndex = new int_t[nSmax];
schurUpdate = new SchurUpdateType[nSmax];
Q_ = new real_t[nSmax*nSmax];
R_ = new real_t[nSmax*nSmax];
M_physicallength = 10*nSmax; /* TODO: Decide good default. */
M_vals = new real_t[M_physicallength];
M_ir = new sparse_int_t[M_physicallength];
M_jc = new sparse_int_t[nSmax+1];
detS = 1.0;
rcondS = 1.0;
}
else
{
S = 0;
Q_ = 0;
R_ = 0;
detS = 0.0;
rcondS = 0.0;
schurUpdateIndex = 0;
schurUpdate = 0;
M_physicallength = 0;
M_vals = 0;
M_ir = 0;
M_jc = 0;
}
numFactorizations = 0;
}
/*
* Q P r o b l e m
*/
SQProblemSchur::SQProblemSchur( const SQProblemSchur& rhs ) : SQProblem( rhs )
{
#ifdef SOLVER_MA57
sparseSolver = new Ma57SparseSolver();
#elif defined SOLVER_MA27
sparseSolver = new Ma27SparseSolver();
#elif defined SOLVER_NONE
sparseSolver = new DummySparseSolver();
#endif
copy( rhs );
}
/*
* ~ Q P r o b l e m
*/
SQProblemSchur::~SQProblemSchur( )
{
delete sparseSolver;
clear( );
}
/*
* o p e r a t o r =
*/
SQProblemSchur& SQProblemSchur::operator=( const SQProblemSchur& rhs )
{
if ( this != &rhs )
{
clear( );
SQProblem::operator=( rhs );
copy( rhs );
}
return *this;
}
/*
* r e s e t
*/
returnValue SQProblemSchur::reset( )
{
/* AW: We probably want to avoid resetting factorization in QProblem */
if ( SQProblem::reset( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_RESET_FAILED );
sparseSolver->reset();
nS = -1;
return SUCCESSFUL_RETURN;
}
/*****************************************************************************
* P R O T E C T E D *
*****************************************************************************/
/*
* c l e a r
*/
returnValue SQProblemSchur::clear( )
{
nSmax = 0;
nS = -1;
detS = 0.0;
rcondS = 0.0;
numFactorizations = 0;
delete [] S; S=0;
delete [] Q_; Q_=0;
delete [] R_; R_=0;
delete [] schurUpdateIndex; schurUpdateIndex=0;
delete [] schurUpdate; schurUpdate=0;
M_physicallength = 0;
delete [] M_vals; M_vals=0;
delete [] M_ir; M_ir=0;
delete [] M_jc; M_jc=0;
return SUCCESSFUL_RETURN;
}
/*
* c o p y
*/
returnValue SQProblemSchur::copy( const SQProblemSchur& rhs
)
{
int_t i, j, length;
*sparseSolver = *(rhs.sparseSolver);
nS = rhs.nS;
nSmax = rhs.nSmax;
if ( nSmax > 0 )
{
detS = rhs.detS;
rcondS = rhs.rcondS;
S = new real_t[nSmax*nSmax];
Q_ = new real_t[nSmax*nSmax];
R_ = new real_t[nSmax*nSmax];
schurUpdateIndex = new int_t[nSmax];
schurUpdate = new SchurUpdateType[nSmax];
if ( nS>0 )
{
for ( i=0; i<nS; i++)
for ( j=0; j<nS; j++)
{
S[i*nSmax + j] = rhs.S[i*nSmax + j];
Q_[i*nSmax + j] = rhs.Q_[i*nSmax + j];
R_[i*nSmax + j] = rhs.R_[i*nSmax + j];
}
memcpy( schurUpdateIndex, rhs.schurUpdateIndex, ((unsigned int)nS)*sizeof(int_t));
memcpy( schurUpdate, rhs.schurUpdate, ((unsigned int)nS)*sizeof(SchurUpdateType));
}
M_physicallength = rhs.M_physicallength;
if ( M_physicallength>0 )
{
M_vals = new real_t[M_physicallength];
M_ir = new sparse_int_t[M_physicallength];
M_jc = new sparse_int_t[nSmax+1];
if ( nS>0 )
{
memcpy(M_jc, rhs.M_jc, ((unsigned int)(nS+1))*sizeof(sparse_int_t));
length = M_jc[nS];
memcpy(M_vals, rhs.M_vals, ((unsigned int)length)*sizeof(real_t));
memcpy(M_ir, rhs.M_ir, ((unsigned int)length)*sizeof(sparse_int_t));
}
else if ( nS==0 )
M_jc[0] = rhs.M_jc[0];
}
}
else
{
S = 0;
Q_ = 0;
R_ = 0;
detS = 0.0;
rcondS = 0.0;
schurUpdateIndex = 0;
schurUpdate = 0;
M_physicallength = 0;
M_vals = 0;
M_ir = 0;
M_jc = 0;
}
numFactorizations = rhs.numFactorizations;
boundsFreeStart = rhs.boundsFreeStart;
constraintsActiveStart = rhs.constraintsActiveStart;
return SUCCESSFUL_RETURN;
}
/*
* s e t u p A u x i l i a r y Q P
*/
returnValue SQProblemSchur::setupAuxiliaryQP( SymmetricMatrix *H_new,
Matrix *A_new,
const real_t *lb_new,
const real_t *ub_new,
const real_t *lbA_new,
const real_t *ubA_new
)
{
int_t i;
int_t nV = getNV( );
int_t nC = getNC( );
returnValue returnvalue;
if ( ( getStatus( ) == QPS_NOTINITIALISED ) ||
( getStatus( ) == QPS_PREPARINGAUXILIARYQP ) ||
( getStatus( ) == QPS_PERFORMINGHOMOTOPY ) )
{
return THROWERROR( RET_UPDATEMATRICES_FAILED_AS_QP_NOT_SOLVED );
}
status = QPS_PREPARINGAUXILIARYQP;
/* I) SETUP NEW QP MATRICES AND VECTORS: */
/* 1) Shift constraints' bounds vectors by (A_new - A)'*x_opt to ensure
* that old optimal solution remains feasible for new QP data. */
/* Firstly, shift by -A'*x_opt and ... */
if ( nC > 0 )
{
if ( A_new == 0 )
return THROWERROR( RET_INVALID_ARGUMENTS );
for ( i=0; i<nC; ++i )
{
lbA[i] = -Ax_l[i];
ubA[i] = Ax_u[i];
}
/* Set constraint matrix as well as ... */
setA( A_new );
/* ... secondly, shift by +A_new'*x_opt. */
for ( i=0; i<nC; ++i )
{
lbA[i] += Ax[i];
ubA[i] += Ax[i];
}
/* update constraint products. */
for ( i=0; i<nC; ++i )
{
Ax_u[i] = ubA[i] - Ax[i];
Ax_l[i] = Ax[i] - lbA[i];
}
}
/* 2) Set new Hessian matrix,determine Hessian type and
* regularise new Hessian matrix if necessary. */
/* a) Setup new Hessian matrix and determine its type. */
if ( H_new != 0 )
{
setH( H_new );
hessianType = HST_UNKNOWN;
if ( determineHessianType( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
/* b) Regularise new Hessian if necessary. */
if ( ( hessianType == HST_ZERO ) ||
( hessianType == HST_SEMIDEF ) ||
( usingRegularisation( ) == BT_TRUE ) )
{
regVal = 0.0; /* reset previous regularisation */
if ( regulariseHessian( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
}
}
else
{
if ( H != 0 )
return THROWERROR( RET_NO_HESSIAN_SPECIFIED );
}
/* 3) Setup QP gradient. */
if ( setupAuxiliaryQPgradient( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
/* II) SETUP WORKING SET AND MATRIX FACTORISATION: */
/* 1) Check if current active set is linearly independent and has the correct inertia */
returnvalue = resetSchurComplement( BT_FALSE );
int_t neig = sparseSolver->getNegativeEigenvalues( );
if ( returnvalue == SUCCESSFUL_RETURN && neig == getNAC( ) )
{
/* a) This means the proposed working set is linearly independent and
* leaves no zero curvature exposed in the nullspace and can be used to start QP solve. */
if ( options.printLevel == PL_HIGH )
MyPrintf( "In hotstart for new matrices, old working set is linearly independent and has correct inertia.\n");
status = QPS_AUXILIARYQPSOLVED;
return SUCCESSFUL_RETURN;
}
else if ( returnvalue == SUCCESSFUL_RETURN && neig > getNAC( ) )
{
/* b) KKT matrix has too many negative eigenvalues. Try to correct the inertia by adding bounds (reduce nullspace dimension). */
if ( options.printLevel == PL_HIGH )
MyPrintf( "WARNING: In hotstart for new matrices, reduced Hessian for initial working set has %i negative eigenvalues, should be %i.\n", neig, getNAC( ) );
/* If enabling inertia correction is disabled, exit here */
if ( options.enableInertiaCorrection )
{
returnvalue = correctInertia();
if ( returnvalue == SUCCESSFUL_RETURN )
{
status = QPS_AUXILIARYQPSOLVED;
return SUCCESSFUL_RETURN;
}
}
else
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
}
/* 2) If inertia correction has failed or factorization yielded some other error,
* try to rebuild the active set with all simple bounds set according to initialStatusBounds
* (Note: in exact arithmetic, this cannot happen) */
if ( options.printLevel == PL_HIGH )
MyPrintf( "WARNING: hotstart for old active set failed. Trying to rebuild a working set.\n");
Bounds oldBounds = bounds;
Constraints oldConstraints = constraints;
/* Move all inactive variables to a bound */
for ( i=0; i<nV; i++ )
{
#ifdef __ALWAYS_INITIALISE_WITH_ALL_EQUALITIES__
if ( bounds.getType( i ) == ST_EQUALITY )
{
if ( oldBounds.setStatus( i,ST_LOWER ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
}
else
#endif
{
if ( oldBounds.getStatus( i ) == ST_INACTIVE )
if ( oldBounds.setStatus( i, options.initialStatusBounds ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
}
}
/* Set all equalities active */
#ifdef __ALWAYS_INITIALISE_WITH_ALL_EQUALITIES__
for( i=0; i<nC; ++i )
{
if ( constraints.getType( i ) == ST_EQUALITY )
if ( oldConstraints.setStatus( i,ST_LOWER ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
}
#endif
/* Set all inequalities inactive */
for( i=0; i<nC; ++i )
{
if ( constraints.getType( i ) != ST_EQUALITY )
if ( oldConstraints.setStatus( i,ST_INACTIVE ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
}
/* Reset bounds and constraints */
bounds.init( nV );
constraints.init( nC );
if ( setupSubjectToType(lb_new,ub_new,lbA_new,ubA_new ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
if ( bounds.setupAllFree( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
if ( constraints.setupAllInactive( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
/* Setup working sets afresh. */
if ( setupAuxiliaryWorkingSet( &oldBounds,&oldConstraints,BT_TRUE ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
/* adjust lb/ub */
for (int_t ii = 0; ii < nC; ++ii)
Ax_l[ii] = Ax_u[ii] = Ax[ii];
setupAuxiliaryQPbounds (&bounds, &constraints, BT_FALSE);
status = QPS_AUXILIARYQPSOLVED;
return SUCCESSFUL_RETURN;
}
/*
* s e t u p A u x i l i a r y W o r k i n g S e t
*/
returnValue SQProblemSchur::setupAuxiliaryWorkingSet( const Bounds* const auxiliaryBounds,
const Constraints* const auxiliaryConstraints,
BooleanType setupAfresh
)
{
int_t i;
int_t nV = getNV( );
int_t nC = getNC( );
/* consistency checks */
if ( auxiliaryBounds != 0 )
{
for( i=0; i<nV; ++i )
if ( ( bounds.getStatus( i ) == ST_UNDEFINED ) || ( auxiliaryBounds->getStatus( i ) == ST_UNDEFINED ) )
return THROWERROR( RET_UNKNOWN_BUG );
}
else
{
return THROWERROR( RET_INVALID_ARGUMENTS );
}
if ( auxiliaryConstraints != 0 )
{
for( i=0; i<nC; ++i )
if ( ( constraints.getStatus( i ) == ST_UNDEFINED ) || ( auxiliaryConstraints->getStatus( i ) == ST_UNDEFINED ) )
return THROWERROR( RET_UNKNOWN_BUG );
}
else
{
return THROWERROR( RET_INVALID_ARGUMENTS );
}
/* I.) REMOVE INEQUALITY BOUNDS/CONSTRAINTS */
/* I.1) Remove inequality bounds that are active now but shall be
* inactive or active at the other bound according to auxiliaryBounds */
for( i=0; i<nV; ++i )
{
if ( ( bounds.getStatus( i ) == ST_LOWER ) && ( auxiliaryBounds->getStatus( i ) != ST_LOWER ) )
if ( bounds.moveFixedToFree( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
if ( ( bounds.getStatus( i ) == ST_UPPER ) && ( auxiliaryBounds->getStatus( i ) != ST_UPPER ) )
if ( bounds.moveFixedToFree( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
}
/* I.2.) Remove inequality constraints that are active now but shall be
* inactive or active at the other bound according to auxiliaryConstraints */
for( i=0; i<nC; ++i )
{
if ( ( constraints.getStatus( i ) == ST_LOWER ) && ( auxiliaryConstraints->getStatus( i ) != ST_LOWER ) )
if ( constraints.moveActiveToInactive( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
if ( ( constraints.getStatus( i ) == ST_UPPER ) && ( auxiliaryConstraints->getStatus( i ) != ST_UPPER ) )
if ( constraints.moveActiveToInactive( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
}
/* II.) ADD BOUNDS/CONSTRAINTS */
/* II.1.) Add bounds according to auxiliaryBounds */
for( i=0; i<nV; ++i )
{
if ( ( bounds.getStatus( i ) == ST_INACTIVE ) && ( auxiliaryBounds->getStatus( i ) != ST_INACTIVE ) )
if ( bounds.moveFreeToFixed( i, auxiliaryBounds->getStatus( i ) ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
}
/* II.2.) Add constraints according to auxiliaryConstraints */
for( i=0; i<nC; ++i )
{
if ( ( constraints.getStatus( i ) == ST_INACTIVE ) && ( auxiliaryConstraints->getStatus( i ) != ST_INACTIVE ) )
if ( constraints.moveInactiveToActive( i,auxiliaryConstraints->getStatus( i ) ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
}
/* III) FACTORIZATION */
/* III.1.) Factorize (resolves linear dependency) */
if( resetSchurComplement( BT_FALSE ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
/* III.2.) Check if inertia is correct. If so, we now have a linearly independent working set with a pos def reduced Hessian */
int_t neig = sparseSolver->getNegativeEigenvalues( );
if ( neig == getNAC( ) )
{
/* We now have a linearly independent working set with a pos def reduced Hessian.
* We need to correct the QP bounds and gradient after this. */
return SUCCESSFUL_RETURN;
}
/* IV.) INERTIA CORRECTION IF NECESSARY */
/* We now have a fresh factorization and can start the usual inertia correction routine */
if ( options.printLevel == PL_HIGH )
MyPrintf( "WARNING: In setupAuxiliaryWorkingSet: Initial working set reduced Hessian has %i negative eigenvalues, should be %i.\n", neig, getNAC( ) );
if ( options.enableInertiaCorrection == BT_TRUE )
return correctInertia( );
else
return THROWERROR( RET_SETUP_WORKINGSET_FAILED );
}
/*
* c h o l e s k y D e c o m p o s i t i o n P r o j e c t e d
*/
returnValue SQProblemSchur::computeProjectedCholesky( )
{
return SUCCESSFUL_RETURN;
}
/*
* c o m p u t e I n i t i a l C h o l e s k y
*/
returnValue SQProblemSchur::computeInitialCholesky( )
{
return SUCCESSFUL_RETURN;
}
/*
* s e t u p T Q f a c t o r i s a t i o n
*/
returnValue SQProblemSchur::setupTQfactorisation( )
{
return SUCCESSFUL_RETURN;
}
/*
* a d d C o n s t r a i n t
*/
returnValue SQProblemSchur::addConstraint( int_t number,
SubjectToStatus C_status,
BooleanType updateCholesky,
BooleanType ensureLI
)
{
int_t idxDeleted = -1;
/* consistency checks */
if ( constraints.getStatus( number ) != ST_INACTIVE )
return THROWERROR( RET_CONSTRAINT_ALREADY_ACTIVE );
if ( ( constraints.getNC( ) - getNAC( ) ) == constraints.getNUC( ) )
return THROWERROR( RET_ALL_CONSTRAINTS_ACTIVE );
if ( ( getStatus( ) == QPS_NOTINITIALISED ) ||
( getStatus( ) == QPS_AUXILIARYQPSOLVED ) ||
( getStatus( ) == QPS_HOMOTOPYQPSOLVED ) ||
( getStatus( ) == QPS_SOLVED ) )
{
return THROWERROR( RET_UNKNOWN_BUG );
}
/* I) ENSURE LINEAR INDEPENDENCE OF THE WORKING SET,
* i.e. remove a constraint or bound if linear dependence occurs. */
if ( ensureLI == BT_TRUE )
{
returnValue ensureLIreturnvalue = addConstraint_ensureLI( number,C_status );
switch ( ensureLIreturnvalue )
{
case SUCCESSFUL_RETURN:
break;
case RET_LI_RESOLVED:
break;
case RET_ENSURELI_FAILED_NOINDEX:
return RET_ADDCONSTRAINT_FAILED_INFEASIBILITY;
case RET_ENSURELI_FAILED_CYCLING:
return RET_ADDCONSTRAINT_FAILED_INFEASIBILITY;
case RET_ENSURELI_DROPPED:
return SUCCESSFUL_RETURN;
default:
return THROWERROR( RET_ENSURELI_FAILED );
}
}
/* IV) UPDATE INDICES */
tabularOutput.idxAddC = number;
if ( constraints.moveInactiveToActive( number,C_status ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_ADDCONSTRAINT_FAILED );
/* Also update the Schur complement. */
/* First check if this constraint had been removed before. In that
case delete this constraint from the Schur complement. */
bool found = false;
for ( int_t i=0; i<nS; i++ )
{
if ( schurUpdate[i] == SUT_ConRemoved && number == schurUpdateIndex[i] )
{
if ( deleteFromSchurComplement( i ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_ADDCONSTRAINT_FAILED );
found = true;
idxDeleted = i;
break;
}
}
if ( !found )
{
if ( nS < 0 || nS==nSmax )
{
/* The schur complement has become too large, reset. */
/* Correct inertia if necessary. */
returnValue retval = resetSchurComplement( BT_TRUE );
if ( retval != SUCCESSFUL_RETURN )
{
if ( retval == RET_KKT_MATRIX_SINGULAR && options.printLevel == PL_HIGH )
MyPrintf( "In addConstraint: KKT matrix singular when resetting Schur complement\n" );
else if ( options.printLevel == PL_HIGH )
MyPrintf( "In addConstraint, resetSchurComplement failed with retval = %d\n", retval);
return THROWERROR( RET_ADDCONSTRAINT_FAILED );
}
found = true;
}
else
{
/* If the constraint was not yet in Schur complement, add it now. */
int_t nFRStart = boundsFreeStart.getLength();
int_t* FR_idxStart;
boundsFreeStart.getNumberArray( &FR_idxStart );
sparse_int_t* MNpos = new sparse_int_t[nFRStart+nS]; // This is an overestimate
real_t* MNvals = new real_t[nFRStart+nS];
int_t* irn = new int_t[nFRStart+nS];
int_t* jcn = new int_t[nFRStart+nS];
real_t* vals = new real_t[nFRStart+nS];
int_t* icolsNumber = new int_t[nFRStart+nS];
int_t* icolsSIdx = new int_t[nS];
for ( int_t i=0; i<nFRStart; i++)
icolsNumber[i] = FR_idxStart[i];
int_t icolsLength = nFRStart;
for ( int_t i=0; i<nS; i++)
if ( schurUpdate[i] == SUT_VarFreed )
{
icolsNumber[icolsLength] = schurUpdateIndex[i];
icolsSIdx[icolsLength-nFRStart] = i;
icolsLength++;
}
if ( constraintProduct != 0 )
{
MyPrintf( "In SQProblemSchur::addConstraint, constraintProduct not yet implemented.\n");
return THROWERROR(RET_NOT_YET_IMPLEMENTED);
}
int_t numNonzerosA;
A->getSparseSubmatrix( 1, &number, icolsLength, icolsNumber, 0, 0, numNonzerosA, irn, jcn, vals );
delete [] irn;
int_t numNonzerosM = 0;
int_t numNonzerosN = 0;
for ( int_t i=0; i<numNonzerosA; i++ )
if ( jcn[i] < nFRStart )
{
MNpos[numNonzerosM] = jcn[i];
MNvals[numNonzerosM] = vals[i];
numNonzerosM++;
}
else
{
MNpos[nFRStart+numNonzerosN] = icolsSIdx[jcn[i]-nFRStart];
MNvals[nFRStart+numNonzerosN] = vals[i];
numNonzerosN++;
}
returnValue returnvalue = addToSchurComplement( number, SUT_ConAdded, numNonzerosM, MNpos, MNvals, numNonzerosN, MNpos+nFRStart, MNvals+nFRStart, 0.0 );
delete [] icolsSIdx;
delete [] icolsNumber;
delete [] vals;
delete [] jcn;
delete [] MNvals;
delete [] MNpos;
if ( returnvalue != SUCCESSFUL_RETURN )
return THROWERROR( RET_ADDCONSTRAINT_FAILED );
found = true;
}
}
if ( !found )
return THROWERROR( RET_ADDCONSTRAINT_FAILED );
updateSchurQR( idxDeleted );
/* If reciprocal of condition number becomes to small, refactorize KKT matrix */
if( rcondS < options.rcondSMin )
{
returnValue retval = resetSchurComplement( BT_TRUE );
if ( retval != SUCCESSFUL_RETURN )
{
if ( retval == RET_KKT_MATRIX_SINGULAR && options.printLevel == PL_HIGH )
MyPrintf( "In addConstraint: KKT matrix singular when resetting Schur complement\n" );
else if ( options.printLevel == PL_HIGH )
MyPrintf( "In addConstraint, resetSchurComplement failed with retval = %d\n", retval);
return THROWERROR( RET_ADDCONSTRAINT_FAILED );
}
}
return SUCCESSFUL_RETURN;
}
/*
* a d d C o n s t r a i n t _ c h e c k L I
*/
returnValue SQProblemSchur::addConstraint_checkLI( int_t number )
{
/* Get space for the multipliers xi in linear independence test */
int_t nAC = getNAC();
int_t nFX = getNFX();
real_t *xiC = new real_t[nAC];
real_t *xiB = new real_t[nFX];
/* I) Check if new constraint is linearly independent from the active ones. */
returnValue returnvalueCheckLI = addConstraint_checkLISchur( number, xiC, xiB );
delete [] xiB;
delete [] xiC;
return returnvalueCheckLI;
}
/*
* a d d C o n s t r a i n t _ c h e c k L I S c h u r
*/
returnValue SQProblemSchur::addConstraint_checkLISchur( int_t number, real_t* xiC, real_t* xiB )
{
returnValue returnvalue = RET_LINEARLY_DEPENDENT;
int_t ii;
int_t nV = getNV( );
int_t nFR = getNFR( );
int_t nC = getNC( );
int_t nAC = getNAC();
int_t nFX = getNFX();
int_t *FR_idx;
bounds.getFree( )->getNumberArray( &FR_idx );
/* For the Schur complement version we only use options.enableFullLITests = TRUE */
{
/*
* expensive LI test. Backsolve with refinement using special right
* hand side. This gives an estimate for what should be considered
* "zero". We then check linear independence relative to this estimate.
*/
int_t *FX_idx, *AC_idx, *IAC_idx;
real_t *delta_g = new real_t[nV];
real_t *delta_xFX = new real_t[nFX];
real_t *delta_xFR = new real_t[nFR];
real_t *delta_yAC = xiC;
real_t *delta_yFX = xiB;
bounds.getFixed( )->getNumberArray( &FX_idx );
constraints.getActive( )->getNumberArray( &AC_idx );
constraints.getInactive( )->getNumberArray( &IAC_idx );
int_t dim = (nC>nV)?nC:nV;
real_t *nul = new real_t[dim];
for (ii = 0; ii < dim; ++ii)
nul[ii]=0.0;
A->getRow (number, 0, 1.0, delta_g);
returnValue dsdreturnvalue = determineStepDirection ( delta_g,
nul, nul, nul, nul,
BT_FALSE, BT_FALSE,
delta_xFX, delta_xFR, delta_yAC, delta_yFX);
if (dsdreturnvalue!=SUCCESSFUL_RETURN)
returnvalue = dsdreturnvalue;
delete[] nul;
/* compute the weight in inf-norm */
real_t weight = 0.0;
for (ii = 0; ii < nAC; ++ii)
{
real_t a = getAbs (delta_yAC[ii]);
if (weight < a) weight = a;
}
for (ii = 0; ii < nFX; ++ii)
{
real_t a = getAbs (delta_yFX[ii]);
if (weight < a) weight = a;
}
/* look at the "zero" in a relative inf-norm */
real_t zero = 0.0;
for (ii = 0; ii < nFX; ++ii)
{
real_t a = getAbs (delta_xFX[ii]);
if (zero < a) zero = a;
}
for (ii = 0; ii < nFR; ++ii)
{
real_t a = getAbs (delta_xFR[ii]);
if (zero < a) zero = a;
}
/* relative test against zero in inf-norm */
if (zero > options.epsLITests * weight)
returnvalue = RET_LINEARLY_INDEPENDENT;
delete[] delta_xFR;
delete[] delta_xFX;
delete[] delta_g;
}
return THROWINFO( returnvalue );
}
/*
* a d d C o n s t r a i n t _ e n s u r e L I
*/
returnValue SQProblemSchur::addConstraint_ensureLI( int_t number, SubjectToStatus C_status )
{
/* Get space for the multipliers xi in linear independence test */
int_t nAC = getNAC();
int_t nFX = getNFX();
real_t *xiC = new real_t[nAC];
real_t *xiB = new real_t[nFX];
/* I) Check if new constraint is linearly independent from the active ones. */
returnValue returnvalueCheckLI = addConstraint_checkLISchur( number, xiC, xiB );
if ( returnvalueCheckLI == RET_INDEXLIST_CORRUPTED )
{
delete [] xiB;
delete [] xiC;
return THROWERROR( RET_ENSURELI_FAILED );
}
if ( returnvalueCheckLI == RET_LINEARLY_INDEPENDENT )
{
delete [] xiB;
delete [] xiC;
return SUCCESSFUL_RETURN;
}
/* II) NEW BOUND IS LINEARLY DEPENDENT: */
/* 1) Coefficients of linear combination, have already been computed, but we need to correct the sign. */
int_t i, ii;
if ( C_status != ST_LOWER )
{
for( i=0; i<nAC; ++i )
xiC[i] = -xiC[i];
for( i=0; i<nFX; ++i )
xiB[i] = -xiB[i];
}
int_t nV = getNV( );
int_t* FX_idx;
bounds.getFixed( )->getNumberArray( &FX_idx );
int_t* AC_idx;
constraints.getActive( )->getNumberArray( &AC_idx );
real_t* num = new real_t[nV];
real_t y_min = options.maxDualJump;
int_t y_min_number = -1;