[R] Problems using quadprog for solving quadratic programming problem
Fabian Barth
Fabian.Barth at web.de
Tue Jun 6 12:13:22 CEST 2006
Hi,
I'm using the package quadprog to solve the following quadratic programming problem.
I want to minimize the function
(b_1-b_2)^2+(b_3-b_4)^2
by the following constraints b_i, i=1,...,4:
b_1+b_3=1
b_2+b_4=1
0.1<=b_1<=0.2
0.2<=b_2<=0.4
0.8<=b_3<=0.9
0.6<=b_4<=0.8
In my opinion the solution should be b_1=b_2=0.2 und b_3=b_4=0.8.
Unfortunately R doesn't find this solution and what's surprising to me, evaluation the solution of solve.QP with my function doesn't lead the minimal "value" calculated by solve.QP
I would be very happy, if anyone could help and tell me, where's my mistake. Thank you very much. Fabian
My R-code also containing the sampel of quadprog starts here:
#sample from quadprog package
library(quadprog)
Dmat <- matrix(0,3,3)
diag(Dmat) <- 1
dvec <- c(0,5,0)
Amat <- matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3)
bvec <- c(-8,2,0)
erg<-solve.QP(Dmat,dvec,Amat,bvec=bvec)
print(erg)
erg<-erg$solution
-dvec%*%erg+.5*erg^T%*%Dmat%*%erg
# my "non working" sample
n<-2
k<-2
Dmatj<-diag((n-1),n)
Dmatj[lower.tri(Dmatj)]<--2
Dmat<-matrix(0,nrow=n*k,ncol=n*k)
for(j in 1:k){
Dmat[((j-1)*n+1):(j*n),((j-1)*n+1):(j*n)]<-Dmatj
}
print(Dmat)
Amat<-matrix(ncol=n,nrow=k*n,0)
ind<-seq(1,((k-1)*n+1),n)
for(i in 1:n){
Amat[(ind+(i-1)),i]<-1
}
Amat<-cbind(Amat,diag(n*k),-diag(n*k))
print(Amat)
bvec<-c(rep(1,n),c(0.1,0.2,0.8,0.6,-0.2,-0.4,-0.9,-0.8))
print(bvec)
dvec=rep(0,(n*k))
erg<-solve.QP(Dmat,dvec,Amat,bvec,meq=n)
print(erg)
erg<-erg$solution
-dvec%*%erg+.5*erg^T%*%Dmat%*%erg
More information about the R-help
mailing list