简体   繁体   English

用R解决约束二次规划

[英]Solve Constrained Quadratic Programming with R

I really love R but from time to time it really gives me a headache... 我真的很喜欢R,但有时它确实让我头疼。

I have the following simple quadratic minimization problem which can be formulated and solved within no time in Excel (click on picture to enlarge): 我有以下简单的二次最小化问题,可以在Excel中立即解决并解决(单击图片放大):

在此处输入图片说明

and

![在此处输入图片描述

The problem itself is pretty straightforward: I want to minimize (w1^2+w2^2)/2 by finding the best combination of w1 , w2 and b under the constraints that for all Y*(w1*X1+w2*X2+b) >= 1 问题本身很简单:我想通过在所有Y*(w1*X1+w2*X2+b) >= 1的约束下找到w1w2b的最佳组合来最小化(w1^2+w2^2)/2 Y*(w1*X1+w2*X2+b) >= 1

I know that there is the quadprog package for solving these kinds of problems but I find it so unintuitive that I am not able to specify the problem correctly :-( I hate to say it but Excel seems to be better suited for specifying optimization problems like these :-((( 我知道有解决此类问题的quadprog软件包,但我发现它不太直观,以至于我无法正确指定问题:-(我不想这么说,但Excel似乎更适合于指定优化问题,例如这些:-(((

My question 我的问题
How to formulate the above problem correctly so that it can be solved with R (no matter which package) and the program arrives at the correct values for w1 , w2 and b (as can be seen in the picture above). 如何正确地表达上述问题,以便可以用R(无论使用哪种包装)解决该问题,并且程序将得出w1w2b的正确值(如上图所示)。 Please don't just post links but please give actual code that works. 请不仅发布链接,而且请提供有效的实际代码。 It would be great if you could comment your code so it becomes clear why you do the things you do. 如果可以注释您的代码,那将很好,这样您就可以清楚为什么要做自己要做的事情。 Thank you! 谢谢!

The necessary data is here: 必要的数据在这里:

data <- matrix(c(2.947814,6.626878, 1,
                 2.530388,7.785050, 1,
                 3.566991,5.651046, 1,
                 3.156983,5.467077, 1,
                 2.582346,4.457777,-1,
                 2.155826,6.222343,-1,
                 3.273418,3.520687,-1),ncol=3,byrow=T)
colnames(data) <- c("X1","X2","y")

Addendum 附录
Some people took offense at my request to provide code (and not simply links). 有些人应我的要求冒犯了提供代码的要求(而不仅仅是提供链接)。 I apologized for that and gave my reasons that I did not find any good approaches in the answers so far on SO. 我为此表示歉意,并给出了我到目前为止在SO答案中找不到任何好的方法的理由。 The deeper reason for that is that the problem is unusual in the sense that b is only in the constraint and not in the objective function. 这样做的更深层原因是,从b仅在约束而非目标函数的意义上讲,这个问题是不寻常的。 So I still think that this question is a good fit for SO. 所以我仍然认为这个问题很适合SO。

Actually, the problem is a little tricky because b is only present in the inequality constraint matrix but not in the objective function. 实际上,这个问题有点棘手,因为b仅出现在不等式约束矩阵中,而没有出现在目标函数中。 Therefore the matrix in the quadratic programming problem is only positive semidefinite but not positive definite. 因此,二次规划问题中的矩阵仅是正半定的,而不是正定的。

My approach is therefore to set the matrix entry corresponding to b to a very small value - in my case 1e-9 . 因此,我的方法是将与b对应的矩阵项设置为非常小的值-在我的情况下为1e-9 Someone else more familiar with such optimization problems might know how to solve the problem properly... 其他更熟悉此类优化问题的人可能知道如何正确解决问题...

Calculate solve.QP input 计算solve.QP输入

c1=data[,"X1"]*data[,"y"]
c2=data[,"X2"]*data[,"y"]

#I use 1e-9 for the b entry
Dmat=matrix(`[<-`(numeric(9),c(1,5,9),c(1,1,1e-9)),3,3)
dvec=rep(0,3)
Amat=cbind(c1,c2,data[,"y"])
bvec=rep(1,nrow(Amat))

Solve with solve.QP solve.QP解决

library(quadprog)
sol=solve.QP(Dmat=Dmat,dvec=dvec,Amat=t(Amat),bvec=bvec)$solution
sol
#[1]   2.903910   1.201258 -14.734964

Same as excel. 与Excel相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM