R中的约束优化 [英] constrained optimization in R

查看:31
本文介绍了R中的约束优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http://rss.acs.unt.edu/Rdoc/library/stats/html/constrOptim.html 在 R 中使用一些给定的线性约束在 R 中进行优化,但无法弄清楚如何设置问题.

I am trying to use http://rss.acs.unt.edu/Rdoc/library/stats/html/constrOptim.html in R to do optimization in R with some given linear constraints but not able to figure out how to set up the problem.

例如,我需要最大化 $f(x,y) = log(x) + frac{x^2}{y^2}$ 受约束 $g_1(x,y) = x+y<1$, $g_2(x,y) = x > 0$ 和 $g_3(x,y) = y > 0$.我如何在 R 中做到这一点?这只是一个假设的例子.不要担心它的结构,相反我很想知道如何在 R 中设置它.

For example, I need to maximize $f(x,y) = log(x) + frac{x^2}{y^2}$ subject to constraints $g_1(x,y) = x+y < 1$, $g_2(x,y) = x > 0$ and $g_3(x,y) = y > 0$. How do I do this in R? This is just a hypothetical example. Do not worry about its structure, instead I am interested to know how to set this up in R.

谢谢!

推荐答案

设置函数很简单:

fr <- function(x) {      x1 <- x[1]
    x2 <- x[2]
    -(log(x1) + x1^2/x2^2)  # need negative since constrOptim is a minimization routine
}

由于缺乏大量文档,设置约束矩阵存在问题,我求助于实验.帮助页面说可行区域由 ui %*% theta - ci >= 0 定义".所以我进行了测试,这似乎有效":

Setting up the constraint matrix was problematic due to a lack of much documentation, and I resorted to experimentation. The help page says "The feasible region is defined by ui %*% theta - ci >= 0". So I tested and this seemed to "work":

> rbind(c(-1,-1),c(1,0), c(0,1) ) %*% c(0.99,0.001) -c(-1,0, 0)
      [,1]
[1,] 0.009
[2,] 0.990
[3,] 0.001

所以我为每个约束/边界排成一行:

So I put in a row for each constraint/boundary:

constrOptim(c(0.99,0.001), fr, NULL, ui=rbind(c(-1,-1),  # the -x-y > -1
                                              c(1,0),    # the x > 0
                                              c(0,1) ),  # the y > 0
                                           ci=c(-1,0, 0)) # the thresholds

对于这个问题,存在一个潜在的困难,即对于 x 的所有值,函数都变为 Inf 为 y -> 0.即使我推送起始值,我也确实得到了 x=.95 和 y=0 附近的最大值到角落",但我有点怀疑这不是我猜到的真正最大值在角落".对此,我推断渐变可能会提供额外的方向"并添加了渐变函数:

For this problem there is a potential difficulty in that for all values of x the function goes to Inf as y -> 0. I do get a max around x=.95 and y=0 even when I push the starting values out to the "corner", but I'm somewhat suspicious that this is not the true maximum which I would have guessed was in the "corner". Pursuing this I reasoned that the gradient might provide additional "direction" and added a gradient function:

grr <- function(x) { ## Gradient of 'fr'
    x1 <- x[1]
    x2 <- x[2]
    c(-(1/x[1] + 2 * x[1]/x[2]^2),
       2 * x[1]^2 /x[2]^3 )
}

这确实引导"优化更接近 c(.999..., 0) 角,而不是像某些起始值那样远离它.当起始值接近可行区域的中心时,这个过程似乎走向悬崖",我仍然有些失望:

This did "steer" the optimization a bit closer to the c(.999..., 0) corner, instead of moving away from it, as it did for some starting values. I remain somewhat disappointed that the process seems to "head for the cliff" when the starting values are close to the center of the feasible region:

 constrOptim(c(0.99,0.001), fr, grr, ui=rbind(c(-1,-1),  # the -x-y > -1
                                               c(1,0),    # the x > 0
                                               c(0,1) ),  # the y > 0
                                            ci=c(-1,0, 0) )
$par
[1]  9.900007e-01 -3.542673e-16

$value
[1] -7.80924e+30

$counts
function gradient 
    2001       37 

$convergence
[1] 11

$message
[1] "Objective function increased at outer iteration 2"

$outer.iterations
[1] 2

$barrier.value
[1] NaN

注意:Hans Werner Borchers 在 R-Help 上发布了一个更好的示例,该示例通过将约束设置为稍微远离边缘来成功获取角值:

Note: Hans Werner Borchers posted a better example on R-Help that succeeded in getting the corner values by setting the constraint slightly away from the edge:

> constrOptim(c(0.25,0.25), fr, NULL, 
              ui=rbind( c(-1,-1), c(1,0),   c(0,1) ),  
              ci=c(-1, 0.0001, 0.0001)) 
$par
[1] 0.9999 0.0001

这篇关于R中的约束优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆