TheoreticalEcosystemEcology / alien

Predict All Links In Ecological Networks
MIT License
12 stars 0 forks source link

Weird behaviour of GenSA() when return value is -inf #50

Open KevCaz opened 4 years ago

KevCaz commented 4 years ago

When the function to be monimized in in GenSA keeps returning -inf the random generation of parameters is always the same, even when set.seed() takes different values. I don't know if this is actually desired but this is problematic for fitMC().

A workaround is to replace -inf by a very large negative value but this has some limits.

Reprex:

Rastrigin <- function(x) {
                 print(x[1])
                 sum(x^2 - 10 * cos(2 * pi  * x)) + 10 * length(x)
                 log(0)
             }

set.seed(1234)
             dimension <- 2
             global.min <- 0
             tol <- 1e-13
             lower <- rep(-5.12, dimension)
             upper <- rep(5.12, dimension)
             out <- GenSA(lower = lower, upper = upper, fn = Rastrigin,
                     control=list(maxit = 10, threshold.stop=global.min+tol,verbose=TRUE))
             out[c("value","par","counts")]

set.seed(789)
             dimension <- 2
             global.min <- 0
             tol <- 1e-13
             lower <- rep(-5.12, dimension)
             upper <- rep(5.12, dimension)
             out <- GenSA(lower = lower, upper = upper, fn = Rastrigin,
                     control=list(maxit = 10, threshold.stop=global.min+tol,verbose=TRUE))
             out[c("value","par","counts")]

and check the values printed in both case.