简体   繁体   中英

quadratic programming in R matrix form

I'm using R software quadprog . 在此处输入图片说明

to solve the following optimization:

Dmat <- matrix(c(1,1.5,1.5,5),nrow=2,ncol=2)
dvec <- c(0.5,0)
Amat <- -matrix(c(3,15,2,-3),nrow=2,ncol=2)
bvec <- matrix(c(-2,1),nrow=2,ncol=1)

solve.QP(Dmat,dvec,Amat,bvec)

the solution that I get from solving the above problem is:

$`solution`
[1] -0.2307692  0.1794872

$value
[1] 0.1604208

The correct solution is

$`par`
[1] -0.8064516  0.2096774

$value
[1] -0.04032258

What am I doing wrong?

You have to:

  • double Dmat
  • negate dvec
  • transpose Amat
  • negate bvec

That is:

Dmat <- matrix(c(2,3,3,10),nrow=2,ncol=2)
dvec <- c(-0.5,0)
Amat <- -matrix(c(3,15,2,-3),nrow=2,ncol=2,byrow=TRUE)
bvec <- -matrix(c(-2,1),nrow=2,ncol=1)

> solve.QP(Dmat,dvec,Amat,bvec)
$solution
[1] -0.8064516  0.2096774

$value
[1] -0.04032258

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM