Permalink
Browse files

- computationally irrelevant: added ... for additional arguments to o…

…bjective function
  • Loading branch information...
TillF committed Nov 17, 2016
1 parent 4f5c27b commit 73321aa1e555c5dee47dede9eb366f24d6da092e
Showing with 32 additions and 26 deletions.
  1. +2 −2 DESCRIPTION
  2. +4 −4 R/optim_dds.R
  3. +4 −4 R/optim_pdds_robust.R
  4. +3 −3 R/optim_ppso_robust.R
  5. +5 −5 R/optim_pso.R
  6. +6 −4 R/prepare_mpi_cluster.R
  7. +8 −4 man/optim_ppso.Rd
View
@@ -1,8 +1,8 @@
Package: ppso
Type: Package
Title: Particle Swarm Optimization and Dynamically Dimensioned Search, optionally using parallel computing based on Rmpi
Version: 0.9-9991
Date: 2016-08-01
Version: 0.9-9992
Date: 2016-11-17
Author: Till Francke <francke@uni-potsdam.de>
Maintainer: Till Francke <francke@uni-potsdam.de>
Description: (Optionally parallelized) optimization using PSO (Particle Swarm Optimzation) or DDS (Dynamically Dimensioned Search) algorithms, which excell for multidimensional (multi-parameter) functions with many local extrema and a restricted number of function evaluations. The parallelized version builds on Rmpi and is intended for highly computationally intensive objective functions (>20 s evaluation time). Another focus of this package is the possibility to resume interrupted optimization runs from the intermediate project files. These features make this package useful for the automatic calibration of complex numerical models (e.g. hydrological models).\cr-- experimental version, use with care. Comments welcome --
View
@@ -2,7 +2,7 @@ optim_dds <-
function (objective_function=sample_function, number_of_parameters=2, number_of_particles= 1, max_number_function_calls=500, r=0.2, abstol=-Inf, reltol=-Inf, max_wait_iterations=50,
parameter_bounds=cbind(rep(-1,number_of_parameters),rep(1,number_of_parameters)), initial_estimates=NULL, lhc_init=FALSE, part_xchange=2,
#runtime & display parameters
do_plot=NULL, wait_for_keystroke=FALSE, logfile="dds.log",projectfile="dds.pro", save_interval=ceiling(number_of_particles/4),load_projectfile="try",break_file=NULL, plot_progress=FALSE, tryCall=FALSE, verbose=FALSE)
do_plot=NULL, wait_for_keystroke=FALSE, logfile="dds.log",projectfile="dds.pro", save_interval=ceiling(number_of_particles/4),load_projectfile="try",break_file=NULL, plot_progress=FALSE, tryCall=FALSE, verbose=FALSE,...)
# do Dynamically Dimensioned Search (DDS) optimization (Tolson & Shoemaker 2007)
{
@@ -91,7 +91,7 @@ if (verbose_master) {print(paste(Sys.time(),"starting initialization runs..."))
globvars$status[]=1; globvars$status[pre_run_computations]=0 #do computations only for the particles to be initialized, skip those that have been initialized from file
globvars$computation_start[pre_run_computations]=Sys.time()
globvars$fitness_X [pre_run_computations]=apply(as.matrix(globvars$X[pre_run_computations,],nrow=length(pre_run_computations)),1,objective_function) #execute pending pre-runs
globvars$fitness_X [pre_run_computations]=apply(as.matrix(globvars$X[pre_run_computations,],nrow=length(pre_run_computations)),1,objective_function, ...) #execute pending pre-runs
globvars$status [pre_run_computations] =1 #mark as pre-runs "finished"
if (!is.null(logfile)) write.table(file = logfile, cbind(format(globvars$computation_start[pre_run_computations],"%Y-%m-%d %H:%M:%S"), matrix(globvars$X[pre_run_computations,],ncol=ncol(globvars$X)), globvars$fitness_X[pre_run_computations], #write pre-runs to logfile, too
@@ -150,15 +150,15 @@ while (is.null(globvars$break_flag))
globvars$computation_start[] =Sys.time() #store time of start of this computation loop
if (tryCall) #catch error message during evaluation (slower)
{
globvars$fitness_X=try(apply(globvars$X,1,objective_function),silent=TRUE)
globvars$fitness_X=try(apply(globvars$X,1,objective_function,...),silent=TRUE)
if (!is.numeric(globvars$fitness_X)) #an error occured during execution
{
globvars$break_flag=paste("aborted: ",as.character(globvars$fitness_X))
next
}
}
else
globvars$fitness_X=apply(globvars$X,1,objective_function) #no error message during evaluation (faster)
globvars$fitness_X=apply(globvars$X,1,objective_function, ...) #no error message during evaluation (faster)
globvars$status [] =1 #mark as "finished"
globvars$function_calls[] =globvars$function_calls[]+1 #increase iteration counter
View
@@ -3,7 +3,7 @@ function (objective_function=sample_function, number_of_parameters=2, number_of_
parameter_bounds=cbind(rep(-1,number_of_parameters),rep(1,number_of_parameters)), initial_estimates=NULL, lhc_init=FALSE, part_xchange=2,
#runtime & display parameters
do_plot=NULL, wait_for_keystroke=FALSE, logfile="dds.log",projectfile="dds.pro", save_interval=ceiling(number_of_particles/4),load_projectfile="try",break_file=NULL, plot_progress=FALSE,
tryCall=FALSE, nslaves=-1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE)
tryCall=FALSE, nslaves=-1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE,...)
# do Dynamically Dimensioned Search (DDS) optimization (Tolson & Shoemaker 2007)
{
@@ -70,7 +70,7 @@ globvars$fitness_lbest[] = Inf
globvars$fitness_gbest = Inf
if (verbose_master) {print(paste(Sys.time(),"initializing slaves...")); flush.console()}
if (!is.null(globvars$nslaves)) prepare_mpi_cluster(nslaves=globvars$nslaves, working_dir_list=working_dir_list,verbose_slave=verbose_slave) else globvars$nslaves=NULL #initiate cluster, if enabled
if (!is.null(globvars$nslaves)) prepare_mpi_cluster(nslaves=globvars$nslaves, working_dir_list=working_dir_list,verbose_slave=verbose_slave, ...) else globvars$nslaves=NULL #initiate cluster, if enabled
if (verbose_master) {print(paste(Sys.time(),"...slaves initialized.")); flush.console()}
#presearch / initialisation:
@@ -110,7 +110,7 @@ globvars$status_org=globvars$status # store original contents
}
globvars$status[]=1; globvars$status[pre_run_computations]=0 #do computations only for the particles to be initialized, skip those that have been initialized from file
mpi_loop(init_search=TRUE) #perform mpi-loop for pre-search
mpi_loop(init_search=TRUE, ...) #perform mpi-loop for pre-search
# browser()
max_number_function_calls=max_number_function_calls-length(pre_run_computations) #reduce number of available calls due to pre-search
@@ -163,7 +163,7 @@ if (is.null(globvars$break_flag))
update_tasklist_dds()
if (!is.null(globvars$break_flag))
globvars$break_flag=paste("nothing done; project file fulfills abortion criteria:",globvars$break_flag) else
mpi_loop(init_search=FALSE, method="dds") #perform mpi-loop for main search
mpi_loop(init_search=FALSE, method="dds", ...) #perform mpi-loop for main search
if (verbose_master) {print(paste(Sys.time(),"finished actual runs.")); flush.console()}
}
View
@@ -3,7 +3,7 @@ function (objective_function=sample_function, number_of_parameters=2, number_of_
wait_complete_iteration=FALSE,parameter_bounds=cbind(rep(-1,number_of_parameters),rep(1,number_of_parameters)), initial_estimates=NULL, Vmax=(parameter_bounds[,2]-parameter_bounds[,1])/3,lhc_init=FALSE,
#runtime & display parameters
do_plot=NULL, wait_for_keystroke=FALSE, logfile="ppso.log",projectfile="ppso.pro", save_interval=ceiling(number_of_particles/4),load_projectfile="try",break_file=NULL, plot_progress=FALSE,
tryCall=FALSE, nslaves=-1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE)
tryCall=FALSE, nslaves=-1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE, ...)
# do particle swarm optimization
{
#export r/w globals to separate environment
@@ -89,7 +89,7 @@ globvars$break_flag=NULL #flag indicating if a termination criterium has b
globvars$fitness_gbest = Inf;
globvars$fitness_lbest[] = Inf
if (!is.null(globvars$nslaves)) prepare_mpi_cluster(nslaves=globvars$nslaves,working_dir_list=working_dir_list) else globvars$nslaves=NULL #initiate cluster, if enabled
if (!is.null(globvars$nslaves)) prepare_mpi_cluster(nslaves=globvars$nslaves,working_dir_list=working_dir_list, ...) else globvars$nslaves=NULL #initiate cluster, if enabled
if (!is.null(logfile) && ((load_projectfile!="loaded") || (!file.exists(logfile)))) #create logfile header, if it is not to be appended, or if it does not yet exist
{
@@ -113,7 +113,7 @@ if (max_number_function_calls < 0)
update_tasklist_pso()
if (!is.null(globvars$break_flag))
globvars$break_flag=paste("nothing done; project file fulfills abortion criteria:",globvars$break_flag) else
mpi_loop(init_search=FALSE, method="pso") #perform mpi-loop
mpi_loop(init_search=FALSE, method="pso", ...) #perform mpi-loop
if (verbose_master) print(paste(Sys.time(),"finished actual runs."))
View
@@ -2,7 +2,7 @@ optim_pso <-
function (objective_function=sample_function, number_of_parameters=2, number_of_particles=40,max_number_of_iterations=5, max_number_function_calls=500, w=1, C1=2, C2=2, abstol=-Inf, reltol=-Inf, max_wait_iterations=50,
wait_complete_iteration=FALSE,parameter_bounds=cbind(rep(-1,number_of_parameters),rep(1,number_of_parameters)), initial_estimates=NULL, Vmax=(parameter_bounds[,2]-parameter_bounds[,1])/3,lhc_init=FALSE,
#runtime & display parameters
do_plot=NULL, wait_for_keystroke=FALSE, logfile="ppso.log",projectfile="ppso.pro", save_interval=ceiling(number_of_particles/4),load_projectfile="try",break_file=NULL, plot_progress=FALSE,tryCall=FALSE, verbose=FALSE)
do_plot=NULL, wait_for_keystroke=FALSE, logfile="ppso.log",projectfile="ppso.pro", save_interval=ceiling(number_of_particles/4),load_projectfile="try",break_file=NULL, plot_progress=FALSE,tryCall=FALSE, verbose=FALSE, ...)
# do particle swarm optimization
{
@@ -100,15 +100,15 @@ while (is.null(globvars$break_flag))
{
if (tryCall) #catch error message during evaluation (slower)
{
globvars$fitness_X=try(apply(globvars$X,1,objective_function),silent=TRUE)
globvars$fitness_X=try(apply(globvars$X,1,objective_function, ...),silent=TRUE)
if (!is.numeric(globvars$fitness_X)) #an error occured during execution
{
globvars$break_flag=paste("aborted: ",as.character(globvars$fitness_X))
next
}
}
else
globvars$fitness_X=apply(globvars$X,1,objective_function) #no error message during evaluation (faster)
globvars$fitness_X=apply(globvars$X,1,objective_function, ...) #no error message during evaluation (faster)
globvars$status [] =1 #mark as "finished"
globvars$node_id [] =0
@@ -119,15 +119,15 @@ while (is.null(globvars$break_flag))
{
if (tryCall) #catch error message during evaluation (slower)
{
globvars$fitness_X [current_particle]=try(objective_function(globvars$X[current_particle,]),silent=TRUE)
globvars$fitness_X [current_particle]=try(objective_function(globvars$X[current_particle,], ...),silent=TRUE)
if (!is.numeric(globvars$fitness_X [current_particle])) #an error occured during execution
{
globvars$break_flag=paste("aborted: ",as.character(globvars$fitness_X [current_particle]))
break
}
}
else
globvars$fitness_X [current_particle] =objective_function(globvars$X[current_particle,]) #no error message during evaluation (faster)
globvars$fitness_X [current_particle] =objective_function(globvars$X[current_particle,], ...) #no error message during evaluation (faster)
globvars$status [current_particle] =1 #mark as "finished"
globvars$node_id [current_particle] =0
View
@@ -65,7 +65,9 @@ prepare_mpi_cluster=function(nslaves, working_dir_list=NULL, verbose_slave=FALSE
if (tryCall)
{
if (verbose_slave) print(paste(Sys.time(),"slave",mpi.comm.rank(),": calling objective function..."))
results=try(objective_function(params),silent=TRUE) # call the objective function with the respective parameters, and create results (with error handling, slower)
#results=try(objective_function(params),silent=TRUE) # call the objective function with the respective parameters, and create results (with error handling, slower)
results=try(do.call(what = objective_function, args=c(params, additional_args)),silent=TRUE) # call the objective function with the respective parameters, and create results (with error handling, slower)
if (verbose_slave)
{
print(paste(Sys.time(),"slave",mpi.comm.rank(),": ...objective function evaluation completed"))
@@ -84,7 +86,7 @@ prepare_mpi_cluster=function(nslaves, working_dir_list=NULL, verbose_slave=FALSE
} else #non-tryCall option
{
if (verbose_slave) print(paste(Sys.time(),"slave",mpi.comm.rank(),": calling objective function..."))
results=objective_function(params) # call the objective function with the respective parameters, and create results (without error handling, faster)
results=do.call(what = objective_function, args=c(params, additional_args)) # call the objective function with the respective parameters, and create results (without error handling, faster)
if (verbose_slave) print(paste(Sys.time(),"slave",mpi.comm.rank(),": ...objective function evaluation completed"))
if (verbose_slave) print(paste(Sys.time(),"slave",mpi.comm.rank(),": returning results to master..."))
mpi.send.Robj(results,0,2) # Send the results back as a task_done slave_message #ii isend doesn't work - why?
@@ -94,8 +96,8 @@ prepare_mpi_cluster=function(nslaves, working_dir_list=NULL, verbose_slave=FALSE
return()
} #end function "perform_task"
additional_args <- list(...) #get list of arguments hidden in ...
mpi.bcast.Robj2slave(additional_args) #send any additional arguments to slaves
mpi.bcast.Robj2slave(verbose_slave) #send verbose-flags to slaves
if (verbose_slave)
View
@@ -17,17 +17,17 @@
\usage{
optim_pso (objective_function = sample_function, number_of_parameters = 2, number_of_particles = 40, max_number_of_iterations = 5, max_number_function_calls=500,
w = 1, C1 = 2, C2 = 2, abstol = -Inf, reltol = -Inf, max_wait_iterations = 50, wait_complete_iteration = FALSE, parameter_bounds = cbind(rep(-1, number_of_parameters),
rep(1, number_of_parameters)), initial_estimates=NULL, Vmax = (parameter_bounds[, 2] - parameter_bounds[, 1])/3, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile = "ppso.log", projectfile = "ppso.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, verbose=FALSE)
rep(1, number_of_parameters)), initial_estimates=NULL, Vmax = (parameter_bounds[, 2] - parameter_bounds[, 1])/3, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile = "ppso.log", projectfile = "ppso.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, verbose=FALSE, ...)
optim_ppso_robust(objective_function = sample_function, number_of_parameters = 2, number_of_particles = 40, max_number_of_iterations = 5, max_number_function_calls=500,
w = 1, C1 = 2, C2 = 2, abstol = -Inf, reltol = -Inf, max_wait_iterations = 50, wait_complete_iteration = FALSE, parameter_bounds = cbind(rep(-1, number_of_parameters),
rep(1, number_of_parameters)), initial_estimates=NULL, Vmax = (parameter_bounds[, 2] - parameter_bounds[, 1])/3, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile = "ppso.log", projectfile = "ppso.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, nslaves = -1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE)
rep(1, number_of_parameters)), initial_estimates=NULL, Vmax = (parameter_bounds[, 2] - parameter_bounds[, 1])/3, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile = "ppso.log", projectfile = "ppso.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, nslaves = -1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE, ...)
optim_dds (objective_function = sample_function, number_of_parameters = 2, number_of_particles = 1, max_number_function_calls= 500,
r=0.2, abstol = -Inf, reltol = -Inf, max_wait_iterations=50, parameter_bounds = cbind(rep(-1, number_of_parameters),
rep(1, number_of_parameters)), initial_estimates=NULL, part_xchange=2, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile= "dds.log", projectfile = "dds.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, verbose=FALSE)
rep(1, number_of_parameters)), initial_estimates=NULL, part_xchange=2, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile= "dds.log", projectfile = "dds.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, verbose=FALSE, ...)
optim_pdds_robust(objective_function = sample_function, number_of_parameters = 2, number_of_particles = 1, max_number_function_calls= 500,
r=0.2, abstol = -Inf, reltol = -Inf, max_wait_iterations=50, parameter_bounds = cbind(rep(-1, number_of_parameters),
rep(1, number_of_parameters)), initial_estimates=NULL, part_xchange=2, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile= "dds.log", projectfile = "dds.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, nslaves = -1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE)
rep(1, number_of_parameters)), initial_estimates=NULL, part_xchange=2, lhc_init=FALSE, do_plot = NULL, wait_for_keystroke = FALSE, logfile= "dds.log", projectfile = "dds.pro", save_interval = ceiling(number_of_particles/4), load_projectfile = "try", break_file=NULL, plot_progress=FALSE, tryCall=FALSE, nslaves = -1, working_dir_list=NULL, execution_timeout=NULL, maxtries=10, verbose=FALSE, ...)
}
%- maybe also 'usage' for other objects documented here.
@@ -106,6 +106,10 @@ If set to TRUE, corresponds to repeated call of \code{\link{plot_optimization_pr
\item{tryCall}{
If set to \code{TRUE}, \code{objective_function} is executed using \code{try}, which yield error messages if \code{objective_function} fails on any slave. The optimization retries, but terminates when an error is produced a second time for the same parameter set. For very fast evaluating functions, this may increase evaluation time.
}
\item{...}{
additional arguments passed to \code{objective_function}
}

0 comments on commit 73321aa

Please sign in to comment.