-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathQProblem.cpp
6270 lines (5155 loc) · 157 KB
/
QProblem.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-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/QProblem.cpp
* \author Hans Joachim Ferreau, Andreas Potschka, Christian Kirches
* \version 3.1
* \date 2007-2015
*
* Implementation of the QProblem class which is able to use the newly
* developed online active set strategy for parametric quadratic programming.
*/
#include <qpOASES/QProblem.hpp>
BEGIN_NAMESPACE_QPOASES
/*****************************************************************************
* P U B L I C *
*****************************************************************************/
/*
* Q P r o b l e m
*/
QProblem::QProblem( ) : QProblemB( )
{
freeConstraintMatrix = BT_FALSE;
A = 0;
lbA = 0;
ubA = 0;
sizeT = 0;
T = 0;
Q = 0;
Ax = 0;
Ax_l = 0;
Ax_u = 0;
constraintProduct = 0;
tempA = 0;
ZFR_delta_xFRz = 0;
delta_xFRy = 0;
delta_xFRz = 0;
tempB = 0;
delta_yAC_TMP = 0;
}
/*
* Q P r o b l e m
*/
QProblem::QProblem( int _nV, int _nC, HessianType _hessianType ) : QProblemB( _nV,_hessianType )
{
int i;
/* consistency checks */
if ( _nV <= 0 )
{
_nV = 1;
THROWERROR( RET_INVALID_ARGUMENTS );
}
if ( _nC < 0 )
{
_nC = 0;
THROWERROR( RET_INVALID_ARGUMENTS );
}
if ( _nC > 0 )
{
freeConstraintMatrix = BT_FALSE;
A = 0;
lbA = new real_t[_nC];
for( i=0; i<_nC; ++i ) lbA[i] = 0.0;
ubA = new real_t[_nC];
for( i=0; i<_nC; ++i ) ubA[i] = 0.0;
}
else
{
/* prevent segmentation faults in case nC == 0
* (avoiding checks for A!=0 around all calls to A->... */
freeConstraintMatrix = BT_TRUE;
A = new DenseMatrix( );
lbA = 0;
ubA = 0;
}
constraints.init( _nC );
delete[] y; /* y of no constraints version too short! */
y = new real_t[_nV+_nC];
for( i=0; i<_nV+_nC; ++i ) y[i] = 0.0;
sizeT = getMin( _nV,_nC );
T = new real_t[sizeT*sizeT];
Q = new real_t[_nV*_nV];
if ( _nC > 0 )
{
Ax = new real_t[_nC];
Ax_l = new real_t[_nC];
Ax_u = new real_t[_nC];
}
else
{
Ax = 0;
Ax_l = 0;
Ax_u = 0;
}
constraintProduct = 0;
tempA = new real_t[_nV]; /* nFR */
ZFR_delta_xFRz = new real_t[_nV]; /* nFR */
delta_xFRz = new real_t[_nV]; /* nZ */
if ( _nC > 0 )
{
tempB = new real_t[_nC]; /* nAC */
delta_xFRy = new real_t[_nC]; /* nAC */
delta_yAC_TMP = new real_t[_nC]; /* nAC */
}
else
{
tempB = 0;
delta_xFRy = 0;
delta_yAC_TMP = 0;
}
flipper.init( (unsigned int)_nV,(unsigned int)_nC );
}
/*
* Q P r o b l e m
*/
QProblem::QProblem( const QProblem& rhs ) : QProblemB( rhs )
{
freeConstraintMatrix = BT_FALSE;
A = 0;
copy( rhs );
}
/*
* ~ Q P r o b l e m
*/
QProblem::~QProblem( )
{
clear( );
}
/*
* o p e r a t o r =
*/
QProblem& QProblem::operator=( const QProblem& rhs )
{
if ( this != &rhs )
{
clear( );
QProblemB::operator=( rhs );
copy( rhs );
}
return *this;
}
/*
* r e s e t
*/
returnValue QProblem::reset( )
{
int i;
int nV = getNV( );
int nC = getNC( );
if ( nV == 0 )
return THROWERROR( RET_QPOBJECT_NOT_SETUP );
/* 1) Reset bounds, Cholesky decomposition and status flags. */
if ( QProblemB::reset( ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_RESET_FAILED );
/* 2) Reset constraints. */
constraints.init( nC );
/* 3) Reset TQ factorisation. */
for( i=0; i<sizeT*sizeT; ++i )
T[i] = 0.0;
for( i=0; i<nV*nV; ++i )
Q[i] = 0.0;
/* 4) Reset constraint product pointer. */
constraintProduct = 0;
/* 5) Reset flipper object */
flipper.init( (unsigned int)nV,(unsigned int)nC );
return SUCCESSFUL_RETURN;
}
/*
* i n i t
*/
returnValue QProblem::init( SymmetricMatrix *_H, const real_t* const _g, Matrix *_A,
const real_t* const _lb, const real_t* const _ub,
const real_t* const _lbA, const real_t* const _ubA,
int& nWSR, real_t* const cputime,
const real_t* const xOpt, const real_t* const yOpt,
const Bounds* const guessedBounds, const Constraints* const guessedConstraints,
const real_t* const _R
)
{
int i;
int nV = getNV( );
int nC = getNC( );
if ( nV == 0 )
return THROWERROR( RET_QPOBJECT_NOT_SETUP );
/* 1) Consistency checks. */
if ( isInitialised( ) == BT_TRUE )
{
THROWWARNING( RET_QP_ALREADY_INITIALISED );
reset( );
}
if ( guessedBounds != 0 )
{
for( i=0; i<nV; ++i )
{
if ( guessedBounds->getStatus( i ) == ST_UNDEFINED )
return THROWERROR( RET_INVALID_ARGUMENTS );
}
}
if ( guessedConstraints != 0 )
{
for( i=0; i<nC; ++i )
if ( guessedConstraints->getStatus( i ) == ST_UNDEFINED )
return THROWERROR( RET_INVALID_ARGUMENTS );
}
/* exclude these possibilities in order to avoid inconsistencies */
if ( ( xOpt == 0 ) && ( yOpt != 0 ) && ( ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) ) )
return THROWERROR( RET_INVALID_ARGUMENTS );
if ( ( _R != 0 ) && ( ( xOpt != 0 ) || ( yOpt != 0 ) || ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) ) )
return THROWERROR( RET_NO_CHOLESKY_WITH_INITIAL_GUESS );
/* 2) Setup QP data. */
if ( setupQPdata( _H,_g,_A,_lb,_ub,_lbA,_ubA ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_INVALID_ARGUMENTS );
/* 3) Call to main initialisation routine. */
return solveInitialQP( xOpt,yOpt,guessedBounds,guessedConstraints,_R, nWSR,cputime );
}
/*
* i n i t
*/
returnValue QProblem::init( const real_t* const _H, const real_t* const _g, const real_t* const _A,
const real_t* const _lb, const real_t* const _ub,
const real_t* const _lbA, const real_t* const _ubA,
int& nWSR, real_t* const cputime,
const real_t* const xOpt, const real_t* const yOpt,
const Bounds* const guessedBounds, const Constraints* const guessedConstraints,
const real_t* const _R
)
{
int i;
int nV = getNV( );
int nC = getNC( );
if ( nV == 0 )
return THROWERROR( RET_QPOBJECT_NOT_SETUP );
/* 1) Consistency checks. */
if ( isInitialised( ) == BT_TRUE )
{
THROWWARNING( RET_QP_ALREADY_INITIALISED );
reset( );
}
if ( guessedBounds != 0 )
{
for( i=0; i<nV; ++i )
{
if ( guessedBounds->getStatus( i ) == ST_UNDEFINED )
return THROWERROR( RET_INVALID_ARGUMENTS );
}
}
if ( guessedConstraints != 0 )
{
for( i=0; i<nC; ++i )
if ( guessedConstraints->getStatus( i ) == ST_UNDEFINED )
return THROWERROR( RET_INVALID_ARGUMENTS );
}
/* exclude these possibilities in order to avoid inconsistencies */
if ( ( xOpt == 0 ) && ( yOpt != 0 ) && ( ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) ) )
return THROWERROR( RET_INVALID_ARGUMENTS );
if ( ( _R != 0 ) && ( ( xOpt != 0 ) || ( yOpt != 0 ) || ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) ) )
return THROWERROR( RET_NO_CHOLESKY_WITH_INITIAL_GUESS );
/* 2) Setup QP data. */
if ( setupQPdata( _H,_g,_A,_lb,_ub,_lbA,_ubA ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_INVALID_ARGUMENTS );
/* 3) Call to main initialisation routine. */
return solveInitialQP( xOpt,yOpt,guessedBounds,guessedConstraints,_R, nWSR,cputime );
}
/*
* i n i t
*/
returnValue QProblem::init( const char* const H_file, const char* const g_file, const char* const A_file,
const char* const lb_file, const char* const ub_file,
const char* const lbA_file, const char* const ubA_file,
int& nWSR, real_t* const cputime,
const real_t* const xOpt, const real_t* const yOpt,
const Bounds* const guessedBounds, const Constraints* const guessedConstraints,
const char* const R_file
)
{
int i;
int nV = getNV( );
int nC = getNC( );
if ( nV == 0 )
return THROWERROR( RET_QPOBJECT_NOT_SETUP );
/* 1) Consistency checks. */
if ( isInitialised( ) == BT_TRUE )
{
THROWWARNING( RET_QP_ALREADY_INITIALISED );
reset( );
}
if ( guessedBounds != 0 )
{
for( i=0; i<nV; ++i )
{
if ( guessedBounds->getStatus( i ) == ST_UNDEFINED )
return THROWERROR( RET_INVALID_ARGUMENTS );
}
}
if ( guessedConstraints != 0 )
{
for( i=0; i<nC; ++i )
if ( guessedConstraints->getStatus( i ) == ST_UNDEFINED )
return THROWERROR( RET_INVALID_ARGUMENTS );
}
/* exclude these possibilities in order to avoid inconsistencies */
if ( ( xOpt == 0 ) && ( yOpt != 0 ) && ( ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) ) )
return THROWERROR( RET_INVALID_ARGUMENTS );
if ( ( R_file != 0 ) && ( ( xOpt != 0 ) || ( yOpt != 0 ) || ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) ) )
return THROWERROR( RET_NO_CHOLESKY_WITH_INITIAL_GUESS );
/* 2) Setup QP data from files. */
if ( setupQPdataFromFile( H_file,g_file,A_file,lb_file,ub_file,lbA_file,ubA_file ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_UNABLE_TO_READ_FILE );
if ( R_file == 0 )
{
/* 3) Call to main initialisation routine. */
return solveInitialQP( xOpt,yOpt,guessedBounds,guessedConstraints,0, nWSR,cputime );
}
else
{
/* Also read Cholesky factor from file and store it directly into R [thus... */
returnValue returnvalue = readFromFile( R, nV,nV, R_file );
if ( returnvalue != SUCCESSFUL_RETURN )
return THROWWARNING( returnvalue );
/* 3) Call to main initialisation routine. ...passing R here!] */
return solveInitialQP( xOpt,yOpt,guessedBounds,guessedConstraints,R, nWSR,cputime );
}
}
/*
* h o t s t a r t
*/
returnValue QProblem::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,
const Bounds* const guessedBounds, const Constraints* const guessedConstraints
)
{
int i, nActiveFar;
int nV = getNV ();
int nC = getNC ();
real_t starttime = 0.0;
real_t auxTime = 0.0;
if ( nV == 0 )
return THROWERROR( RET_QPOBJECT_NOT_SETUP );
/* Possibly update working sets according to guesses for working sets of bounds and constraints. */
if ( ( guessedBounds != 0 ) || ( guessedConstraints != 0 ) )
{
if ( cputime != 0 )
starttime = getCPUtime( );
const Bounds* actualGuessedBounds = ( guessedBounds != 0 ) ? guessedBounds : &bounds;
const Constraints* actualGuessedConstraints = ( guessedConstraints != 0 ) ? guessedConstraints : &constraints;
if ( setupAuxiliaryQP( actualGuessedBounds,actualGuessedConstraints ) != SUCCESSFUL_RETURN )
return THROWERROR( RET_SETUP_AUXILIARYQP_FAILED );
status = QPS_AUXILIARYQPSOLVED;
/* Allow only remaining CPU time for usual hotstart. */
if ( cputime != 0 )
{
auxTime = getCPUtime( ) - starttime;
*cputime -= auxTime;
}
}
returnValue returnvalue = SUCCESSFUL_RETURN;
/* Simple check for consistency of bounds and constraints. */
if ( areBoundsConsistent(lb_new, ub_new, lbA_new, ubA_new) != SUCCESSFUL_RETURN )
return setInfeasibilityFlag(returnvalue,BT_TRUE);
++count;
int nWSR_max = nWSR;
int nWSR_performed = 0;
real_t cputime_remaining = INFTY, *pcputime_rem;
real_t cputime_needed = 0.0;
real_t farbound = options.initialFarBounds;
/* writeQpDataIntoMatFile( "qpData.mat" ); */
/* writeQpWorkspaceIntoMatFile( "qpWorkspace.mat" ); */
if ( haveCholesky == BT_FALSE )
{
returnvalue = setupInitialCholesky( );
if (returnvalue != SUCCESSFUL_RETURN)
return THROWERROR(returnvalue);
}
BooleanType isFirstCall = BT_TRUE;
if ( options.enableFarBounds == BT_FALSE )
{
/* Automatically call standard solveQP if regularisation is not active. */
returnvalue = solveRegularisedQP( g_new,lb_new,ub_new,lbA_new,ubA_new,
nWSR,cputime,0,
isFirstCall
);
}
else
{
real_t *ub_new_far = new real_t[nV];
real_t *lb_new_far = new real_t[nV];
real_t *ubA_new_far = new real_t[nC];
real_t *lbA_new_far = new real_t[nC];
/* possibly extend initial far bounds to largest bound/constraint data */
if (ub_new)
for (i = 0; i < nV; i++)
if ((ub_new[i] < INFTY) && (ub_new[i] > farbound)) farbound = ub_new[i];
if (lb_new)
for (i = 0; i < nV; i++)
if ((lb_new[i] > -INFTY) && (lb_new[i] < -farbound)) farbound = -lb_new[i];
if (ubA_new)
for (i = 0; i < nC; i++)
if ((ubA_new[i] < INFTY) && (ubA_new[i] > farbound)) farbound = ubA_new[i];
if (lbA_new)
for (i = 0; i < nC; i++)
if ((lbA_new[i] > -INFTY) && (lbA_new[i] < -farbound)) farbound = -lbA_new[i];
updateFarBounds( farbound,nV+nC,
lb_new,lb_new_far, ub_new,ub_new_far,
lbA_new,lbA_new_far, ubA_new,ubA_new_far
);
for ( ;; )
{
nWSR = nWSR_max;
if ( cputime != 0 )
{
cputime_remaining = *cputime - cputime_needed;
pcputime_rem = &cputime_remaining;
}
else
pcputime_rem = 0;
/* Automatically call standard solveQP if regularisation is not active. */
returnvalue = solveRegularisedQP( g_new,lb_new_far,ub_new_far,lbA_new_far,ubA_new_far,
nWSR,pcputime_rem,nWSR_performed,
isFirstCall
);
nWSR_performed = nWSR;
cputime_needed += cputime_remaining;
isFirstCall = BT_FALSE;
/* Check for active far-bounds and move them away */
nActiveFar = 0;
farbound *= options.growFarBounds;
if ( infeasible == BT_TRUE )
{
if ( farbound >= INFTY )
{
returnvalue = RET_HOTSTART_STOPPED_INFEASIBILITY;
break; // goto farewell;
}
updateFarBounds( farbound,nV+nC,
lb_new,lb_new_far, ub_new,ub_new_far,
lbA_new,lbA_new_far, ubA_new,ubA_new_far
);
}
else if ( status == QPS_SOLVED )
{
real_t tol = farbound/options.growFarBounds * options.boundTolerance;
for ( i=0; i<nV; ++i )
{
if ( ( ( lb_new == 0 ) || ( lb_new_far[i] > lb_new[i] ) ) && ( getAbs ( lb_new_far[i] - x[i] ) < tol ) )
++nActiveFar;
if ( ( ( ub_new == 0 ) || ( ub_new_far[i] < ub_new[i] ) ) && ( getAbs ( ub_new_far[i] - x[i] ) < tol ) )
++nActiveFar;
}
for ( i=0; i<nC; ++i )
{
if ( ( ( lbA_new == 0 ) || ( lbA_new_far[i] > lbA_new[i] ) ) && ( getAbs ( lbA_new_far[i] - Ax[i] ) < tol ) )
++nActiveFar;
if ( ( ( ubA_new == 0 ) || ( ubA_new_far[i] < ubA_new[i] ) ) && ( getAbs ( ubA_new_far[i] - Ax[i] ) < tol ) )
++nActiveFar;
}
if ( nActiveFar == 0 )
break;
status = QPS_HOMOTOPYQPSOLVED;
if ( farbound >= INFTY )
{
unbounded = BT_TRUE;
returnvalue = RET_HOTSTART_STOPPED_UNBOUNDEDNESS;
goto farewell;
}
updateFarBounds( farbound,nV+nC,
lb_new,lb_new_far, ub_new,ub_new_far,
lbA_new,lbA_new_far, ubA_new,ubA_new_far
);
}
else
{
/* some other error when solving QP */
break;
}
/* advance ramp offset to avoid Ramping cycles */
rampOffset++;
}
farewell:
/* add time to setup auxiliary QP */
if ( cputime != 0 )
*cputime = cputime_needed + auxTime;
delete[] lbA_new_far; delete[] ubA_new_far;
delete[] lb_new_far; delete[] ub_new_far;
}
return ( returnvalue != SUCCESSFUL_RETURN ) ? THROWERROR( returnvalue ) : returnvalue;
}
/*
* h o t s t a r t
*/
returnValue QProblem::hotstart( 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,
int& nWSR, real_t* const cputime,
const Bounds* const guessedBounds, const Constraints* const guessedConstraints
)
{
int nV = getNV( );
int nC = getNC( );
if ( nV == 0 )
return THROWERROR( RET_QPOBJECT_NOT_SETUP );
/* consistency check */
if ( g_file == 0 )
return THROWERROR( RET_INVALID_ARGUMENTS );
/* 1) Allocate memory (if bounds exist). */
real_t* g_new = new real_t[nV];
real_t* lb_new = ( lb_file != 0 ) ? new real_t[nV] : 0;
real_t* ub_new = ( ub_file != 0 ) ? new real_t[nV] : 0;
real_t* lbA_new = ( lbA_file != 0 ) ? new real_t[nC] : 0;
real_t* ubA_new = ( ubA_file != 0 ) ? new real_t[nC] : 0;
/* 2) Load new QP vectors from file. */
returnValue returnvalue;
returnvalue = loadQPvectorsFromFile( g_file,lb_file,ub_file,lbA_file,ubA_file,
g_new,lb_new,ub_new,lbA_new,ubA_new
);
if ( returnvalue != SUCCESSFUL_RETURN )
{
if ( ubA_file != 0 )
delete[] ubA_new;
if ( lbA_file != 0 )
delete[] lbA_new;
if ( ub_file != 0 )
delete[] ub_new;
if ( lb_file != 0 )
delete[] lb_new;
delete[] g_new;
return THROWERROR( RET_UNABLE_TO_READ_FILE );
}
/* 3) Actually perform hotstart. */
returnvalue = hotstart( g_new,lb_new,ub_new,lbA_new,ubA_new,
nWSR,cputime,
guessedBounds,guessedConstraints
);
/* 4) Free memory. */
if ( ubA_file != 0 )
delete[] ubA_new;
if ( lbA_file != 0 )
delete[] lbA_new;
if ( ub_file != 0 )
delete[] ub_new;
if ( lb_file != 0 )
delete[] lb_new;
delete[] g_new;
return returnvalue;
}
/*
* s o l v e C u r r e n t E Q P
*/
returnValue QProblem::solveCurrentEQP( const int n_rhs,
const real_t* g_in,
const real_t* lb_in,
const real_t* ub_in,
const real_t* lbA_in,
const real_t* ubA_in,
real_t* x_out,
real_t* y_out
)
{
if ( ( x_out == 0 ) || ( y_out == 0 ) )
return THROWERROR( RET_INVALID_ARGUMENTS );
returnValue returnvalue = SUCCESSFUL_RETURN;
int ii, jj;
int nV = getNV( );
int nC = getNC( );
int nFR = getNFR( );
int nFX = getNFX( );
int nAC = getNAC( );
real_t *delta_xFX = new real_t[nFX];
real_t *delta_xFR = new real_t[nFR];
real_t *delta_yAC = new real_t[nAC];
real_t *delta_yFX = new real_t[nFX];
/* 1) Determine index arrays. */
int* FR_idx;
int* FX_idx;
int* AC_idx;
bounds.getFree( )->getNumberArray( &FR_idx );
bounds.getFixed( )->getNumberArray( &FX_idx );
constraints.getActive( )->getNumberArray( &AC_idx );
for ( ii = 0 ; ii < (nV+nC)*n_rhs; ++ii )
y_out[ii] = 0.0;
for ( ii = 0 ; ii < n_rhs; ++ii )
{
returnvalue = determineStepDirection(
g_in, lbA_in, ubA_in, lb_in, ub_in, BT_FALSE, BT_FALSE,
delta_xFX, delta_xFR, delta_yAC, delta_yFX );
for ( jj = 0; jj < nFX; ++jj )
x_out[FX_idx[jj]] = delta_xFX[jj];
for ( jj = 0; jj < nFR; ++jj )
x_out[FR_idx[jj]] = delta_xFR[jj];
for ( jj = 0; jj < nFX; ++jj )
y_out[FX_idx[jj]] = delta_yFX[jj];
for ( jj = 0; jj < nAC; ++jj )
y_out[nV+AC_idx[jj]] = delta_yAC[jj];
g_in += nV;
lb_in += nV;
ub_in += nV;
lbA_in += nC;
ubA_in += nC;
x_out += nV;
y_out += nV+nC;
}
delete[] delta_yFX;
delete[] delta_yAC;
delete[] delta_xFR;
delete[] delta_xFX;
return returnvalue;
}
/*
* g e t W o r k i n g S e t
*/
returnValue QProblem::getWorkingSet( real_t* workingSet )
{
int nV = this->getNV();
if ( workingSet == 0 )
return THROWERROR( RET_INVALID_ARGUMENTS );
/* At which limit are the bounds active? */
getWorkingSetBounds( workingSet );
/* At which limit are the contraints active? */
getWorkingSetConstraints( &(workingSet[nV]) );
return SUCCESSFUL_RETURN;
}
/*
* g e t W o r k i n g S e t B o u n d s
*/
returnValue QProblem::getWorkingSetBounds( real_t* workingSetB )
{
return QProblemB::getWorkingSetBounds( workingSetB );
}
/*
* g e t W o r k i n g S e t C o n s t r a i n t s
*/
returnValue QProblem::getWorkingSetConstraints( real_t* workingSetC )
{
int i;
int nC = this->getNC();
if ( workingSetC == 0 )
return THROWERROR( RET_INVALID_ARGUMENTS );
for ( i=0; i<nC; ++i )
{
switch ( constraints.getStatus(i) )
{
case ST_LOWER: workingSetC[i] = -1.0; break;
case ST_UPPER: workingSetC[i] = +1.0; break;
default: workingSetC[i] = 0.0; break;
}
}
return SUCCESSFUL_RETURN;
}
/*
* g e t N Z
*/
int QProblem::getNZ( ) const
{
/* nZ = nFR - nAC */
return getNFR( ) - getNAC( );
}
/*
* g e t D u a l S o l u t i o n
*/
returnValue QProblem::getDualSolution( real_t* const yOpt ) const
{
int i;
for( i=0; i<getNV( )+getNC( ); ++i )
yOpt[i] = y[i];
/* return optimal dual solution vector
* only if current QP has been solved */
if ( ( getStatus( ) == QPS_AUXILIARYQPSOLVED ) ||
( getStatus( ) == QPS_HOMOTOPYQPSOLVED ) ||
( getStatus( ) == QPS_SOLVED ) )
{
return SUCCESSFUL_RETURN;
}
else
{
return RET_QP_NOT_SOLVED;
}
}
/*
* s e t C o n s t r a i n t P r o d u c t
*/
returnValue QProblem::setConstraintProduct( ConstraintProduct* const _constraintProduct )
{
constraintProduct = _constraintProduct;
return SUCCESSFUL_RETURN;
}
/*
* p r i n t P r o p e r t i e s
*/
returnValue QProblem::printProperties( )
{
#ifndef __XPCTARGET__
/* Do not print properties if print level is set to none! */
if ( options.printLevel == PL_NONE )
return SUCCESSFUL_RETURN;
char myPrintfString[MAX_STRING_LENGTH];
myPrintf( "\n################# qpOASES -- QP PROPERTIES #################\n" );
myPrintf( "\n" );
/* 1) Variables properties. */
snprintf( myPrintfString,MAX_STRING_LENGTH, "Number of Variables: %4.1d\n",getNV( ) );
myPrintf( myPrintfString );
if ( bounds.hasNoLower( ) == BT_TRUE )
myPrintf( "Variables are not bounded from below.\n" );
else
myPrintf( "Variables are bounded from below.\n" );
if ( bounds.hasNoUpper( ) == BT_TRUE )
myPrintf( "Variables are not bounded from above.\n" );
else
myPrintf( "Variables are bounded from above.\n" );
myPrintf( "\n" );
/* 2) Constraints properties. */
snprintf( myPrintfString,MAX_STRING_LENGTH, "Total number of Constraints: %4.1d\n",getNC( ) );
myPrintf( myPrintfString );
snprintf( myPrintfString,MAX_STRING_LENGTH, "Number of Equality Constraints: %4.1d\n",getNEC( ) );
myPrintf( myPrintfString );
snprintf( myPrintfString,MAX_STRING_LENGTH, "Number of Inequality Constraints: %4.1d\n",getNC( )-getNEC( ) );
myPrintf( myPrintfString );
if ( getNC( ) > 0 )
{
if ( constraints.hasNoLower( ) == BT_TRUE )
myPrintf( "Constraints are not bounded from below.\n" );
else
myPrintf( "Constraints are bounded from below.\n" );
if ( constraints.hasNoUpper( ) == BT_TRUE )
myPrintf( "Constraints are not bounded from above.\n" );
else
myPrintf( "Constraints are bounded from above.\n" );
}
myPrintf( "\n" );
/* 3) Further properties. */
switch ( hessianType )
{
case HST_ZERO:
myPrintf( "Hessian is zero matrix (i.e. actually an LP is solved).\n" );
break;
case HST_IDENTITY:
myPrintf( "Hessian is identity matrix.\n" );
break;
case HST_POSDEF:
myPrintf( "Hessian matrix is (strictly) positive definite.\n" );
break;
case HST_POSDEF_NULLSPACE:
myPrintf( "Hessian matrix is positive definite on null space of active constraints.\n" );
break;
case HST_SEMIDEF:
myPrintf( "Hessian matrix is positive semi-definite.\n" );
break;
case HST_INDEF:
myPrintf( "Hessian matrix is indefinite.\n" );
break;
default:
myPrintf( "Hessian matrix has unknown type.\n" );
break;
}
if ( infeasible == BT_TRUE )
myPrintf( "QP was found to be infeasible.\n" );
else
myPrintf( "QP seems to be feasible.\n" );
if ( unbounded == BT_TRUE )
myPrintf( "QP was found to be unbounded from below.\n" );
else
myPrintf( "QP seems to be bounded from below.\n" );
myPrintf( "\n" );
/* 4) QP object properties. */
switch ( status )
{
case QPS_NOTINITIALISED:
myPrintf( "Status of QP object: freshly instantiated or reset.\n" );
break;
case QPS_PREPARINGAUXILIARYQP:
myPrintf( "Status of QP object: an auxiliary QP is currently setup.\n" );
break;
case QPS_AUXILIARYQPSOLVED:
myPrintf( "Status of QP object: an auxilary QP was solved.\n" );
break;
case QPS_PERFORMINGHOMOTOPY:
myPrintf( "Status of QP object: a homotopy step is performed.\n" );
break;