Search code examples
crfreezedifferential-evolution

DEoptim appears to freeze in R


I have encountered a situation where DEoptim appears to freeze. I can't figure out why and was hoping somebody with more experience in C could take a look at it.

It is rather difficult to create a reproducible example, so I simply saved the entire environment 50 iterations before the DEoptim freezes. The file below, 'Envir650.Rdata' can be found here.

rm(list = ls())
library(DstarM)
library(DEoptim)

load('Envir650.Rdata') # load the environment

# Adjust one function
argsList$fun.density = DstarM::Voss.density 

argsList$control$trace = 1 # show intermediate output
argsList$control$parallelType = 0 # don't use parallel processing
.Random.seed = randomseed # set seed

out = do.call(DEoptim, argsList) # freezes at iteration 21 and crashes R.

Many thanks in advance!

EDIT: I hope the problem is now reproducible.


Solution

  • The problem is in rtdists package, source file density.c, function integrate. The loop

    for(x = a+0.5*step; x < b; x += step) {
        result += step * F->f(x, F->data);
    }
    

    becomes infinite because step is too small. It is so small that x+step==x and x never reaches b. The code of integrate should be changed so that step is never smaller than EPSILON:

    --- orig/rtdists/src/density.c  2016-07-15 10:28:56.000000000 +0200
    +++ mine/rtdists/src/density.c  2016-08-29 17:41:53.831078335 +0200
    @@ -72 +72 @@
    -   double step = width / N;
    +   double step = fmax(width / N, EPSILON);
    

    With this change applied, your example finishes at iteration 51 without looping or crashing. I've notified the rtdists authors; the fix is now in the github version of the package.