36 using namespace Eigen;
150 int infeasibleQPhandling;
174 cout <<
"--> Condesing banded QP ...\n";
186 cout <<
"<-- Condesing banded QP done.\n";
204 for(
uint run1 = 0; run1 <
getNX(); run1++ )
213 for(
uint run1 = 0; run1 <
getNP(); run1++ )
228 cout <<
"--> Solving condesed QP ...\n";
236 cout <<
"<-- Solving condesed QP done.\n";
257 cout <<
"--> Expanding condensed QP solution ...\n";
271 cout <<
"<-- Expanding condensed QP solution done.\n";
286 p_( i ) = (*
denseCP.
x)( startIdx+i );
300 u0_( i ) = (*
denseCP.
x)( startIdx+i );
368 for (
unsigned el = 0; el < D.size(); el++)
369 if (D( el ) <= 0.1 * dampingFactor)
371 if (fabs(D( el )) >= dampingFactor)
372 D( el ) = fabs(D( el ));
374 D( el ) = dampingFactor;
380 H_ = V * D.asDiagonal() * V.inverse();
409 double hessianProjectionFactor;
416 double levenbergMarquard;
419 if( levenbergMarquard >
EPS )
426 if (denseHConditionNumber > 1.0e16)
429 <<
"Condition number of the condensed Hessian is quite high: log_10(kappa( H )) = " 430 << log10( denseHConditionNumber ) << endl;
436 LOG(
LVL_WARNING ) <<
"Ill formed condensed Hessian: min(.) < -1e16 or max(.) > 1e16" << endl;
451 returnvalue =
solveQP( maxQPiter );
456 switch( returnvalue )
476 int infeasibleQPhandling;
519 uint run1, run2, run3;
543 for( run3 = 0; run3 <
N; run3++ ){
551 for( run3 = 0; run3 < N-1; run3++ ){
556 for( run3 = 0; run3 < N-1; run3++ ){
574 for( run3 = 1; run3 <
N; run3++ ){
589 for( run1 = 0; run1 <
N; run1++ ){
612 for( run3 = 1; run3 <
N; run3++ ){
658 for( run1 = 0; run1 <
getNP(); run1++ )
666 for( run1 = 0; run1 < tmp.
getNumRows(); run1++ )
667 for( run3 = 0; run3 < tmp.
getNumCols(); run3++ )
668 denseCP.
A(offset + run1,run3) = tmp(run1,run3);
671 for( run1 = 0; run1 < tmp.
getNumRows(); run1++ )
675 for( run1 = 0; run1 < tmp.
getNumRows(); run1++ )
682 for( run1 = 0; run1 <
getNP(); run1++ )
685 for( run1 = 0; run1 <
getNP(); run1++ )
699 uint run1, run2, run3;
711 for( run1 = 0; run1 < nn; run1++ )
712 for( run2 = 0; run2 <
getNX(); run2++ )
713 denseCP.
H(rowOffset1+run1,run2) = tmp(run1,run2);
714 colOffset1 +=
getNX();
718 for( run3 = 0; run3 <
N; run3++ ){
721 for( run1 = 0; run1 < nn; run1++ )
722 for( run2 = 0; run2 <
getNXA(); run2++ )
723 denseCP.
H(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
731 for( run1 = 0; run1 < nn; run1++ )
732 for( run2 = 0; run2 <
getNP(); run2++ )
733 denseCP.
H(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
734 colOffset1 +=
getNP();
738 for( run3 = 0; run3 < N-1; run3++ ){
741 for( run1 = 0; run1 < nn; run1++ )
742 for( run2 = 0; run2 <
getNU(); run2++ )
743 denseCP.
H(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
744 colOffset1 +=
getNU();
749 for( run3 = 0; run3 < N-1; run3++ ){
752 for( run1 = 0; run1 < nn; run1++ )
753 for( run2 = 0; run2 <
getNW(); run2++ )
754 denseCP.
H(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
755 colOffset1 +=
getNW();
768 uint run1, run2, run3;
779 for( run1 = 0; run1 < nn; run1++ )
780 for( run2 = 0; run2 <
getNX(); run2++ )
781 denseCP.
A(rowOffset1+run1,run2) = tmp(run1,run2);
782 colOffset1 +=
getNX();
786 for( run3 = 0; run3 <
N; run3++ ){
789 for( run1 = 0; run1 < nn; run1++ )
790 for( run2 = 0; run2 <
getNXA(); run2++ )
791 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
799 for( run1 = 0; run1 < nn; run1++ )
800 for( run2 = 0; run2 <
getNP(); run2++ )
801 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
802 colOffset1 +=
getNP();
806 for( run3 = 0; run3 < N-1; run3++ ){
809 for( run1 = 0; run1 < nn; run1++ )
810 for( run2 = 0; run2 <
getNU(); run2++ )
811 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
812 colOffset1 +=
getNU();
817 for( run3 = 0; run3 < N-1; run3++ ){
820 for( run1 = 0; run1 < nn; run1++ )
821 for( run2 = 0; run2 <
getNW(); run2++ )
822 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
823 colOffset1 +=
getNW();
836 uint run1, run2, run3;
847 for( run1 = 0; run1 < nn; run1++ )
848 for( run2 = 0; run2 <
getNX(); run2++ )
849 denseCP.
A(rowOffset1+run1,run2) = tmp(run1,run2);
850 colOffset1 +=
getNX();
854 for( run3 = 0; run3 <
N; run3++ ){
857 for( run1 = 0; run1 < nn; run1++ )
858 for( run2 = 0; run2 <
getNXA(); run2++ )
859 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
867 for( run1 = 0; run1 < nn; run1++ )
868 for( run2 = 0; run2 <
getNP(); run2++ )
869 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
870 colOffset1 +=
getNP();
874 for( run3 = 0; run3 < N-1; run3++ ){
877 for( run1 = 0; run1 < nn; run1++ )
878 for( run2 = 0; run2 <
getNU(); run2++ )
879 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
880 colOffset1 +=
getNU();
885 for( run3 = 0; run3 < N-1; run3++ ){
888 for( run1 = 0; run1 < nn; run1++ )
889 for( run2 = 0; run2 <
getNW(); run2++ )
890 denseCP.
A(rowOffset1+run1,colOffset1+run2) = tmp(run1,run2);
891 colOffset1 +=
getNW();
908 for( run1 = 0; run1 < nn; run1++ )
912 for( run1 = 0; run1 < nn; run1++ )
928 for( run1 = 0; run1 < nn; run1++ )
932 for( run1 = 0; run1 < nn; run1++ )
954 for( run1 = 0; run1 <
getNX(); run1++ )
955 denseCP.
lb(rowOffset1+run1) = tmp(run1,0);
957 for( run1 = 0; run1 <
getNX(); run1++ )
958 denseCP.
ub(rowOffset1+run1) = tmp(run1,0);
959 rowOffset1 +=
getNX();
962 for( run3 = 0; run3 <
N; run3++ ){
965 for( run1 = 0; run1 <
getNXA(); run1++ )
966 denseCP.
lb(rowOffset1+run1) = tmp(run1,0);
968 for( run1 = 0; run1 <
getNXA(); run1++ )
969 denseCP.
ub(rowOffset1+run1) = tmp(run1,0);
977 for( run1 = 0; run1 <
getNP(); run1++ )
978 denseCP.
lb(rowOffset1+run1) = tmp(run1,0);
980 for( run1 = 0; run1 <
getNP(); run1++ )
981 denseCP.
ub(rowOffset1+run1) = tmp(run1,0);
982 rowOffset1 +=
getNP();
986 for( run3 = 0; run3 < N-1; run3++ ){
989 for( run1 = 0; run1 <
getNU(); run1++ )
990 denseCP.
lb(rowOffset1+run1) = tmp(run1,0);
992 for( run1 = 0; run1 <
getNU(); run1++ )
993 denseCP.
ub(rowOffset1+run1) = tmp(run1,0);
994 rowOffset1 +=
getNU();
1000 for( run3 = 0; run3 < N-1; run3++ ){
1003 for( run1 = 0; run1 <
getNW(); run1++ )
1004 denseCP.
lb(rowOffset1+run1) = tmp(run1,0);
1006 for( run1 = 0; run1 <
getNW(); run1++ )
1007 denseCP.
ub(rowOffset1+run1) = tmp(run1,0);
1008 rowOffset1 +=
getNW();
1022 uint rowOffset1 = 0;
1030 for( run1 = 0; run1 <
getNX(); run1++ )
1031 denseCP.
g(rowOffset1+run1) = tmp(0,run1);
1032 rowOffset1 +=
getNX();
1036 for( run3 = 0; run3 <
N; run3++ ){
1039 for( run1 = 0; run1 <
getNXA(); run1++ )
1040 denseCP.
g(rowOffset1+run1) = tmp(0,run1);
1048 for( run1 = 0; run1 <
getNP(); run1++ )
1049 denseCP.
g(rowOffset1+run1) = tmp(0,run1);
1050 rowOffset1 +=
getNP();
1054 for( run3 = 0; run3 < N-1; run3++ ){
1057 for( run1 = 0; run1 <
getNU(); run1++ )
1058 denseCP.
g(rowOffset1+run1) = tmp(0,run1);
1059 rowOffset1 +=
getNU();
1064 for( run3 = 0; run3 < N-1; run3++ ){
1067 for( run1 = 0; run1 <
getNW(); run1++ )
1068 denseCP.
g(rowOffset1+run1) = tmp(0,run1);
1069 rowOffset1 +=
getNW();
1104 primalDense.
init( 3*N, 1 );
1112 for( run1 = 0; run1 <
getNX(); run1++ )
1115 rowCount1 +=
getNX();
1118 for( run2 = 0; run2 <
N; run2++ ){
1121 for( run1 = 0; run1 <
getNXA(); run1++ )
1122 tmp(run1,0) = (*
denseCP.
x)(rowCount1+run1);
1123 primalDense.
setDense( rowCount, 0, tmp );
1131 for( run1 = 0; run1 <
getNP(); run1++ )
1132 tmp(run1,0) = (*
denseCP.
x)(rowCount1+run1);
1133 primalDense.
setDense( rowCount, 0, tmp );
1134 rowCount1 +=
getNP();
1138 for( run2 = 0; run2 < N-1; run2++ ){
1141 for( run1 = 0; run1 <
getNU(); run1++ )
1142 tmp(run1,0) = (*
denseCP.
x)(rowCount1+run1);
1143 primalDense.
setDense( rowCount, 0, tmp );
1144 rowCount1 +=
getNU();
1149 for( run2 = 0; run2 < N-1; run2++ ){
1152 for( run1 = 0; run1 <
getNW(); run1++ )
1153 tmp(run1,0) = (*
denseCP.
x)(rowCount1+run1);
1154 primalDense.
setDense( rowCount, 0, tmp );
1155 rowCount1 +=
getNW();
1173 for( run2 = 0; run2 <
N; run2++ ){
1176 for( run4 = 0; run4 <
getNX(); run4++ ){
1177 aux2(run2*
getNX()+run4) += denseDualSolution(nF+run+run3)*tmp(run3,run4);
1186 for( run2 = 0; run2 <
getNX(); run2++ )
1187 aux3(run2) = denseDualSolution(run2);
1192 for( run1 = 0; run1 < N-1; run1++ )
1193 for( run2 = 0; run2 <
getNX(); run2++ )
1194 aux3((run1+1)*
getNX()+run2) = denseDualSolution(run+run1*
getNX()+run2);
1202 for( run1 = 0; run1 < N-1; run1++ ){
1206 for( run2 = 0; run2 <
getNX(); run2++ ){
1207 aux4[run1](run2) = tmp(0,run2) - aux2( (run1+1)*
getNX()+run2 ) - aux3( (run1+1)*
getNX()+run2 );
1216 lambdaDyn[N-2] = aux4[N-2];
1218 for( run1 = N-2; run1 >= 1; run1-- ){
1220 lambdaDyn[run1-1] = (Gx.transpose() * lambdaDyn[run1]) + aux4[run1-1];
1225 for( run1 = 0; run1 < N-1; run1++ ){
1226 tmp.
init( lambdaDyn[run1].getDim(), 1 );
1227 for( run2 = 0; run2 < lambdaDyn[run1].
getDim(); run2++ )
1228 tmp(run2,0) = lambdaDyn[run1].operator()(run2);
1237 for( run1 = 0; run1 < N-1; run1++ ){
1238 tmp.
init( lambdaDyn[run1].getDim(), 1 );
1239 tmp.
setAll(1.0/((
double) lambdaDyn[run1].getDim()));
1251 for( run1 = 0; run1 <
getNP(); run1++ )
1252 tmp( run1, 0 ) = (*
denseCP.
x)(run1);
1266 for( run2 = 0; run2 <
blockDims(run1); run2++ ){
1267 tmp(run2,0) = denseDualSolution(nF+run+run2);
1278 for( run1 = 0; run1 <
getNX(); run1++ )
1279 tmp(run1,0) = denseDualSolution(run1);
1285 for( run2 = 1; run2 <
N; run2++ ){
1288 for( run1 = 0; run1 <
getNX(); run1++ )
1289 tmp(run1,0) = denseDualSolution(nF+
getNC()+rowCount1+run1);
1291 rowCount1 +=
getNX();
1295 rowCount1 =
getNX();
1297 for( run2 = 0; run2 <
N; run2++ ){
1300 for( run1 = 0; run1 <
getNXA(); run1++ )
1301 tmp(run1,0) = denseDualSolution(rowCount1+run1);
1310 for( run1 = 0; run1 <
getNP(); run1++ )
1311 tmp(run1,0) = denseDualSolution(rowCount1+run1);
1313 rowCount1 +=
getNP();
1317 for( run2 = 0; run2 < N-1; run2++ ){
1320 for( run1 = 0; run1 <
getNU(); run1++ )
1321 tmp(run1,0) = denseDualSolution(rowCount1+run1);
1323 rowCount1 +=
getNU();
1329 for( run2 = 0; run2 < N-1; run2++ ){
1332 for( run1 = 0; run1 <
getNW(); run1++ )
1333 tmp(run1,0) = denseDualSolution(rowCount1+run1);
1335 rowCount1 +=
getNW();
1359 for(
uint run1 = 0; run1 <
N; run1++ ){
1388 for( run1 = 0; run1 < N-1; run1++ )
1405 for( run2 = 0; run2 <= run1; run2++ ){
1411 if( run1 == run2 )
T.
setDense( run1+1, run2+1, G );
1412 else T.
setDense( run1+1, run2+1, Gx*tmp );
1422 if( tmp.getDim() != 0 ){
1435 for( run2 = 0; run2 <= run1; run2++ ){
1441 if( run1 == run2 )
T.
setDense( run1+1, run2+2+N, G );
1442 else T.
setDense( run1+1, run2+2+N, Gx*tmp );
1451 for( run2 = 0; run2 <= run1; run2++ ){
1457 if( run1 == run2 )
T.
setDense( run1+1, run2+1+2*N, G );
1458 else T.
setDense( run1+1, run2+1+2*N, Gx*tmp );
1488 switch( infeasibleQPhandling )
1512 _denseCPrelaxed.
H.
init( nV+nC,nV+nC );
1513 _denseCPrelaxed.
A.
init( nC,nV+nC );
1514 _denseCPrelaxed.
g.
init( nV+nC );
1515 _denseCPrelaxed.
lb.
init( nV+nC );
1516 _denseCPrelaxed.
ub.
init( nV+nC );
1527 for(
uint i=0; i<nV; ++i )
1528 for(
uint j=0; j<nV; ++j )
1529 _denseCPrelaxed.
H(i,j) =
denseCP.
H(i,j);
1532 for(
uint i=0; i<nC; ++i )
1533 for(
uint j=0; j<nV; ++j )
1534 _denseCPrelaxed.
A(i,j) =
denseCP.
A(i,j);
1535 for(
uint i=0; i<nC; ++i )
1536 _denseCPrelaxed.
A(i,nV+i) = 1.0;
1539 for(
uint i=0; i<nV; ++i )
1542 for(
uint i=nV; i<nV+nC; ++i )
1543 _denseCPrelaxed.
g(i) = l1entry;
1546 for(
uint i=0; i<nV; ++i )
1548 for(
uint i=nV; i<nV+nC; ++i )
1549 _denseCPrelaxed.
lb(i) = 0.0;
1552 for(
uint i=0; i<nV; ++i )
1554 for(
uint i=nV; i<nV+nC; ++i )
1555 _denseCPrelaxed.
ub(i) =
INFTY;
1569 _denseCPrelaxed.
H.
init( nV+nC,nV+nC );
1570 _denseCPrelaxed.
A.
init( nC,nV+nC );
1571 _denseCPrelaxed.
g.
init( nV+nC );
1572 _denseCPrelaxed.
lb.
init( nV+nC );
1573 _denseCPrelaxed.
ub.
init( nV+nC );
1584 for(
uint i=0; i<nV; ++i )
1585 for(
uint j=0; j<nV; ++j )
1586 _denseCPrelaxed.
H(i,j) =
denseCP.
H(i,j);
1588 for(
uint i=nV; i<nV+nC; ++i )
1589 _denseCPrelaxed.
H(i,i) = diagonalEntry;
1592 for(
uint i=0; i<nC; ++i )
1593 for(
uint j=0; j<nV; ++j )
1594 _denseCPrelaxed.
A(i,j) =
denseCP.
A(i,j);
1595 for(
uint i=0; i<nC; ++i )
1596 _denseCPrelaxed.
A(i,nV+i) = 1.0;
1599 for(
uint i=0; i<nV; ++i )
1603 for(
uint i=0; i<nV; ++i )
1605 for(
uint i=nV; i<nV+nC; ++i )
1606 _denseCPrelaxed.
lb(i) = -
INFTY;
1609 for(
uint i=0; i<nV; ++i )
1611 for(
uint i=nV; i<nV+nC; ++i )
1612 _denseCPrelaxed.
ub(i) =
INFTY;
1688 for(
uint i=0; i < nV; ++i )
1689 deltaDense(i) = deltaDenseTmp(i);
1698 for(
uint i=0; i < nV; ++i )
1699 lambdaDense(i) = lambdaDenseTmp(i);
1700 for(
uint i=0; i < nC; ++i )
1701 lambdaDense(nV+i) = lambdaDenseTmp(denseCPrelaxed.
getNV()+i);
returnValue setLast(LogName _name, int lastValue, double time=-INFTY)
#define LOG(level)
Just define a handy macro for getting the logger.
Data class for storing generic optimization variables.
Implements a very rudimentary block sparse matrix class.
returnValue projectHessian(DMatrix &H_, double dampingFactor)
void init(unsigned _nRows=0, unsigned _nCols=0)
virtual returnValue initializeCPsolver(InfeasibleQPhandling infeasibleQPhandling)
DenseQPsolver * cpSolverRelaxed
virtual returnValue finalizeSolve(BandedCP &cp)
virtual returnValue getVarianceCovariance(DMatrix &var)=0
int acadoMax(const int x, const int y)
Allows real time measurements based on the system's clock.
BlockMatrix upperConstraintResiduum
returnValue generateObjectiveGradient()
virtual returnValue getFirstControl(DVector &u0_) const
BooleanType areRealTimeParametersDefined() const
virtual returnValue reset()
#define ACADOINFO(retval)
virtual returnValue unfreezeCondensing()
UserInteraction * userInteraction
BEGIN_NAMESPACE_ACADO const double EPS
returnValue setDense(uint rowIdx, uint colIdx, const DMatrix &value)
void init(unsigned _dim=0)
BlockMatrix objectiveGradient
Computes eigenvalues and eigenvectors of selfadjoint matrices.
returnValue expand(BandedCP &cp)
Allows to pass back messages to the calling function.
iterative scaling algorithm to equilibrate rows and column norms in matrices
BandedCPsolver & operator=(const BandedCPsolver &rhs)
returnValue generateHessianBlockLine(uint nn, uint rowOffset, uint &rowOffset1)
CondensingBasedCPsolver()
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
void setAll(const T &_value)
BlockMatrix lambdaConstraint
returnValue setQPsolution(const DVector &x_, const DVector &y_)
virtual returnValue setupRelaxedQPdata(InfeasibleQPhandling infeasibleQPhandling, DenseCP &_denseCPrelaxed) const
virtual returnValue freezeCondensing()
Base class for algorithms solving banded conic programs arising in optimal control.
returnValue computeCondensingOperator(BandedCP &cp)
virtual returnValue solveQP(uint maxIter, InfeasibleQPhandling infeasibleQPhandling=IQH_UNDEFINED)
#define CLOSE_NAMESPACE_ACADO
returnValue generateConstraintBlockLine(uint nn, uint rowOffset, uint &rowOffset1)
virtual returnValue init(const DenseCP *cp_)=0
BlockMatrix lambdaDynamic
returnValue generateStateBoundVectors(uint nn, uint rowOffset, uint &rowOffset1)
virtual returnValue solve(DenseCP *cp_)=0
EIGEN_STRONG_INLINE Index rows() const
virtual BandedCPsolver * clone() const
BlockMatrix lowerBoundResiduum
uint getNumPoints() const
DVector getMergedDualSolution() const
Returned value is a warning.
returnValue getSubBlock(uint rowIdx, uint colIdx, DMatrix &value) const
Data class for storing generic conic programs.
CondensingBasedCPsolver & operator=(const CondensingBasedCPsolver &rhs)
virtual returnValue getVarianceCovariance(DMatrix &var)
T getConditionNumber() const
returnValue init(uint _nRows, uint _nCols)
virtual returnValue getDualSolution(DVector &yOpt) const =0
static Logger & instance()
returnValueLevel getLogLevel()
const MatrixType & eigenvectors() const
Returns the eigenvectors of given matrix.
virtual returnValue prepareSolve(BandedCP &cp)
BlockMatrix lowerConstraintResiduum
BlockMatrix upperBoundResiduum
Derived & setZero(Index size)
virtual returnValue init(const DenseCP *cp)
virtual returnValue getParameters(DVector &p_) const
Encapsulates all user interaction for setting options, logging data and plotting results.
virtual returnValue solve(BandedCP &cp)
returnValue generateStateBoundBlockLine(uint nn, uint rowOffset, uint &rowOffset1)
void rhs(const real_t *x, real_t *f)
virtual returnValue getPrimalSolution(DVector &xOpt) const =0
virtual returnValue start()
virtual uint getNumberOfVariables() const =0
unsigned getNumRows() const
CondensingStatus condensingStatus
virtual returnValue setRealTimeParameters(const DVector &DeltaX, const DVector &DeltaP=emptyConstVector)
virtual DenseQPsolver * cloneDenseQPsolver() const =0
unsigned getNumCols() const
returnValue generateConstraintVectors(uint nn, uint rowOffset, uint &rowOffset1)
virtual returnValue stop()
virtual returnValue setupRelaxedQPdataL2(DenseCP &_denseCPrelaxed) const
returnValue setZero(uint rowIdx, uint colIdx)
virtual returnValue solve(DenseCP *cp_)
virtual uint getNumberOfConstraints() const =0
returnValue generateBoundVectors()
returnValue initializeCondensingOperator()
#define ACADOWARNING(retval)
#define BEGIN_NAMESPACE_ACADO
virtual ~CondensingBasedCPsolver()
returnValue condense(BandedCP &cp)
virtual returnValue solveCPsubproblem()
returnValue init(uint nV_, uint nC_)
BlockMatrix constraintGradient
Solves banded conic programs arising in optimal control using condensing.
virtual returnValue init(const OCPiterate &iter_)
returnValue setIdentity(uint rowIdx, uint colIdx, uint dim)
virtual returnValue setupRelaxedQPdataL1(DenseCP &_denseCPrelaxed) const
virtual uint getNumberOfIterations() const =0
const RealVectorType & eigenvalues() const
Returns the eigenvalues of given matrix.
virtual DenseCPsolver * clone() const =0
#define ACADOERROR(retval)
virtual returnValue getTime(double &_elapsedTime)
Data class for storing conic programs arising from optimal control.