DEOptim keeps telling: NaN value of objective function

…衆ロ難τιáo~ 提交于 2019-12-10 17:36:36

问题


I've written a simulation program in C++ and like to find parameters in R, using DEoptim. Sometimes everything works well and sometimes DEoptim stops and tells:

Error in DEoptim(simulate, lower = lb, upper = ub, control = opt) : 
  NaN value of objective function! 
Perhaps adjust the bounds.

My R-script defines a function that calls the external binary. The parameters are attached to the command. I tested my C++ program and have never seen NaN's returned. Further, to investigate I check for NaN's in the simulate() R-function, such that it would stop and tell that there is actually a NaN value. However, it does never stop there - but later in DEoptim. What is the problem? Is this a DEoptim-Bug?

library("DEoptim")
setwd("some-path")

simulate <- function(theta)
{
  strcom <- paste(c("./ExternalBinary", theta),collapse=" ")
  ret <- as.numeric(system(strcom, intern=T)) #will return a couple of integer numbers
  ret <- mean(ret) #average those numbers
  if(any(is.nan(ret))){ #check against NaNs
    stop('Found a NaN?!') #this line is NEVER called, even if DEoptim stops
  }
  return(ret)
}

lb <- rep(-10.,18) #18 parameters in the range of -10...10
ub <- -lb

opt <- list(NP=500,itermax=10, storepopfrom=1, storepopfreq=1, parallelType=1)
est <- DEoptim(simulate,lower=lb,upper=ub, control=opt)

EDIT: I found that the return is actually not NaN but NA. The simulate() function stops if I replace is.nan(ret) with is.na(ret). I also went through my c++ program again and could not find a way how it quits without writing a number to cout. Hence I asked this question: Can a main() return before all cout has been written to the consol?

来源:https://stackoverflow.com/questions/35709648/deoptim-keeps-telling-nan-value-of-objective-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!