% -----------------------------------------------------------------------------------------------------------
% Dung Beetle Optimizer: (DBO) (demo)
% Programmed by Jian-kai Xue
% Updated 28 Nov. 2022.
%
% This is a simple demo version only implemented the basic
% idea of the DBO for solving the unconstrained problem.
% The details about DBO are illustratred in the following paper.
% (To cite this article):
% Jiankai Xue & Bo Shen (2022) Dung beetle optimizer: a new meta-heuristic
% algorithm for global optimization. The Journal of Supercomputing, DOI:
% 10.1007/s11227-022-04959-6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% qq群:439115722
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear
%读取数据
load data input output
%节点个数
inputnum=2;
hiddennum=5;
outputnum=1;
%训练数据和预测数据
input_train=input(1:1900,:)';
input_test=input(1901:2000,:)';
output_train=output(1:1900)';
output_test=output(1901:2000)';
%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);
%构建网络
net=newff(inputn,outputn,hiddennum);
% 参数初始化
dim=21;
maxgen=100; % 进化次数
sizepop=30; %种群规模
popmax=5;
popmin=-5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P_percent = 0.2; % The population size of producers accounts for "P_percent" percent of the total population size
pNum = round( sizepop * P_percent ); % The population size of the producers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:sizepop
pop(i,:)=5*rands(1,21);
% V(i,:)=rands(1,21);
fitness(i)=fun(pop(i,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
XX= pop;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pFit = fitness;
[ fMin, bestI ] = min( fitness ); % fMin denotes the global optimum fitness value
bestX = pop( bestI, : ); % bestX denotes the global optimum position corresponding to fMin
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 蜣螂优化算法 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t = 1 : maxgen
[ ans, sortIndex ] = sort( pFit );% Sort.
[fmax,B]=max( pFit );
worse= pop(B,:);
r2=rand(1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : pNum
if(r2<0.9)
r1=rand(1);
a=rand(1,1);
if (a>0.1)
a=1;
else
a=-1;
end
pop( i , : ) = pop( i , :)+0.3*abs(pop(i , : )-worse)+a*0.1*(XX( i , :)); % Equation (1)
else
aaa= randperm(180,1);
if ( aaa==0 ||aaa==90 ||aaa==180 )
pop( i , : ) = pop( i , :);
end
theta= aaa*pi/180;
pop( i , : ) = pop( i , :)+tan(theta).*abs( pop(i , : )-XX( i , :)); % Equation (2)
end
fitness( i )=fun(pop(i ,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
[ fMMin, bestII ] = min( fitness );
bestXX = pop( bestII, : );
R=1-t/maxgen; %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xnew1 = bestXX.*(1-R);
Xnew2 =bestXX.*(1+R); %%% Equation (3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xnew11 = bestX.*(1-R);
Xnew22 =bestX.*(1+R); %%% Equation (5)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = ( pNum + 1 ) :12 % Equation (4)
pop( i, : )=bestXX+((rand(1,dim)).*(pop( i , : )-Xnew1)+(rand(1,dim)).*(pop( i , : )-Xnew2));
fitness( i )=fun(pop(i ,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
for i = 13: 19 % Equation (6)
pop( i, : )=pop( i , : )+((randn(1)).*(pop( i , : )-Xnew11)+((rand(1,dim)).*(pop( i , : )-Xnew22)));
fitness( i )=fun(pop(i ,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
for j = 20 : sizepop % Equation (7)
pop( j,: )=bestX+randn(1,dim).*((abs(( pop(j,: )-bestXX)))+(abs(( pop(j,: )-bestX))))./2;
fitness( j )=fun(pop(j ,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
XX=pop;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : sizepop
if ( fitness( i ) < pFit( i ) )
pFit( i ) = fitness( i );
pop(i,:) = pop(i,:);
end
if( pFit( i ) < fMin )
fMin= pFit( i );
bestX =pop( i, : );
end
end
yy(t)=fMin;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 迭代寻优
x=bestX
%% 结果分析
plot(yy)
title(['适应度曲线 ' '终止代数=' num2str(maxgen)]);
xlabel('进化代数');ylabel('适应度');
%% 把最优初始阀值权值赋予网络预测
% %用蜣螂优化算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2;
%% 训练
%网络进化参数
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00001;
%网络训练
[net,tr]=train(net,inputn,outputn);
%%预测
%数据归一化
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);
test_simu=mapminmax('reverse',an,outputps);
error=test_simu-output_test;
figure(2)
plot(error)
title('仿真预测误差','fontsize',12);
xlabel('仿真次数','fontsize',12);ylabel('误差百分值','fontsize',12);