%function BAS()
%bas:beetle antenna searching for global minimum天牛须算法搜索全局最小值
clear
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%parameter setup参数设置
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%antenna distance天牛须间的距离
d0=0.001;
d1=3;
d=d1;
eta_d=0.95;
%random walk随机步进
l0=0.0;
l1=0.0;
l=l1;
eta_l=0.95;
%steps
step=0.8;%step length步长
eta_step=0.95;
n=100;%iterations迭代次数
k=2;%space dimension空间维数
x0=2*rands(k,1);
x=x0;
xbest=x0;
fbest=fun(xbest);
fbest_store=fbest;
x_store=[0;x;fbest];
display([‘0:’,’xbest=[‘,num2str(xbest(1)),num2str(xbest(2)),’],fbest=’,num2str(fbest)])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%iteration迭代
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:n
dir=rands(k,1);
dir=dir/(eps+norm(dir));
xleft=x+dir*d;
fleft=fun(xleft);
xright=x-dir*d;
fright=fun(xright);
w=l*rands(k,1);
x=x-step*dir*sign(fleft-fright)+w;
f=fun(x);
%%%%%%%%%%%
if f
xbest=x;
fbest=f;
end
%————混沌———————————
xlhd = xbest(1:k);
for t=1:n %次数
%1生成
cxl=rand(1,k);
for j=1:k
if cxl(j)==0
cxl(j)=0.1;
end
if cxl(j)==0.25
cxl(j)=0.26;
end
if cxl(j)==0.5
cxl(j)=0.51;
end
if cxl(j)==0.75
cxl(j)=0.76;
end
if cxl(j)==1
cxl(j)=0.9;
end
end
%2映射
al=-4;bl=4;
rxl=al+(bl-al)*cxl;
%3搜索
bate = 0.3;
xlhd=xlhd+(bate*rxl)’;
if fun(xlhd)
xbest(1:k)=xlhd; %更新全局最优值
xbest(end)=fun(xlhd);
end
%4更新
for j=1:k
cxl(j)=4*cxl(j)*(1-cxl(j)); %logic混沌方程
end
end
%————-混沌——————————–
%当前代的最优粒子的适应度(取自最后一列)保存
fbest_store = xbest(end);
end
%%%%%%%%%%%
x_store=cat(2,x_store,[i;x;f]);
fbest_store=[fbest_store;fbest];
display([num2str(i),’:xbest=[‘,num2str(xbest(1)),num2str(xbest(2)),’],fbest=’,num2str(fbest)])
%%%%%%%%%%%
d=d*eta_d+d0;
l=l*eta_l+l0;
step=step*eta_step;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%data visualization数据可视化
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1),clf(1),
plot(x_store(2,:),x_store(3,:),’r-.’),axis equal
xlim0=[min(x_store(2,:)),max(x_store(2,:))];
ylim0=[min(x_store(3,:)),max(x_store(3,:))];
[x,y]=meshgrid(xlim0(1):(xlim0(2)-xlim0(1))/50:xlim0(2),ylim0(1):(ylim0(2)-ylim0(1))/50:ylim0(2));
f_val=x;
[s1,s2]=size(x);
for i=1:s1
for j=1:s2
f_val(i,j)=fun([x(i,j),y(i,j)]);
end
end
hold on,contour(x,y,f_val,50);
colorbar;
xlim([xlim0(1),xlim0(2)]);
ylim([ylim0(1),ylim0(2)]);
hold on, plot(x_store(2,end),x_store(3,end),’b*’)
hold on, plot(xbest(1),xbest(2),’r*’)
figure(3),clf(3),
plot(x_store(1,:),x_store(4,:),’r-o’)
hold on,
plot(x_store(1,:),fbest_store,’b-.’)
xlabel(‘iteration’)
ylabel(‘minimum value’)
function yout=fun(x)
theta=x;
x=theta(1);
y=theta(2);
% Michalewicz function函数
yout=-sin(x).*(sin(x.^2/pi)).^20-sin(y).*(sin(2*y.^2/pi)).^20;
end