English 中文(简体)
我的方案产出为何微乎其微?
原标题:Why is the gr count 0 in my program output?

I m New to R andDM/ML, and I written a small program totries out the optim function.

我使用了SANN方法。 我界定了自己的<条码>gr功能,并与<条码>控制参数混在一起。

问题在于,当我印刷产出时,显示灰色功能的电话数量为gr,而呼声数量是正确的。

在此,我可操作的法典(我认为,成本功能是不可开发的,因此,我只贴出一个简单的版本):

people = list( Seymour = BOS ,
 Franny = DAL ,
 Zooey = CAK ,
 Walt = MIA ,
 Buddy = ORD ,
 Les = OMA )

schedulecost <- function(schedule){
    return(sum(schedule))
}

annealingOptimize <- function(domains, step=1, T=10000,costf=schedulecost){
    solution <- sample(1:9, 2 * length(people), replace=T)
    grFunction <- function(sol){
        index <- sample(c(1:length(domains$Up)), 1, replace=T)
        delta <- as.integer(runif(1,min=-step-1,max=step+1))
        newValue <- sol[index] + delta
        if (newValue > domains$Up[index]){
            newValue <- domains$Up[index]
        }
        if (newValue < domains$Down[index]){
            newValue <- domains$Down[index]
        }
        sol[index] <- newValue
        return(sol)
    }
    values <- optim(solution,costf,gr=grFunction, method= SANN ,
            control=list(temp=T, REPORT=1, maxit=200, tmax=10))
    print(values)
    return(values$par)
}
domains <- list(Down=rep(1,length(people) * 2), Up=rep(9, length(people) * 2))
schedule <-annealingOptimize(domains)

产出是:

$par
 [1] 2 2 6 2 3 5 5 1 9 1 1 7

$value
[1] 44

$counts
function gradient 
     200       NA 

$convergence
[1] 0

$message
NULL

我的理解是,<代码>gr的数值应等于fn,因为你需要打上<代码>fn。 发送一名新候选人,请上gr

Is my understanding incorrect(if yes, what s their relationship) or there s something wrong with my code.

谁能提供帮助?

感谢如此之多!

<>Update:

正如@Dwin所指出的,在帮助文件中,它说:“标准”“SANN”只使用功能价值,但相对缓慢。 “...... 但是,在对参数<代码>gr的说明中,它说:“对于“SANN”方法,它规定了产生新候选人点的职能。 “......

如果你在我的<条码>中添加一些印刷说明,那么你就可以看到,它实际上被非常密集地称作。

此外,如果SANN方法仅使用fn ,而且n t use gr,那么gr/code>的句子实际上指的是?

最佳回答

调查<代码>src/main/optim.c 这表明,SANN方法确实称梯度功能(当然你通过你的印刷说明证实了这一功能),但是,它确实要求双向更新梯度。 下面请内部SANN功能samin:

   samin (npar, dpar, &val, fminfn, maxit, tmax, temp, trace, (void *)OS);
        for (i = 0; i < npar; i++)
            REAL(par)[i] = dpar[i] * (OS->parscale[i]);
        fncount = npar > 0 ? maxit : 1;
        grcount = NA_INTEGER;

and I can confirm that samin calls genptry, which calls the gradient function.

This strikes me as a bug (which could be reported to the R development list, or on the R bugs tracker), but a harmless one. As you point out, presumably the gradient (= candidate point generator) function always gets called exactly the same number of times (OK, give or take one or two during the set-up stage) as the objective function ... (but I understand the confusion when the function doesn t do what it says in the documentation!)

问题回答

暂无回答




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签