[Deoptim-commits] r101 - in pkg/DEoptim: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue May 15 22:22:50 CEST 2012


Author: braverock
Date: 2012-05-15 22:22:49 +0200 (Tue, 15 May 2012)
New Revision: 101

Modified:
   pkg/DEoptim/DESCRIPTION
   pkg/DEoptim/R/DEoptim.R
   pkg/DEoptim/man/DEoptim.control.Rd
Log:
- clean up parallel args for Josh to test.  maintains backwards compatiblity, but standardizes args across engines

Modified: pkg/DEoptim/DESCRIPTION
===================================================================
--- pkg/DEoptim/DESCRIPTION	2012-04-18 12:08:16 UTC (rev 100)
+++ pkg/DEoptim/DESCRIPTION	2012-05-15 20:22:49 UTC (rev 101)
@@ -1,5 +1,5 @@
 Package: DEoptim
-Version: 2.2-1
+Version: 2.3
 Date: $Date$
 Title: Global optimization by Differential Evolution
 Author: David Ardia, Katharine Mullen, Brian Peterson, Joshua Ulrich

Modified: pkg/DEoptim/R/DEoptim.R
===================================================================
--- pkg/DEoptim/R/DEoptim.R	2012-04-18 12:08:16 UTC (rev 100)
+++ pkg/DEoptim/R/DEoptim.R	2012-05-15 20:22:49 UTC (rev 101)
@@ -1,10 +1,14 @@
 DEoptim.control <- function(VTR = -Inf, strategy = 2, bs = FALSE, NP = NA,
                             itermax = 200, CR = 0.5, F = 0.8, trace = TRUE,
                             initialpop = NULL, storepopfrom = itermax + 1,
-                            storepopfreq = 1,  p = 0.2, c = 0,
-                            reltol, steptol, parallelType = 0,
-                            packages = c(), parVar = c(), 
-                            foreachArgs = list() ) {
+                            storepopfreq = 1,  p = 0.2, c = 0, 
+                            ...,
+                            reltol, steptol, 
+                            parallelType = c('auto','none','parallel','foreach'),
+                            parallelArgs) 
+{ #begin function
+  
+  #check for bounds  
   if (itermax <= 0) {
     warning("'itermax' <= 0; set to default value 200\n", immediate. = TRUE)
     itermax <- 200
@@ -51,13 +55,55 @@
   if (missing(steptol)) {
     steptol <- itermax
   }
+  
 
+  #####################
+  # handle parallel options
+    
+  #check for a single parallelType
+  if(missing(parallelType) | length(parallelType)>1){
+      parallelType<-parallelType[1]
+  }  
+  
+
+  #support old deprecated parallelType arguments
+  switch(parallelType,
+          0 = {
+              parallelType='none' 
+          },
+          1= {
+              parallelType='parallel' 
+          },
+          2 = {
+              parallelType='foreach' 
+              # handle deprecated foreachArgs
+   
+          }
+  )
+  switch(parallelType,
+          foreach = {
+              if(missing(parallelArgs) && hasArg(foreachArgs)){
+                  parallelArgs<-match.call(expand.dots=TRUE)$foreachArgs
+              }   
+              if(is.null(parallelArgs$.packages) && hasArg(packages)){
+                  parallelArgs$.packages<-packages
+              }
+          },
+          parallel = {
+              if(missing(packages) || !hasArg(packages)){
+                  packages<-(.packages())
+              }
+          }
+  )
+  # end parallel options
+  ######################
+  
+  # format and return  
   list(VTR = VTR, strategy = strategy, NP = NP, itermax = itermax, CR
        = CR, F = F, bs = bs, trace = trace, initialpop = initialpop,
        storepopfrom = storepopfrom, storepopfreq = storepopfreq, p =
-       p, c = c, reltol = reltol, steptol = steptol, parallelType =
-       parallelType, packages = packages, parVar = parVar, foreachArgs =
-       foreachArgs)
+       p, c = c, reltol = reltol, steptol = steptol, 
+       parallelType = parallelType, parallelArgs = parallelArgs, ...)
 }
 
 DEoptim <- function(fn, lower, upper, control = DEoptim.control(), ...,
@@ -109,38 +155,40 @@
   ctrl$trace <- as.numeric(ctrl$trace)
   ctrl$specinitialpop <- as.numeric(ctrl$specinitialpop)
   ctrl$initialpop <- as.numeric(ctrl$initialpop)
-  if(ctrl$parallelType == 2) { ## use foreach 
+  if(ctrl$parallelType == 'foreach') { ## use foreach 
     use.foreach <- suppressMessages(require(foreach,quietly=TRUE))
     if(!use.foreach)
       stop("foreach package not available but parallelType set to 2")
     if(!foreach:::getDoParRegistered()) {
       foreach:::registerDoSEQ()
     }
-    args <-  ctrl$foreachArgs
+    args <-  ctrl$parallelArgs
     fnPop <- function(params, ...) {
       my_chunksize <- ceiling(NROW(params)/foreach:::getDoParWorkers())
       my_iter <- iter(params,by="row",chunksize=my_chunksize)
       args$i <- my_iter
       if(is.null(args$.combine)) args$.combine <- c
-      args$.export <- "fn" 
+      args$.export <-c(args$.export,"fn")
       do.call(foreach, args) %dopar% {
         apply(i,1,fn,...)
       }
     }
   }
-  else if(ctrl$parallelType == 1){ ## use parallel 
+  else if(ctrl$parallelType == 'parallel'){ ## use parallel 
     use.parallel <- suppressMessages(require(parallel,quietly=TRUE))
     if(!use.parallel)
-      stop("parallel package not available but parallelType set to 1")
+      stop("parallel package not available but parallelType set to 'parallel")
     cl <- parallel:::makeCluster(parallel:::detectCores())
     packFn <- function(packages) {
       for(i in packages)
         library(i, character.only = TRUE)
     }
     parallel:::clusterCall(cl, packFn, ctrl$packages)
+    if(is.null(ctrl$parVar)) ctrl$parVar <- ls()
     parallel:::clusterExport(cl, ctrl$parVar)
     fnPop <- function(params, ...) {
-      parallel:::parApply(cl=cl,params,1,fn,...)
+      #clusterApply(cl, x, fun, ...)
+      parallel:::parApply(cl=cl,x=params,fun=fn, ctrl$parallelArgs, ...)
     }
   }
   else {  ## use regular for loop / apply
@@ -184,7 +232,7 @@
 
   outC <- .Call("DEoptimC", lower, upper, fnPop, ctrl, new.env(), fnMapC, PACKAGE="DEoptim")
 
-  if(ctrl$parallelType == 1)
+  if(ctrl$parallelType == 'parallel')
     parallel:::stopCluster(cl) 
   
   if (length(outC$storepop) > 0) {

Modified: pkg/DEoptim/man/DEoptim.control.Rd
===================================================================
--- pkg/DEoptim/man/DEoptim.control.Rd	2012-04-18 12:08:16 UTC (rev 100)
+++ pkg/DEoptim/man/DEoptim.control.Rd	2012-05-15 20:22:49 UTC (rev 101)
@@ -78,29 +78,15 @@
  \item{steptol}{see \code{reltol}. Defaults to \code{itermax}.}
  \item{parallelType}{Defines the type of parallelization to employ, if
    any.
-    \code{0}: The default, this uses \code{DEoptim} one only one core.
-    \code{1}: This uses all available cores, via the \pkg{parallel}
-    package, to run \code{DEoptim}.  If \code{parallelType=1}, then the
-    \code{packages} argument and the \code{parVar} argument need to
-    specify the packages required by the objective function and the
-    variables required in the environment, respectively. 
-    \code{2}: This uses the \pkg{foreach} package for parallelism; see
-    the \code{sandbox} directory in the source code for examples.  If
-    \code{parallelType=1}, then the \code{foreachArgs} argument can pass
-    the options to be called with \code{foreach}.  
+    \code{none}: The default, this uses \code{DEoptim} one only one core.
+    \code{parallel}: This uses all available cores, via the \pkg{parallel}
+    package, to run \code{DEoptim}.  
+    \code{foreach}: This uses the \pkg{foreach} package for parallelism; see
+    the \code{sandbox} directory in the source code for examples. 
   }
-  \item{packages}{Used if  \code{parallelType=1}; a list of
-    package names (as strings) that need to be loaded for use by the objective
-  function. }
-\item{parVar}{Used if  \code{parallelType=1};  a list of variable names
-  (as strings) that need to exist in the environment for use by the
-  objective function or are used as arguments by the objective
-  function. }  
-  \item{foreachArgs}{A list of named arguments for the \code{foreach}
-    function from the
-    package \pkg{foreach}.  The arguments \code{i}, \code{.combine} and
-    \code{.export} are not possible to set here; they are set
-    internally. }
+  \item{parallelArgs}{A list of named arguments for the parallel engine.
+    For package \pkg{foreach, the argument \code{i}
+    is not possible to set here; it is set internally. }
  }
 \value{
   The default value of \code{control} is the return value of 



More information about the Deoptim-commits mailing list