[英]Optimization in R: Levenberg-Marquardt using nls.lm in minpack.lm: resetting `maxiter' to 1024
I am trying to learn how to work with nls.lm
in the R library minpack.lm
by using the Rosenbrock function to see if the algorithm converges to the global minimum at f(x,y) = (1,1).我想学习如何与工作
nls.lm
在R库minpack.lm
使用Rosenbrock函数,看是否算法收敛到全局最小值在f(X,Y)=(1,1)。 I do so both with and without the analytic Jacobian.无论有没有解析雅可比行列式,我都是这样做的。 In both instances, I get a warning telling me that the algorithm has decided to revert the maximum number of iterations specified in the call to
nls.lm
to 1024:在这两种情况下,我都会收到一条警告,告诉我算法已决定将调用
nls.lm
指定的最大迭代次数恢复为 1024:
Warning messages:
1: In nls.lm(par = initpar, fn = objective_rosenbrock, jac = gradient_rosenbrock, :
resetting `maxiter' to 1024!
2: In nls.lm(par = initpar, fn = objective_rosenbrock, jac = gradient_rosenbrock, :
lmder: info = -1. Number of iterations has reached `maxiter' == 1024.
The algorithm never quite reaches (1,1) as a result given my initial guess of (-1.2, 1.0).考虑到我对 (-1.2, 1.0) 的初始猜测,该算法永远不会完全达到 (1,1)。 I found the source code for the library on GitHub and the following lines of code are pertinent here:
我在 GitHub 上找到了该库的源代码,以下代码行与此处相关:
https://github.com/cran/minpack.lm/blob/master/src/nls_lm.c https://github.com/cran/minpack.lm/blob/master/src/nls_lm.c
OS->maxiter = INTEGER_VALUE(getListElement(control, "maxiter"));
if(OS->maxiter > 1024) {
OS->maxiter = 1024;
warning("resetting `maxiter' to 1024!");
}
Is there any logic to why the maximum number of iterations is capped to 1024?为什么最大迭代次数上限为 1024 有什么逻辑吗? Something with bits and 2^10?
有位和 2^10 的东西? I would like to use the library for a different application, but this cap on iterations might prevent that.
我想将该库用于不同的应用程序,但此迭代上限可能会阻止这种情况。 Any insight would be appreciated.
任何见解将不胜感激。
Git blame says that this code limiting the max iterations was introduced in version 1.1-0, in 2008. The NEWS file for the package only goes back as far as version 1.1-6. Gitblame说这个限制最大迭代次数的代码是在 2008 年的 1.1-0 版中引入的。该包的NEWS 文件只能追溯到 1.1-6 版。 I can't find the code in any public repo other than the one you point to (which is only a CRAN mirror; it doesn't contain any comments/commit messages/etc. from developers that might give us clues.)
除了您指向的那个之外,我在任何公共 repo 中都找不到代码(这只是一个 CRAN 镜像;它不包含来自开发人员的任何评论/提交消息/等,它们可能会给我们提供线索。)
Other than contacting the maintainer I think it's going to be hard to figure out what the rationale is for this limit.除了联系维护者之外,我认为很难弄清楚这个限制的基本原理是什么。
I do have some guesses though.不过我确实有一些猜测。
The only places that maxiter
is actually used in the code are here and here - in R code, not Fortran or C code, so it seems extremely unlikely that we are dealing with something like a 10-bit unsigned integer type (which seems an unlikely choice in any case).在代码中实际使用
maxiter
的唯一地方是这里和这里- 在 R 代码中,而不是 Fortran 或 C 代码中,因此我们处理诸如 10 位无符号整数类型之类的东西似乎极不可能(这似乎不太可能)任何情况下的选择)。 I think the limitation is there because we also have a buffer defined for holding trace information here :我认为,限制是有,因为我们也有保持跟踪信息确定的缓冲器这里:
double rsstrace[1024];
which, as you can see, is hard-coded to a length of 1024. Presumably bad things would happen if we tried to stuff 1025 iterations'-worth of tracing information into this array ...正如你所看到的,它被硬编码为 1024 的长度。如果我们试图将 1025 次迭代的跟踪信息填充到这个数组中,大概会发生不好的事情......
My suggestions:我的建议:
$ find . -type f -exec grep -Hn 1024 {} \;
./src/nls_lm.c:141: if(OS->maxiter > 1024) {
./src/nls_lm.c:142: OS->maxiter = 1024;
./src/nls_lm.c:143: warning("resetting `maxiter' to 1024!");
./src/minpack_lm.h:20: double rsstrace[1024];
#define MAXITER 2048
(or whatever) in src/minpack_lm.h
and use that instead of the numerical value.src/minpack_lm.h
#define MAXITER 2048
(或其他)并使用它而不是数值。maintainer("minpack.lm")
) and ask them about this issue.maintainer("minpack.lm")
)并询问他们这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.