r - 使用 cvxr 将目标函数中的两个变量相乘
问题描述
我想最小化以下目标函数
有一些限制
另一个用户(我认为是G. Grothendieck)建议使用CVXR
R 的包。
所以我按照A Gentle Introduction to 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 信封的提示。尤其是那部分。
在josliber的回答结束时,他评论说,所有变量都应该有一个界限。在我的约束中没有上限,因此我插入了一个上限。这是一个随意的选择。如果解决方案在边界上,您必须重新计算新的边界......
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()
then 解决相同的目标函数时,我得到和。为了建立代码看看我的另一个问题......
让我们检查一下代码:
# 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...
问题:
- 为什么两个结果不同?
- 在哪些解决方案中我犯了错误?可以指出它们吗?
- 当我从结果中输入 x 和 y 时,我没有得到
$value
替代solve.QP()
方案- 做数学的时候
- 正如你所看到的结果并不重合
- 我在这里做错了吗?!
提前谢谢了!
解决方案
问题在于
- 问题是使用 factorized=TRUE
- 问题顶部的目标函数意味着与问题代码中显示的 dvec[1] 不同。
如果我们省略因式分解(默认为 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] -297
推荐阅读
- java - 将密钥发送到您无法检查的文本框(使用 Java)
- django - 如何在 Docker 容器上运行 Django
- rstudio - 使用面板数据进行固定效应回归 - 输出中不包括虚拟变量
- c# - 创建一个 TopMost 不透明的可点击切换窗口
- c# - 我希望能够使用 id 或用户名验证登录表单,同时使用 c# 从 mysql 数据库获取数据
- uml - 扩展用例规范
- php - Laravel Accessor 或 HasManyThrough 还是其他?
- mysql - 隐藏聊天中比不同表格中的时间戳更早的消息
- css - 如何修复加载页面上的移动菜单?
- laravel - 无法添加,录音