Detailed introduction and matlab simulation of WOA whale optimization algorithm

Table of contents

1. Theoretical Overview

2. Theoretical knowledge of WOA whale optimization algorithm

2.1 Surrounding and Predating  

2.2 Bubble attack 

2.3 Food-seeking stage 

3.MATLAB program

4.MATLAB simulation results


1. Theoretical Overview

       WOA (Whale Optimization Algorithm) is an optimization algorithm based on the foraging behavior of whales in nature. It was proposed by Mirjalili et al. in Iran in 2016. The algorithm is inspired by the various behavioral characteristics of whales in search of food, such as circling, swimming up, and diving down. These behaviors can help whales find food more efficiently, and similarly, the WOA algorithm can also exert its advantages when solving many optimization problems.

       The main idea of ​​the whale optimization algorithm is to simulate the foraging behavior of whales. During the process of foraging, whales will judge the direction and distance of food by sensing information such as sounds and tastes in the surrounding environment, and adopt corresponding swimming behaviors. Similarly, the WOA algorithm searches the solution space of the optimization problem by simulating these behaviors of whales.

2. Theoretical knowledge of WOA whale optimization algorithm


The whale optimization algorithm mainly consists of three steps, namely the surrounding predation, bubble attack and food-seeking stages.

2.1 Surrounding and Predating  

   
       In the initial stage of the algorithm, humpback whales are not completely sure of the specific location of the food. They all obtain the location information of the food through group cooperation. Therefore, the whale closest to the food is equivalent to the current local optimal solution, and the others are Individual whales will approach this position and gradually surround the food. Therefore, the following mathematical model is used to express:

2.2 Bubble attack 


      During the whale's hunting process, bubbles are used to attack, and the behavior of the whale feeding and spitting out bubbles is simulated by contracting the surrounding and spirally updating the position, thereby achieving the purpose of local optimization of the whale. When a humpback whale finds food, if the probability is less than 0.5, it will still be updated in the traditional way. Otherwise, the distance between the individual and the current optimal whale will be calculated, and then it will swim away in a spiral manner. Therefore, when searching for food, The mathematical model of spiral walking is as follows:

2.3 Food-seeking stage 


       In order to better ensure search and convergence, individual whales can also randomly search for food. When |A|>1, the randomly selected humpback whale individuals will guide other humpback whales to move closer to them, and vice versa. The position is only a local optimal position. This method ensures that individual humpback whales can conduct a global search and obtain the global optimal solution. The mathematical model is expressed as follows:

3.MATLAB program


% The Whale Optimization Algorithm
function [Leader_score,Leader_pos,Convergence_curve]=WOA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

% 初始化领导者的位置向量和得分  
Leader_pos=zeros(1,dim);
Leader_score=inf; % 对于最大化问题,将其更改为-inf


% 初始化搜索代理的位置  
Positions=initialization(SearchAgents_no,dim,ub,lb);

Convergence_curve=zeros(1,Max_iter);

t=0;% Loop counter
% 主循环 
while t<Max_iter
    for i=1:size(Positions,1)
        
        % 将超出搜索空间边界的搜索代理返回回来 
        Flag4ub=Positions(i,:)>ub;
        Flag4lb=Positions(i,:)<lb;
        Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
        
        % 计算每个搜索代理的目标函数值 
        fitness=fobj(Positions(i,:));
        
        % 更新领导者 
        if fitness<Leader_score % 对于最大化问题,将其更改为>  
            Leader_score=fitness; % 更新领导者得分  
            Leader_pos=Positions(i,:);
        end
        
    end
    
    a=2-t*((2)/Max_iter); % a 从2到0线性减小 
    
    % a2 从-1到-2线性减小 
    a2=-1+t*((-1)/Max_iter);
    
    % 更新搜索代理的位置
    for i=1:size(Positions,1)
        r1=rand(); % r1 是 [0,1] 中的一个随机数 
        r2=rand(); % r2是 [0,1] 中的一个随机数 
        
        A=2*a*r1-a;   
        C=2*r2;    
        
        
        b=1;               
        l=(a2-1)*rand+1;    
        
        p = rand();        
        % 从当前搜索代理的位置和领导者位置中选择一个随机位置 X_rand,计算公式 (2.7) 中的 D。根据公式 (2.8) 和 (2.9),更新搜索代理的位置。如果更新后的位置超出了搜索空间的边界,则将其返回到边界上。最后,根据目标函数计算新的适应度值,并更新领导者的位置和得分。如果达到最大迭代次数,则退出循环并返回领导者的得分、位置和收敛曲线。
        for j=1:size(Positions,2)
            
            if p<0.5   
                if abs(A)>=1
                    rand_leader_index = floor(SearchAgents_no*rand()+1);
                    X_rand = Positions(rand_leader_index, :);
                    D_X_rand=abs(C*X_rand(j)-Positions(i,j));  
                    Positions(i,j)=X_rand(j)-A*D_X_rand;       
                    
                elseif abs(A)<1
                    D_Leader=abs(C*Leader_pos(j)-Positions(i,j));   
                    Positions(i,j)=Leader_pos(j)-A*D_Leader;       
                end
                
            elseif p>=0.5
              
                distance2Leader=abs(Leader_pos(j)-Positions(i,j));
                % Eq. (2.5)
                Positions(i,j)=distance2Leader*exp(b.*l).*cos(l.*2*pi)+Leader_pos(j);
                
            end
            
        end
    end
    t=t+1;
    Convergence_curve(t)=Leader_score;
    [t Leader_score]
end
up2226

The designed objective function is as follows:


% F1

function o = F1(x)
o=sum(x.^2);
end

% F2

function o = F2(x)
o=sum(abs(x))+prod(abs(x));
end

% F3

function o = F3(x)
dim=size(x,2);
o=0;
for i=1:dim
    o=o+sum(x(1:i))^2;
end
end

% F4

function o = F4(x)
o=max(abs(x));
end

% F5

function o = F5(x)
dim=size(x,2);
o=sum(100*(x(2:dim)-(x(1:dim-1).^2)).^2+(x(1:dim-1)-1).^2);
end

% F6

function o = F6(x)
o=sum(abs((x+.5)).^2);
end

% F7

function o = F7(x)
dim=size(x,2);
o=sum([1:dim].*(x.^4))+rand;
end

% F8

function o = F8(x)
o=sum(-x.*sin(sqrt(abs(x))));
end

% F9

function o = F9(x)
dim=size(x,2);
o=sum(x.^2-10*cos(2*pi.*x))+10*dim;
end

% F10

function o = F10(x)
dim=size(x,2);
o=-20*exp(-.2*sqrt(sum(x.^2)/dim))-exp(sum(cos(2*pi.*x))/dim)+20+exp(1);
end

% F11

function o = F11(x)
dim=size(x,2);
o=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1;
end

% F12

function o = F12(x)
dim=size(x,2);
o=(pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)+sum((((x(1:dim-1)+1)./4).^2).*...
(1+10.*((sin(pi.*(1+(x(2:dim)+1)./4)))).^2))+((x(dim)+1)/4)^2)+sum(Ufun(x,10,100,4));
end

% F13

function o = F13(x)
dim=size(x,2);
o=.1*((sin(3*pi*x(1)))^2+sum((x(1:dim-1)-1).^2.*(1+(sin(3.*pi.*x(2:dim))).^2))+...
((x(dim)-1)^2)*(1+(sin(2*pi*x(dim)))^2))+sum(Ufun(x,5,100,4));
end

% F14

function o = F14(x)
aS=[-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32;,...
-32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 0 16 16 16 16 16 32 32 32 32 32];

for j=1:25
    bS(j)=sum((x'-aS(:,j)).^6);
end
o=(1/500+sum(1./([1:25]+bS))).^(-1);
end

% F15

function o = F15(x)
aK=[.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246];
bK=[.25 .5 1 2 4 6 8 10 12 14 16];bK=1./bK;
o=sum((aK-((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2);
end

% F16

function o = F16(x)
o=4*(x(1)^2)-2.1*(x(1)^4)+(x(1)^6)/3+x(1)*x(2)-4*(x(2)^2)+4*(x(2)^4);
end

% F17

function o = F17(x)
o=(x(2)-(x(1)^2)*5.1/(4*(pi^2))+5/pi*x(1)-6)^2+10*(1-1/(8*pi))*cos(x(1))+10;
end

% F18

function o = F18(x)
o=(1+(x(1)+x(2)+1)^2*(19-14*x(1)+3*(x(1)^2)-14*x(2)+6*x(1)*x(2)+3*x(2)^2))*...
    (30+(2*x(1)-3*x(2))^2*(18-32*x(1)+12*(x(1)^2)+48*x(2)-36*x(1)*x(2)+27*(x(2)^2)));
end

% F19

function o = F19(x)
aH=[3 10 30;.1 10 35;3 10 30;.1 10 35];cH=[1 1.2 3 3.2];
pH=[.3689 .117 .2673;.4699 .4387 .747;.1091 .8732 .5547;.03815 .5743 .8828];
o=0;
for i=1:4
    o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end

% F20

function o = F20(x)
aH=[10 3 17 3.5 1.7 8;.05 10 17 .1 8 14;3 3.5 1.7 10 17 8;17 8 .05 10 .1 14];
cH=[1 1.2 3 3.2];
pH=[.1312 .1696 .5569 .0124 .8283 .5886;.2329 .4135 .8307 .3736 .1004 .9991;...
.2348 .1415 .3522 .2883 .3047 .6650;.4047 .8828 .8732 .5743 .1091 .0381];
o=0;
for i=1:4
    o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end

% F21

function o = F21(x)
aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];

o=0;
for i=1:5
    o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end

% F22

function o = F22(x)
aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];

o=0;
for i=1:7
    o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end

% F23

function o = F23(x)
aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];
cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];

o=0;
for i=1:10
    o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end

function o=Ufun(x,a,k,m)
o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a));
end

4.MATLAB simulation results

Guess you like

Origin blog.csdn.net/ccsss22/article/details/133319587