尝试使用optim()进行约束优化时出现错误 [英] Errors when attempting constrained optimisation using optim()

查看:127
本文介绍了尝试使用optim()进行约束优化时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Excel求解器来解决以下问题

I have been using the Excel solver to handle the following problem

求解方程式中的b和c:

solve for a b and c in the equation:

y = a*b*c*x/((1 - c*x)(1 - c*x + b*c*x))

受约束

0 < a < 100
0 < b < 100
0 < c < 100

f(x[1]) < 10
f(x[2]) > 20
f(x[3]) < 40

我有大约10(x,y)个值对.我将abs(y-f(x))的总和最小化.而且,我可以限制函数在每个x处的结果的系数和值的范围.

where I have about 10 (x,y) value pairs. I minimize the sum of abs(y - f(x)). And I can constrain both the coefficients and the range of values for the result of my function at each x.

我尝试了nls(没有尝试施加约束),而Excel提供了我愿意提供的几乎所有初始值的估算值,而nls几乎从未返回答案.

I tried nls (without trying to impose the constraints) and while Excel provided estimates for almost any starting values I cared to provide, nls almost never returned an answer.

我改用了optim,但是在应用约束时遇到了困难.

I switched to using optim, but I'm having trouble applying the constraints.

这是我到目前为止的去处-

This is where I have gotten so far-

best = function(p,x,y){sum(abs(y - p[1]*p[2]*p[3]*x/((1 - p[3]*x)*(1 - p[3]*x + p[2]*p[3]*x))))}
p = c(1,1,1)
x = c(.1,.5,.9)
y = c(5,26,35)
optim(p,best,x=x,y=y)

我这样做是为了添加第一组约束-

I did this to add the first set of constraints-

optim(p,best,x=x,y=y,method="L-BFGS-B",lower=c(0,0,0),upper=c(100,100,100))

我收到错误消息" ERROR:ABNORMAL_TERMINATION_IN_LNSRCH"

I get the error ""ERROR: ABNORMAL_TERMINATION_IN_LNSRCH"

并以较高的错误值($ value)结束.所以看来我做错了什么.我完全不知道如何应用我的其他约束.

and end up with a higher value of the error ($value). So it seems like I am doing something wrong. I couldn't figure out how to apply my other set of constraints at all.

有人可以给我一个基本的想法,如何解决非统计学家可以理解的问题?我看了很多文章,看了几本R书.R书停止使用最简单的优化方法.

Could someone provide me a basic idea how to solve this problem that a non-statistician can understand? I looked at a lot of posts and looked in a few R books. The R books stopped at the simplest use of optim.

推荐答案

绝对值引入了奇异性:您可能要改用正方形,尤其是对于基于梯度的方法(例如L-BFGS).

The absolute value introduces a singularity: you may want to use a square instead, especially for gradient-based methods (such as L-BFGS).

函数的分母可以为零.

参数出现在产品中的事实并且您允许它们为零(任意接近)也会引起问题.

The fact that the parameters appear in products and that you allow them to be (arbitrarily close to) zero can also cause problems.

您可以尝试使用其他优化器(优化任务视图上的完整列表),直到找到优化收敛的目标为止.

You can try with other optimizers (complete list on the optimization task view), until you find one for which the optimization converges.

x0 <- c(.1,.5,.9)
y0 <- c(5,26,35)
p <- c(1,1,1)
lower <- 0*p
upper <- 100 + lower
f <- function(p,x=x0,y=y0) sum( 
  (
    y - p[1]*p[2]*p[3]*x / ( (1 - p[3]*x)*(1 - p[3]*x + p[2]*p[3]*x) ) 
  )^2
)

library(dfoptim)
nmkb(p, f, lower=lower, upper=upper) # Converges

library(Rvmmin)
Rvmmin(p, f, lower=lower, upper=upper) # Does not converge

library(DEoptim)
DEoptim(f, lower, upper) # Does not converge

library(NMOF)
PSopt(f, list(min=lower, max=upper))[c("xbest", "OFvalue")] # Does not really converge
DEopt(f, list(min=lower, max=upper))[c("xbest", "OFvalue")] # Does not really converge

library(minqa)
bobyqa(p, f, lower, upper) # Does not really converge

万不得已,您始终可以使用网格搜索.

As a last resort, you can always use a grid search.

library(NMOF)
r <- gridSearch( f, 
  lapply(seq_along(p), function(i) seq(lower[i],upper[i],length=200)) 
)

这篇关于尝试使用optim()进行约束优化时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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