Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DEoptim stack imbalance problems

Tags:

optimization

r

When running the following optimization task (R v.3.0.2)

library(DEoptim)

x <- seq(-6,6,length=100); y <- tanh(x)
goal <- function(par) return(1-abs(cor(x*par,y,method='spearman')))

ctrl <- DEoptim::DEoptim.control(VTR=0, trace=FALSE)

res <- DEoptim::DEoptim(goal,lower=-1,upper=1, ctrl)                  

I get stack imbalance warnings

Warning: stack imbalance in '<-', 14 then 13
Warning: stack imbalance in 'withVisible', 7 then 6

and unprotect() errors. If VTR is set below 0 (i.e. to a value impossible to obtain) then the problem disappears, but I'd rather not do that due to performance issues.

The result gets returned despite the errors however I fear it might be unstable/incorrect. Any ideas how to solve this?

like image 949
dratewka Avatar asked Feb 14 '14 09:02

dratewka


1 Answers

It's a problem in the C code, not something you can fix. But it's something I can fix, and it's fixed as of revision 116 on R-Forge. Here's the patch:

Index: DEoptim/src/de4_0.c
===================================================================
--- DEoptim/src/de4_0.c (revision 115)
+++ DEoptim/src/de4_0.c (working copy)
@@ -423,7 +423,6 @@

     /*------Trial mutation now in t_tmpP-----------------*/
     /* evaluate mutated population */
-    if(i_iter > 1) UNPROTECT(1);  // previous iteration's sexp_t_tmpC
     PROTECT(sexp_map_pop = popEvaluate(l_nfeval, sexp_t_tmpP,  fnMap, rho, 0));
     memmove(REAL(sexp_t_tmpP), REAL(sexp_map_pop), i_NP * i_D * sizeof(double));
     UNPROTECT(1);  // sexp_map_pop
@@ -458,6 +457,7 @@

       }
     } /* End mutation loop through ensemble */
+    UNPROTECT(1);  // sexp_t_tmpC

     if (d_c > 0) { /* calculate new meanCR and meanF */
       meanCR = (1-d_c)*meanCR + d_c*goodCR;
@@ -555,7 +555,7 @@
   *gt_bestC = t_bestC;

   PutRNGstate();
-  UNPROTECT(P+1); // +1 is for last iteration's sexp_t_tmpC
+  UNPROTECT(P);

 }
like image 182
Joshua Ulrich Avatar answered Nov 15 '22 21:11

Joshua Ulrich