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

问题描述

我想最小化以下目标函数

objective_function

有一些限制

constraints

一个用户(我认为是 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提示,它有助于解决双线性公式部分

part_of_OF

的问题。尤其是

part_of_OF

部分。

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 <= 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() 解决相同的目标函数时,我得到

result_x

result_y

。要建立代码,请查看我的其他 question...

让我们检查代码

# Parameters of the objective funtion and the constraints
D=matrix(c(5,7,10),ncol=2,byrow=TRUE)
d=c(-78,-108)
A=matrix(c(1,2,1,1),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()
    • 计算时

      formula_question

    • 正如你所看到的结果不一致
    • 在这里做错了吗?!

非常感谢!

解决方法

问题在于

  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,10),ncol=2,byrow=TRUE)
d=c(-78,-108) 
A=matrix(c(1,2,1,1),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