example1.cpp
Go to the documentation of this file.
00001 // qpOASES testing
00002 
00003 // calculate time
00004 #include <cstdio>
00005 #include <sys/time.h>
00006 #include <unistd.h>
00007 #include <stdarg.h>
00008 
00009 struct __mtimer__ {
00010   double start;
00011   char msg[100];
00012   bool is_sec_print;
00013   __mtimer__(const char* format, ...)
00014   __attribute__((format(printf, 2, 3)))
00015   {
00016     va_list args;
00017     va_start(args, format);
00018     vsnprintf(msg, sizeof(msg), format, args);
00019     va_end(args);
00020 
00021     start = sec();
00022   }
00023   ~__mtimer__() {
00024     fprintf(stderr, "[%s] => %.6f [s]\n", msg, sec() - start);
00025   }
00026   double sec() {
00027     struct timeval tv;
00028     gettimeofday(&tv, NULL);
00029     return tv.tv_sec + tv.tv_usec * 1e-6;
00030   }
00031   operator bool() { return false; }
00032 };
00033 
00034 #define mtimer(...) if(__mtimer__ __b__ = __mtimer__(__VA_ARGS__));else
00035 
00036 
00037 // testing codes
00038 #include <qpOASES.hpp>
00039 
00040 int main( )
00041 {
00042   USING_NAMESPACE_QPOASES
00043     {
00044       printf( "demo-qp1\n");
00045       /* Setup data of first QP. */
00046       real_t H[2*2] = {2,1,1,4};
00047       real_t A[2*1] = {1};
00048       real_t g[2] = {-8,-12};
00049       real_t ub[1*2] = {1e10, 1e10};
00050       real_t lb[1*2] = {-1e10, -1e10};
00051       real_t ubA[1*1] = {1e10};
00052       real_t lbA[1*1] = {-1e10};
00053       /* Setting up QProblem object. */
00054       QProblem example( 2,1 );
00055       Options options;
00056       options.printLevel = PL_NONE;
00057       example.setOptions( options );
00058       /* Solve first QP. */
00059       int nWSR = 10;
00060       getSimpleStatus(example.init( H,g,A,lb,ub,lbA,ubA, nWSR ), BT_FALSE);
00061       /* Get and print solution of second QP. */
00062       real_t xOpt[2];
00063       example.getPrimalSolution( xOpt );
00064       printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n", 
00065               xOpt[0],xOpt[1],example.getObjVal() );
00066       //      example.printOptions();
00067     }
00068   {
00069       printf( "demo-qp2\n");
00070       /* Setup data of first QP. */
00071       real_t H[2*2] = {2,1,1,4};
00072       real_t A[2*2] = { 2.0, 1.0};
00073       real_t g[2] = {-8,-12};
00074       real_t ub[1*2] = {1e10, 1e10};
00075       real_t lb[1*2] = {-1e10, -1e10};
00076       real_t ubA[1*2] = {2.0, 2.0};
00077       real_t lbA[1*2] = {2.0,2.0};
00078       /* Setting up QProblem object. */
00079       QProblem example( 2,1 );
00080       Options options;
00081       options.printLevel = PL_NONE;
00082       example.setOptions( options );
00083       /* Solve first QP. */
00084       int nWSR = 10;
00085       getSimpleStatus(example.init( H,g,A,lb,ub,lbA,ubA, nWSR ), BT_FALSE);
00086       /* Get and print solution of second QP. */
00087       real_t xOpt[2];
00088       example.getPrimalSolution( xOpt );
00089       printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n", 
00090               xOpt[0],xOpt[1],example.getObjVal() );
00091       //      example.printOptions();
00092   }
00093   {
00094     printf( "demo-qp3\n");
00095     /* Setup data of first QP. */
00096     real_t H[2*2] = {4,1,1,2};
00097     real_t A[2*1] = {1, 2};
00098     real_t g[2] = {-3, -4};
00099     real_t ub[1*2] = {1e10, 1e10};
00100     real_t lb[1*2] = {0,0};
00101     real_t ubA[1*1] = {1};
00102     real_t lbA[1*1] = {1};
00103     /* Setting up QProblem object. */
00104     QProblem example( 2,1 );
00105     Options options;
00106     options.printLevel = PL_NONE;
00107     example.setOptions( options );
00108     /* Solve first QP. */
00109     int nWSR = 10;
00110     example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
00111     /* Get and print solution of second QP. */
00112     real_t xOpt[2];
00113     example.getPrimalSolution( xOpt );
00114     printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n", 
00115             xOpt[0],xOpt[1],example.getObjVal() );
00116     //    example.printOptions();
00117   }
00118 
00119   {
00120     printf( "demo-qp3, use hotstart\n");
00121     real_t H[2*2] = {4,1,1,2};
00122     real_t A[2*1] = {1, 2};
00123     real_t g[2] = {-3, -4};
00124     real_t ub[1*2] = {1e10, 1e10};
00125     real_t lb[1*2] = {0,0};
00126     real_t ubA[1*1] = {1};
00127     real_t lbA[1*1] = {1};
00128     printf( "  init\n");
00129     mtimer("init x 10 time") {
00130       for (size_t i = 0; i < 10; i++) {
00131         QProblem example( 2,1 );
00132         Options options;
00133         options.printLevel = PL_NONE;
00134         example.setOptions( options );
00135         int nWSR = 10;
00136         example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
00137         real_t xOpt[2];
00138         example.getPrimalSolution( xOpt );
00139         printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n", 
00140                 xOpt[0],xOpt[1],example.getObjVal() );
00141       }
00142     }
00143     printf( "  hotstart\n");
00144     QProblem example( 2,1 );
00145     Options options;
00146     options.printLevel = PL_NONE;
00147     example.setOptions( options );
00148     int nWSR = 10;
00149     example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
00150     real_t xOpt[2];
00151     example.getPrimalSolution( xOpt );
00152     printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n", 
00153             xOpt[0],xOpt[1],example.getObjVal() );
00154     mtimer("hotstart x 10 time") {
00155       for (size_t i = 0; i < 10; i++) {
00156         int nWSR2 = 10;
00157         example.hotstart(g,lb,ub,lbA,ubA, nWSR2 );
00158         example.getPrimalSolution( xOpt );
00159         printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n", 
00160                 xOpt[0],xOpt[1],example.getObjVal() );
00161       }
00162     }
00163   }
00164 
00165   {
00166     printf( "demo-qp3, use SQP init and hotstart\n");
00167     real_t H[2*2] = {4,1,1,2};
00168     real_t A[2*1] = {1, 2};
00169     real_t g[2] = {-3, -4};
00170     real_t ub[1*2] = {1e10, 1e10};
00171     real_t lb[1*2] = {0,0};
00172     real_t ubA[1*1] = {1};
00173     real_t lbA[1*1] = {1};
00174 
00175     SQProblem example( 2,1 );
00176     Options options;
00177     options.printLevel = PL_NONE;
00178     example.setOptions( options );
00179     mtimer("init x 10 time") {
00180       for (size_t i = 0; i < 10; i++) {
00181         int nWSR = 10;
00182         example.init( H,g,A,lb,ub,lbA,ubA, nWSR);
00183         real_t xOpt[2];
00184         example.getPrimalSolution( xOpt );
00185         printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n",
00186                 xOpt[0],xOpt[1],example.getObjVal() );
00187       }
00188     }
00189     mtimer("hotstart x 10 time") {
00190       for (size_t i = 0; i < 10; i++) {
00191         int nWSR = 10;
00192         example.hotstart( H,g,A,lb,ub,lbA,ubA, nWSR);
00193         real_t xOpt[2];
00194         example.getPrimalSolution( xOpt );
00195         printf( "  xOpt = [ %f, %f ]; objVal = %f\n\n",
00196                 xOpt[0],xOpt[1],example.getObjVal() );
00197       }
00198     }
00199   }
00200   return 0;
00201 }
00202 
00203 
00204 /*
00205  *      end of file
00206  */


eus_qpoases
Author(s):
autogenerated on Fri Apr 19 2019 03:45:19