具有无限值的优化函数 [英] optim function with infinite value

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

问题描述

为了最小化3个参数的功能,我使用了优化函数和"L-BFGS-B"方法. 这是错误消息:

To minimize a function with respect to 3 parameters, I use the optim function and "L-BFGS-B" method. Here is the error message :

optim(c(3,0.01,0.75),fn = f,lower = c(0.6,0.0001,0.1),错误: L-BFGS-B需要有限的'fn'

Error in optim(c(3, 0.01, 0.75), fn = f, lower = c(0.6, 0.0001, 0.1), : L-BFGS-B needs finite values of 'fn'

当三个参数中的至少一个到达边界"时(即下限值或上限值),我已经检查了函数的值,但从未给出无限值.

I already checked the values of the function when at least one of the three parameters reaches the "border" (ie the lower or the upper values) but it never gives an infinite value.

如何知道优化函数中哪些值赋予了无限值?

How to know which values gave an infinite value in the optim function?

推荐答案

一种快速而肮脏的方法是包装函数并仅打印出传递给它的参数.这是执行此操作的示例会话.

A quick and dirty way would be to wrap your function and just print out the parameters being passed to it. Here is an example session of doing that.

f <- function(par, data){
    if(0.1 < par & par < .9){
        return(Inf)
    }else{
        sqrt(abs(par))
    }
}

并使用优化来调用它...

and call it using optim...

> optim(2, f, method = "L-BFGS-B", lower = -1)
Error in optim(2, f, method = "L-BFGS-B", lower = -1) : 
  L-BFGS-B needs finite values of 'fn'

现在包装代码...

wrap_f <- function(par, ...){
    cat(par, "\n")
    f(par, ...)
}

现在我们可以看到被称为...

And now we can see what is being called...

> optim(2, wrap_f, method = "L-BFGS-B", lower = -1)
2 
2.001 
1.999 
1.646447 
1.647447 
1.645447 
1.256777 
1.257777 
1.255777 
-0.3018999 
-0.3008999 
-0.3028999 
0.7441084 
Error in optim(2, wrap_f, method = "L-BFGS-B", lower = -1) : 
  L-BFGS-B needs finite values of 'fn'

因此,在此示例中,它最终尝试评估.7441084,该错误给出了函数定义方式的错误.

So in this example it ended up trying to evaluate .7441084 which gave an error by how the function was defined.

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

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