【问题标题】:Calling BFGS optimization in C++ from optim.c从 optim.c 调用 C++ 中的 BFGS 优化
【发布时间】:2019-03-06 11:19:34
【问题描述】:

所以我必须将我的 R 代码重写为 C++。考虑到Rcpp 包,这相对容易。我在解决优化问题时遇到了一个问题。在 R 中我调用:

optimum_optim = optim(par=A, fn=negative_LL, gr=negative_grad_LL, .c = c, .t = t, .i = i, .N = N, method = 'BFGS')

鉴于我已经将 negative_LLnegative_grad_LL 函数重写到我的 C++ 文件中,我想从 R 调用基础例程以进行 BFGS 优化:它是 optim.c 中的 vmmin 函数
我的问题是我无法理解该函数的签名。它是:

vmmin(int n0, double *b, double *Fmin, optimfn fminfn, optimgr fmingr,
      int maxit, int trace, int *mask,
      double abstol, double reltol, int nREPORT, void *ex,
      int *fncount, int *grcount, int *fail)

这并不是说我没有在搜索上付出任何努力——我只是找不到描述......有人可以在我的特殊情况下帮助调用这个函数(并告诉我参数是什么)吗?

【问题讨论】:

    标签: c++ c r optimization statistics


    【解决方案1】:

    听起来您已经在使用此建议,但您需要更深入一点:"Use the source, Luke"

    我的出发点是,从 R 控制台,简单地输入

    optim
    

    这将打印该函数的 R 源代码。在那里我看到它在呼唤

    .External2(C_optim, par, fn1, gr1, method, con, lower, upper)
    

    我最喜欢的 R 源代码镜像是this GitHub repo。如果您前往那里,搜索“optim”,并仅过滤 C 结果,我们将找到最热门的 src/library/stats/src/optim.c。然后我们可以看到 C 级的 optim()(第 177 行)如何作用于 calls vmmin()(第 295 行)。

    optim() 初始化这些参数的方式如下

    int n           length(par)
    double *b       vect(npar); dpar[i] = REAL(par)[i] / (OS->parscale[i])
    double *Fmin    0.0
    optimfn fn      function defined in the C code
    optimgr gr      function defined in the C code
    int maxit       asInteger(getListElement(options, "maxit"))
    int trace       asInteger(getListElement(options, "trace"))
    int *mask       mask = (int *) R_alloc(npar, sizeof(int));
                    for (i = 0; i < npar; i++) mask[i] = 1;
    double abstol   asInteger(getListElement(options, "abstol"))
    double reltol   asInteger(getListElement(options, "reltol"))
    int nREPORT     asInteger(getListElement(options, "REPORT"));
    void *ex        OptStruct OS; /* tons of stuff done to this */
    int *fncount    0
    int *grcount    0
    int *fail       0
    

    我没有在这里详细介绍所有细节,但我相信这应该足以帮助您了解如何在自己的函数中使用这些东西,一旦您发现另一件事:控制列表在optim()。如果您在上面的.External2() 调用中注意到,有一个名为con 的参数。这在 R 代码中定义为

    con <- list(trace = 0, fnscale = 1, parscale = rep.int(1, npar),
            ndeps = rep.int(1e-3, npar),
            maxit = 100L, abstol = -Inf, reltol = sqrt(.Machine$double.eps),
            alpha = 1.0, beta = 0.5, gamma = 2.0,
            REPORT = 10, warn.1d.NelderMead = TRUE,
            type = 1,
            lmm = 5, factr = 1e7, pgtol = 0,
            tmax = 10, temp = 10.0)
    

    虽然这些元素可以被control 参数中的用户输入覆盖,如果您查看help("optim"),您会看到

    “控制”参数是一个可以提供以下任何组件的列表:
    ‘追踪’ ...

    C 函数通过名称 options 引用此列表,您可以在我上面构建的表中看到多次引用该列表。

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 2011-05-30
      • 2015-08-23
      • 2014-04-30
      • 2012-09-27
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2011-03-31
      相关资源
      最近更新 更多