二进制蝙蝠算法Matlab源码实现

function [best,fmin,cg_curve]=BBA(n, A, r, d, Max_iter, CostFunction)
%n is the population size, typically 10 to 25
%A is the loudness  (constant or decreasing)
%r is the pulse rate (constant or decreasing)
%d is the dimension of the search variables
%Max_iter is the maximum number of iteration
% This frequency range determines the scalings
Qmin=0;         % Frequency minimum
Qmax=2;         % Frequency maximum
% Iteration parameters
N_iter=0;       % Total number of function evaluations
% Initial arrays
Q=zeros(n,1);   % Frequency
v=zeros(n,d);   % Velocities
Sol=zeros(n,d);
cg_curve=zeros(1,Max_iter);
% Initialize the population/solutions
for i=1:n,
    for j=1:d % For dimension
        if rand<=0.5
            Sol(i,j)=0;
        else
            Sol(i,j)=1;
        end
    end
end

for i=1:n,
    Fitness(i)=CostFunction(Sol(i,:));
end
% Find the current best
[fmin,I]=min(Fitness);
best=Sol(I,:);
% Start the iterations -- Binary Bat Algorithm
while (N_iter<Max_iter)
        % Loop over all bats/solutions
        N_iter=N_iter+1;
        cg_curve(N_iter)=fmin; % 每一次迭代的最小值
        for i=1:n,
            for j=1:d
                Q(i)=Qmin+(Qmin-Qmax)*rand; % Equation 3 in the paper
                v(i,j)=v(i,j)+(Sol(i,j)-best(j))*Q(i); % Equation 1 in the paper
                V_shaped_transfer_function=abs((2/pi)*atan((pi/2)*v(i,j))); % Equation 9 in the paper
                if rand<V_shaped_transfer_function % Equation 10 in the paper
                    Sol(i,j)=~Sol(i,j);
                else
                    Sol(i,j)=Sol(i,j);
                end
                
                if rand>r  % Pulse rate
                      Sol(i,j)=best(j);
                end   
               
            end       
            
           Fnew=CostFunction(Sol(i,:)); % Evaluate new solutions
     
           if (Fnew<=Fitness(i)) && (rand<A)  % If the solution improves or not too loudness
                Sol(i,:)=Sol(i,:);
                Fitness(i)=Fnew;
           end

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

以上为二进制蝙蝠算法matlab源码,实验函数,可参考我上一篇博客即可。欢迎博客下方留言。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dunwuh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值