R误差QP:不是正定矩阵?

时间:2017-03-20 21:05:44

标签: r quadprog

我试图使用quadprog软件包解决R中的以下问题:

min: vec %*% p + t(p) %*% mat %*% p 
st: p >= 0

,其中

mat <- matrix(c(1162296,0,0,0,0,1,0,0,951.7089,0,1,0,-951.7089,0,0,1),4)
vec <- c(6341934541.1,175800.1,-356401.7,14398073047.1)

我已经

libary(quadprog)
solve.QP(2*mat,-vec, diag(4), integer(4))

但我一直收到以下错误:

Error in solve.QP(2*mat, -vec, diag(4), integer(4)) : 
  matrix D in quadratic function is not positive definite!

然而,谨慎

> eigen(mat)$values > 0
[1] TRUE TRUE TRUE TRUE

我做错了什么?为什么这个错误不断出现?

1 个答案:

答案 0 :(得分:0)

您的矩阵mat不对称。 quadprog包旨在解决二次规划,根据定义,它需要最高阶项中的对称矩阵。例如,请参阅here

要解决此问题,您需要使用通用约束优化算法。例如,您可以尝试constrOptim

# system matrices
mat <- matrix(c(1162296,0,0,0,0,1,0,0,951.7089,0,1,0,-951.7089,0,0,1),4)
vec <- c(6341934541.1,175800.1,-356401.7,14398073047.1)

# an initial value
p0 <- c(1,1,1,1)

# the objective function
objective <- function(p) {
  vec %*% p + t(p) %*% mat %*% p
}

# solve -- warning!  without additional work you won't know if this is a global minimum solution.
solution <- constrOptim(p0, objective, NULL, diag(4), c(0,0,0,0))