Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving quadratic programming using R

I would like to solve the following quadratic programming equation using ipop function from kernlab :

min 0.5*x'*H*x + f'*x  
subject to:  A*x <= b   
Aeq*x = beq  
LB <= x <= UB 

where in our example H 3x3 matrix, f is 3x1, A is 2x3, b is 2x1, LB and UB are both 3x1.

edit 1 My R code is :

library(kernlab)
H <- rbind(c(1,0,0),c(0,1,0),c(0,0,1))
f = rbind(0,0,0)
A = rbind(c(1,1,1), c(-1,-1,-1))
b = rbind(4.26, -1.73)
LB = rbind(0,0,0)
UB = rbind(100,100,100)
> ipop(f,H,A,b,LB,UB,0)
Error in crossprod(r, q) : non-conformable arguments

I know from matlab that is something like this :

H = eye(3);
f = [0,0,0];
nsamples=3;
eps = (sqrt(nsamples)-1)/sqrt(nsamples);
A=ones(1,nsamples);
A(2,:)=-ones(1,nsamples);
b=[nsamples*(eps+1); nsamples*(eps-1)];

Aeq = [];
beq = [];
LB = zeros(nsamples,1);
UB = ones(nsamples,1).*1000;

[beta,FVAL,EXITFLAG] = quadprog(H,f,A,b,Aeq,beq,LB,UB);

and the answer is a vector of 3x1 equals to [0.57,0.57,0.57];

However when I try it on R, using ipop function from kernlab library ipop(f,H,A,b,LB,UB,0)) and I am facing Error in crossprod(r, q) : non-conformable arguments

I appreciate any comment

like image 795
Areza Avatar asked Sep 09 '12 22:09

Areza


1 Answers

The original question asks about the error message Error in crossprod(r, q) : non-conformable arguments. The answer is that r must be specified with the same dimensions as b. So if b is 2x1 then r must also be 2x1.

A secondary question (from the comments) asks about why the system presented in the original question works in Matlab but not in R. The answer is that R and Matlab specify the problems differently. Matlab allows for inequality constraints to be entered separately from the equality constraints. However in R the constraints must all be of the form b<=Ax<=b+r (at least within the kernlab function ipop). So how may we mimic the original inequality constraints? The simple way is to make b very negative and to make r'=-b+r, where r' is your new r vector. Now we still have the same upper bound on the constraints because r'+b=-b+r+b=r. However we have put a lower bound on the constraints, too. My suggestion is to try solving the system with a few different values for b to see if the solution is consistent.

EDIT:

This is probably a better way to handle solving the program:

library(quadprog);  
dvec <- -f;  
Dmat <- H;  
Amat <- -t(A);  
bvec <- -rbind(4.26,-1.73);  
solve.QP(Dmat, dvec, Amat, bvec)

where these definitions depend on the previously defined R code.

like image 121
assumednormal Avatar answered Nov 14 '22 23:11

assumednormal