简体   繁体   English

允许在 DEoptim for R 中使用 Rcpp 函数

[英]Allow Rcpp functions in DEoptim for R

I have been using DEoptim for some time to test different parameters for a hydrology algorithm.一段时间以来,我一直在使用 DEoptim 来测试水文算法的不同参数。 The code is mainly written in R, but there is a function written in Rcpp.代码主要是用R写的,但是有一个函数是用Rcpp写的。 If I run DEoptim in non-parallel mode, it runs fine, but if I run in parallel mode (ie paralleltype=1) the code returns an error saying it can't find my Rcpp function.如果我在非并行模式下运行 DEoptim,它运行良好,但如果我在并行模式下运行(即 paralleltype=1),代码返回一个错误,说它找不到我的 Rcpp 函数。 So the Rcpp function looks like this:所以 Rcpp 函数看起来像这样:

loadcppfunctions <- function() {
eastfunc <<- 'NumericMatrix eastC(NumericMatrix e, NumericMatrix zerocolmatrix, NumericMatrix zerorowmatrix) {
int ecoln = e.ncol();
int ecolnlessone = ecoln - 1;
int erown = e.nrow();
int erownlessone = erown - 1;

NumericMatrix eout(e.nrow(),e.ncol()) ;
for (int j = 0;j < ecoln;j++) {
if (j > 0) {
eout(_,j) = e(_,j-1);
} else {
eout(_,j) = e(_,0);
}
}
eout(_,0) = zerocolmatrix(_,0);
return eout;
}'
eastC <<- cppFunction(eastfunc)
}

and then I just use:然后我只使用:

loadcppfunctions()

Later in the code I call this function as follows:稍后在代码中我调用这个函数如下:

movefdrerunoff <- eastC(fdrerunoff, zerocolmatrix, zerorowmatrix)

As I say, it all works fine - but if I run DEoptim as follows:正如我所说,一切正常 - 但如果我按如下方式运行 DEoptim:

ans <- DEoptimone(Calibrate,lower,upper,DEoptim.control(trace=TRUE,parallelType=1,parVar=c(parVarnames),packages=c("raster","rgdal","maptools","matrixcalc","Rcpp","RcppArmadillo")))

It fails saying:它没有说:

Error in checkForRemoteErrors(val) : 
  7 nodes produced errors; first error: could not find function "eastC"

So how can I make DEoptim see this function when all the other R based functions are fine.那么当所有其他基于 R 的函数都很好时,我如何让 DEoptim 看到这个函数。

Thanks, Antony Walker谢谢,安东尼沃克

I found that by adding the Rcpp function inside the main DEoptim function (Calibrate) worked.我发现通过在主 DEoptim 函数(校准)中添加 Rcpp 函数起作用了。 The Calibrate function looked like:校准功能如下所示:

Calibrate <- function(x) {
eastfunc <<- 'NumericMatrix eastC(NumericMatrix e, NumericMatrix zerocolmatrix, NumericMatrix zerorowmatrix) {
int ecoln = e.ncol();
int ecolnlessone = ecoln - 1;
int erown = e.nrow();
int erownlessone = erown - 1;

NumericMatrix eout(e.nrow(),e.ncol()) ;
for (int j = 0;j < ecoln;j++) {
if (j > 0) {
eout(_,j) = e(_,j-1);
} else {
eout(_,j) = e(_,0);
}
}
eout(_,0) = zerocolmatrix(_,0);
return eout;
}'
eastC <<- cppFunction(eastfunc)

cmax <<- x[1]
Cr <<- x[2]
Cl <<- x[3]
Crb <<- x[4]
Clb <<- x[5]
returnflowriver <<- x[6]
returnflowland <<- x[7]
kd <<- x[8]
startyear()
-NashSutcliffe
}

and then running DEoptim as:然后将 DEoptim 运行为:

ans <- DEoptimone(Calibrate,lower,upper,DEoptim.control(trace=TRUE,parallelType=1,parVar=c(parVarnames),packages=c("raster","rgdal","maptools","matrixcalc","Rcpp","RcppArmadillo","moveCpp")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM