[Rcpp-commits] r2769 - pkg/RcppDE/inst/doc
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Dec 11 21:00:25 CET 2010
Author: edd
Date: 2010-12-11 21:00:24 +0100 (Sat, 11 Dec 2010)
New Revision: 2769
Modified:
pkg/RcppDE/inst/doc/Rcpp.Rnw
Log:
updated
Modified: pkg/RcppDE/inst/doc/Rcpp.Rnw
===================================================================
--- pkg/RcppDE/inst/doc/Rcpp.Rnw 2010-12-11 19:35:14 UTC (rev 2768)
+++ pkg/RcppDE/inst/doc/Rcpp.Rnw 2010-12-11 20:00:24 UTC (rev 2769)
@@ -17,7 +17,7 @@
\Abstract{
\noindent
\pkg{DEoptim} \citep{MullenArdiaEtAl:2009:DEoptim,ArdiaBoudtCarlEtAl:2010:DEoptim,CRAN:DEoptim}
- provides differential evolution optimisation for
+ provides differential evolution optimisation for
\proglang{R}. It is based on an implementation by Storn
\citep{PriceStornLampinen:2006:DE} and was originally implemented as an
interpreted \proglang{R} script. It was then rewritten in ANSI C which
@@ -60,6 +60,20 @@
\begin{document}
+\SweaveOpts{engine=R,eps=FALSE,echo=FALSE,prefix.string=figures/chart}
+
+<<prelim,echo=FALSE,print=FALSE>>=
+options(width=50)
+library(RcppDE)
+RcppDE.version <- packageDescription("RcppDE")$Version
+RcppDE.date <- packageDescription("RcppDE")$Date
+now.date <- strftime(Sys.Date(), "%B %d, %Y")
+# create figures/ if not present
+if ( ! (file.exists("figures") && file.info("figures")$isdir) ) dir.create("figures")
+@
+%
+
+
\section{Introduction}
\pkg{DEoptim}
@@ -121,7 +135,7 @@
also offers further possible gains from template metaprogramming which can
result in the elimination of temporary object in complex expression where,
loosely speaking, compile-time effort is substituted to gain later run-time
- performance.
+ performance.
\end{itemize}
This paper is organised as follows. The next sections describes the structure
@@ -141,11 +155,11 @@
made to the \proglang{C} files: besides the obvious porting from \proglang{C}
to \proglang{C++}, several internal code changes were made. We discuss these
changes below. An important point to note is that the overall architecture
-and API remain as unchanged as possible.
+and API remain as unchanged as possible.
%
On the other hand, very few changes were required at the \proglang{R}
level. The user-facing side of \pkg{DEoptim} persists virtually unchanged
-(with one or two changes discussed below).
+(with one or two changes discussed below).
Because of the dominant number of changes at the level of the compiled
languages, we discuss the structure, and later on changes, of this part first
@@ -158,9 +172,9 @@
\begin{tabular}{lrclr}
\toprule
\multicolumn{2}{c}{\pkg{DEoptim}} & & \multicolumn{2}{c}{\pkg{RcppDE}} \\
- File & Functions & & File & Functions\\
+ File & Functions & & File & Functions\\
% \cmidrule{1-2,3-4} \\
- \midrule
+ \midrule
\verb|de4_0.c| & \verb|DEoptimC()| & & \verb|deoptim.cpp| & \verb|DEoptim()| \\
& \verb|devol()| & & \verb|devol.cpp| & \verb|devol()| \\
& \verb|permute()| & & \verb|permute.cpp| & \verb|permute()| \\[6pt]
@@ -175,12 +189,12 @@
\end{table}
Table~\ref{tab:Cfiles} lists the \proglang{C} and \proglang{C++}
-files in \pkg{DEoptim} and \pkg{RcppDE}, respectively.
+files in \pkg{DEoptim} and \pkg{RcppDE}, respectively.
The large file \verb|de4_0.c| has been split into three files: one each for
the core functions \verb|DEoptim()| (which is called from \proglang{R}),
\verb|devol()| (which is the core differential evolution optimisation
routine) and \verb|permute()| (which is a helper function used to shuffle
-indices).
+indices).
The evalution function has been replaced by a base class and two virtual
classes. These can now use of an objective function written in \proglang{R}
@@ -201,7 +215,7 @@
\begin{center}
\begin{tabular}{lr}
\toprule
- File & Functions \\
+ File & Functions \\
\midrule
% \cmidrule{2}
\verb|DEoptim.R| \phantom{XXXXX} & \verb|DEoptim()| \\
@@ -236,7 +250,7 @@
The \verb|DEoptim()| function (renamed from \verb|DEoptim_C()| as there is no need for a different
name at the \proglang{C} level relative to \proglang{R}) is the entry point
from \proglang{R}. It receives parameters, sets up the call of \verb|devol()|
-and then prepares the return values.
+and then prepares the return values.
\paragraph{Part 1: Start of \texttt{DEoptim()}} The first part concerns
itself with receiving parameters from \proglang{R};
@@ -354,7 +368,7 @@
strategy six has been selected as it is not used otherwise.
\end{enumerate}
-\paragraph{Part 2: Initializations in \texttt{devol()}}
+\paragraph{Part 2: Initializations in \texttt{devol()}}
The second part of \verb|devol()| deals with the creation and initialization
of a number of variables. The \proglang{C} language code in panel A is
@@ -380,7 +394,7 @@
compiled function, or the supplied \proglang{R} functions.
\end{enumerate}
-\paragraph{Part 3: Iteration loop setup and start of population loop in \texttt{devol()}}
+\paragraph{Part 3: Iteration loop setup and start of population loop in \texttt{devol()}}
The next part of \verb|devol()|, shown in figure~\ref{fig:devol_iter}, starts
both the main outer loop over all iterations as well as the main inner loop
@@ -392,7 +406,7 @@
transposed to account for our design choice of operating column-wise. In
the \proglang{C} code, the matrices are somewhat awkwardly `serialised'
into a single vector using the counter \texttt{popcnt} that incremened
- position by position.
+ position by position.
\item Several other vector copies are each excecuted in a single statement rather
than in an explicit loop.
\item At the beginning of the population loop, a vector is once more stored
@@ -400,7 +414,7 @@
suitable indices which will be used next.
\end{enumerate}
-\paragraph{Part 4 and 5: Population strategies in \texttt{devol()}}
+\paragraph{Part 4 and 5: Population strategies in \texttt{devol()}}
Evaluating each population member based on the user-selected strategies is
detailed in both figures~\ref{fig:devol_first_four} and
@@ -418,7 +432,7 @@
initial \proglang{C} implementation and the newer \proglang{C++} code.`4
\end{enumerate}
-\paragraph{Part 6: End of population loop in \texttt{devol()}}
+\paragraph{Part 6: End of population loop in \texttt{devol()}}
Figure~\ref{fig:devol_end_pop} contains two fairly short segments that are
entered once within each outer iteration after the loop over all population
@@ -427,10 +441,10 @@
once again the element-by-element copy of vector elements (in \proglang{C})
versus the single statement using \proglang{C++} objects.
-\paragraph{Part 7: Special case of \texttt{bs} flag in \texttt{devol()}}
+\paragraph{Part 7: Special case of \texttt{bs} flag in \texttt{devol()}}
Similarly, figure~\ref{fig:devol_bs_flag} once more shows differences chiefly
-due to the way interim solutions are copied.
+due to the way interim solutions are copied.
\begin{enumerate}
\item Panel A has a full nine loops for copying vector or matrix elements
which are not needed in panel B.
@@ -445,7 +459,7 @@
default value of false for the \texttt{bs} option in the control list for
\texttt{DEoptim()}.
-\paragraph{Part 8: End of \texttt{devol()}}
+\paragraph{Part 8: End of \texttt{devol()}}
Finallt, figure~\ref{fig:devol_return} contains the final portion of the
\verb|devol()| function. The population and its fitness value are saved. If
@@ -455,7 +469,7 @@
Next, if tracing is enabling and the iteration counter has a value which
signals that tracing display should occur, then updates are printed before a
-few state variables are updated.
+few state variables are updated.
%
The \texttt{devol()} then finishes right after restoring the state of the
random number generator.
@@ -483,7 +497,7 @@
parameters is prepared and the evaluated in an environment. Here, the
function and the environment are supplied once at the beginning---and hence
used to instantiate the class. Each evaluation then brings a new parameter
-vector.
+vector.
The class \texttt{EvalCompiled} does the same, but not for the compiled
function that we access via an external pointer. The support for external
@@ -510,12 +524,12 @@
would.
\item \pkg{RcppDE} therefore has an additional argument \texttt{env} for the
user-supplied environement, as well as an additional creation of a default
- environment if none was supplied.
+ environment if none was supplied.
\item Population matrices are passed from \proglang{C++} to \proglang{R} as
matrix objects; no copy or rearrangement has to be undertaken. This saves
a block of code at the top of panel B in figure~\ref{fig:fig_R_DEoptim2}.
Similarly, we do not have cast the population matrix as we already obtain a
- matrix.
+ matrix.
\end{enumerate}
None of the other functions from the files listed in table~\ref{tab:Rfiles}
@@ -530,7 +544,7 @@
As of release 0.1.0, full regression testing has not been implemented in
\pkg{RcppDE} (and none exist in \pkg{DEoptim} either as of the released
-version 2.0.7).
+version 2.0.7).
However, a directory \verb|tests/| has been added. It contains the file
\verb|compTest.R| which provides a first means of both \textsl{comparing}
@@ -567,7 +581,7 @@
\subsection{Benchmarking Scripts}
The demos file from the preceding section are also being used for performance
-comparisons (as detailed in the next section).
+comparisons (as detailed in the next section).
The files are organised as thin wrapper scripts around the demo files
described in the preceding section.
@@ -580,6 +594,51 @@
\subsection{Small problems}
+<<smallRes,fig=TRUE,width=11>>=
+## # small benchmark at SVN 2419M
+## # At 2010-11-08 06:42:29.018531
+smallLines <- "
+ DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp
+Rastrigin5 0.10912 0.099875 0.91523 8.4765 1.0926
+Rastrigin10 0.23738 0.214875 0.90521 9.4787 1.1047
+Rastrigin20 0.55587 0.501500 0.90218 9.7819 1.1084
+Wild5 0.18288 0.171875 0.93985 6.0150 1.0640
+Wild10 0.40912 0.391125 0.95600 4.3996 1.0460
+Wild20 1.04513 0.987375 0.94474 5.5257 1.0585
+Genrose5 0.18913 0.179250 0.94779 5.2214 1.0551
+Genrose10 0.39538 0.374625 0.94752 5.2482 1.0554
+Genrose20 0.90050 0.848375 0.94212 5.7885 1.0614
+"
+## MEANS 0.44717 0.418764 0.93648 6.3517 1.0678
+## # Done 2010-11-08 06:43:50.88171
+
+con <- textConnection(smallLines)
+smallData <- read.table(con, header=TRUE, sep="")
+close(con)
+
+sb <- trellis.par.get("strip.background")
+sb[["col"]][1:2] <- c("gray80","gray90")
+trellis.par.set("strip.background", sb)
+
+# dput(brewer.pal(7, "Set1"))
+.cols <- c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
+ "#FF7F00", "#FFF33", "#A65628")[-6]
+
+ss <- trellis.par.get("superpose.symbol")
+ss[["col"]][1:6] <- .cols
+ss[["cex"]] <- rep(1.0, 7)
+ss[["pch"]] <- rep(19, 7)
+ss[["alpha"]] <- rep(0.75, 7)
+trellis.par.set("superpose.symbol", ss)
+
+smallWide <- data.frame(val=c(smallData[,1,drop=TRUE], smallData[,2,drop=TRUE]),
+ pkg=rep(c("DEoptim", "RcppDE"), each=9),
+ fun=rep(rep(c("Rastrigin", "Wild", "Genrose"), each=3), 2),
+ n=rep(c(5,10,20), 6))
+print(dotplot(as.factor(n) ~val | fun, group=pkg, data=smallWide, layout=c(1,3),
+ key=simpleKey(text=c("RcppDE","DEoptim"), space="top", col=2)))
+@
+
\subsection{Large problems}
\subsection{Compiled objective function}
@@ -654,9 +713,9 @@
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel A: \proglang{C} version}
- \tiny
+ \tiny
\end{minipage}
\begin{minipage}{0.03\linewidth}
@@ -668,7 +727,7 @@
\begin{CodeChunk}
\begin{CodeInput}
RcppExport SEXP DEoptim(SEXP lowerS, SEXP upperS, SEXP fnS, SEXP controlS, SEXP rhoS) {
-
+
try {
Rcpp::NumericVector f_lower(lowerS), f_upper(upperS); // User-defined bounds
Rcpp::List control(controlS); // named list of params
@@ -679,21 +738,21 @@
long l_nfeval = 0; // nb of function evals (NOT passed in)
int i_D = Rcpp::as<int>(control["npar"]); // Dimension of parameter vector
int i_NP = Rcpp::as<int>(control["NP"]); // Number of population members
- int i_storepopfrom = Rcpp::as<int>(control["storepopfrom"]) - 1; // When to start storing populations
- 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
+ int i_storepopfrom = Rcpp::as<int>(control["storepopfrom"]) - 1; // When to start storing populations
+ 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::NumericMatrix initialpopm = Rcpp::as<Rcpp::NumericMatrix>(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
\end{CodeInput}
\end{CodeChunk}
-
+
\normalsize
\centering{Panel B: \proglang{C++} version using \pkg{Rcpp}}
\end{minipage}
@@ -708,17 +767,17 @@
\begin{CodeInput}
/* 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++)
+ 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++)
+ 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++)
+ for (int i = 0; i < i_NP; i++)
gta_newP[i] = (double *)R_alloc(i_D,sizeof(double));
-
+
double *gt_bestP = (double *)R_alloc(1,sizeof(double) * i_D);
/* Data structures for objective function values associated with
@@ -741,9 +800,9 @@
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel A: \proglang{C} version}
- \tiny
+ \tiny
\end{minipage}
\begin{minipage}{0.03\linewidth}
\phantom{XX}
@@ -757,24 +816,24 @@
arma::colvec maxbound(f_upper.begin(), f_upper.size(), false);
arma::mat initpopm(initialpopm.begin(), initialpopm.rows(), initialpopm.cols(), false);
- arma::mat ta_popP(i_D, i_NP*2); // Data structures for parameter vectors
+ arma::mat ta_popP(i_D, i_NP*2); // Data structures for parameter vectors
arma::mat ta_oldP(i_D, i_NP);
arma::mat ta_newP(i_D, i_NP);
- arma::colvec t_bestP(i_D);
+ arma::colvec t_bestP(i_D);
- arma::colvec ta_popC(i_NP*2); // Data structures for obj. fun. values
+ arma::colvec ta_popC(i_NP*2); // Data structures for obj. fun. values
arma::colvec ta_oldC(i_NP);
arma::colvec ta_newC(i_NP);
- double t_bestC;
+ double t_bestC;
arma::colvec t_bestitP(i_D);
- arma::colvec t_tmpP(i_D);
+ arma::colvec t_tmpP(i_D);
int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
- arma::mat d_pop(i_D, i_NP);
+ arma::mat d_pop(i_D, i_NP);
Rcpp::List d_storepop(i_nstorepop);
- arma::mat d_bestmemit(i_D, i_itermax);
- arma::colvec d_bestvalit(i_itermax);
+ arma::mat d_bestmemit(i_D, i_itermax);
+ arma::colvec d_bestvalit(i_itermax);
int i_iter = 0;
\end{CodeInput}
\end{CodeChunk}
@@ -795,7 +854,7 @@
/*---optimization--------------------------------------*/
devol(VTR, f_weight, f_cross, i_bs_flag, f_lower, f_upper, fn, rho, i_trace,
i_strategy, i_D, i_NP, i_itermax,
- initialpopv, i_storepopfrom, i_storepopfreq,
+ initialpopv, i_storepopfrom, i_storepopfreq,
i_specinitialpop, i_check_winner, i_av_winner,
gta_popP, gta_oldP, gta_newP, gt_bestP,
gta_popC, gta_oldC, gta_newC, gt_bestC,
@@ -821,7 +880,7 @@
j = gi_iter * i_D;
PROTECT(sexp_bestmemit = NEW_NUMERIC(j));
- for (i = 0; i < j; i++)
+ for (i = 0; i < j; i++)
NUMERIC_POINTER(sexp_bestmemit)[i] = gd_bestmemit[i];
j = gi_iter;
PROTECT(sexp_bestvalit = NEW_NUMERIC(j));
@@ -850,9 +909,9 @@
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel A: \proglang{C} version}
- \tiny
+ \tiny
\end{minipage}
\begin{minipage}{0.03\linewidth}
\phantom{XX}
@@ -881,9 +940,9 @@
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel A: \proglang{C} version (in both columns)}
- \tiny
+ \tiny
\bigskip
\phantom{XX}
@@ -897,7 +956,7 @@
\begin{CodeChunk}
\begin{CodeInput}
// call actual Differential Evolution optimization given the parameters
- devol(VTR, f_weight, f_cross, i_bs_flag, minbound, maxbound, fnS, rhoS, i_trace, i_strategy, i_D, i_NP,
+ devol(VTR, f_weight, f_cross, i_bs_flag, minbound, maxbound, fnS, rhoS, i_trace, i_strategy, i_D, i_NP,
i_itermax, initpopm, i_storepopfrom, i_storepopfreq, i_specinitialpop, i_check_winner, i_av_winner,
ta_popP, ta_oldP, ta_newP, t_bestP, ta_popC, ta_oldC, ta_newC, t_bestC, t_bestitP, t_tmpP,
d_pop, d_storepop, d_bestmemit, d_bestvalit, i_iter, i_pPct, l_nfeval);
@@ -909,19 +968,19 @@
Rcpp::Named("bestmemit") = trans(d_bestmemit),
Rcpp::Named("bestvalit") = d_bestvalit,
Rcpp::Named("pop") = trans(d_pop),
- Rcpp::Named("storepop") = d_storepop);
+ Rcpp::Named("storepop") = d_storepop);
- } catch( std::exception& ex) {
- forward_exception_to_r(ex);
- } catch(...) {
- ::Rf_error( "c++ exception (unknown reason)");
+ } catch( std::exception& ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error( "c++ exception (unknown reason)");
}
return R_NilValue;
}
\end{CodeInput}
\end{CodeChunk}
-
- \normalsize
+
+ \normalsize
\centering{Panel B: \proglang{C++} version using \pkg{Rcpp}}
\end{minipage}
\caption{\code{DEoptim()} call of \code{devol()} and return of results to \proglang{R}}
@@ -936,9 +995,9 @@
\begin{CodeChunk}
\begin{CodeInput}
void devol(double VTR, double f_weight, double f_cross, int i_bs_flag,
- double *lower, double *upper, SEXP fcall, SEXP rho, int trace,
+ double *lower, double *upper, SEXP fcall, SEXP rho, int trace,
int i_strategy, int i_D, int i_NP, int i_itermax,
- double *initialpopv, int i_storepopfrom, int i_storepopfreq,
+ 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_popC, double *gta_oldC, double *gta_newC, double *gt_bestC,
@@ -951,25 +1010,25 @@
/* initialize parameter vector to pass to evaluate function */
SEXP par; PROTECT(par = NEW_NUMERIC(i_D));
-
+
int i, j, k, x; /* counting variables */
int i_r1, i_r2, i_r3, i_r4; /* placeholders for random indexes */
int ia_urn2[URN_DEPTH];
int i_nstorepop, i_xav;
i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
-
+
int popcnt, bestacnt, same; /* lazy cnters */
double *fa_minbound = lower;
double *fa_maxbound = upper;
double f_jitter, f_dither;
-
+
double t_bestitC;
- double t_tmpC, tmp_best;
-
+ double t_tmpC, tmp_best;
+
double initialpop[i_NP][i_D];
-
+
/* vars for DE/current-to-p-best/1 */
int i_pbest;
int p_NP = round(i_pPct * i_NP); /* choose at least two best solutions */
@@ -986,9 +1045,9 @@
gta_popP[0][0] = 0;
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel A: \proglang{C} version}
- \tiny
+ \tiny
\end{minipage}
\begin{minipage}{0.03\linewidth}
\phantom{XX}
@@ -1000,38 +1059,38 @@
\begin{CodeInput}
void devol(double VTR, double f_weight, double f_cross, int i_bs_flag,
arma::colvec & fa_minbound, arma::colvec & fa_maxbound, SEXP fcall, SEXP rho, int i_trace,
- int i_strategy, int i_D, int i_NP, int i_itermax, arma::mat & initialpopm,
+ int i_strategy, int i_D, int i_NP, int i_itermax, arma::mat & initialpopm,
int i_storepopfrom, int i_storepopfreq, int i_specinitialpop, int i_check_winner, int i_av_winner,
- arma::mat &ta_popP, arma::mat &ta_oldP, arma::mat &ta_newP, arma::colvec & t_bestP,
+ arma::mat &ta_popP, arma::mat &ta_oldP, arma::mat &ta_newP, arma::colvec & t_bestP,
arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, double & t_bestC,
- arma::colvec & t_bestitP, arma::colvec & t_tmpP,
+ arma::colvec & t_bestitP, arma::colvec & t_tmpP,
arma::mat &d_pop, Rcpp::List &d_storepop, arma::mat & d_bestmemit, arma::colvec & d_bestvalit,
int & i_iterations, double i_pPct, long & l_nfeval) {
Rcpp::DE::EvalBase *ev = NULL; // pointer to abstract base class
if (TYPEOF(fcall) == EXTPTRSXP) { // non-standard mode: we are being passed an external pointer
ev = new Rcpp::DE::EvalCompiled(fcall); // so assign a pointer using external pointer in fcall SEXP
- } else { // standard mode: env_ is an env, fcall_ is a function
+ } else { // standard mode: env_ is an env, fcall_ is a function
ev = new Rcpp::DE::EvalStandard(fcall, rho); // so assign R function and environment
}
- const int urn_depth = 5; // 4 + one index to avoid
- Rcpp::NumericVector par(i_D); // initialize parameter vector to pass to evaluate function
+ const int urn_depth = 5; // 4 + one index to avoid
+ Rcpp::NumericVector par(i_D); // initialize parameter vector to pass to evaluate function
arma::icolvec::fixed<urn_depth> ia_urn2; // fixed-size vector for urn draws
arma::icolvec ia_urntmp(i_NP); // so that we don't need to re-allocated each time in permute
- arma::mat initialpop(i_D, i_NP);
+ arma::mat initialpop(i_D, i_NP);
int i_nstorepop = ceil((i_itermax - i_storepopfrom) / i_storepopfreq);
- int p_NP = round(i_pPct * i_NP); // choose at least two best solutions
+ int p_NP = round(i_pPct * i_NP); // choose at least two best solutions
p_NP = p_NP < 2 ? 2 : p_NP;
- arma::icolvec sortIndex(i_NP); // sorted values of ta_oldC
+ arma::icolvec sortIndex(i_NP); // sorted values of ta_oldC
if (i_strategy == 6) {
- for (int i = 0; i < i_NP; i++)
- sortIndex[i] = i;
+ for (int i = 0; i < i_NP; i++)
+ sortIndex[i] = i;
}
GetRNGstate();
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel B: \proglang{C++} version using \pkg{Rcpp}}
\end{minipage}
\caption{\code{devol()} beginning}
@@ -1070,11 +1129,11 @@
for (int i = 0; i < (i_nstorepop * i_NP * i_D); i++)
gd_storepop[i] = 0.0;
-
+
/* if initial population provided, initialize with values */
if (i_specinitialpop > 0) {
k = 0;
-
+
for (j = 0; j < i_D; j++) {
for (i = 0; i < i_NP; i++) {
initialpop[i][j] = initialpopv[k];
@@ -1097,19 +1156,19 @@
}
else /* or user-specified initial member */
gta_popP[i][j] = initialpop[i][j];
- }
+ }
gta_popC[i] = evaluate(l_nfeval, gta_popP[i], 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++)
+ for (j = 0; j < i_D; j++)
gt_bestP[j]=gta_popP[i][j];
}
}
\end{CodeInput}
\end{CodeChunk}
- %\normalsize \centering{Panel A: \proglang{C} version} \tiny
+ %\normalsize \centering{Panel A: \proglang{C} version} \tiny
\end{minipage}
\begin{minipage}{0.03\linewidth}
\phantom{XX}
@@ -1123,7 +1182,7 @@
/*---assign pointers to current ("old") population---*/
gta_oldP = gta_popP;
gta_oldC = gta_popC;
-
+
/*------Iteration loop--------------------------------------------*/
int i_iter = 0;
popcnt = 0;
@@ -1132,9 +1191,9 @@
\end{CodeInput}
\end{CodeChunk}
- \normalsize
+ \normalsize
\centering{Panel A: \proglang{C} version (in both columns)}
- \tiny
+ \tiny
\bigskip
\phantom{XX}
@@ -1147,25 +1206,25 @@
\begin{CodeChunk}
\begin{CodeInput}
- initialpop.zeros(); // initialize initial popuplation
+ initialpop.zeros(); // initialize initial popuplation
d_bestmemit.zeros(); // initialize best members
- d_bestvalit.zeros(); // initialize best values
+ d_bestvalit.zeros(); // initialize best values
d_pop.zeros(); // initialize best population
i_nstorepop = (i_nstorepop < 0) ? 0 : i_nstorepop;
-
- if (i_specinitialpop > 0) { // if initial population provided, initialize with values
+
+ if (i_specinitialpop > 0) { // if initial population provided, initialize with values
initialpop = trans(initialpopm); // transpose as we prefer columns for population members here
}
for (int i = 0; i < i_NP; i++) { // ------Initialization-----------------------------
- if (i_specinitialpop <= 0) { // random initial member
+ if (i_specinitialpop <= 0) { // random initial member
for (int j = 0; j < i_D; j++) {
ta_popP.at(j,i) = fa_minbound[j] + ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]);
}
- } else { // or user-specified initial member
+ } else { // or user-specified initial member
ta_popP.col(i) = initialpop.col(i);
- }
- memcpy(REAL(par), ta_popP.colptr(i), Rf_nrows(par) * sizeof(double));
+ }
+ memcpy(REAL(par), ta_popP.colptr(i), Rf_nrows(par) * sizeof(double));
ta_popC[i] = ev->eval(par);
if (i == 0 || ta_popC[i] <= t_bestC) {
t_bestC = ta_popC[i];
@@ -1175,14 +1234,14 @@
ta_oldP = ta_popP.cols(0, i_NP-1); // ---assign pointers to current ("old") population---
ta_oldC = ta_popC.rows(0, i_NP-1);
-
+
int i_iter = 0; // ------Iteration loop--------------------------------------------
int popcnt = 0;
int i_xav = 1;
\end{CodeInput}
\end{CodeChunk}
-
- \normalsize
+
+ \normalsize
\centering{Panel B: \proglang{C++} version using \pkg{Rcpp}}
\end{minipage}
\caption{\code{devol()} initializations}
@@ -1209,7 +1268,7 @@
}
}
} /* end store pop */
-
+
/* store the best member */
for(j = 0; j < i_D; j++) {
gd_bestmemit[bestacnt] = gt_bestP[j];
@@ -1217,22 +1276,22 @@
}
/* store the best value */
gd_bestvalit[i_iter] = gt_bestC[0];
-
- for (j = 0; j < i_D; j++)
+
+ for (j = 0; j < i_D; j++)
t_bestitP[j] = gt_bestP[j];
t_bestitC = gt_bestC[0];
-
+
i_iter++;
-
+
/*----computer dithering factor -----------------*/
f_dither = f_weight + unif_rand() * (1.0 - f_weight);
-
+
/*---DE/current-to-p-best/1 ---------------------------------*/
if (i_strategy == 6) {
/* create a copy of gta_oldC to avoid changing it */
double temp_oldC[i_NP];
for(j = 0; j < i_NP; j++) temp_oldC[j] = gta_oldC[j];
-
+
/* sort temp_oldC to use sortIndex later */
rsort_with_index( (double*)temp_oldC, (int*)sortIndex, i_NP );
}
@@ -1241,7 +1300,7 @@
for (i = 0; i < i_NP; i++) {
/*t_tmpP is the vector to mutate and eventually select*/
- for (j = 0; j < i_D; j++)
+ for (j = 0; j < i_D; j++)
t_tmpP[j] = gta_oldP[i][j];
t_tmpC = gta_oldC[i];
@@ -1254,7 +1313,7 @@
\end{CodeInput}
\end{CodeChunk}
- \normalsize \centering{Panel A: \proglang{C} version} \tiny
+ \normalsize \centering{Panel A: \proglang{C} version} \tiny
\end{minipage}
\begin{minipage}{0.03\linewidth}
\phantom{XX}
@@ -1267,29 +1326,29 @@
while ((i_iter < i_itermax) && (t_bestC > VTR)) { // main loop ====================================
if (i_iter % i_storepopfreq == 0 && i_iter >= i_storepopfrom) { // store intermediate populations
d_storepop[popcnt++] = Rcpp::wrap( trans(ta_oldP) );
- } // end store pop
+ } // end store pop
d_bestmemit.col(i_iter) = t_bestP; // store the best member
- d_bestvalit[i_iter] = t_bestC; // store the best value
+ d_bestvalit[i_iter] = t_bestC; // store the best value
t_bestitP = t_bestP;
i_iter++; // increase iteration counter
-
+
double f_dither = f_weight + ::unif_rand() * (1.0 - f_weight); // ----computer dithering factor --------------
-
+
if (i_strategy == 6) { // ---DE/current-to-p-best/1 ------------------------------------------
- arma::colvec temp_oldC = ta_oldC; // create copy of ta_oldC to avoid changing it
- rsort_with_index( temp_oldC.memptr(), sortIndex.begin(), i_NP ); // sort temp_oldC to use sortIndex
+ arma::colvec temp_oldC = ta_oldC; // create copy of ta_oldC to avoid changing it
+ rsort_with_index( temp_oldC.memptr(), sortIndex.begin(), i_NP ); // sort temp_oldC to use sortIndex
}
for (int i = 0; i < i_NP; i++) { // ----start of loop through ensemble------------------------
t_tmpP = ta_oldP.col(i); // t_tmpP is the vector to mutate and eventually select
- permute(ia_urn2.memptr(), urn_depth, i_NP, i, ia_urntmp.memptr()); // Pick 4 random and distinct
- int k = 0; // loop counter used in all strategies below
+ permute(ia_urn2.memptr(), urn_depth, i_NP, i, ia_urntmp.memptr()); // Pick 4 random and distinct
+ int k = 0; // loop counter used in all strategies below
\end{CodeInput}
\end{CodeChunk}
-
+
\normalsize \centering{Panel B: \proglang{C++} version using \pkg{Rcpp}}
\end{minipage}
\caption{\code{devol()} iteration loop setup and beginning of population loop}
@@ -1305,7 +1364,7 @@
/*===Choice of strategy=======================================================*/
/*---classical strategy DE/rand/1/bin-----------------------------------------*/
if (i_strategy == 1) {
-
+
j = (int)(unif_rand() * i_D); /* random parameter */
k = 0;
do {
@@ -1320,13 +1379,13 @@
}
/*---DE/local-to-best/1/bin---------------------------------------------------*/
else if (i_strategy == 2) {
-
+
j = (int)(unif_rand() * i_D); /* random parameter */
k = 0;
do {
/* add fluctuation to random target */
-
- t_tmpP[j] = t_tmpP[j] +
+
+ 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]);
j = (j + 1) % i_D;
@@ -1336,7 +1395,7 @@
}
/*---DE/best/1/bin with jitter------------------------------------------------*/
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/rcpp -r 2769
More information about the Rcpp-commits
mailing list