首页 > 解决方案 > 使用 cvxr 将目标函数中的两个变量相乘

问题描述

我想最小化以下目标函数

目标函数

有一些限制

约束

另一个用户(我认为是G. Grothendieck)建议使用CVXRR 的包。

所以我按照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 信封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 >= 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 解决相同的目标函数时,我得到结果_x结果_y。为了建立代码看看我的另一个问题......

让我们检查一下代码:

# 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...

问题

提前谢谢了!

标签: rmathematical-optimizationcvxcvxr

解决方案


问题在于

  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

推荐阅读