Artificial Bee Colony (ABC) Algorithm
Artificial Bee Colony (ABC) Algorithm
Algorithm
1
Artificial Bee Colony (ABC) is one of the
most recently defined algorithms by Dervis
Karaboga in 2005, motivated by the
intelligent behavior of honey bees.
It is as simple as Particle Swarm
Optimization (PSO) and Differential
Evolution (DE) algorithms, and uses only
common control parameters such as
colony size and maximum cycle number.
http://mf.erciyes.edu.tr/abc/index.htm
2
ABC as an optimization tool, provides a
population-based search procedure.
Individuals called foods positions are modified by
the artificial bees with time and the bees aim is to
discover the places of food sources with high
nectar amount and finally the one with the
highest nectar.
Artificial bees fly around in a multidimensional
search space and some (employed and onlooker
bees) choose food sources depending on the
experience of themselves and their nest mates,
and adjust their positions.
3
Some (scouts) fly and choose the food sources
randomly without using experience.
If the nectar amount of a new source is higher
than that of the previous one in their memory,
they memorize the new position and forget the
previous one.
Thus, ABC system combines local search
methods, carried out by employed and onlooker
bees, with global search methods, managed by
onlookers and scouts, attempting to balance
exploration and exploitation process.
4
General scheme of the ABC algorithm
Initialization Phase
REPEAT
Employed Bees Phase
Onlooker Bees Phase
Scout Bees Phase
Memorize the best solution achieved so far
UNTIL (Termination condition is satisfied)
5
Related Operators
Employed Bees Phase and Onlooker Bees Phase
6
Related Operators
Maximization: return -objValuei;
Minimization: return objValuei;
Maximization: Fiti = 1 + fabs(objValuei);
Minimization: Fiti = 1/(objValuei+1);
7
Basic ABC Source Code
int main()
void initial()
void init(int index)
double objFun(double sol[MaxVar])
double CalculateFitness(double fun)
void SendEmployedBees()
void SendOnlookerBees()
void CalculateProbabilities()
void SendScoutBees()
void MemorizeBestSource()
8
Some variable used
#define MAX_FUN 1 /*Maximum=1 or minimum otherwise*/
#define M_PI 3.14159
/*The number of food sources equals the half of the colony size*/
#define MaxFoodNumber MaxNP/2
/*The number of food sources equals the half of the colony size*/
int FoodNumber= MaxFoodNumber /2;
/*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/
int limit=100;
int maxCycle=2500; /*The number of cycles for foraging {a stopping criteria}*/
9
/* Problem specific variables*/
#define MaxVar 200 /*Max Variables*/
double lb[MaxVar]; /*lower bound of the parameters. */
double ub[MaxVar]; /*upper bound of the parameters. lb and ub can be defined as arrays for the
problems of which parameters have different bounds*/
#define runtime 1 /*Algorithm can be run many times in order to see its robustness*/
for (j=0;j<NVar;j++)
{
lb[j]=0; ub[j]=M_PI;
}
for(run=0;run<runtime;run++)
{
initial();
MemorizeBestSource();
for (iter=0;iter<maxCycle;iter++)
{
SendEmployedBees();
CalculateProbabilities();
SendOnlookerBees();
MemorizeBestSource();
SendScoutBees();
}
for(j=0;j<NVar;j++) printf("GlobalParam[%d]: %f\n",j+1,GlobalParams[j]);
top=1;
for(j=0;j<NVar;j++)
{
if (j%2==0) top=top*(1-cos(sol[j]));
else top=top*(1-sin(sol[j]));
}
//Maximize *(-1)
if (MAX_FUN==1)
{
return -top;
}
//Minimize
else
{
return top;
} 13
}
/*Fitness function*/
double CalculateFitness(double fun)
{
double result=0;
if (fun>=0)
{
result=1/(fun+1);
}
else
{
result=1+fabs(fun);
}
return result;
}
14
void SendEmployedBees() ///*Employed Bee Phase*/
{
int i, j;
for (i=0;i<MaxFoodNumber;i++)
{
/*The parameter to be changed is determined randomly*/
r = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
param2change=(int)(r*NVar);
/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
neighbour=(int)(r*FoodNumber);
/*Randomly selected solution must be different from the solution i*/
while (neighbour==i)
{
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
neighbour=(int)(r*FoodNumber);
}
for(j=0;j<NVar;j++) solution[j]=Foods[i][j];
/*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;
/*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
if (solution[param2change]<lb[param2change]) solution[param2change]=lb[param2change];
if (solution[param2change]>ub[param2change]) solution[param2change]=ub[param2change];
ObjValSol=objFun(solution); FitnessSol=CalculateFitness(ObjValSol);
/*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>fitness[i])
{
/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
trial[i]=0;
for(j=0;j<NVar;j++) Foods[i][j]=solution[j];
f[i]=ObjValSol; fitness[i]=FitnessSol;
}
else
{ /*if the solution i can not be improved, increase its trial counter*/
trial[i]=trial[i]+1;
}
}
} 15
void CalculateProbabilities()
{
int i;
double maxfit;
maxfit=fitness[0];
for (i=1;i<FoodNumber;i++)
{
if (fitness[i]>maxfit)
maxfit=fitness[i];
}
for (i=0;i<FoodNumber;i++)
{
prob[i]=(0.9*(fitness[i]/maxfit))+0.1;
}
}
16
void SendOnlookerBees() ///*onlooker Bee Phase*/
{
int i=0, j, t=0;
while (t<FoodNumber)
{
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
if (r<prob[i]) /*choose a food source depending on its probability to be chosen*/
{
t++;
/*The parameter to be changed is determined randomly*/
r = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) ); param2change=(int)(r*NVar);
/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) ); neighbour=(int)(r*MaxFoodNumber);
while(neighbour==i) /*Randomly selected solution must be different from the solution i*/
{
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) ); neighbour=(int)(r*MaxFoodNumber);
}
for(j=0;j<NVar;j++) solution[j]=Foods[i][j];
/*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;
/*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
if (solution[param2change]<lb[param2change]) solution[param2change]=lb[param2change];
if (solution[param2change]>ub[param2change]) solution[param2change]=ub[param2change];
ObjValSol=objFun(solution); FitnessSol=CalculateFitness(ObjValSol);
if (FitnessSol>fitness[i]) /*a greedy selection is applied between the current solution i and its mutant*/
{
/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
trial[i]=0;
for(j=0;j<NVar;j++) Foods[i][j]=solution[j];
f[i]=ObjValSol; fitness[i]=FitnessSol;
}
else
{ /*if the solution i can not be improved, increase its trial counter*/
trial[i]=trial[i]+1;
}
} /*if */
i++;
if (i==FoodNumber-1) i=0;
}/*while*/
}
17
void SendScoutBees()
{
int maxtrialindex,i;
maxtrialindex=0;
for (i=1;i<FoodNumber;i++)
{
if (trial[i]>trial[maxtrialindex])
maxtrialindex=i;
}
if(trial[maxtrialindex]>=limit)
{
init(maxtrialindex);
}
}
18