在下面对
(测试版本为R 3.5.0。)
optim()
的调用中,由于maxit=1
,我希望fn()
和gr()
各被评估一次。然而,fn()
和gr()
分别被评估了7次。optim(par=1000, fn=function(x) x^2, gr=function(x) 2*x,
method="L-BFGS-B", control=list(maxit=1))$counts
function gradient
7 7
为什么会这样?是个 bug 吗?或者为什么每次迭代 optim()
要执行 7 次评估?
更详细的输出:
optim(par=1000,
fn=function(x) { cat("f(", x, ")", sep="", fill=TRUE); x^2 },
gr=function(x) { cat("g(", x, ")", sep="", fill=TRUE); 2*x },
method="L-BFGS-B", control=list(maxit=1))$counts
f(1000)
g(1000)
f(999)
g(999)
f(995)
g(995)
f(979)
g(979)
f(915)
g(915)
f(659)
g(659)
f(1.136868e-13)
g(1.136868e-13)
function gradient
7 7
(测试版本为R 3.5.0。)