Search code examples
rquadratic-programming

quadratic programming in R matrix form


I'm using R software quadprog. enter image description here

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?


Solution

  • 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