蝙蝠算法


2012年,英國劍橋大學學者楊新社提出一種新的元啟發式優化算法-蝙蝠算法(Bat Algorithm, BA),該算法通過模擬蝙蝠回聲定位行為來尋找函數優化問題的最優解。

1. 蝙蝠算法的基本思想

由於蝙蝠的回聲定位行為與函數優化相似,所以可以利用蝙蝠的回聲定位行為來尋找最優解。蝙蝠算法把蝙蝠看作優化問題的可行解,通過模擬復雜環境中精確捕獲食物的機制解決優化問題。
首先,在搜索空間隨機分布若干個蝙蝠,確定種群個體的初始位置及初始速度,對種群中各個蝙蝠進行適應度評價,尋找最優個體位置;
然后,通過調整頻率產生新的解並修改個體的飛行速度和位置。在蝙蝠的速度和位置的更新過程中,頻率本質上控制着這些蝙蝠群的移動步伐和范圍;
蝙蝠在尋優過程中,通過調節脈沖發生率和響度促使蝙蝠朝着最優解方向移動。蝙蝠在剛開始搜索時具有較小的脈沖發生率,蝙蝠有較大的概率在當前最優解周圍進行局部搜索,同時較大的響度使得局部搜索范圍比較大,有較大的概率探測到更好的解。隨着迭代的增加,脈沖發生率增加,響度減少,局部搜索概率減少,局部挖掘的范圍也很小,蝙蝠不斷掃描定位目標,最終搜索到最優解。

2. 蝙蝠算法的數學描述

為了能使蝙蝠回聲定位機制形成算法,有必要對蝙蝠回聲定位及飛行速度、位置進行理想化建模:
(1) 所有的蝙蝠利用超聲波回聲的感覺差異判斷獵物、障礙物之間的差異;
(2) 蝙蝠是以速度\(v_i\)、位置\(x_i\)、固定頻率\(f_{min}\)、可變化波長\(\lambda\)和響度\(A_0\)隨機飛行的,並用不同的波長\(\lambda\)(或者頻率\(f\))和響度\(A_0\)搜索獵物。它們會根據接近獵物的程度自動調整它們發出脈沖的波長(或頻率);
(3) 盡管響度會以更多的方式變化,可以假定它的變化是從一個很大的值\(A_0\)到最小值\(A_{min}\)
(4) 由於計算量的問題,不能使用無限追蹤來估計時間延遲和三維地形;
(5) 為了簡單起見,使用一些近似值。一般設置頻率范圍為\([f_{min}, f_{max}]\)對應的波長范圍為\([\lambda_{min}, \lambda_{max}]\)
對於給定的問題,為了便於實現,可以使用任意波長,並且可以通過調整波長來搜索范圍,而可探測的區域的選擇方式為先選擇感興趣的區域,然后慢慢縮小。因為波長\(\lambda \times f\)為常數,所以可以在固定波長\(\lambda\)時,改變頻率。
為了簡單期間,可以假定\(f \in [0, f_{min}]\)。顯然,較高的頻率有較短的波長和較短的搜索距離。通常蝙蝠的搜索范圍在幾米內。脈沖發生率可以設定為\([0,1]\)范圍內,其中0表示沒有發出脈沖,1表示脈沖發生率最大。

3. 蝙蝠的速度和位置更新公式

3.1 頻率更新公式

\[f_i = f_{min} + (f_{max} - f_{min})\beta \]

其中,\(\beta \in [0,1]\)是一個隨機向量;\(x_*\) 是當前最優解。
3.2 速度更新公式

\[v_i^{t} = v_i^{t-1} + (x_{i}^{t} - x_*)f_i \]

3.3 位置更新公式

\[x_i^{t} = x_i^{t-1} + v_i^{t} \]

3.4 局部搜索更新公式

\[x_i^{new} = x_i^{old} + \epsilon A^{t} \\ \]

其中,\(\epsilon \in [-1,1]\)是一個隨機數,\(A^{t}\)是當前迭代的平均響度。
3.5 響度和脈沖發生率更新公式
脈沖發射的響度\(A_i\)和脈沖發生率\(r_i\)要隨着迭代過程的進行來更新。蝙蝠一旦發現了獵物,響度會逐漸降低,同時脈沖速率就會提高,響度會以任意簡便值改變。

\[A_{i}^{t+1} = \alpha A_{i}^{t} \\ r_{i}^{t+1} = r_{i}^{0}[1 - exp(-\gamma t)] \]

其中,\(\alpha\)\(\gamma\)是常量。參數的選擇需要一定的經驗。初始時,每只蝙蝠所發出的響度和脈沖發生率的值都是不同的,這可以通過隨機選擇。初始的響度\(A_{i}^{0}\)通常在\([0,1]\)之間,而初始脈沖發生率一般去在0附近。
4. 算法實現
版本一

% =========================================================================
% Title: Bat Algorihtm
% Author: Lee WenTsao
% Time: 2022-04-12
% E-mail: liwenchao36@163.com
% =========================================================================
clc;
clear;

%% 問題參數定義
n = 20;   % 種群規模
d = 10;   % 維度

fobj = @ sphere;  % 目標函數

%% 蝙蝠算法參數定義
t_max = 1000;     % 最大迭代次數

A = 1;            % 初始響度
r0 = 1;           % 初始脈沖發生率

alpha = 0.97;
gamma = 0.1;

Freq_min = 0;     % 蝙蝠發射頻率的下界
Freq_max = 2;     % 蝙蝠發射頻率的上界

iter = 0;

%% 初始化動態參數
Freq = zeros(n,1);     
v  = zeros(n,d);     % 蝙蝠的初始速度

lb = -5*ones(1,d);  % 下界
ub =  5*ones(1,d);  % 上界

%% 初始化種群
for i=1:n
    Sol(i, :) = lb + (ub - lb).*rand(1, d);   % 隨機初始化種群
    Fitness(i) = fobj(Sol(i, :));             % 評估
end
[fmin, idx] = min(Fitness);
best = Sol(idx,:);

%% 添加雲圖


%% 開始仿真
while iter<t_max
    r = r0*(1-exp(-gamma*iter));
    A = alpha*A;

    for i=1:n
        % 全局搜索
        Freq(i) = Freq_min + (Freq_max - Freq_min)*rand;     % 頻率更新公式 
        v(i,:)=v(i,:)+(Sol(i,:)-best)*Freq(i);               % 速度更新
        S(i, :) = Sol(i, :) + v(i,:);                        % 位置更新
        
        % 局部搜索
        if rand<r
             S(i, :) = best + 0.1*randn(1,d)*A;
        end

        % 邊界預處理
        S(i, :) = simplebounds(S(i, :), lb, ub);

        % 評估
        Fnew = fobj(S(i, :));
        if Fnew<=Fitness(i) & rand>A
            Sol(i,:) = S(i, :);
            Fitness(i) = Fnew; 
        end

        if Fnew<fmin
            best = S(i,:);
            fmin = Fnew;
        end
    end
    iter = iter + 1;

    if ~mod(iter,10)
        disp(['Iteration=',num2str(iter),' fmin=',num2str(fmin)]);
    end
end

function s=simplebounds(s,Lb,Ub)
    % Apply the lower bound
    ns_tmp=s;
    I=ns_tmp<Lb;
    ns_tmp(I)=Lb(I);
    
    % Apply the upper bounds 
    J=ns_tmp>Ub;
    ns_tmp(J)=Ub(J);
    % Update this new move 
    s=ns_tmp;
end

第二版本(楊老師原版)

% ----------------------------------------------------------------------- %
% The bat algorithm (BA) for continuous function optimization (demo)      %
% Programmed by Xin-She Yang @Cambridge University 2010                   %
% ----------------------------------------------------------------------- %

% References: ----------------------------------------------------------- %
% 1) Yang X.S. (2010). A New Metaheuristic Bat-Inspired Algorithm, In:    %
%   Nature-Inspired Cooperative Strategies for Optimization (NICSO 2010), %
%   Studies in Computational Intelligence, vol 284. Springer, Berlin.     %
%   https://doi.org/10.1007/978-3-642-12538-6_6                           %
% 2) Yang X.S. (2014). Nature-Inspired Optimization Algorithms, Elsevier. %
% ----------------------------------------------------------------------- %

function [best,fmin,N_iter]=bat_algorithm_new(inp)
if nargin<1,
   inp=[20 1000];    % Default values for n=10 and t_max=1000
end
% Initialize all the default parameters
n=inp(1);       % Population size, typically 20 to 40 
t_max=inp(2);   % Maximum number of iterations
A=1;            % Initial loudness (constant or decreasing)
r0=1;           % The initial pulse rate (constant or decreasing)
alpha=0.97;     % Parameter alpha
gamma=0.1;      % Parameter gamma
% Frequency range
Freq_min=0;     % Frequency minimum
Freq_max=2;     % Frequency maximum
t=0;            % Initialize iteration counter
% Dimensions of the search variables
d=10;          
% Initialization of all the arrays
Freq=zeros(n,1);   % Frequency-tuning array
v=zeros(n,d);      % Equivalnet velocities or increments
Lb=-5*ones(1,d);   % Lower bounds
Ub=5*ones(1,d);    % Upper bounds
% Initialize the population/solutions
for i=1:n,
  Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
  Fitness(i)=Fun(Sol(i,:));
end
% Find the best solution of the initial population
[fmin,I]=min(Fitness);
best=Sol(I,:);

% Start the iterations -- the Bat Algorithm (BA) -- main loop
while (t<t_max)
   % Varying loundness (A) and pulse emission rate (r)
   r=r0*(1-exp(-gamma*t));
   A=alpha*A;
   % Loop over all bats/solutions
   for i=1:n,
       Freq(i)=Freq_min+(Freq_max-Freq_min)*rand;
       v(i,:)=v(i,:)+(Sol(i,:)-best)*Freq(i);
       S(i,:)=Sol(i,:)+v(i,:);
   % Check a switching condition
   if rand<r,
       S(i,:)=best+0.1*randn(1,d)*A;
   end

   % Check if the new solution is within the simple bounds
   S(i,:)=simplebounds(S(i,:),Lb,Ub);
   % Evaluate new solutions
   Fnew=Fun(S(i,:));
   % If the solution improves or not too loudness
    if ((Fnew<=Fitness(i)) & (rand>A)),
       Sol(i,:)=S(i,:);
       Fitness(i)=Fnew;
    end
   % Update the current best solution
    if Fnew<=fmin,
       best=S(i,:);
       fmin=Fnew;
    end
   end % end of for i
  t=t+1;  % Update iteration counter
  % Display the results every 100 iterations
  if ~mod(t,100),
     disp(strcat('Iteration = ',num2str(t)));    
     best, fmin 
  end
end  % End of the main loop

% Output the best solution
disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);

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

% The cost function or objective function
function z=Fun(x)
    z=sum((x-2).^2);      % Optimal solution fmin=0 at (2,2,...,2)


免責聲明!

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



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