【优化算法】基于变异策略的改进型花朵授粉算法matlab源码

118 阅读3分钟

 一、简介


介绍了一种新的元启发式群智能算法——花朵授粉算法(flower pollinate algorithm,FPA)和一种新型的差分进化变异策略——定向变异(targeted mutation,TM)策略。针对FPA存在的收敛速度慢、寻优精度低、易陷入局部最优等问题,提出了一种基于变异策略的改进型花朵授粉算法——MFPA。该算法通过改进TM策略,并应用到FPA的局部搜索过程中,以增强算法的局部开发能力。

二、源代码

function [pdd,fmin ] =pso( c1,c2,Vmax,Vmin,popmax,popmin,sizepop,maxgen)
%UNTITLED2 此处显示有关此函数的摘要
%   此处显示详细说明
PLb=-5.12*ones(1,30);
PUb=5.12*ones(1,30);
pop=zeros(sizepop,30);
V=zeros(1,30);
fitnessP=zeros(1,sizepop);
for i=1:sizepop
    pop(i,:)=PLb+(PUb-PLb)*rand;
    V(i,:)=rands(1,30);
    fitnessP(i)=Fun(pop(i,:));
end
[bestfitness bestindex]=min(fitnessP);
zbest=pop(bestindex,:);   %全局最佳
gbest=pop;    %个体最佳
fitnessgbest=fitnessP;   %个体最佳适应度值
fitnesszbest=bestfitness;   %全局最佳适应度值
% Ntime11=1    ;
% Ntime=Ntime11-1;
% maxgen=0;
% ptol=0.01;
% while(fitnesszbest>ptol),
for i11=1:maxgen
  
    for j=1:sizepop
        
        %速度更新
        V(j,:) = V(j,:) + c1*rand*(gbest(j,:)-pop(j,:)) + c2*rand*(zbest-pop(j,:));
        V(j,find(V(j,:)>Vmax))=Vmax;
        V(j,find(V(j,:)<Vmin))=Vmin;
        
        %种群更新
        pop(j,:)=pop(j,:)+0.2*V(j,:);
        pop(j,find(pop(j,:)>popmax))=popmax;
        pop(j,find(pop(j,:)<popmin))=popmin;
        
        %自适应变异
        pos=unidrnd(30);
        if rand>0.95
            pop(j,pos)=5.12*rands(1,1);
        end
      
        %适应度值
%          pop(j,:)=simpleboundsP(pop(j,:),PLb,PUb);
        fitnessP(j)=Fun(pop(j,:));
      
    end
    
    for j=1:sizepop
    %个体最优更新
    if fitnessP(j) < fitnessgbest(j)
        gbest(j,:) = pop(j,:);
        fitnessgbest(j) = fitnessP(j);
    end
    
    %群体最优更新 
    if fitnessP(j) < fitnesszbest
        zbest = pop(j,:);
        fitnesszbest = fitnessP(j);
    end
%         Ntime11=Ntime11+1;
       
  
     
    
    end
%     maxgen=maxgen+1;
%     if maxgen>10000,
%         fitnesszbest=ptol-1;
%     end
%    if round(i11/30)==i11/30,
     pdd(i11)=fitnesszbest
%    end
       
end
fmin =fitnesszbest;
end
% function sfP=simpleboundsP(sfP,PLb,PUb)
%   % Apply the lower bound
%   ns_tmpfP=sfP;
%   IfP=ns_tmpfP<PLb;
%   ns_tmpfP(IfP)=PLb(IfP);
%   
%   % Apply the upper bounds 
%   JfP=ns_tmpfP>PUb;
%   ns_tmpfP(JfP)=PUb(JfP);
%   % Update this new move 
%   sfP=ns_tmpfP;
% end
% ======================================================== % 
% Files of the Matlab programs included in the book:       %
% Xin-She Yang, Nature-Inspired Metaheuristic Algorithms,  %
% Second Edition, Luniver Press, (2010).   www.luniver.com %
% ======================================================== %    

% -------------------------------------------------------- %
% Bat-inspired algorithm for continuous optimization (demo)%
% Programmed by Xin-She Yang @Cambridge University 2010    %
% -------------------------------------------------------- %
% Usage: bat_algorithm([20 0.25 0.5]);                     %

function [pblt,fminbl]=bat_algorithm(nb,A,r,BQmin,BQmax,db,NB)
% Display help
%  help bat_algorithm.m

% Default parameters
% if nargin<1,  para=[10 0.25 0.5];  end
% nb=para(1);      % Population size, typically 10 to 25
% A=para(2);      % Loudness  (constant or decreasing)
% r=para(3);      % Pulse rate (constant or decreasing)
% % This frequency range determines the scalings
% BQmin=0;         % Frequency minimum
% BQmax=2;         % Frequency maximum
% % Iteration parameters
%  % Stop tolerance
% N_iter=0;       % Total number of function evaluations
% Dimension of the search variables
% db=5;
% Initial arrays
BLb=-5.12*ones(1,db);
BUb=5.12*ones(1,db);
Q=zeros(nb,1);   % Frequency
v=zeros(nb,db);   % Velocities
Solb=zeros(nb,db);
Fitnessb=zeros(1,nb);
Sb=zeros(nb,db);
% Initialize the population/solutions
for i=1:nb,
  Solb(i,:)=BLb+(BUb-BLb)*rand;
  Fitnessb(i)=Fun(Solb(i,:));
end
% Find the current best
[fminb,Ib]=min(Fitnessb);
bestb=Solb(Ib,:);

% ======================================================  %
% Note: As this is a demo, here we did not implement the  %
% reduction of loudness and increase of emission rates.   %
% Interested readers can do some parametric studies       %
% and also implementation various changes of A and r etc  %
% ======================================================  %
% btol=0.01;
% NB=0;
% Start the iterations -- Bat Algorithm
% while(fminb>btol),
for tb =1: NB,
        % Loop over all bats/solutions
        for i=1:nb,
          Q(i)=BQmin+(BQmin-BQmax)*rand;
          v(i,:)=v(i,:)+(Solb(i,:)-bestb)*Q(i);
          Sb(i,:)=Solb(i,:)+v(i,:);
          % Pulse rate
          if rand>r
              Sb(i,:)=bestb+0.01*randn(1,db);
          end

     % Evaluate new solutions
       Sb(i,:)=BsimpleboundsP(Sb(i,:),BLb,BUb);
           Fnewb=Fun(Sb(i,:));
     % If the solution improves or not too loudness
           if (Fnewb<=Fitnessb(i)) & (rand<A) ,
                Solb(i,:)=Sb(i,:);
                Fitnessb(i)=Fnewb;
           end

          % Update the current best
          if Fnewb<=fminb,
                bestb=Sb(i,:);
                fminb=Fnewb;
          end
        end
        function [aa,fminf,Ntime ] = fpa(n,p,N_iter,d )
%UNTITLED3 此处显示有关此函数的摘要
%   此处显示详细说明
Lb=-600*ones(1,d);
Ub=600*ones(1,d);
 Sol=zeros(n,d);
  Fitness=zeros(1,n);
for i=1:n,
  Sol(i,:)=Lb+(Ub-Lb)*rand;
  Fitness(i)=Fun(Sol(i,:));
end

% Find the current best
[fmin,I]=min(Fitness);
best=Sol(I,:);
S=Sol;
 Ntime=1;
  Ntime= Ntime-1;
for t=1:N_iter,
        % Loop over all bats/solutions
        for i=1:n,
          % Pollens are carried by insects and thus can move in
          % large scale, large distance.
          % This L should replace by Levy flights  
          % Formula: x_i^{t+1}=x_i^t+ L (x_i^t-gbest)
          if rand<p,
          %% L=rand;
          L=Levy(d);
          
          dS=L.*(Sol(i,:)-best);
          S(i,:)=Sol(i,:)+dS;
          
          % Check if the simple limits/bounds are OK
          S(i,:)=simplebounds(S(i,:),Lb,Ub);
          
          % If not, then local pollenation of neighbor flowers 
          else
              epsilon=rand;
              % Find random flowers in the neighbourhood
              JK=randperm(n);
             

%               end
              % As they are random, the first two entries also random
              % If the flower are the same or similar species, then
              % they can be pollenated, otherwise, no action.
              % Formula: x_i^{t+1}+epsilon*(x_j^t-x_k^t)
%            
               S(i,:)=S(i,:)+epsilon*(Sol(JK(1))-Sol(JK(2)));
%               
              % Check if the simple limits/bounds are OK
              S(i,:)=simplebounds(S(i,:),Lb,Ub);
          end

三、运行结果