【问题标题】:R: Question about Optimizing - Invalid Function Value in OptimizeR:关于优化的问题 - 优化中的无效函数值
【发布时间】:2011-10-03 06:56:47
【问题描述】:

我们无法在优化代码中查明导致Invalid Function Value in Optimize 错误的原因。如果您能提供任何见解,将不胜感激。

H_fun <- function(c) 
{ 
val = -current_c_weight*c - X_counts%*%log( 
exp(rep(c,length(current_Theta))*current_Theta) - 
current_elongation_rates ) 
print('#########iteration display#############') 
print('c') 
print(c) 
print('val') 
print(val) 
print('current_c_weight') 
print(current_c_weight) 
print('current_Theta') 
print(current_Theta) 
print('current_elongation_rates') 
print(current_elongation_rates) 
} 

#...snip...

# minimize -H(c) without the non-negativity constraint 
#tmp = optim(c(0,1),H_fun,NULL, method = "BFGS", hessian = TRUE); 
tmp = optimize(H_fun,interval = c(0,1)); 

这里是代码链接:

http://www.text-upload.com/read.php?id=102950&c=8605046

【问题讨论】:

  • 不幸的是,您的示例不可重现。请发布current_c_weightX_countscurrent_Thetacurrent_elongation_rates 的值。
  • @Andrie,完整的源代码在 text-upload.com 链接。

标签: r optimization


【解决方案1】:

您确定H_fun 正在返回一个一维值吗?

fcn1()中的R optimize() source code

static double fcn1(double x, struct callinfo *info)
{
    SEXP s;
    REAL(CADR(info->R_fcall))[0] = x;
    s = eval(info->R_fcall, info->R_env);
    switch(TYPEOF(s)) {
    case INTSXP:
        if (length(s) != 1) goto badvalue;
        if (INTEGER(s)[0] == NA_INTEGER) {
            warning(_("NA replaced by maximum positive value"));
        return DBL_MAX;
        }
        else return INTEGER(s)[0];
        break;
    case REALSXP:
        if (length(s) != 1) goto badvalue;
        if (!R_FINITE(REAL(s)[0])) {
            warning(_("NA/Inf replaced by maximum positive value"));
            return DBL_MAX;
        }
        else return REAL(s)[0];
        break;
    default:
        goto badvalue;
    }
 badvalue:
    error(_("invalid function value in 'optimize'"));
    return 0;/* for -Wall */
}

goto badvalue 在长度不为 1 时发生。此外,package summary 指出 optimize() 可用于一维无约束函数。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多