约束优化 [英] constrained optimization in R

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

问题描述

我想使用 http://rss.acs.unt.edu/ Rdoc / library / stats / html / constrOptim.html 在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} 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

$ b b

所以我为每个约束/边界添加一行:

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 = 0.95和y = 0时得到一个max当我把初始值推到角落,但我有点怀疑,这不是真正的最大值,我会猜到的是在角落。
编辑:
追求这个我推理的梯度可能提供额外的方向,并添加一个梯度函数:

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

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

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