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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 16 03:50:55 CEST 2010


Author: edd
Date: 2010-10-16 03:50:54 +0200 (Sat, 16 Oct 2010)
New Revision: 2302

Modified:
   pkg/RcppDE/benchmark.txt
   pkg/RcppDE/src/de4_0.cpp
Log:
three population data matrices are now Armadillo matrices which will eventually save on looping when copy entire rows etc


Modified: pkg/RcppDE/benchmark.txt
===================================================================
--- pkg/RcppDE/benchmark.txt	2010-10-16 00:58:48 UTC (rev 2301)
+++ pkg/RcppDE/benchmark.txt	2010-10-16 01:50:54 UTC (rev 2302)
@@ -14,5 +14,19 @@
 MEANS       0.564400 0.563728          0.99881      0.119104
 
 
+# At  2010-10-15 20:03:17.732317
+# SVN  2301  
+             DEoptim   RcppDE ratioRcppToBasic pctGainOfRcpp
+Rastrigin2  0.040500 0.040389          0.99726      0.274348
+Rastrigin5  0.106778 0.106389          0.99636      0.364204
+Rastrigin20 0.539667 0.540667          1.00185     -0.185300
+Wild2       0.066278 0.066056          0.99665      0.335289
+Wild5       0.181778 0.181333          0.99756      0.244499
+Wild20      1.042722 1.035333          0.99291      0.708615
+Genrose2    0.070333 0.071056          1.01027     -1.026856
+Genrose5    0.186111 0.186833          1.00388     -0.388060
+Genrose20   0.891778 0.891889          1.00012     -0.012460
+Genrose50   2.546500 2.528111          0.99278      0.722124
+MEANS       0.567244 0.564806          0.99570      0.429954
 
 

Modified: pkg/RcppDE/src/de4_0.cpp
===================================================================
--- pkg/RcppDE/src/de4_0.cpp	2010-10-16 00:58:48 UTC (rev 2301)
+++ pkg/RcppDE/src/de4_0.cpp	2010-10-16 01:50:54 UTC (rev 2302)
@@ -1,6 +1,6 @@
 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
 //
-// "Port" of DEoptim (2.0.7) to Rcpp/RcppArmadillo/Armadillo
+// Port of DEoptim (2.0.7) to Rcpp/RcppArmadillo/Armadillo
 // Copyright (C) 2010  Dirk Eddelbuettel <edd at debian.org>
 
 /*
@@ -33,7 +33,10 @@
            int i_strategy, int i_D, int i_NP, int i_itermax,
            double *initialpopv, int i_storepopfreq, int i_storepopfrom,
            int i_specinitialpop, int i_check_winner, int i_av_winner,
-           double **gta_popP, double **gta_oldP, double **gta_newP, double *gt_bestP,
+           /*double **gta_popP,*/ arma::mat &ta_popP,
+	   /*double **gta_oldP,*/ arma::mat &ta_oldP,
+	   /*double **gta_newP,*/ arma::mat &ta_newP,
+	   double *gt_bestP,
            double *gta_popC, double *gta_oldC, double *gta_newC, double *gt_bestC,
            double *t_bestitP, double *t_tmpP, double *tempP,
            double *gd_pop, double *gd_storepop, double *gd_bestmemit, double *gd_bestvalit,
@@ -44,11 +47,11 @@
 RcppExport SEXP DEoptimC(SEXP lower, SEXP upper, SEXP fnSexp, SEXP controlSexp, SEXP rhoSexp) {
     BEGIN_RCPP ;
 
-    Rcpp::Function fn(fnSexp);
-    Rcpp::Environment rho(rhoSexp);
+    Rcpp::Function fn(fnSexp);						// function to mininise
+    Rcpp::Environment rho(rhoSexp); 					// environment to do it in
 
-    Rcpp::NumericVector f_lower(lower), f_upper(upper); 	// User-defined bounds
-    Rcpp::List          control(controlSexp); 			// named list of params
+    Rcpp::NumericVector f_lower(lower), f_upper(upper); 		// User-defined bounds
+    Rcpp::List          control(controlSexp); 				// named list of params
 
     double VTR           = Rcpp::as<double>(control["VTR"]);		// value to reach
     int i_strategy       = Rcpp::as<int>(control["strategy"]);    	// chooses DE-strategy
@@ -60,27 +63,17 @@
     int i_storepopfreq   = Rcpp::as<int>(control["storepopfreq"]);  	// How often to store populations 
     int i_specinitialpop = Rcpp::as<int>(control["specinitialpop"]);  	// User-defined inital population 
     Rcpp::NumericVector initialpopv = Rcpp::as<Rcpp::NumericVector>(control["initialpop"]);
-    double f_weight = Rcpp::as<double>(control["F"]);  			// stepsize 
-    double f_cross = Rcpp::as<double>(control["CR"]);  			// crossover probability 
-    int i_bs_flag = Rcpp::as<int>(control["bs"]);   			// Best of parent and child 
-    int i_trace = Rcpp::as<int>(control["trace"]);  			// Print progress? 
-    int i_check_winner = Rcpp::as<int>(control["checkWinner"]); 	// Re-evaluate best parameter vector? 
-    int i_av_winner = Rcpp::as<int>(control["avWinner"]);  		// Average 
-    double i_pPct = Rcpp::as<double>(control["p"]); 			// p to define the top 100p% best solutions 
+    double f_weight      = Rcpp::as<double>(control["F"]);  		// stepsize 
+    double f_cross       = Rcpp::as<double>(control["CR"]);  		// crossover probability 
+    int i_bs_flag        = Rcpp::as<int>(control["bs"]);   		// Best of parent and child 
+    int i_trace          = Rcpp::as<int>(control["trace"]);  		// Print progress? 
+    int i_check_winner   = Rcpp::as<int>(control["checkWinner"]); 	// Re-evaluate best parameter vector? 
+    int i_av_winner      = Rcpp::as<int>(control["avWinner"]);  	// Average 
+    double i_pPct        = Rcpp::as<double>(control["p"]); 		// p to define the top 100p% best solutions 
 
-
-    /* Data structures for parameter vectors */
-    double **gta_popP = (double **)R_alloc(i_NP*2,sizeof(double *));
-    for (int i = 0; i < (i_NP*2); i++) 
-	gta_popP[i] = (double *)R_alloc(i_D,sizeof(double));
-
-    double **gta_oldP = (double **)R_alloc(i_NP,sizeof(double *));
-    for (int i = 0; i < i_NP; i++) 
-	gta_oldP[i] = (double *)R_alloc(i_D,sizeof(double));
-
-    double **gta_newP = (double **)R_alloc(i_NP,sizeof(double *));
-    for (int i = 0; i < i_NP; i++) 
-	gta_newP[i] = (double *)R_alloc(i_D,sizeof(double));
+    arma::mat ta_popP(i_NP*2, i_D);    					// Data structures for parameter vectors 
+    arma::mat ta_oldP(i_NP,   i_D);
+    arma::mat ta_newP(i_NP,   i_D);
   
     Rcpp::NumericVector t_bestP(i_D); 		// double *t_bestP = (double *)R_alloc(1,sizeof(double) * i_D);
 
@@ -106,7 +99,7 @@
 	  i_strategy, i_D, i_NP, i_itermax,
 	  initialpopv.begin(), i_storepopfrom, i_storepopfreq, 
 	  i_specinitialpop, i_check_winner, i_av_winner,
-	  gta_popP, gta_oldP, gta_newP, t_bestP.begin(),
+	  ta_popP, ta_oldP, ta_newP, t_bestP.begin(),
 	  gta_popC, gta_oldC, gta_newC, &t_bestC,
 	  t_bestitP.begin(), t_tmpP.begin(), tempP.begin(),
 	  d_pop.begin(), d_storepop.begin(), d_bestmemit.begin(), d_bestvalit.begin(),
@@ -130,7 +123,10 @@
            int i_strategy, int i_D, int i_NP, int i_itermax,
            double *initialpopv, int i_storepopfrom, int i_storepopfreq, 
            int i_specinitialpop, int i_check_winner, int i_av_winner,
-           double **gta_popP, double **gta_oldP, double **gta_newP, double *gt_bestP,
+           /*double **gta_popP*/ arma::mat &ta_popP, 
+	   /*double **gta_oldP*/ arma::mat &ta_oldP, 
+	   /*double **gta_newP*/ arma::mat &ta_newP, 
+	   double *gt_bestP,
            double *gta_popC, double *gta_oldC, double *gta_newC, double *gt_bestC,
            double *t_bestitP, double *t_tmpP, double *tempP,
            double *gd_pop, double *gd_storepop, double *gd_bestmemit, double *gd_bestvalit,
@@ -174,7 +170,7 @@
 
     GetRNGstate();
 
-    gta_popP[0][0] = 0;
+    ta_popP.at(0,0) = 0;
     
     /* initialize initial popuplation */
     for (int i = 0; i < i_NP; i++) {
@@ -223,25 +219,25 @@
   for (i = 0; i < i_NP; i++) {
     for (j = 0; j < i_D; j++) {
       if (i_specinitialpop <= 0) { /* random initial member */
-        gta_popP[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 */
 	  //gta_popP[i][j] = initialpop[i][j];
-	  gta_popP[i][j] = initialpop.at(i,j);
+	  ta_popP.at(i,j) = initialpop.at(i,j);
     } 
-    gta_popC[i] = evaluate(l_nfeval, gta_popP[i], par, fcall, rho);
+    arma::rowvec r = ta_popP.row(i);
+    gta_popC[i] = evaluate(l_nfeval, r.memptr(), par, fcall, rho);
 
     if (i == 0 || gta_popC[i] <= gt_bestC[0]) {
       gt_bestC[0] = gta_popC[i];
       for (j = 0; j < i_D; j++)  
-        gt_bestP[j]=gta_popP[i][j];
+	  gt_bestP[j] = ta_popP.at(i,j);
     }
   }
 
   /*---assign pointers to current ("old") population---*/
-  gta_oldP = gta_popP;
+  ta_oldP = ta_popP;
   gta_oldC = gta_popC;
   
   /*------Iteration loop--------------------------------------------*/
@@ -257,8 +253,8 @@
       if (i_iter % i_storepopfreq == 0 && i_iter >= i_storepopfrom) {
 	for (i = 0; i < i_NP; i++) {
 	  for (j = 0; j < i_D; j++) {
-	    gd_storepop[popcnt] = gta_oldP[i][j];
-	    popcnt++;
+	      gd_storepop[popcnt] = ta_oldP.at(i,j);
+	      popcnt++;
 	  }
 	}
       } /* end store pop */
@@ -295,7 +291,7 @@
 
 	/*t_tmpP is the vector to mutate and eventually select*/
 	for (j = 0; j < i_D; j++) 
-	  t_tmpP[j] = gta_oldP[i][j];
+	    t_tmpP[j] = ta_oldP.at(i,j);
 	t_tmpC = gta_oldC[i];
 
 	permute(ia_urn2, urn_depth, i_NP, i, ia_urntmp.begin()); /* Pick 4 random and distinct */
@@ -312,9 +308,8 @@
 	  j = (int)(unif_rand() * i_D); /* random parameter */
 	  k = 0;
 	  do {
-	    /* add fluctuation to random target */
-	    t_tmpP[j] = gta_oldP[i_r1][j] +
-	      f_weight * (gta_oldP[i_r2][j] - gta_oldP[i_r3][j]);
+	      /* 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;
 	    k++;
@@ -329,9 +324,7 @@
 	  do {
 	    /* add fluctuation to random target */
 	   
-	    t_tmpP[j] = t_tmpP[j] + 
-	      f_weight * (t_bestitP[j] - t_tmpP[j]) +
-	      f_weight * (gta_oldP[i_r2][j] - gta_oldP[i_r3][j]);
+	    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;
 	    k++;
 	  }while((unif_rand() < f_cross) && (k < i_D));
@@ -345,8 +338,7 @@
 	  do {
 	    /* add fluctuation to random target */
 	    f_jitter = 0.0001 * unif_rand() + f_weight;
-	    t_tmpP[j] = t_bestitP[j] +
-	      f_jitter * (gta_oldP[i_r1][j] - gta_oldP[i_r2][j]);
+	    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;
 	    k++;
@@ -359,10 +351,8 @@
 	  j = (int)(unif_rand() * i_D); /* random parameter */
 	  k = 0;
 	  do {
-	    /* add fluctuation to random target */
-	    t_tmpP[j] = gta_oldP[i_r1][j] +
-	      (f_weight + unif_rand()*(1.0 - f_weight))*
-	      (gta_oldP[i_r2][j]-gta_oldP[i_r3][j]);
+	      /* 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));
 
 	    j = (j + 1) % i_D;
 	    k++;
@@ -375,12 +365,11 @@
 	  j = (int)(unif_rand() * i_D); /* random parameter */
 	  k = 0;
 	  do {
-	    /* add fluctuation to random target */
-	    t_tmpP[j] = gta_oldP[i_r1][j] +
-	      f_dither * (gta_oldP[i_r2][j] - gta_oldP[i_r3][j]);
+	      /* 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;
-	    k++;
+	      j = (j + 1) % i_D;
+	      k++;
 	  }while((unif_rand() < f_cross) && (k < i_D));
        
 	}
@@ -393,13 +382,11 @@
           j = (int)(unif_rand() * i_D); /* random parameter */
 	  k = 0;
 	  do {
-	    /* add fluctuation to random target */
-	    t_tmpP[j] = gta_oldP[i][j] +
-              f_weight * (gta_oldP[i_pbest][j] - gta_oldP[i][j]) +
-              f_weight * (gta_oldP[i_r1][j]    - gta_oldP[i_r2][j]);
+	      /* 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;
-	    k++;
+	      j = (j + 1) % i_D;
+	      k++;
 	  }while((unif_rand() < f_cross) && (k < i_D));
 
         }
@@ -410,21 +397,18 @@
 	  k = 0;
 	  if (unif_rand() < 0.5) { /* differential mutation, Pmu = 0.5 */
 	    do {
-	      /* add fluctuation to random target */
-	      t_tmpP[j] = gta_oldP[i_r1][j] +
-		f_weight * (gta_oldP[i_r2][j] - gta_oldP[i_r3][j]);
+		/* 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;
-	      k++;
+		j = (j + 1) % i_D;
+		k++;
 	    }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] = gta_oldP[i_r1][j] +
-		0.5 * (f_weight + 1.0) * (gta_oldP[i_r2][j]
-					  + gta_oldP[i_r3][j] - 2 * gta_oldP[i_r1][j]);
+		/* 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;
 	      k++;
@@ -453,19 +437,19 @@
 	/* note that i_bs_flag means that we will choose the
 	 *best NP vectors from the old and new population later*/
 	if (t_tmpC <= gta_oldC[i] || i_bs_flag) {
-	  /* replace target with mutant */
-	  for (j = 0; j < i_D; j++) 
-	    gta_newP[i][j]=t_tmpP[j];
-	  gta_newC[i]=t_tmpC;
-	  if (t_tmpC <= gt_bestC[0]) {
+	    /* replace target with mutant */
 	    for (j = 0; j < i_D; j++) 
-	      gt_bestP[j]=t_tmpP[j];
-	    gt_bestC[0]=t_tmpC;
+		ta_newP.at(i,j) = t_tmpP[j];
+	    gta_newC[i] = t_tmpC;
+	    if (t_tmpC <= gt_bestC[0]) {
+		for (j = 0; j < i_D; j++) 
+		    gt_bestP[j]=t_tmpP[j];
+		gt_bestC[0]=t_tmpC;
 	  }
 	} 
 	else {
 	  for (j = 0; j < i_D; j++) 
-	    gta_newP[i][j]=gta_oldP[i][j];
+	      ta_newP.at(i,j) = ta_oldP.at(i,j);
 	  gta_newC[i]=gta_oldC[i];
 	  
 	}
@@ -477,12 +461,12 @@
 	 * into next generation */
 	for (i = 0; i < i_NP; i++) {
 	  for (j = 0; j < i_D; j++) 
-	    gta_popP[i][j] = gta_oldP[i][j];
+	      ta_popP.at(i,j) = ta_oldP.at(i,j);
 	  gta_popC[i] = gta_oldC[i];
 	}
 	for (i = 0; i < i_NP; i++) {
 	  for (j = 0; j < i_D; j++) 
-	    gta_popP[i_NP+i][j] = gta_newP[i][j];
+	      ta_popP.at(i_NP+i,j) = ta_newP.at(i,j);
 	  gta_popC[i_NP+i] = gta_newC[i];
 	}
 	i_len = 2 * i_NP;
@@ -496,13 +480,13 @@
 		i = j + step + 1;
 		if (gta_popC[j] > gta_popC[i-1]) {
 		    for (k = 0; k < i_D; k++) 
-		      tempP[k] = gta_popP[i-1][k];
+			tempP[k] = ta_popP.at(i-1, k);
 		    tempC = gta_popC[i-1];
 		    for (k = 0; k < i_D; k++) 
-		      gta_popP[i-1][k] = gta_popP[j][k];
+			ta_popP.at(i-1,k) = ta_popP.at(j,k);
 		    gta_popC[i-1] = gta_popC[j];
 		    for (k = 0; k < i_D; k++) 
-		      gta_popP[j][k] = tempP[k];
+			ta_popP.at(j,k) = tempP[k];
 		    gta_popC[j] = tempC;
 		      done = 0; 
 		      /* if a swap has been made we are not finished yet */
@@ -513,7 +497,7 @@
 	/* now the best NP are in first NP places in gta_pop, use them */
 	for (i = 0; i < i_NP; i++) {
 	  for (j = 0; j < i_D; j++) 
-	    gta_newP[i][j] = gta_popP[i][j];
+	      ta_newP.at(i,j) = ta_popP.at(i,j);
 	  gta_newC[i] = gta_popC[i];
 	}
       } /*i_bs_flag*/
@@ -521,7 +505,7 @@
       /* have selected NP mutants move on to next generation */
       for (i = 0; i < i_NP; i++) {
 	for (j = 0; j < i_D; j++) 
-	  gta_oldP[i][j] = gta_newP[i][j];
+	    ta_oldP.at(i,j) = ta_newP.at(i,j);
 	gta_oldC[i] = gta_newC[i];
       }
       /* check if the best stayed the same, if necessary */
@@ -567,8 +551,8 @@
   k = 0;
   for (i = 0; i < i_NP; i++) {
     for (j = 0; j < i_D; j++) {
-      gd_pop[k] = gta_oldP[i][j];      
-      k++;
+	gd_pop[k] = ta_oldP.at(i,j);      
+	k++;
     }
   }
 



More information about the Rcpp-commits mailing list