Skip to content

Commit

Permalink
Updating the package...
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Nov 3, 2016
1 parent 687f165 commit 7dcea86
Show file tree
Hide file tree
Showing 32 changed files with 1,116 additions and 870 deletions.
5 changes: 4 additions & 1 deletion .Rbuildignore
@@ -1,12 +1,15 @@
src/
\.pdf$
\.Rproj.*
\.Rhist.*
R/abc\.R
^.*\.Rproj$
^\.Rproj\.user$
README\.md
README\.Rmd
^playground$
LICENSE
\.git/
source/
.*\.sh
^appveyor\.yml$
^\.travis\.yml$
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: R
sudo: false
cache: packages
4 changes: 4 additions & 0 deletions ChangeLog
@@ -0,0 +1,4 @@
2016-11-03 George G. Vega Yon (g.vegayon@gmail.com)
* R/*.R: Adding roxygen to the project.
* src/abc.cpp: Adding pure C++ version of the function
* playground/*: Cleaning up
18 changes: 13 additions & 5 deletions DESCRIPTION
@@ -1,14 +1,22 @@
Package: ABCoptim
Type: Package
Title: Implementation of Artificial Bee Colony (ABC) Optimization
Version: 0.13.11
Date: 2013-11-05
Author: George Vega Yon <g.vegayon@gmail.com> [aut], Enyelbert Muñoz <enyeldoc2011@gmail.com> [ctb]
Maintainer: George Vega Yon <g.vegayon@gmail.com>
Description: An implementation of Karaboga (2005) Artificial Bee Colony Optimization algorithm. This (working) version is a Work-in-progress, which is why it has been implemented using pure R code. This was developed upon the basic version programmed in C and distributed at the algorithm's official website.
Version: 0.2.9000
Date: 2016-11-03
Authors@R: c(
person("George", "Vega Yon", email="g.vegayon@gmail.com", role=c("aut", "cre")),
person("Enyelbert", "Muñoz", email="enyeldoc2011@gmail.com", role=c("ctb")))
Description: An implementation of Karaboga (2005) Artificial Bee Colony
Optimization algorithm. This (working) version is a Work-in-progress, which is
why it has been implemented using pure R code. This was developed upon the basic
version programmed in C and distributed at the algorithm's official website.
Classification/ACM: G.1.6
Classification/JEL: C61
Encoding: UTF-8
URL: http://github.com/gvegayon/ABCoptim, http://mf.erciyes.edu.tr/abc/
License: GPL (>= 3)
LazyLoad: yes
LinkingTo: Rcpp
Imports:
Rcpp
RoxygenNote: 5.0.1
674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

7 changes: 6 additions & 1 deletion NAMESPACE
@@ -1,2 +1,7 @@
export(abc_optim)
# Generated by roxygen2: do not edit by hand

export(abc_cpp)
export(abc_optim)
importFrom(Rcpp,sourceCpp)
importFrom(stats,runif)
useDynLib(ABCoptim)
51 changes: 51 additions & 0 deletions R/ABCoptim-package.R
@@ -0,0 +1,51 @@


#' An implementation of the Artificial Bee Colony (ABC) Algorithm
#'
#' This is an implementation of Karaboga (2005) ABC optimization algorithm. It
#' was developed upon the basic version programmed in \code{C} and distributed
#' at the algorithm's official website (see the references).
#'
#' Please consider that this version is in alpha state of development, thus any
#' evident (precision) error should be blaimed to the package author (not to
#' the algorithm itself)
#'
#' Please visit the project home for more information:
#' \url{https://github.com/gvegayon/ABCoptim}.
#'
#' \tabular{ll}{ Package: \tab ABCoptim\cr Type: \tab Package\cr Version: \tab
#' 0.2.9000\cr Date: \tab 2016-11-03\cr License: \tab GPL version 2 or later\cr
#' }
#'
#' @name ABCoptim-package
#' @aliases ABCoptim-package ABCoptim abc
#' @docType package
#' @author George Vega Yon \email{g.vegayon@@gmail.com} [aut],
#'
#' Enyelbert Muñoz \email{enyeldoc2011@@gmail.com} [cnt)
#' @references D. Karaboga, \emph{An Idea based on Honey Bee Swarm for
#' Numerical Optimization}, tech. report TR06,Erciyes University, Engineering
#' Faculty, Computer Engineering Department, 2005
#' \url{http://mf.erciyes.edu.tr/abc/pub/tr06_2005.pdf}
#'
#' Artificial Bee Colony (ABC) Algorithm (website)
#' \url{http://mf.erciyes.edu.tr/abc/index.htm}
#'
#' Basic version of the algorithm implemented in \code{C} (ABC's official
#' website) \url{http://mf.erciyes.edu.tr/abc/form.aspx}
#' @keywords package
#' @examples
#'
#' \dontrun{
#' demo(ABCoptim) # Some functions...
#' }
#'
NULL

#' @useDynLib ABCoptim
#' @importFrom Rcpp sourceCpp
#' @importFrom stats runif
NULL



9 changes: 9 additions & 0 deletions R/RcppExports.R
@@ -0,0 +1,9 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' @rdname abc_optim
#' @export
abc_cpp <- function(par, fn, ub = 1e20, lb = -1e20, FoodNumber = 20L, limit = 100L, maxCycle = 1000L, criter = 50L) {
.Call('ABCoptim_abc_cpp', PACKAGE = 'ABCoptim', par, fn, ub, lb, FoodNumber, limit, maxCycle, criter)
}

215 changes: 137 additions & 78 deletions R/abc_optim.R
@@ -1,5 +1,70 @@
rm(list=ls())

#' Optimization through ABC algorithm
#'
#' Optimizes through the ABC algorithm
#'
#' This is an implementation of Karaboga (2005) ABC optimization algorithm. It
#' was developed upon the basic version programmed in \code{C} and distributed
#' at the algorithm's official website (see the references).
#'
#' By default, lower and upper bounds are set as \code{+-Inf}. This last thing
#' is just conceptual as all infinite bounds are replaced by
#' \code{.Machine$double.xmax*1e-10} (still a pretty big number).
#'
#' If \code{D} (the number of parameters to be optimzed) is greater than one,
#' then \code{lb} and \code{ub} can be either scalars (assuming that all the
#' parameters share the same boundaries) or vectors (the parameters have
#' different boundaries each other).
#'
#' @param par Initial values for the parameters to be optimized over
#' @param fn A function to be minimized, with first argument of the vector of
#' parameters over which minimization is to take place. It should return a
#' scalar result.
#' @param D Number of parameters to be optimized.
#' @param ... Further arguments to be passed to 'fn'.
#' @param NP Number of bees.
#' @param FoodNumber Number of food sources to exploit.
#' @param lb Lower bound of the parameters to be optimized.
#' @param ub Upper bound of the parameters to be optimized.
#' @param limit Limit of a food source.
#' @param maxCycle Maximum number of iterations.
#' @param optiinteger Whether to optimize binary parameters or not.
#' @param criter Stop criteria (numer of unchanged results) until stopping
#' @return A list containing the optimized parameters (\code{$par}), the value
#' of the function (\code{$value}) and the number of iterations taken to reach
#' the optimum (\code{$counts}).
#' @author George Vega Yon \email{g.vegayon@@gmail.com}
#' @references D. Karaboga, \emph{An Idea based on Honey Bee Swarm for
#' Numerical Optimization}, tech. report TR06,Erciyes University, Engineering
#' Faculty, Computer Engineering Department, 2005
#' \url{http://mf.erciyes.edu.tr/abc/pub/tr06_2005.pdf}
#'
#' Artificial Bee Colony (ABC) Algorithm (website)
#' \url{http://mf.erciyes.edu.tr/abc/index.htm}
#'
#' Basic version of the algorithm implemented in \code{C} (ABC's official
#' website) \url{http://mf.erciyes.edu.tr/abc/form.aspx}
#' @keywords optimization
#' @examples
#'
#' # EXAMPLE 1: The minimum is at (pi,pi)
#' fun <- function(x) {
#' -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))
#' }
#'
#' abc_optim(rep(0,2), fun, lb=-20, ub=20, criter=100)
#'
#' # EXAMPLE 2: global minimum at about (-15.81515)
#' fw <- function (x)
#' 10*sin(0.3*x)*sin(1.3*x^2) + 0.00001*x^4 + 0.2*x+80
#'
#' abc_optim(50, fw, lb=-100, ub=100, criter=100)
#'
#' # EXAMPLE 3: 5D sphere, global minimum at about (0,0,0,0,0)
#' fs <- function(x) sum(x^2)
#'
#' abc_optim(rep(10,5), fs, lb=-100, ub=100, criter=200)
#'
#' @export abc_optim
abc_optim <- function(
par, # Vector de parametros a opti
fn, # Funcion objetivo
Expand Down Expand Up @@ -111,27 +176,24 @@ abc_optim <- function(

SendEmployedBees <- function() {
for (i in 1:FoodNumber) {
r <- runif(1)
# The parameter to be changed is determined randomly
param2change <- floor(r*D) + 1
param2change <- sample(1:D, 1) # floor(runif(1)*D) + 1

# A randomly chosen solution is used in producing a mutant solution of the solution i
neighbour <- floor(r*FoodNumber) + 1

# Randomly selected solution must be different from the solution i
neighbour <- i
while(neighbour==i)
neighbour <- floor(runif(1)*FoodNumber) + 1
neighbour <- sample(1:FoodNumber, 1) # floor(runif(1)*FoodNumber) + 1

solution <<- Foods[i,]

# v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})
r <- runif(1)

if (optiinteger) solution[param2change] <<- r > 0.5

if (optiinteger) solution[param2change] <<- runif(1) > 0.5
else {
solution[param2change] <<-
Foods[i,param2change]+
(Foods[i,param2change]-Foods[neighbour,param2change])*(r-0.5)*2
(Foods[i,param2change]-Foods[neighbour,param2change])*(runif(1)-0.5)*2

# if generated parameter value is out of boundaries, it is shifted onto the boundaries
if (solution[param2change]<lb[param2change])
Expand Down Expand Up @@ -182,33 +244,30 @@ abc_optim <- function(
t <- 0
while (t < FoodNumber)
{
r <- runif(1)

# choose a food source depending on its probability to be chosen
if (r < prob[i]) {
if (runif(1) < prob[i]) {
t <- t + 1
r <- runif(1)


# The parameter to be changed is determined randomly
param2change <- floor(r*D) + 1
param2change <- sample(1:D, 1) # floor(runif(1)*D) + 1

# A randomly chosen solution is used in producing a mutant solution of the solution i
neighbour <- floor(r*FoodNumber) + 1

#Randomly selected solution must be different from the solution i*/
neighbour <- i
while(neighbour==i)
neighbour <- floor(runif(1)*FoodNumber) + 1
neighbour <- sample(1:FoodNumber, 1) # floor(runif(1)*FoodNumber) + 1

solution <<- Foods[i,]

# v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
r <- runif(1)

if (optiinteger) solution[param2change] <<- r > .5

if (optiinteger) solution[param2change] <<- runif(1) > .5
else
{
solution[param2change] <<-
Foods[i,param2change]+
(Foods[i,param2change]-Foods[neighbour,param2change])*(r-0.5)*2
(Foods[i,param2change]-Foods[neighbour,param2change])*(runif(1)-0.5)*2

# if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
if (solution[param2change]<lb[param2change])
Expand Down Expand Up @@ -282,59 +341,59 @@ abc_optim <- function(

}

################################################################################
# Ejemplos
################################################################################

X <- c(3,2,3,1)

# Funcion de matching
fun <- function(lambda, x0, X, M)
{
norm((x0 - X)*lambda, type="2") + exp(abs(sum(lambda > 0) - M))
}

# Mejor vecino para
# x0 = 2
# X = c(3,2,3,1)
# M = 1
# El mejor resultado debe ser [0,1,0,0]
x1 <- abc_optim(rep(0,4), fun, x0=2, X=X, M=1, lb=0, ub=1, optiinteger=T)
x1

# Mejores dos vecinos para
# x0 = 3
# X = c(3,2,3,1)
# M = 2
# El mejor resultado debe ser [1,0,1,0]
x2 <- abc_optim(rep(0,4), fun, x0=3, X=X, M=2, lb=0, ub=1, optiinteger=T)
x2

################################################################################
# Definicion de la funcion
fun <- function(x) {
-cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))
}

abc_optim(rep(0,2), fun, lb=-5, ub=5, criter=50)

optim(rep(0,2), fn=fun) #lower=-5,upper=5)

################################################################################
# Definicion de la funcion

fun <- function(x) {
-4+(x[1]^2 + x[2]^2)
}

abc_optim(c(1,1), fn=fun, lb=-100000, ub=100000,criter=100)

################################################################################
# Definicion de la funcion

fun <- function(x) {
-(x^4 - 2*x^2 - 8)
}

abc_optim(0, fn=fun, lb=-2, ub=2,criter=100)
# ################################################################################
# # Ejemplos
# ################################################################################
#
# X <- c(3,2,3,1)
#
# # Funcion de matching
# fun <- function(lambda, x0, X, M)
# {
# norm((x0 - X)*lambda, type="2") + exp(abs(sum(lambda > 0) - M))
# }
#
# # Mejor vecino para
# # x0 = 2
# # X = c(3,2,3,1)
# # M = 1
# # El mejor resultado debe ser [0,1,0,0]
# x1 <- abc_optim(rep(0,4), fun, x0=2, X=X, M=1, lb=0, ub=1, optiinteger=T)
# x1
#
# # Mejores dos vecinos para
# # x0 = 3
# # X = c(3,2,3,1)
# # M = 2
# # El mejor resultado debe ser [1,0,1,0]
# x2 <- abc_optim(rep(0,4), fun, x0=3, X=X, M=2, lb=0, ub=1, optiinteger=T)
# x2
#
# ################################################################################
# # Definicion de la funcion
# fun <- function(x) {
# -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))
# }
#
# abc_optim(rep(0,2), fun, lb=-5, ub=5, criter=50)
#
# optim(rep(0,2), fn=fun) #lower=-5,upper=5)
#
# ################################################################################
# # Definicion de la funcion
#
# fun <- function(x) {
# -4+(x[1]^2 + x[2]^2)
# }
#
# abc_optim(c(1,1), fn=fun, lb=-100000, ub=100000,criter=100)
#
# ################################################################################
# # Definicion de la funcion
#
# fun <- function(x) {
# -(x^4 - 2*x^2 - 8)
# }
#
# abc_optim(0, fn=fun, lb=-2, ub=2,criter=100)
# #

0 comments on commit 7dcea86

Please sign in to comment.