example.cpp
Go to the documentation of this file.
1 /* file exmaple.C
2 
3  This file is an example on how eiquadprog can be used
4  by invoking solve_quadprog() function
5 
6  In order to compile this example, Eigen library must be installed
7  on your system.
8 
9  The test problem is the following:
10 
11  Given:
12  G = 2.1 0.0 1.0 g0^T = [6.0 1.0 1.0]
13  1.5 2.2 0.0
14  1.2 1.3 3.1
15  Solve:
16  min f(x) = 1/2 x G x + g0 x
17  s.t.
18  x_1 + 2*x_2 + x_3 + -4 = 0
19 
20  x_1 >= 0
21  x_2 >= 0
22  x_3 >= 0
23  -x_1 - x_2 >= -10
24 
25  The solution is x^T = [0 2 0] and f(x) = 6.4
26 
27  LICENSE
28 
29  Copyright (2010) Gael Guennebaud
30  Copyright (2008) Angelo Furfaro
31 
32 
33 
34 This file is a part of eiquadprog.
35 
36 uquadprog is free software; you can redistribute it and/or modify
37 it under the terms of the GNU General Public License as published by
38 the Free Software Foundation; either version 2 of the License, or
39 (at your option) any later version.
40 
41 uquadprog is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 GNU General Public License for more details.
45 
46 You should have received a copy of the GNU General Public License
47 along with uquadprog; if not, write to the Free Software
48 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
49 */
50 
51 
52 
53 
54 #include <iostream>
55 #include <Eigen/Dense>
56 
57 #include "eiquadprog.hpp"
58 #include "qp_lib.cpp"
59 
60 using namespace Eigen;
61 
62 template<typename Vec, typename Mat> void foo() {
63  Mat G(3,3);
64  Vec g0(3);
65  Mat CE(3,1);
66  Vec ce0(1);
67  Mat CI(3,4);
68  Vec ci0(4);
69  Vec x(3);
70 
71 
72  G(0,0)=2.1; G(0,1)=0.0; G(0,2)=1.0;
73  G(1,0)=1.5; G(1,1)=2.2; G(1,2)=0.0;
74  G(2,0)=1.2; G(2,1)=1.3; G(2,2)=3.1;
75 
76 
77  g0(0)=6.0; g0(1)=1.0; g0(2)=1.0;
78 
79  CE(0,0)=1.0;
80  CE(1,0)=2.0;
81  CE(2,0)=-1.0;
82 
83  ce0(0)=-4;
84 
85  CI(0,0)=1.0; CI(0,1)=0.0;CI(0,2)=0.0; CI(0,3)=-1.0;
86  CI(1,0)=0.0; CI(1,1)=1.0;CI(1,2)=0.0; CI(1,3)=-1.0;
87  CI(2,0)=0.0; CI(2,1)=0.0;CI(2,2)=1.0; CI(2,3)=0.0;
88 
89 
90  ci0(0)=0.0; ci0(1)=0.0;ci0(2)=0.0; ci0(3)=10.0;
91 
92 
93  std::cout << "f: " ;
94  std::cout << solve_quadprog(G, g0, CE, ce0, CI, ci0, x) ;
95  std::cout << std::endl;
96  std::cout << "x: ";
97  for (int i = 0; i < x.size(); i++)
98  std::cout << x(i) << ' ';
99  std::cout << std::endl;
100 }
101 
102 void bar() {
103  double g0[3];
104  double G[3*3] ;
105  double CE[1*3];
106  double ce0[1];
107  double CI[4*3];
108  double ci0[4];
109  double x[3];
110  double ret_buf[1];
111 
112  int x_len = 3;
113  int ce_len = 1;
114  int ci_len = 4;
115 
116  g0[0]=6.0; g0[1]=1.0; g0[2]=1.0;
117 
118  CE[0]=1.0;
119  CE[1]=2.0;
120  CE[2]=-1.0;
121 
122  ce0[0]=-4;
123 
124  G[0]=2.1; G[3]=0.0; G[6]=1.0;
125  G[1]=1.5; G[4]=2.2; G[7]=0.0;
126  G[2]=1.2; G[5]=1.3; G[8]=3.1;
127 
128  CI[0]=1.0; CI[3]=0.0; CI[6]=0.0; CI[9]=-1.0;
129  CI[1]=0.0; CI[4]=1.0; CI[7]=0.0; CI[10]=-1.0;
130  CI[2]=0.0; CI[5]=0.0; CI[8]=1.0; CI[11]=0.0;
131 
132  ci0[0]=0.0; ci0[1]=0.0; ci0[2]=0.0; ci0[3]=10.0;
133 
134  double ce_err[ce_len] ;
135  double ci_err[ci_len] ;
136  solve_eiquadprog(G, g0, CE, ce0, CI, ci0, x,
137  x_len, ce_len, ci_len, 1e-1,
138  2, ret_buf, ce_err, ci_err) ;
139 }
140 
141 int main(int argc, char** argv){
142 
143  foo<Eigen::VectorXd, MatrixXd>();
144  std::cout << "----------------------------" << std::endl ;
145  bar();
146 }
147 
double solve_quadprog(MatrixXd &G, VectorXd &g0, const MatrixXd &CE, const VectorXd &ce0, const MatrixXd &CI, const VectorXd &ci0, VectorXd &x)
Definition: eiquadprog.hpp:131
double * solve_eiquadprog(double *G, double *g0, double *CE, double *ce0, double *CI, double *ci0, double *x, int x_len, int ce_len, int ci_len, double eqthre, int debug, double *ret_buf, double *ce_err, double *ci_err)
Definition: qp_lib.cpp:45
int main(int argc, char **argv)
Definition: example.cpp:141
void bar()
Definition: example.cpp:102
void foo()
Definition: example.cpp:62


eus_qp
Author(s):
autogenerated on Fri May 14 2021 02:51:43