蝙蝠算法初探


蝙蝠算法初探

function [best,fmin,N_iter]=bat_algorithm()  
n=20;  % Population size, typically 10 to 40  蝙蝠個體數
N_gen=1000;  % Number of generations  迭代次數
% This frequency range determines the scalings. You should change these values if necessary
Qmin=0;         % Frequency minimum
Qmax=2;         % Frequency maximum

% Iteration parameters  迭代參數
N_iter=0;       % Total number of function evaluations  功能評價總數 
% Dimension of the search variables    搜索維數
d=10;           % Number of dimensions 

A=1+rand(n,1);    % Loudness  (constant or decreasing)響度,按照p8要求產生[1,2]的隨機數
r=rand(n,1);      % Pulse rate (constant or decreasing)脈沖率,設置為[0,1]的隨機數
al = 0.85;        
rr = 0.9;
r0 = r;

% Lower limit/bounds/ a vector
Lb=-2*ones(1,d);
% Upper limit/bounds/ a vector
Ub=2*ones(1,d);
% Initializing arrays  初始化數組
Q=zeros(n,1);   % Frequency 頻率
v=zeros(n,d);   % Velocities 速度

% Initialize the population/solutions
for i=1:n
  Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);  %rand(m*n)會生成  m*n的矩陣,矩陣元素是[0,10]隨機數
  Fitness(i)=Fun(Sol(i,:));
end
% Find the initial best solution
[fmin,I]=min(Fitness);   %I 記錄取得fmin的Fitness的位置,而這位置正是Sol中解的位置;fmin是Fitness中最小的值
best=Sol(I,:);           %記錄最好的解

% Start the iterations -- Bat Algorithm (essential part)  %
for t=1:N_gen 
% Loop over all bats/solutions
        for i=1:n 
          Q(i)=Qmin+(Qmin-Qmax)*rand;
          v(i,:)=v(i,:)+(Sol(i,:)-best)*Q(i);
          S(i,:)=Sol(i,:)+v(i,:);
          % Apply simple bounds/limits
          Sol(i,:)=simplebounds(Sol(i,:),Lb,Ub);  %越界檢查
          % Pulse rate
          if rand>r(i,1)
          % The factor 0.001 limits the step sizes of random walks 
              S(i,:)=best+0.001*randn(1,d);%這里的新的蝙蝠個體是由當前全局最好的個體產生的
              %論文中是以“上一代的蝙蝠體”+“響度的隨機的倍數”,這里不再實現  
          end

     % Evaluate new solutions
           Fnew=Fun(S(i,:));
     % Update if the solution improves, or not too loud
           if ((Fnew<=Fitness(i)) && (rand<A(i,1)))
                Sol(i,:)=S(i,:);
                Fitness(i)=Fnew;
                A(i,1) = al*A(i,1);               %對響度進行更新
                r(i,1) = r0(i,1)*(1-exp(-1*rr*t ));  %對脈沖率進行更新
           end

          % Update the current best solution
          if Fnew<=fmin 
                best=S(i,:);
                fmin=Fnew;
          end
        end
        N_iter=N_iter+n;
end
% Output/display
disp(['Number of evaluations: ',num2str(N_iter)]);
disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);

% Application of simple limits/bounds   越界檢查
function s=simplebounds(s,Lb,Ub)
  % Apply the lower bound vector
  ns_tmp=s;
  I=ns_tmp<Lb;
  ns_tmp(I)=Lb(I);
  
  % Apply the upper bound vector 
  J=ns_tmp>Ub;
  ns_tmp(J)=Ub(J);
  % Update this new move 
  s=ns_tmp;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Objective function: your own objective function can be written here
% Note: When you use your own function, please remember to 
%       change limits/bounds Lb and Ub (see lines 52 to 55) 
%       and the number of dimension d (see line 51). 
% 在這里設置你自己函數,並且更新搜索區間上限和下限,以及自變量x的維度
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y=Fun(x)
% Griewan函數
% 輸入x,給出相應的y值,在x = ( 0 , 0 ,…, 0 )處有全局極小點0.
    [row,col] = size(x);
    if  row > 1 
        error( ' 輸入的參數錯誤 ' );
    end
    y1 = 1 / 4000 * sum(x.^2 );    
    y2 = 1 ;
    for  h = 1 :col
        y2 = y2 * cos(x(h) / sqrt(h));
    end    
    y = y1 - y2 + 1 ; 
%%%%% ============ end ====================================

  

參考文獻:蝙蝠算法的改進與應用   何子曠  廣東工業大學碩士學位論文  2016.5

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM