function [Best_Val,Best_Pos,Convergence_Curve]=WOA(Pop_Size,MaxT,LB,UB,D,FitFun)
Best_Pos = zeros(1,D);
Best_Val = inf;
Position = initialize(Pop_Size,D,UB,LB);
Convergence_Curve = zeros(1,MaxT);
T = 0;
while T<MaxT
for i = 1:size(Position,1)
CheckUB = Position(i,:)>UB;
CheckLB = Position(i,:)<LB;
Position(i,:) = (Position(i,:).*(~(CheckUB+CheckLB)))+UB.*CheckUB+LB.*CheckLB;
%Calculate Fitness Values
Fitness_Val = FitFun(Position(i,:));
%Compare Fitness Values
if Fitness_Val<Best_Val
Best_Val = Fitness_Val;
Best_Pos = Position(i,:);
end
end
a = 2-T*((2)/MaxT);
a2 = -1+T*((-1)/MaxT);
%Agents Position Update (New Positions)
for i=1:size(Position,1)
r1=rand();
r2=rand();
A = 2*a*r1-a;
C = 2 * r2;
b = 1;
l = (a2-1)*rand+1;
p = rand();
for j = 1:size(Position,2)
if p<0.5
if abs(A)>1
rand_best_index=floor(Pop_Size*rand()+1);
X_rand = Position(rand_best_index,:);
D_X_rand = abs(C*X_rand(j)-Position(i,j));
Position(i,j) = X_rand(j)-A*D_X_rand;
elseif abs(A)<1
D_Best = abs(C*Best_Pos(j)-Position(i,j));
Position(i,j) = Best_Pos(j)-A*D_Best;
end
elseif p>=0.5
distance2Best = abs(Best_Pos(j)-Position(i,j));
Position(i,j) = distance2Best * exp(b.*1).*cos(1.*2*pi)+Best_Pos(j);
end
end
end
T = T + 1; %Counter Increment
Convergence_Curve(T) = Best_Val;
[T Best_Val]
end
end
clc
clear all
Pop_Size = 100;
Objective_Fun = 'F2';
MaxT = 500;
[LB,UB,D,FitFun] = test_fun_info(Objective_Fun);
[Best_Val,Best_Pos,Sol_Convergence]=WOA(Pop_Size,MaxT,LB,UB,D,FitFun);
subplot(1,1,1);
semilogy(Sol_Convergence,'Color','r');
title('Convergence Curve');
xlabel('Iteration');
ylabel('Best Value');
axis tight
grid on
box on
legend ('WOA')
display(['Best Position',num2str(Best_Pos)]);
display(['Best_Value ',num2str(Best_Val)]);
Comments
Post a Comment