Created
January 23, 2012 14:28
-
-
Save zachmayer/1663393 to your computer and use it in GitHub Desktop.
parDEoptim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setClass("parDEoptim", representation(optim="list", member="list")) | |
parDEoptim <- function (fn, lower, upper, n=1, control = DEoptim.control(), .packages=NULL, ...) { | |
library("DEoptim") | |
library("foreach") | |
segSizes <- (upper-lower)/n | |
segments <- lapply(1:n, function(x){ | |
lower+segSizes*(x-1) | |
}) | |
out <- foreach(L=segments, .packages=c(.packages,'DEoptim')) %dopar% { | |
U <- L + segSizes | |
opt <- DEoptim (fn, L, U, control = control) | |
new('parDEoptim',optim=opt$optim,member=opt$member) | |
} | |
best <- which.min(unlist(lapply(out, function(x) x@optim$bestval))) | |
out[[best]] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rosenbrock <- function(x){ | |
x1 <- x[1] | |
x2 <- x[2] | |
100 * (x2 - x1 * x1)^2 + (1 - x1)^2 | |
} | |
lower <- c(-10,-10) | |
upper <- -lower | |
#Serial | |
library("DEoptim") | |
set.seed(1) | |
system.time(out1 <- DEoptim(Rosenbrock, lower, upper, DEoptim.control(NP = 100, itermax=2000, trace=FALSE))) | |
#Parallel | |
library(doMC) | |
registerdoMC() | |
system.time(out2 <- parDEoptim(Rosenbrock, lower, upper, DEoptim.control(NP = 100, itermax=150, trace=FALSE), n=20)) | |
#Check | |
all.equal(out1$optim$bestmem,out2@optim$bestmem) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment