function [best,fmin,N_iter]=bat_algorithm()
n=25; % Population size, typically 10 to 40 蝙蝠个体数
N_gen=20; % Number of generations 迭代次数
% This frequency range determines the scalings. You should change these values if necessary
Qmin=0; % Frequency minimum
Qmax=1; % Frequency maximum
% Iteration parameters 迭代参数
N_iter=0; % Total number of function evaluations 功能评价总数
% Dimension of the search variables 搜索维数
d=2; % Number of dimensions
A=1+rand(n,1); % Loudness (constant or decreasing)响度,产生[1,2]的随机数
r=rand(n,1); % Pulse rate (constant or decreasing)脉冲率,产生[0,1]的随机数
Af = 0.9;
Rf = 0.9;
r0 = r;
% Lower limit/bounds/ a vector
Lb=-2.048*ones(1,d);
% Upper limit/bounds/ a vector
Ub=2.048*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) = Af*A(i,1); %对响度进行更新
r(i,1) = r0(i,1)*(1-exp(-1*Rf*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)
% Rosenbrock函数
% 二维情况下,在点(1,1)处有全局最优解 .
y=(1-x(1)^2)^2+100*(x(2)-x(1)^2)^2;
plot(x(1),x(2),'.');
hold on;
%%%%% ============ end ====================================
bat_algorithm_batalgorithm_蝙蝠算法_源码
版权申诉
124 浏览量
2021-10-01
05:19:37
上传
评论
收藏 2KB ZIP 举报
食肉库玛
- 粉丝: 57
- 资源: 4740
最新资源
- Python 程序语言设计模式思路-行为型模式:策略模式:将算法封装成独立的类,并使它们可以互相替换及支付模式数据压缩
- main.py
- Last Loaded Test.DBK
- Screenshot_20240520_163011.jpg
- ubuntu-python3-whisper-tornado docker镜像 Dockerfile
- ubuntu-python3-whisper-tornado docker镜像07
- 新录音 8.m4a
- ubuntu-python3-whisper-tornado docker镜像
- ubuntu-python3-whisper-tornado docker镜像
- ubuntu-python3-whisper-tornado docker镜像09
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈