package rgp in r gives "NaNs produced" output that cannot be suppressed

296 views Asked by At

I've been using package rgp (genetic programming) in r to predict survival on the Titanic. The input data frame, train, is the training data from Kaggle with the Sex variable changed to 0 for females and 1 for males. Given this, the code is:

fs  <- functionSet("+", "-", "*", "exp", "sin", "cos")
ivs <- inputVariableSet("Sex")
cfs <- constantFactorySet(function() rnorm(1))
ff  <- function(f) {
  err <- rmse(as.numeric(f(train$Sex)), as.numeric(train$Survived))
  if (is.na(err)) return(1e12) else return(err)
}

set.seed(42)
gp <- geneticProgramming(functionSet = fs,
                         inputVariables = ivs,
                         constantSet = cfs,
                         fitnessFunction = ff,
                         stopCondition = makeTimeStopCondition(60),
                         verbose = FALSE)

The issue is the geneticProgramming function outputs "NaNs produced" frequently. In fact, the output is enough to hang r and rstudio if I run it long enough (e.g. overnight). (The actual code I want to run is more involved and "NaNs produced" prints much more frequently, but this shortened version suffices to show the issue.)

I would like to suppress all output from geneticProgramming. The verbose = FALSE option doesn't address the "NaNs produced" output.

I have tried using capture.output, sink, and invisible. I've tried echo = FALSE in an R markdown cell. All to no avail. I would have thought sink("/dev/null") would solve the problem, but it doesn't.

Where is this output coming from, and how can I suppress it if the usual methods don't work?

Thanks very much.

1

There are 1 answers

0
rlgjr On

In your functionSet, remove "sin" and "cos". When these are removed the NaNs are not produced.