[Rcpp-commits] r2330 - in pkg/RcppDE: . src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Oct 18 15:01:30 CEST 2010


Author: edd
Date: 2010-10-18 15:01:30 +0200 (Mon, 18 Oct 2010)
New Revision: 2330

Modified:
   pkg/RcppDE/benchmark.r
   pkg/RcppDE/benchmark.txt
   pkg/RcppDE/src/de4_0.cpp
Log:
call ::unif_rand() to make the R 'namespace' explicit
removed some unnecessary explicit wrap() calls
removed some comments
benchmark now runs 10 per default, not 20


Modified: pkg/RcppDE/benchmark.r
===================================================================
--- pkg/RcppDE/benchmark.r	2010-10-18 11:31:19 UTC (rev 2329)
+++ pkg/RcppDE/benchmark.r	2010-10-18 13:01:30 UTC (rev 2330)
@@ -33,15 +33,18 @@
 
     gc()
     set.seed(42)
-    bt <- mean(replicate(20, system.time(invisible(basicDE(n, maxIt, fun)))[3]), trim=0.05)
+    bt <- mean(replicate(10, system.time(invisible(basicDE(n, maxIt, fun)))[3]), trim=0.1)
 
     gc()
     set.seed(42)
-    ct <- mean(replicate(20, system.time(invisible(cppDE(n, maxIt, fun)))[3]), trim=0.05)
+    ct <- mean(replicate(10, system.time(invisible(cppDE(n, maxIt, fun)))[3]), trim=0.1)
 
     return(data.frame(DEoptim=bt, RcppDE=ct))
 }
 
+svnver <- system("svnversion", intern=TRUE)
+cat("# At ", format(Sys.time()), "\n# SVN ", svnver, "\n")
+
 res <- rbind(runPair(2, maxIt, function(...) Rastrigin(...)),
              runPair(5, maxIt, function(...) Rastrigin(...)),
              runPair(20, maxIt, function(...) Rastrigin(...)),
@@ -62,6 +65,5 @@
 res$ratioRcppToBasic <- res[,2]/res[,1]
 res$pctGainOfRcpp <- (1-res[,2]/res[,1])*100
 
-svnver <- system("svnversion", intern=TRUE)
-cat("# At ", format(Sys.time()), "\n# SVN ", svnver, "\n")
 print(res)
+cat("# Done ", format(Sys.time()), "\n")

Modified: pkg/RcppDE/benchmark.txt
===================================================================
--- pkg/RcppDE/benchmark.txt	2010-10-18 11:31:19 UTC (rev 2329)
+++ pkg/RcppDE/benchmark.txt	2010-10-18 13:01:30 UTC (rev 2330)
@@ -366,3 +366,53 @@
 Genrose20   0.876778 0.902833           1.0297       -2.9717
 Genrose50   2.494500 2.597722           1.0414       -4.1380
 MEANS       0.559989 0.577772           1.0318       -3.1757
+
+
+# At  2010-10-18 06:41:15.901748 
+# SVN  2329M 
+             DEoptim   RcppDE ratioRcppToBasic pctGainOfRcpp
+Rastrigin2  0.040611 0.041667           1.0260       -2.5992
+Rastrigin5  0.105889 0.109722           1.0362       -3.6201
+Rastrigin20 0.542000 0.554056           1.0222       -2.2243
+Wild2       0.065667 0.067500           1.0279       -2.7919
+Wild5       0.179278 0.183722           1.0248       -2.4791
+Wild20      1.037944 1.054278           1.0157       -1.5736
+Genrose2    0.068167 0.071167           1.0440       -4.4010
+Genrose5    0.179944 0.187222           1.0404       -4.0445
+Genrose20   0.874944 0.893333           1.0210       -2.1017
+Genrose50   2.526500 2.593056           1.0263       -2.6343
+MEANS       0.562094 0.575572           1.0240       -2.3978
+
+
+# At  2010-10-18 07:15:49.59145
+# SVN  2329M
+             DEoptim   RcppDE ratioRcppToBasic pctGainOfRcpp
+Rastrigin2  0.040500 0.041625           1.0278      -2.77778
+Rastrigin5  0.106500 0.108625           1.0200      -1.99531
+Rastrigin20 0.547000 0.555375           1.0153      -1.53108
+Wild2       0.066250 0.067750           1.0226      -2.26415
+Wild5       0.182250 0.184750           1.0137      -1.37174
+Wild20      1.042250 1.049125           1.0066      -0.65963
+Genrose2    0.068875 0.069750           1.0127      -1.27042
+Genrose5    0.180875 0.183250           1.0131      -1.31306
+Genrose20   0.877500 0.883375           1.0067      -0.66952
+Genrose50   2.516750 2.565750           1.0195      -1.94696
+MEANS       0.562875 0.570937           1.0143      -1.43238
+# At  2010-10-18 07:17:58.781577
+
+
+# At  2010-10-18 07:20:58.739872
+# SVN  2329M
+             DEoptim   RcppDE ratioRcppToBasic pctGainOfRcpp
+Rastrigin2  0.041000 0.041375           1.0091      -0.91463
+Rastrigin5  0.105625 0.108500           1.0272      -2.72189
+Rastrigin20 0.546125 0.549625           1.0064      -0.64088
+Wild2       0.066125 0.067750           1.0246      -2.45747
+Wild5       0.179875 0.183250           1.0188      -1.87630
+Wild20      1.037125 1.052000           1.0143      -1.43425
+Genrose2    0.068750 0.069625           1.0127      -1.27273
+Genrose5    0.179250 0.182375           1.0174      -1.74338
+Genrose20   0.878375 0.883000           1.0053      -0.52654
+Genrose50   2.494250 2.568750           1.0299      -2.98687
+MEANS       0.559650 0.570625           1.0196      -1.96105
+# Done  2010-10-18 07:23:07.486604 

Modified: pkg/RcppDE/src/de4_0.cpp
===================================================================
--- pkg/RcppDE/src/de4_0.cpp	2010-10-18 11:31:19 UTC (rev 2329)
+++ pkg/RcppDE/src/de4_0.cpp	2010-10-18 13:01:30 UTC (rev 2330)
@@ -19,7 +19,7 @@
            arma::mat    & ta_popP, arma::mat    & ta_oldP, arma::mat    & ta_newP, arma::rowvec & t_bestP, 
 	   arma::rowvec & ta_popC, arma::rowvec & ta_oldC, arma::rowvec & ta_newC, double       & t_bestC,	
            arma::rowvec & t_bestitP, arma::rowvec & t_tmpP, arma::rowvec & tempP,
-           arma::mat & d_pop, /*arma::rowvec*/ Rcpp::List & d_storepop, arma::mat & d_bestmemit, arma::rowvec & d_bestvalit,
+           arma::mat & d_pop, Rcpp::List & d_storepop, arma::mat & d_bestmemit, arma::rowvec & d_bestvalit,
            int & i_iterations, double i_pPct, long & l_nfeval);
 //RcppExport void permute(arma::icolvec & ia_urn2, int i_avoid, arma::icolvec & ia_urntmp);
 //RcppExport double evaluate(long & l_nfeval, const arma::rowvec & param, SEXP par, SEXP fcall, SEXP env);
@@ -73,7 +73,6 @@
 
     int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
     arma::mat d_pop(i_NP, i_D); 
-    //arma::rowvec d_storepop(i_NP*i_D*i_nstorepop); 
     Rcpp::List d_storepop(i_nstorepop);
     arma::mat d_bestmemit(i_itermax, i_D);       
     arma::rowvec d_bestvalit(i_itermax); 	 
@@ -86,14 +85,14 @@
 	  d_pop, d_storepop, d_bestmemit, d_bestvalit, i_iter, i_pPct, l_nfeval);
 
     // and return a named list to R
-    return Rcpp::List::create(Rcpp::Named("bestmem")   = Rcpp::wrap(t_bestP),		// sexp_bestmem,
+    return Rcpp::List::create(Rcpp::Named("bestmem")   = t_bestP,	// sexp_bestmem,
 			      Rcpp::Named("bestval")   = t_bestC,       // sexp_bestval,
 			      Rcpp::Named("nfeval")    = l_nfeval,   	// sexp_nfeval,
 			      Rcpp::Named("iter")      = i_iter,	// sexp_iter,
-			      Rcpp::Named("bestmemit") = Rcpp::wrap(d_bestmemit), 	// sexp_bestmemit,
-			      Rcpp::Named("bestvalit") = Rcpp::wrap(d_bestvalit),	// sexp_bestvalit,
-			      Rcpp::Named("pop")       = Rcpp::wrap(d_pop),    		// sexp_pop,
-			      Rcpp::Named("storepop")  = Rcpp::wrap(d_storepop));   	// sexp_storepop)
+			      Rcpp::Named("bestmemit") = d_bestmemit, 	// sexp_bestmemit,
+			      Rcpp::Named("bestvalit") = d_bestvalit,	// sexp_bestvalit,
+			      Rcpp::Named("pop")       = d_pop,		// sexp_pop,
+			      Rcpp::Named("storepop")  = d_storepop); 	// sexp_storepop)
     END_RCPP   // macro to fill in catch() part of try/catch exception handler
 }
 
@@ -105,7 +104,7 @@
            arma::mat &ta_popP, arma::mat &ta_oldP, arma::mat &ta_newP, arma::rowvec & t_bestP, 
            arma::rowvec & ta_popC, arma::rowvec & ta_oldC, arma::rowvec & ta_newC, double & t_bestC,
            arma::rowvec & t_bestitP, arma::rowvec & t_tmpP, arma::rowvec & tempP,
-           arma::mat &d_pop, /*arma::rowvec*/ Rcpp::List &d_storepop, arma::mat & d_bestmemit, arma::rowvec & d_bestvalit,
+           arma::mat &d_pop, Rcpp::List &d_storepop, arma::mat & d_bestmemit, arma::rowvec & d_bestvalit,
            int & i_iterations, double i_pPct, long & l_nfeval) {
 
     const int urn_depth = 5;   			// 4 + one index to avoid 
@@ -137,13 +136,10 @@
 
     GetRNGstate();
 
-    ta_popP.at(0,0) = 0;
-    
     initialpop.zeros();		 		// initialize initial popuplation 
     d_bestmemit.zeros();    			// initialize best members
     d_bestvalit.zeros();			// initialize best values 
     d_pop.zeros();				// initialize best population
-    //d_storepop.zeros();			// initialize stored populations 
     i_nstorepop = (i_nstorepop < 0) ? 0 : i_nstorepop;
       
     if (i_specinitialpop > 0) {    		// if initial population provided, initialize with values 
@@ -154,7 +150,7 @@
     for (i = 0; i < i_NP; i++) {		// ------Initialization-----------------------------
 	if (i_specinitialpop <= 0) { 		// random initial member 
 	    for (j = 0; j < i_D; j++) {
-		ta_popP.at(i,j) = fa_minbound[j] + unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
+		ta_popP.at(i,j) = fa_minbound[j] + ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
 	    }
 	} else { 				// or user-specified initial member 
 	    ta_popP.row(i) = initialpop.row(i);
@@ -176,13 +172,7 @@
     i_xav = 1;
   
     while ((i_iter < i_itermax) && (t_bestC > VTR)) {    // main loop ====================================
-	if (i_iter % i_storepopfreq == 0 && i_iter >= i_storepopfrom) {  	// store intermediate populations -- FIXME could be list (or arma::field) of matrices
-	    // for (i = 0; i < i_NP; i++) {
-	    // 	for (j = 0; j < i_D; j++) {
-	    // 	    d_storepop[popcnt] = ta_oldP.at(i,j);
-	    // 	    popcnt++;
-	    // 	}
-	    // }
+	if (i_iter % i_storepopfreq == 0 && i_iter >= i_storepopfrom) {  	// store intermediate populations
 	    d_storepop[popcnt++] = Rcpp::wrap(ta_oldP);
 	} // end store pop 
       
@@ -192,7 +182,7 @@
 	t_bestitC = t_bestC;
 	i_iter++;				// increase iteration counter
      
-	f_dither = f_weight + unif_rand() * (1.0 - f_weight);	// ----computer dithering factor -----------------
+	f_dither = f_weight + ::unif_rand() * (1.0 - f_weight);	// ----computer dithering factor -----------------
       
 	if (i_strategy == 6) {			// ---DE/current-to-p-best/1 -----------------------------------------------------
 	    arma::rowvec temp_oldC = ta_oldC;					// create a copy of ta_oldC to avoid changing it 
@@ -216,81 +206,80 @@
 	    switch (i_strategy) { 		// and putting default value one first
 
 	    case 2:				// ---DE/local-to-best/1/bin---------------------------------------------------
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
 		do {				// add fluctuation to random target 
 		    t_tmpP[j] = t_tmpP[j] + f_weight * (t_bestitP[j] - t_tmpP[j]) + f_weight * (ta_oldP.at(i_r2,j) - ta_oldP.at(i_r3,j));
 		    j = (j + 1) % i_D;
-		} while ((unif_rand() < f_cross) && (++k < i_D));
+		} while ((::unif_rand() < f_cross) && (++k < i_D));
 		break;
 
 	    case 1:				// ---classical strategy DE/rand/1/bin-----------------------------------------
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
 		do {				// add fluctuation to random target 
 		    t_tmpP[j] = ta_oldP.at(i_r1,j) + f_weight * (ta_oldP.at(i_r2,j) - ta_oldP.at(i_r3,j));
 		    j = (j + 1) % i_D;
-		} while ((unif_rand() < f_cross) && (++k < i_D));
+		} while ((::unif_rand() < f_cross) && (++k < i_D));
 		break;
 
 	    case 3:				// ---DE/best/1/bin with jitter------------------------------------------------
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
 		do {				// add fluctuation to random target 
-		    f_jitter = 0.0001 * unif_rand() + f_weight; 
+		    f_jitter = 0.0001 * ::unif_rand() + f_weight; 
 		    t_tmpP[j] = t_bestitP[j] + f_jitter * (ta_oldP.at(i_r1,j) - ta_oldP.at(i_r2,j));
 		    j = (j + 1) % i_D;
-		} while ((unif_rand() < f_cross) && (++k < i_D));
+		} while ((::unif_rand() < f_cross) && (++k < i_D));
 		break;
 
 	    case 4:				// ---DE/rand/1/bin with per-vector-dither-------------------------------------
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
 		do {				// add fluctuation to random target *
-		    t_tmpP[j] = ta_oldP.at(i_r1,j) + (f_weight + unif_rand()*(1.0 - f_weight))* (ta_oldP.at(i_r2,j) - ta_oldP.at(i_r3,j));
+		    t_tmpP[j] = ta_oldP.at(i_r1,j) + (f_weight + ::unif_rand()*(1.0 - f_weight))* (ta_oldP.at(i_r2,j) - ta_oldP.at(i_r3,j));
 		    j = (j + 1) % i_D;
-		} while ((unif_rand() < f_cross) && (++k < i_D));
+		} while ((::unif_rand() < f_cross) && (++k < i_D));
 		break;
 
 	    case 5:				// ---DE/rand/1/bin with per-generation-dither---------------------------------
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
 		do {				// add fluctuation to random target 
 		    t_tmpP[j] = ta_oldP.at(i_r1,j) + f_dither * (ta_oldP.at(i_r2,j) - ta_oldP.at(i_r3,j));
 		    j = (j + 1) % i_D;
-		} while ((unif_rand() < f_cross) && (++k < i_D));
+		} while ((::unif_rand() < f_cross) && (++k < i_D));
 		break;
 
 	    case 6:				// ---DE/current-to-p-best/1 (JADE)--------------------------------------------
-		i_pbest = sortIndex[static_cast<int>(unif_rand() * p_NP)]; // select from [0, 1, 2, ..., (pNP-1)] 
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
+		i_pbest = sortIndex[static_cast<int>(::unif_rand() * p_NP)]; // select from [0, 1, 2, ..., (pNP-1)] 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
 		do {				// add fluctuation to random target 
 		    t_tmpP[j] = ta_oldP.at(i,j) + f_weight * (ta_oldP.at(i_pbest,j) - ta_oldP.at(i,j)) + f_weight * (ta_oldP.at(i_r1,j) - ta_oldP.at(i_r2,j));
 		    j = (j + 1) % i_D;
-		} while ((unif_rand() < f_cross) && (++k < i_D));
+		} while ((::unif_rand() < f_cross) && (++k < i_D));
 		break;
 
 	    default:				// ---variation to DE/rand/1/bin: either-or-algorithm--------------------------
-		j = static_cast<int>(unif_rand() * i_D); 	// random parameter 
-		if (unif_rand() < 0.5) { 	// differential mutation, Pmu = 0.5 
+		j = static_cast<int>(::unif_rand() * i_D); 	// random parameter 
+		if (::unif_rand() < 0.5) { 	// differential mutation, Pmu = 0.5 
 		    do {
 			// add fluctuation to random target */
 			t_tmpP[j] = ta_oldP.at(i_r1,j) + f_weight * (ta_oldP.at(i_r2,j) - ta_oldP.at(i_r3,j));
 			j = (j + 1) % i_D;
-		    } while ((unif_rand() < f_cross) && (++k < i_D));
+		    } while ((::unif_rand() < f_cross) && (++k < i_D));
 
 		} else { 			// recombination with K = 0.5*(F+1) -. F-K-Rule 
 		    do {
 			// add fluctuation to random target */
 			t_tmpP[j] = ta_oldP.at(i_r1,j) + 0.5 * (f_weight + 1.0) * (ta_oldP.at(i_r2,j) + ta_oldP.at(i_r3,j) - 2 * ta_oldP.at(i_r1,j));
 			j = (j + 1) % i_D;
-		    } while ((unif_rand() < f_cross) && (++k < i_D));
+		    } while ((::unif_rand() < f_cross) && (++k < i_D));
 		}
 		break;
 	    } // end switch (i_strategy) ...
 	
-	    // ----boundary constraints, bounce-back method was not enforcing bounds correctly
-	    for (j = 0; j < i_D; j++) {
+	    for (j = 0; j < i_D; j++) {		// ----boundary constraints, bounce-back method was not enforcing bounds correctly
 		if (t_tmpP[j] < fa_minbound[j]) {
-		    t_tmpP[j] = fa_minbound[j] + unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
+		    t_tmpP[j] = fa_minbound[j] + ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
 		}
 		if (t_tmpP[j] > fa_maxbound[j]) {
-		    t_tmpP[j] = fa_maxbound[j] - unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
+		    t_tmpP[j] = fa_maxbound[j] - ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
 		}
 	    }
 
@@ -422,7 +411,7 @@
 	ia_urn1[i_urn1] = ia_urn1[k-1];    /* move highest index to fill gap */
 	k = k - 1;                         /* reduce number of accessible indices */
 	i_urn2 = i_urn2 + 1;               /* next position in urn2 */
-	i_urn1 = static_cast<int>(unif_rand() * k);   /* choose a random index */
+	i_urn1 = static_cast<int>(::unif_rand() * k);   /* choose a random index */
     }
     PutRNGstate();
 }



More information about the Rcpp-commits mailing list