%
% Project Title: Optimal foraging algorithm (OFA)
% Developer: Guang-Yu Zhu
% Contact Info: zhugy@fzu.edu.cn
%
% Reference:
% [1] Zhu, Guang-Yu, and Zhang, Wei-Bo. "Optimal foraging algorithm for global optimization." Applied Soft Computing 51 (2017): 294-313.
%%
% This file is the core program of OFA.
% Input: Units -> The foraging group with popsize (or N) individuals
% The composition of the individual in "Units" is a d+1 dimensional vector, including the d dimensional state vector
% and the objective function value
% BOUND -> The lower and upper bounds of the state vector
% Itermax -> The maximum group's foraging number
% Output: GBestIndiv -> The best individual for global
% GBestFitness -> The best objective function value of GBestIndiv
% BestValProcess -> The change process of GBestFitness
function [GBestIndiv,GBestFitness,BestValProcess] = OFA(Units,BOUND,Itermax)
xZomeLength = size(Units,2);
DIMENSION = xZomeLength-1; % The dimensions of the objective function. d is used for this parameter in Fig.2 of reference [1].
N = size(Units,1); % The number of individuals.
[tmpUnitsFitness,uIndex] = sort( Units(:,xZomeLength) ); % Line 7 in Fig.2 of reference [1].
GBestFitness = tmpUnitsFitness(1);
GBestIndiv = Units(uIndex(1), : );
clear i j s1 s2 tmpUnitsFitness
t = 1; % Line 8 in Fig.2 of reference [1].
BestValProcess(t) = GBestFitness; % keep the change process of GBestFitness
%%
while( t <= Itermax ) % Line 9 in Fig.2 of reference [1].
clear UnitForaging;
K = t/Itermax;
for i=1:N % Line 11 in Fig.2 of reference [1].
current = uIndex(i);
if (i == 1) % The individuals in the foraging group (Units) has been sorted, so, the first dividual is the best dividual,
% Line 12 in Fig.2
for (j=1:DIMENSION)
r1=rand();
r2=rand();
UnitForaging(current,j)=Units(current,j)-K*( Units(current,j)-Units(uIndex(N),j) )*r1 + K*( Units(current,j)-Units(uIndex(N),j) )*r2; % Line 15 in Fig.2
end
else
front=floor(1+(i-1)*rand()); % Line 20 in Fig.2
position = uIndex(front); % Line 20 in Fig.2
for(j=1:DIMENSION)
r1=rand();
r2=rand();
UnitForaging(current,j)=Units(current,j)-K*( Units(current,j)-Units(position,j) )*r1+K*( Units(current,j)-Units(position,j) )*r2; % Line 23 in Fig.2
end
end
for j=1:DIMENSION % Line 16,17 or 24, 25 in Fig.2
if( UnitForaging(current,j)>BOUND(2) )
UnitForaging(current,j) = 2*BOUND(2) - UnitForaging(current,j);
end
if( UnitForaging(current,j)<BOUND(1) )
UnitForaging(current,j) = 2*BOUND(1) - UnitForaging(current,j);
end
end
% Compute the objective function value of new position. Line 28 in Fig.2
if( Units(current,(1:DIMENSION)) == UnitForaging(current,(1:DIMENSION)) )
UnitForaging(current,xZomeLength) = Units(current,xZomeLength);
else
[UnitForaging(current,:),UnitForaging(current,xZomeLength)] = Sphere( UnitForaging(current,:) );
end
end
for i=1:N % Line 30-36 in Fig.2
lambta=rand();
if( (lambta*UnitForaging(i,xZomeLength)/(1+lambta*(t+1))) < (Units(i,xZomeLength)/t) )
Units(i, : )=UnitForaging(i, : );
end
end
clear r1 r2 s1 s2 lambta
[tmpUnitsFitness,uIndex]=sort( Units(:,xZomeLength) ); % Line 37 in Fig.2
if(GBestFitness > tmpUnitsFitness(1)) % Line 38,39 in Fig.2
GBestFitness = tmpUnitsFitness(1);
GBestIndiv=Units(uIndex(1), : );
end
BestValProcess(t) = GBestFitness;
t = t+1; % Line 40 in Fig.2
end