/* * s o l v e O Q P b e n c h m a r k */ returnValue solveOQPbenchmark( int nQP, int nV, const real_t* const _H, const real_t* const g, const real_t* const lb, const real_t* const ub, BooleanType isSparse, const Options& options, int& nWSR, real_t& maxCPUtime, real_t& maxStationarity, real_t& maxFeasibility, real_t& maxComplementarity ) { int k; /* I) SETUP AUXILIARY VARIABLES: */ /* 1) Keep nWSR and store current and maximum number of * working set recalculations in temporary variables */ int nWSRcur; int maxNWSR = 0; real_t CPUtimeLimit = maxCPUtime; real_t CPUtimeCur = CPUtimeLimit; real_t stat, feas, cmpl; maxCPUtime = 0.0; maxStationarity = 0.0; maxFeasibility = 0.0; maxComplementarity = 0.0; /* 2) Pointers to data of current QP ... */ const real_t* gCur; const real_t* lbCur; const real_t* ubCur; /* 3) Vectors for solution obtained by qpOASES. */ real_t* x = new real_t[nV]; real_t* y = new real_t[nV]; /* 4) Prepare matrix objects */ SymmetricMatrix *H; if ( isSparse == BT_TRUE ) { SymSparseMat *Hs; H = Hs = new SymSparseMat(nV, nV, nV, _H); Hs->createDiagInfo(); } else { H = new SymDenseMat(nV, nV, nV, const_cast<real_t *>(_H)); } /* II) SETUP QPROBLEM OBJECT */ QProblemB qp( nV ); qp.setOptions( options ); qp.setPrintLevel( PL_LOW ); /* III) RUN BENCHMARK SEQUENCE: */ returnValue returnvalue; for( k=0; k<nQP; ++k ) { /* 1) Update pointers to current QP data. */ gCur = &( g[k*nV] ); lbCur = &( lb[k*nV] ); ubCur = &( ub[k*nV] ); /* 2) Set nWSR and maximum CPU time. */ nWSRcur = nWSR; CPUtimeCur = CPUtimeLimit; /* 3) Solve current QP. */ if ( k == 0 ) { /* initialise */ returnvalue = qp.init( H,gCur,lbCur,ubCur, nWSRcur,&CPUtimeCur ); if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) ) { delete H; delete[] y; delete[] x; return THROWERROR( returnvalue ); } } else { /* hotstart */ returnvalue = qp.hotstart( gCur,lbCur,ubCur, nWSRcur,&CPUtimeCur ); if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) ) { delete H; delete[] y; delete[] x; return THROWERROR( returnvalue ); } } /* 4) Obtain solution vectors and objective function value ... */ qp.getPrimalSolution( x ); qp.getDualSolution( y ); /* 5) Compute KKT residuals */ getKKTResidual( nV,0, _H,gCur,0,lbCur,ubCur,0,0, x,y, stat,feas,cmpl ); /* 6) update maximum values. */ if ( nWSRcur > maxNWSR ) maxNWSR = nWSRcur; if (stat > maxStationarity) maxStationarity = stat; if (feas > maxFeasibility) maxFeasibility = feas; if (cmpl > maxComplementarity) maxComplementarity = cmpl; if ( CPUtimeCur > maxCPUtime ) maxCPUtime = CPUtimeCur; } delete H; delete[] y; delete[] x; return SUCCESSFUL_RETURN; }
int main( ) { USING_NAMESPACE_QPOASES long i; int nWSR; real_t tic, toc; real_t errP=0.0, errD=0.0; real_t *x1 = new real_t[180]; real_t *y1 = new real_t[271]; real_t *x2 = new real_t[180]; real_t *y2 = new real_t[271]; /* create sparse matrices */ SymSparseMat *H = new SymSparseMat(180, 180, H_ir, H_jc, H_val); SparseMatrix *A = new SparseMatrix(91, 180, A_ir, A_jc, A_val); H->createDiagInfo(); real_t* H_full = H->full(); real_t* A_full = A->full(); SymDenseMat *Hd = new SymDenseMat(180,180,180,H_full); DenseMatrix *Ad = new DenseMatrix(91,180,180,A_full); /* solve with dense matrices */ nWSR = 1000; QProblem qrecipeD(180, 91); tic = getCPUtime(); qrecipeD.init(Hd, g, Ad, lb, ub, lbA, ubA, nWSR, 0); toc = getCPUtime(); qrecipeD.getPrimalSolution(x1); qrecipeD.getDualSolution(y1); fprintf(stdFile, "Solved dense problem in %d iterations, %.3f seconds.\n", nWSR, toc-tic); /* Compute KKT tolerances */ real_t stat, feas, cmpl; getKKTResidual( 180,91, H_full,g,A_full,lb,ub,lbA,ubA, x1,y1, stat,feas,cmpl ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n\n", stat,feas,cmpl ); /* solve with sparse matrices */ nWSR = 1000; QProblem qrecipeS(180, 91); tic = getCPUtime(); qrecipeS.init(H, g, A, lb, ub, lbA, ubA, nWSR, 0); toc = getCPUtime(); qrecipeS.getPrimalSolution(x2); qrecipeS.getDualSolution(y2); fprintf(stdFile, "Solved sparse problem in %d iterations, %.3f seconds.\n", nWSR, toc-tic); /* Compute KKT tolerances */ real_t stat2, feas2, cmpl2; getKKTResidual( 180,91, H_full,g,A_full,lb,ub,lbA,ubA, x2,y2, stat2,feas2,cmpl2 ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n\n", stat2,feas2,cmpl2 ); /* check distance of solutions */ for (i = 0; i < 180; i++) if (getAbs(x1[i] - x2[i]) > errP) errP = getAbs(x1[i] - x2[i]); fprintf(stdFile, "Primal error: %9.2e\n", errP); for (i = 0; i < 271; i++) if (getAbs(y1[i] - y2[i]) > errD) errD = getAbs(y1[i] - y2[i]); fprintf(stdFile, "Dual error: %9.2e (might not be unique)\n", errD); delete H; delete A; delete[] H_full; delete[] A_full; delete Hd; delete Ad; delete[] y2; delete[] x2; delete[] y1; delete[] x1; QPOASES_TEST_FOR_TRUE( stat <= 1e-15 ); QPOASES_TEST_FOR_TRUE( feas <= 1e-15 ); QPOASES_TEST_FOR_TRUE( cmpl <= 1e-15 ); QPOASES_TEST_FOR_TRUE( stat2 <= 1e-14 ); QPOASES_TEST_FOR_TRUE( feas2 <= 1e-14 ); QPOASES_TEST_FOR_TRUE( cmpl2 <= 1e-13 ); QPOASES_TEST_FOR_TRUE( errP <= 1e-13 ); return TEST_PASSED; }
/** Example for qpOASES main function using the QProblem class. */ int main( ) { USING_NAMESPACE_QPOASES /* Setup data of first QP. */ real_t H[4*4] = { 1.0, 0.0, 0.0, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 1.0 }; real_t A[3*4] = { 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0 }; real_t g[4] = { 1.5, 1.0, -1.0, -1.0 }; real_t lb[4] = { 0.5, -2.0, 0.0, 0.0 }; real_t ub[4] = { 1.0, 2.0, 1.0, 0.5 }; real_t lbA[3] = { -1.0, -1.0, -1.0 }; real_t ubA[3] = { 0.0, 0.25, 1.0 }; /* Setting up QProblem object. */ QProblem example( 4,3 ); Options options; example.setOptions( options ); /* Solve first QP. */ int nWSR = 10; example.init( H,g,A,lb,ub,lbA,ubA, nWSR ); /* Get and print solution of second QP. */ real_t xOpt[4]; real_t yOpt[4+3]; example.getPrimalSolution( xOpt ); example.getDualSolution( yOpt ); print( xOpt,4,"xOpt" ); print( yOpt,4+3,"yOpt" ); printf( "objVal = %e\n\n", example.getObjVal() ); /* Compute KKT tolerances */ real_t stat, feas, cmpl; getKKTResidual( 4,3, H,g,A,lb,ub,lbA,ubA, xOpt,yOpt, stat,feas,cmpl ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl ); QPOASES_TEST_FOR_TRUE( stat <= 1e-15 ); QPOASES_TEST_FOR_TRUE( feas <= 1e-15 ); QPOASES_TEST_FOR_TRUE( cmpl <= 1e-15 ); /* Solve first QP again (with optimal guess for working set). */ Bounds prevBounds; Constraints prevConstraints; example.getBounds( prevBounds ); example.getConstraints( prevConstraints ); nWSR = 10; example.hotstart( g,lb,ub,lbA,ubA, nWSR,0,&prevBounds,&prevConstraints ); /* Get and print solution of second QP. */ example.getPrimalSolution( xOpt ); example.getDualSolution( yOpt ); print( xOpt,4,"xOpt" ); print( yOpt,4+3,"yOpt" ); printf( "objVal = %e\n\n", example.getObjVal() ); /* Compute KKT tolerances */ getKKTResidual( 4,3, H,g,A,lb,ub,lbA,ubA, xOpt,yOpt, stat,feas,cmpl ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl ); QPOASES_TEST_FOR_TRUE( stat <= 1e-15 ); QPOASES_TEST_FOR_TRUE( feas <= 1e-15 ); QPOASES_TEST_FOR_TRUE( cmpl <= 1e-15 ); /* Solve first QP again (with inaccurate guess for working set). */ prevBounds.print(); prevBounds.rotate(1); //prevBounds.moveFixedToFree(0); prevBounds.print(); prevConstraints.print(); //prevConstraints.moveInactiveToActive(0,ST_LOWER); prevConstraints.moveActiveToInactive(1); prevConstraints.print(); nWSR = 10; example.hotstart( g,lb,ub,lbA,ubA, nWSR,0,&prevBounds,&prevConstraints ); /* Get and print solution of second QP. */ example.getPrimalSolution( xOpt ); example.getDualSolution( yOpt ); print( xOpt,4,"xOpt" ); print( yOpt,4+3,"yOpt" ); printf( "objVal = %e\n\n", example.getObjVal() ); /* Compute KKT tolerances */ getKKTResidual( 4,3, H,g,A,lb,ub,lbA,ubA, xOpt,yOpt, stat,feas,cmpl ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl ); QPOASES_TEST_FOR_TRUE( stat <= 1e-15 ); QPOASES_TEST_FOR_TRUE( feas <= 1e-15 ); QPOASES_TEST_FOR_TRUE( cmpl <= 1e-15 ); return TEST_PASSED; }
/** Example for qpOASES main function using the QProblemB class. */ int main( ) { USING_NAMESPACE_QPOASES /* Setup data of first QP. */ real_t H[2*2] = { 1.0, 0.0, 0.0, 0.5 }; real_t g[2] = { 1.5, 1.0 }; real_t lb[2] = { 0.5, -2.0 }; real_t ub[2] = { 5.0, 2.0 }; /* Setup data of second QP. */ real_t g_new[2] = { 1.0, 1.5 }; real_t lb_new[2] = { 0.0, -1.0 }; real_t ub_new[2] = { 5.0, -0.5 }; /* Setting up QProblemB object. */ QProblemB example( 2 ); Options options; //options.enableFlippingBounds = BT_FALSE; options.initialStatusBounds = ST_INACTIVE; options.numRefinementSteps = 1; options.enableCholeskyRefactorisation = 1; example.setOptions( options ); /* Solve first QP. */ int nWSR = 10; example.init( H,g,lb,ub, nWSR,0 ); // printf( "\nnWSR = %d\n\n", nWSR ); real_t xOpt[2]; real_t yOpt[2]; example.getPrimalSolution( xOpt ); example.getDualSolution( yOpt ); /* Compute KKT tolerances */ real_t stat, feas, cmpl; getKKTResidual( 2, H,g,lb,ub, xOpt,yOpt, stat,feas,cmpl ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl ); QPOASES_TEST_FOR_TRUE( stat <= 1e-15 ); QPOASES_TEST_FOR_TRUE( feas <= 1e-15 ); QPOASES_TEST_FOR_TRUE( cmpl <= 1e-15 ); /* Solve second QP. */ nWSR = 10; example.hotstart( g_new,lb_new,ub_new, nWSR,0 ); // printf( "\nnWSR = %d\n\n", nWSR ); /* Get and print solution of second QP. */ example.getPrimalSolution( xOpt ); example.getDualSolution( yOpt ); printf( "\nxOpt = [ %e, %e ]; objVal = %e\n\n", xOpt[0],xOpt[1],example.getObjVal() ); /* Compute KKT tolerances */ getKKTResidual( 2, H,g_new,lb_new,ub_new, xOpt,yOpt, stat,feas,cmpl ); printf( "stat = %e\nfeas = %e\ncmpl = %e\n", stat,feas,cmpl ); QPOASES_TEST_FOR_TRUE( stat <= 1e-15 ); QPOASES_TEST_FOR_TRUE( feas <= 1e-15 ); QPOASES_TEST_FOR_TRUE( cmpl <= 1e-15 ); return TEST_PASSED; }