globpso

Features

This package collects the powerful variants of particle swarm optimization (PSO) and differential evolution (DE) algorithms for R users.

Currently it supports five types of PSO:

and six types of DE (Storn, R. and Price, K., 1997):

Installation

Install the stable version of globpso on CRAN via

install.packages("globpso")

(Not recommended) Or, install the latest under-development version from github with

install.packages("devtools")
# devtools::install_github("PingYangChen/globpso")
install.packages("globpso", repos = c('https://pingyangchen.r-universe.dev', 'https://cloud.r-project.org'))

Examples of PSO

For setting the PSO type, please find the instruction through the command.

?getPSOInfo
library(globpso)

# Optimize the 3-dimensional quadratic objective function with location shift
objf <- function(x, loc) {
 val <- 0
 for (i in 1:length(x)) val <- val + (x[i] - loc)^2
 return(val)
}

# The search domain is [-5, 5]^3
upp_bound <- rep(5, 3)
low_bound <- rep(-5, 3)

# Define the location shift to be 1
loc_shift <- 1

# Run PSO for this optimization problem
# Also input the enviorment variable, the location shift 'loc_shift'
res <- globpso(objFunc = objf, lower = low_bound, upper = upp_bound, 
               loc = loc_shift)
res$par
res$val

# Set initial values to run PSO
initSwarm <- c(2, 2, 2)
# initSwarm <- rbind(c(2, 2, 2), c(1, 0.1, 1)) # for more than 2 initial points
res_i <- globpso(objFunc = objf, lower = low_bound, upper = upp_bound, 
                 init = initSwarm, loc = loc_shift)

# One can also write C++ objective function to further accelerate the computation
library(Rcpp)
library(RcppArmadillo)
objf_c <- cppFunction('double objf_c(SEXP x, SEXP loc) {
   double val = 0;
   double loc_c = (double)Rcpp::as<double>(loc);
   arma::rowvec x_c = (arma::rowvec)Rcpp::as<arma::rowvec>(x);
   for (arma::uword i = 0; i < x_c.n_elem; i++) {
     val += (x_c(i) - loc_c)*(x_c(i) - loc_c);
   }
   return val;
 }', depends = "RcppArmadillo")
res_c <- globpso(objFunc = objf_c, lower = low_bound, upper = upp_bound, 
                 loc = loc_shift)
res_c$par
res_c$val

# Use getPSOInfo() to change the PSO options
alg_setting <- getPSOInfo(nSwarm = 64, maxIter = 200, psoType = "quantum")
res_c_large <- globpso(objFunc = objf_c, lower = low_bound, upper = upp_bound, PSO_INFO = alg_setting, loc = loc_shift)
res_c_large$history

Examples of DE

For setting the DE type, please find the instruction through the command.

?getDEInfo
library(globpso)

# Optimize the 3-dimensional quadratic objective function with location shift
objf <- function(x, loc) {
  val <- 0
  for (i in 1:length(x)) val <- val + (x[i] - loc)^2
  return(val)
}

# The search domain is [-5, 5]^3
upp_bound <- rep(5, 3)
low_bound <- rep(-5, 3)

# Define the location shift to be 1
loc_shift <- 1

# Use getDEInfo() to setup the DE options
de_setting <- getDEInfo(nPop = 64, maxIter = 200, deType = "best-2", sf = 0.5, cr = 0.1)

# Run DE for this optimization problem
# Also input the enviorment variable, the location shift 'loc_shift'
res <- diffevo(objFunc = objf, lower = low_bound, upper = upp_bound, 
               DE_INFO = de_setting, verbose = FALSE, loc = loc_shift)
res$par
res$val

# Simialr to PSO, 
# One can also write C++ objective function to further accelerate the computation

Reference

If you encounter a bug, please file a reproducible example on github.