1 简介

蝗虫算法( Grasshopper Optimization Algorithm,GOA ) 是 由 Saremi 等[1]于2017 年提出的一种元启发式仿生优化算法。具体原理如下:

【优化求解】基于蝗虫算法求解单目标问题matlab代码_优化算法

【优化求解】基于蝗虫算法求解单目标问题matlab代码_蝗虫算法_02

2 部分代码

%_____________________________

% Multi-objective Grasshopper Optimization Algorithm (MOGOA) source codes version 1.0

%


clc;

clear;

close all;


% Change these details with respect to your problem%%%%%%%%%%%%%%

ObjectiveFunction=@ZDT1;

dim=5;

lb=0;

ub=1;

obj_no=2;


if size(ub,2)==1

 ub=ones(1,dim)*ub;

 lb=ones(1,dim)*lb;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

flag=0;

if (rem(dim,2)~=0)

 dim = dim+1;

 ub = [ub, 1];

 lb = [lb, 0];

 flag=1;

end



max_iter=100;

N=200;

ArchiveMaxSize=100;


Archive_X=zeros(100,dim);

Archive_F=ones(100,obj_no)*inf;


Archive_member_no=0;


%Initialize the positions of artificial whales

GrassHopperPositions=initialization(N,dim,ub,lb);


TargetPosition=zeros(dim,1);

TargetFitness=inf*ones(1,obj_no);


cMax=1;

cMin=0.00004;

%calculate the fitness of initial grasshoppers


for iter=1:max_iter

 for i=1:N

   

   Archive_mem_ranks=RankingProcess(Archive_F, ArchiveMaxSize, obj_no);

   [Archive_X, Archive_F, Archive_mem_ranks, Archive_member_no]=HandleFullArchive(Archive_X, Archive_F, Archive_member_no, Archive_mem_ranks, ArchiveMaxSize);

 else

   Archive_mem_ranks=RankingProcess(Archive_F, ArchiveMaxSize, obj_no);

 end

 

 Archive_mem_ranks=RankingProcess(Archive_F, ArchiveMaxSize, obj_no);

 index=RouletteWheelSelection(1./Archive_mem_ranks);

 if index==-1

   index=1;

 end

 TargetFitness=Archive_F(index,:);

 TargetPosition=Archive_X(index,:)';

 

 c=cMax-iter*((cMax-cMin)/max_iter); % Eq. (3.8) in the paper

 

 for i=1:N

   

   temp= GrassHopperPositions;

   

   for k=1:2:dim

     S_i=zeros(2,1);

     for j=1:N

       if i~=j

         Dist=distance(temp(k:k+1,j), temp(k:k+1,i));

         r_ij_vec=(temp(k:k+1,j)-temp(k:k+1,i))/(Dist+eps);

         xj_xi=2+rem(Dist,2);

           

         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Eq. (3.2) in the paper

         s_ij=((ub(k:k+1)' - lb(k:k+1)') .c/2)S_func(xj_xi).*r_ij_vec;

         S_i=S_i+s_ij;

         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

       end

     end

     S_i_total(k:k+1, :) = S_i;

     

   end

   

   X_new=c*S_i_total'+(TargetPosition)'; % Eq. (3.7) in the paper

   GrassHopperPositions_temp(i,:)=X_new';

 end

 % GrassHopperPositions

 GrassHopperPositions=GrassHopperPositions_temp';

 

 display(['At the iteration ', num2str(iter), ' there are ', num2str(Archive_member_no), ' non-dominated solutions in the archive']);

end



if (flag==1)

 TargetPosition = TargetPosition(1:dim-1);

end


figure


Draw_ZDT1();


hold on


plot(Archive_F(:,1),Archive_F(:,2),'ro','MarkerSize',8,'markerfacecolor','k');


legend('True PF','Obtained PF');

title('MOGOA');


set(gcf, 'pos', [403  466  230  200])

img =gcf; %获取当前画图的句柄

print(img, '-dpng', '-r600', './img.png')     %即可得到对应格式和期望dpi的图像

3 仿真结果

【优化求解】基于蝗虫算法求解单目标问题matlab代码_优化算法_03

【优化求解】基于蝗虫算法求解单目标问题matlab代码_蝗虫算法_04


4 参考文献

[1]潘峰, and 孙红霞. "基于蝗虫算法的图像多阈值分割方法." 电子测量与仪器学报 033.001(2019):149-155.

【优化求解】基于蝗虫算法求解单目标问题matlab代码_蝗虫算法_05