example.cpp
Go to the documentation of this file.
00001 /* file exmaple.C
00002 
00003  This file is an example on how eiquadprog can be used
00004  by invoking solve_quadprog() function
00005 
00006  In order to compile this example, Eigen library must be installed
00007  on your system.
00008  
00009  The test problem is the following:
00010  
00011  Given:
00012  G =  2.1 0.0 1.0   g0^T = [6.0 1.0 1.0]
00013       1.5 2.2 0.0      
00014       1.2 1.3 3.1 
00015  Solve:
00016  min f(x) = 1/2 x G x + g0 x
00017  s.t.
00018    x_1 + 2*x_2 + x_3 + -4 = 0
00019 
00020    x_1 >= 0
00021    x_2 >= 0
00022    x_3 >= 0     
00023    -x_1 - x_2 >= -10
00024  
00025  The solution is x^T = [0 2 0] and f(x) = 6.4
00026  
00027  LICENSE
00028  
00029  Copyright (2010) Gael Guennebaud
00030  Copyright (2008) Angelo Furfaro
00031  
00032 
00033 
00034 This file is a part of eiquadprog. 
00035 
00036 uquadprog is free software; you can redistribute it and/or modify
00037 it under the terms of the GNU General Public License as published by
00038 the Free Software Foundation; either version 2 of the License, or
00039 (at your option) any later version.
00040 
00041 uquadprog is distributed in the hope that it will be useful,
00042 but WITHOUT ANY WARRANTY; without even the implied warranty of
00043 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00044 GNU General Public License for more details.
00045 
00046 You should have received a copy of the GNU General Public License
00047 along with uquadprog; if not, write to the Free Software
00048 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00049 */
00050 
00051 
00052 
00053 
00054 #include <iostream>
00055 #include <Eigen/Dense>
00056 
00057 #include "eiquadprog.hpp"
00058 #include "qp_lib.cpp"
00059 
00060 using namespace Eigen;
00061 
00062 template<typename Vec, typename Mat> void foo() {
00063   Mat G(3,3); 
00064   Vec g0(3);
00065   Mat CE(3,1);
00066   Vec ce0(1);
00067   Mat CI(3,4); 
00068   Vec ci0(4);
00069   Vec x(3);
00070 
00071   
00072   G(0,0)=2.1; G(0,1)=0.0; G(0,2)=1.0;
00073   G(1,0)=1.5; G(1,1)=2.2; G(1,2)=0.0;
00074   G(2,0)=1.2; G(2,1)=1.3; G(2,2)=3.1;
00075   
00076   
00077   g0(0)=6.0; g0(1)=1.0; g0(2)=1.0;
00078 
00079   CE(0,0)=1.0;  
00080   CE(1,0)=2.0;  
00081   CE(2,0)=-1.0; 
00082   
00083   ce0(0)=-4;
00084 
00085   CI(0,0)=1.0; CI(0,1)=0.0;CI(0,2)=0.0; CI(0,3)=-1.0;
00086   CI(1,0)=0.0; CI(1,1)=1.0;CI(1,2)=0.0; CI(1,3)=-1.0;
00087   CI(2,0)=0.0; CI(2,1)=0.0;CI(2,2)=1.0; CI(2,3)=0.0;
00088 
00089 
00090   ci0(0)=0.0; ci0(1)=0.0;ci0(2)=0.0; ci0(3)=10.0;
00091 
00092 
00093   std::cout << "f: " ;
00094   std::cout << solve_quadprog(G, g0,  CE, ce0,  CI, ci0, x) ;
00095   std::cout << std::endl;
00096   std::cout << "x: ";
00097   for (int i = 0; i < x.size(); i++)
00098     std::cout << x(i) << ' ';
00099   std::cout << std::endl;
00100 }
00101 
00102 void bar() {
00103   double g0[3];
00104   double G[3*3] ;
00105   double CE[1*3];
00106   double ce0[1];
00107   double CI[4*3];
00108   double ci0[4];
00109   double x[3];
00110   double ret_buf[1];
00111 
00112   int x_len = 3;
00113   int ce_len = 1;
00114   int ci_len = 4;
00115 
00116   g0[0]=6.0; g0[1]=1.0; g0[2]=1.0;
00117 
00118   CE[0]=1.0;
00119   CE[1]=2.0;
00120   CE[2]=-1.0;
00121 
00122   ce0[0]=-4;
00123 
00124   G[0]=2.1; G[3]=0.0; G[6]=1.0;
00125   G[1]=1.5; G[4]=2.2; G[7]=0.0;
00126   G[2]=1.2; G[5]=1.3; G[8]=3.1;
00127 
00128   CI[0]=1.0; CI[3]=0.0; CI[6]=0.0; CI[9]=-1.0;
00129   CI[1]=0.0; CI[4]=1.0; CI[7]=0.0; CI[10]=-1.0;
00130   CI[2]=0.0; CI[5]=0.0; CI[8]=1.0; CI[11]=0.0;
00131 
00132   ci0[0]=0.0; ci0[1]=0.0; ci0[2]=0.0; ci0[3]=10.0;
00133 
00134   double ce_err[ce_len] ;
00135   double ci_err[ci_len] ;
00136   solve_eiquadprog(G, g0, CE, ce0, CI, ci0, x,
00137                 x_len, ce_len, ci_len, 1e-1,
00138                 2, ret_buf, ce_err, ci_err) ;
00139 }
00140 
00141 int main(int argc, char** argv){
00142         
00143   foo<Eigen::VectorXd, MatrixXd>();
00144   std::cout << "----------------------------" << std::endl ;
00145   bar();
00146 }
00147 


eus_qp
Author(s):
autogenerated on Wed Jul 19 2017 02:54:18