/
abc_optim.Rd
191 lines (146 loc) · 6.59 KB
/
abc_optim.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/abc_optim.R
\name{abc_optim}
\alias{abc_optim}
\alias{abc_answer}
\alias{print.abc_answer}
\alias{abc_cpp}
\alias{plot.abc_answer}
\title{Artificial Bee Colony Optimization}
\usage{
abc_optim(par, fn, ..., FoodNumber = 20, lb = rep(-Inf, length(par)),
ub = rep(+Inf, length(par)), limit = 100, maxCycle = 1000,
optiinteger = FALSE, criter = 50, parscale = rep(1, length(par)),
fnscale = 1)
\method{print}{abc_answer}(x, ...)
abc_cpp(par, fn, ..., FoodNumber = 20, lb = rep(-Inf, length(par)),
ub = rep(+Inf, length(par)), limit = 100, maxCycle = 1000,
criter = 50, parscale = rep(1, length(par)), fnscale = 1)
\method{plot}{abc_answer}(x, y = NULL,
main = "Trace of the Objective Function", xlab = "Number of iteration",
ylab = "Value of the objective Function", type = "l", ...)
}
\arguments{
\item{par}{Numeric vector. Initial values for the parameters to be optimized over}
\item{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.}
\item{...}{In the case of \code{abc_*}, further arguments to be passed to 'fn',
otherwise, further arguments passed to the method.}
\item{FoodNumber}{Number of food sources to exploit. Notice that the param
\code{NP} has been deprecated.}
\item{lb, ub}{Numeric vectors or scalars. Upper and lower bounds of the
parameters to be optimized.}
\item{limit}{Integer scalar. Limit of a food source.}
\item{maxCycle}{Integer scalar. Maximum number of iterations.}
\item{optiinteger}{Logical scalar. Whether to optimize binary parameters or not.}
\item{criter}{Integer scalar. Stop criteria (numer of unchanged results) until stopping}
\item{parscale}{Numeric vector of length \code{length(par)}. Scale applied
to the parameters (see \code{\link[stats:optim:optim]{stats:optim::optim()}}).}
\item{fnscale}{Numeric scalar. Scale applied function. If \code{fnscale < 0},
then the problem becomes a maximization problem (see \code{\link[stats:optim:optim]{stats:optim::optim()}}).}
\item{x}{An object of class \code{abc_answer}.}
\item{y}{Ignored}
\item{main, xlab, ylab, type}{Passed to \code{\link[graphics:plot.default:plot]{graphics:plot.default::plot()}}.}
}
\value{
An list of class \code{abc_answer}, holding the following elements:
\item{Foods}{Numeric matrix. Last position of the bees.}
\item{f}{Numeric vector. Value of the function evaluated at each set of \code{Foods}.}
\item{fitness}{Numeric vector. Fitness of each \code{Foods}.}
\item{trial}{Integer vector. Number of trials at each \code{Foods}.}
\item{value}{Numeric scalar. Value of the function evaluated at the optimum.}
\item{par}{Numeric vector. Optimum found.}
\item{counts}{Integer scalar. Number of cycles.}
\item{hist}{Numeric matrix. Trace of the global optimums.}
}
\description{
Implements Karaboga (2005) Artificial Bee Colony (ABC) Optimization algorithm.
}
\details{
This implementation of the ABC algorithm was developed based on the basic
version written in \code{C} and published at the algorithm's official
website (see references).
\code{abc_optim} and \code{abc_cpp} are two different implementations of the
algorithm, the former using pure \code{R} code, and the later using \code{C++},
via the \pkg{Rcpp} package. Besides of the output, another important
difference between the two implementations is speed, with \code{abc_cpp}
showing between 50\% and 100\% faster performance.
Upper and Lower bounds (\code{ub}, \code{lb}) equal to infinite will be replaced
by either \code{.Machine$double.xmax} or \code{-.Machine$double.xmax}.
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).
The \code{plot} method shows the trace of the objective function
as the algorithm unfolds. The line is merely the result of the objective
function evaluated at each point (row) of the \code{hist} matrix return by
\code{abc_optim}/\code{abc_cpp}.
For now, the function will return with error if \code{...} was passed to
\code{abc_optim}/\code{abc_cpp}, since those argumens are not stored with the
result.
}
\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=-10, ub=10, criter=50)
# This should be equivalent
abc_cpp(rep(0,2), fun, lb=-10, ub=10, criter=50)
# We can also turn this into a maximization problem, and get the same
# results
fun <- function(x) {
# We've removed the '-' from the equation
cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^2 + (x[2] - pi)^2))
}
abc_cpp(rep(0,2), fun, lb=-10, ub=10, criter=50, fnscale = -1)
# 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
ans <- abc_optim(50, fw, lb=-100, ub=100, criter=100)
ans[c("par", "counts", "value")]
# EXAMPLE 3: 5D sphere, global minimum at about (0,0,0,0,0) -----------------
fs <- function(x) sum(x^2)
ans <- abc_optim(rep(10,5), fs, lb=-100, ub=100, criter=200)
ans[c("par", "counts", "value")]
# EXAMPLE 4: An Ordinary Linear Regression ----------------------------------
set.seed(1231)
k <- 4
n <- 5e2
# Data generating process
w <- matrix(rnorm(k), ncol=1) # This are the model parameters
X <- matrix(rnorm(k*n), ncol = k) # This are the controls
y <- X \%*\% w # This is the observed data
# Objective function
fun <- function(x) {
sum((y - X\%*\%x)^2)
}
# Running the regression
ans <- abc_optim(rep(0,k), fun, lb = -10000, ub=10000)
# Here are the outcomes: Both columns should be the same
cbind(ans$par, w)
# [,1] [,2]
# [1,] -0.08051177 -0.08051177
# [2,] 0.69528553 0.69528553
# [3,] -1.75956316 -1.75956316
# [4,] 0.36156427 0.36156427
# This is just like OLS, with no constant
coef(lm(y~0+X))
# X1 X2 X3 X4
#-0.08051177 0.69528553 -1.75956316 0.36156427
}
\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
http://mf.erciyes.edu.tr/abc/pub/tr06_2005.pdf
Artificial Bee Colony (ABC) Algorithm (website)
http://mf.erciyes.edu.tr/abc/index.htm
Basic version of the algorithm implemented in \code{C} (ABC's official
website) http://mf.erciyes.edu.tr/abc/form.aspx
}
\author{
George Vega Yon \email{g.vegayon@gmail.com}
}
\keyword{optimization}