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...10ub <- -lbopt <- 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?