This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Problem with QP example from quadprog.

Tags: None
(comma "," separated)
vvakk
Registered Member
Posts
2
Karma
0
So I ran the code http://www.labri.fr/perso/guenneba/code/QuadProg/ (changing internal:: by std::) and it all works as expected: I get

Code: Select all
f: 6.4
x: 4.23793e-17 2 -7.67063e-17


Now, I try to be a bit bolder and run the example here http://www.inside-r.org/packages/cran/quadprog/docs/solve.QP, which is:

min! -(0 5 0) %*% b + 1/2 b^T b
u.c.: A^T b >= b0
with b0 = (-8,2,0)^T
and
....(-4 2 0)
A=(-3 1 -2)
....( 0 0 1)

(a better edited formulation of the toy problem is at the link above)

(removing the copyrights to save space), I think, this gives the following code:

#include <iostream>
#include <Eigen/Dense>
#include "eiquadprog.hpp"

using namespace Eigen;

template<typename Vec, typename Mat> void foo() {
Mat G(3,3);
Vec g0(3);
Mat CE(3,1);
Vec ce0(1);
Mat CI(3,3);
Vec ci0(3);
Vec x(3);



G(0,0)=1.0; G(0,1)=0.0; G(0,2)=0.0;
G(1,0)=0.0; G(1,1)=1.0; G(1,2)=0.0;
G(2,0)=0.0; G(2,1)=0.0; G(2,2)=1.0;


g0(0)=0.0;g0(1)=-5.0;g0(2)=0.0;

CE(0,0)=0.0;
CE(1,0)=0.0;
CE(2,0)=0.0;

ce0(0)=0.0;

CI(0,0)=-4.0;CI(0,1)=2.0;CI(0,2)=0.0;
CI(1,0)=-3.0;CI(1,1)=1.0;CI(1,2)=-2.0;
CI(2,0)=0.0;CI(2,1)=0.0;CI(2,2)=1.0;

ci0(0)=8.0;ci0(1)=-2.0;ci0(2)=0.0;


std::cout << "f: " << solve_quadprog(G, g0, CE, ce0, CI, ci0, x) << std::endl;
std::cout << "x: ";
for (int i = 0; i < x.size(); i++)
std::cout << x(i) << ' ';
std::cout << std::endl;
}

int main(int argc, char** argv){

foo<Eigen::VectorXd,MatrixXd>();

}



But when I run this I get
Code: Select all
f: -12.5
x: 0 5 0
 


where the correct solution should be:

Code: Select all
f: -2.380952
x:  0.4761905 1.0476190 2.0952381


I tried transposing the CI matrix in the eigen-example code
but could still not get the same solutions as those from the quadprog example.
Probably I'm setting the problem wrongly and I was wondering whether someone
has encountered a similar problem setting up a quadprog code in
eigquadprog and could point to what I am doing wrong.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
CE and ce0 should be null:

CE.resize(3,0);
ce0.resize(0);

then set the other entries as follows:
Code: Select all
G.setIdentity();
g0 << 0, -5, 0;
CI.resize(3,3); 
CI << -4, 2, 0,
        -3, 1, -2,
        0, 0, 1;
ci0.resize(3);
ci0 << 8, -2, 0;
solve_quadprog(G, g0,  CE, ce0,  CI, ci0, x)


btw, I've updated the eiquadprog.hpp file to work with eigen 3.2.
vvakk
Registered Member
Posts
2
Karma
0
Thanks a lot!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]