首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用cvxr将目标函数中的两个变量相乘

用cvxr将目标函数中的两个变量相乘
EN

Stack Overflow用户
提问于 2021-06-15 12:49:52
回答 1查看 363关注 0票数 1

我要最小化以下目标函数

有一些约束

另一个用户(我认为是G.格罗森迪克)建议使用R的CVXR包。

因此,我按照CVXR简介上的说明来编写代码

代码语言:javascript
复制
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信封的提示,它有助于解决双线性公式部分的问题。

。特别是

部分。

约斯利伯答案的末尾,他指出,所有变量都应该有一个界。在我的约束中没有上限,因此我插入了一个上限。这是个武断的选择。如果解在边界上,你必须用新的界重新计算.

代码语言:javascript
复制
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()求解相同的目标函数时,我得到

。为了建立代码看看我的另一个问题..。

让我们检查一下代码:

代码语言:javascript
复制
# 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时,我没有得到$valuesolve.QP() alternatives
    • 做数学的时候

代码语言:javascript
复制
- And as you can see the results don't coincide
- Am I doing here an mistake?!

事先非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-16 14:32:26

问题是

  1. 问题是使用factorized=TRUE
  2. 问题顶部的目标函数意味着与问题中的代码中显示的dvec1不同。

如果我们省略因式分解(默认值为FALSE),并且在定义Dmat、dvec、Amat和bvec的问题中始终使用代码,那么我们从solve.QP获得一致的结果,并根据解决向量手动计算目标,两者都是-297。从问题中逐字复制直到######的代码。

代码语言:javascript
复制
# 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67986607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档