【问题标题】:Multiply two variables in an objective function using cvxr使用 cvxr 将目标函数中的两个变量相乘
【发布时间】:2021-08-31 07:48:24
【问题描述】:

我想最小化以下目标函数

有一些限制

另一位用户(我认为是G. Grothendieck)建议使用R 的CVXR 包。

所以我按照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.

How to convert quadratic to linear program? 上,我发现McCormick envelopes 的提示有助于解决双线性公式部分 的问题。尤其是 部分。

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() 解决相同的目标函数时,我得到 和。要建立代码,请查看我的其他 question...

让我们检查一下代码:

# 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 时,我没有得到 solve.QP() 替代项中的 $value
    • 做数学时
    • 如您所见,结果并不重合
    • 我在这里做错了吗?!

非常感谢!

【问题讨论】:

  • 我在评论中的建议是您查看solve.、QP 和优化任务视图的示例。当我在单独的评论中提到 CVXR 时,它是在我经常使用的优化包的上下文中,而不是在它必然适合您的问题的上下文中。关于 McCormick,它不是一个等价的表述;它是原始问题的松弛,其解决方案给出了原始问题的下限。下限可能等于原始问题的解决方案,但不能保证。
  • @G。 Grothendieck:我是优化主题的新手,因此如果有人向我推荐他经常使用的求解器,我会很高兴。我知道还有其他可能更适合的求解器...感谢您的提示,McCormick 是一种放松!然后我想我必须像 josliber 提到的那样将我的界限分成多个间隔来获得确切的值。你能解释一下为什么solve.QP()$solution 会在-587.9768 中产生吗?当我将x=2y=4 放入目标函数时,我得到0
  • @G。 Grothendieck:我认为正确的值是x=3y=3。也许工作簿的任务是错误的,因为这里是x=2y=4。但有一件事我仍然想知道:为什么目标函数中的结果是f(x=3,y=3)=-291solver.QP$value=-297?这是由舍入错误引起的吗?!省略meqfactorized 时会出现这两个结果。
  • @G。格洛腾迪克:是的,确实如此,但采用目标函数f(x,y)=5*x^2 + 14*x*y + 10*y^2 -76*x -108*y=5*3^2 + 14*3*3 + 10*3^2 -76*3 -108*3=-291
  • 我已将评论移至答案。

标签: r mathematical-optimization cvx cvxr


【解决方案1】:

问题是

  1. 问题是使用 factorized=TRUE
  2. 问题顶部的目标函数意味着与问题代码中显示的 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

【讨论】:

  • 如上所述:是的,确实如此,但是采用我的问题顶部所示的目标函数 - 这是结果:f(x,y)=5*x^2 + 14*x*y + 10*y^2 -76*x -108*y=5*3^2 + 14*3*3 + 10*3^2 -76*3 -108*3=-291。为什么结果分别不同 为什么不是所有三种方法都会导致相同的结果? 1) solve.QP(Dmat,dvec,Amat,bvec)$value=-297 2) t(ans$solution) %*% Dmat %*% ans$solution / 2 - dvec %*% ans$solution=-2973) f(3,3)=5*3^2 + 14*3*3 + 10*3^2 -76*3 -108*3=-291
  • 你为什么选择78*x 而不是76*x?这里出现了6的区别...
  • 我已经规定了:-/ 当您正确看待我的问题时,我使用 7678... 我的错。谢谢你的一切!
  • 已将我的评论移至答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多