我要最小化以下目标函数

有一些约束

另一个用户(我认为是G.格罗森迪克)建议使用R的CVXR包。
因此,我按照CVXR简介上的说明来编写代码
library(CVXR) # if necessary
x <- Variable(1)
y <- Variable(1)
objective <- Minimize(5*x^2 + 14*x*y + 10*y^2 -76*x -108*y +292)
constraints <- list(x >= 0, y >= 0, x + 2*y <=10, x + y<=6)
prob_OF <- Problem(objective, constraints)
solution_OF <- solve(prob_OF) # and here the error occured
## Error in construct_intermediate_chain(object, candidate_solvers, gp = gp): Problem does not follow DCP rules.在如何将二次规划转换成线性规划?上,我发现了对McCormick信封的提示,它有助于解决双线性公式部分的问题。

。特别是

部分。
在约斯利伯答案的末尾,他指出,所有变量都应该有一个界。在我的约束中没有上限,因此我插入了一个上限。这是个武断的选择。如果解在边界上,你必须用新的界重新计算.
library(CVXR) # if necessary
x <- Variable(1)
y <- Variable(1)
w <- Variable(1)
objective <- Minimize(5*x^2 + 14*w + 10*y^2 -76*x -108*y +292)
constraints <- list(x >= 0, x <= 100,
y >= 0, y <= 100,
x+2*y <= 10,
x+y <= 6,
w >= 0, w >= 100*x + 100*y - 10000, # constraints according to McCormick envelopes
w <= 100*y, w <= 100*x) # constraints according to McCormick envelopes
prob_OF <- Problem(objective, constraints)
solution_OF <- solve(prob_OF)
solution_OF$value
## -125.0667
solution_OF$getValue(x)
## 2.933333
solution_OF$getValue(y)
## 3.066667
solution_OF$getValue(w)
## 1.000135e-30这里的解决方案不是我所期望的..。当我用solve.QP()求解相同的目标函数时,我得到

和

。为了建立代码看看我的另一个问题..。
让我们检查一下代码:
# Parameters of the objective funtion and the constraints
D=matrix(c(5,7,7,10),ncol=2,byrow=TRUE)
d=c(-78,-108)
A=matrix(c(1,2,1,1),ncol=2,byrow=TRUE)
b=c(10,6)
# Convert the parameters to an appropriate state of solve.QP()
Dmat=2*D
dvec=-d
Amat=-t(A)
bvec=-b
# load the package and run solve.QP()
library(quadprog)
solve.QP(Dmat,dvec,Amat,bvec,meq=0,factorized=TRUE)
## $solution
## [1] 2 4 # these are the x and y results
##
## $value
## -587.9768
##
## and some more results...问题
$value的solve.QP() alternatives 
- And as you can see the results don't coincide
- Am I doing here an mistake?!事先非常感谢!
发布于 2021-06-16 14:32:26
问题是
如果我们省略因式分解(默认值为FALSE),并且在定义Dmat、dvec、Amat和bvec的问题中始终使用代码,那么我们从solve.QP获得一致的结果,并根据解决向量手动计算目标,两者都是-297。从问题中逐字复制直到######的代码。
# Parameters of the objective funtion and the constraints
D=matrix(c(5,7,7,10),ncol=2,byrow=TRUE)
d=c(-78,-108)
A=matrix(c(1,2,1,1),ncol=2,byrow=TRUE)
b=c(10,6)
# Convert the parameters to an appropriate state of solve.QP()
Dmat=2*D
dvec=-d
Amat=-t(A)
bvec=-b
######
library(quadprog)
ans <- solve.QP(Dmat,dvec,Amat,bvec)
str(ans)
## List of 6
## $ solution : num [1:2] 3 3
## $ value : num -297
## ...snip...
# manual calculation of objective function
t(ans$solution) %*% Dmat %*% ans$solution / 2 - dvec %*% ans$solution
## [,1]
## [1,] -297
# another manual calculation - coefficients come from Dmat and dvec
f <- function(x, y) (10 * x^2 + 2 * 14 * x * y + 20 * y^2) / 2 - (78 * x + 108 * y)
f(3, 3)
## [1] -297https://stackoverflow.com/questions/67986607
复制相似问题