目录
智能优化算法优化BP神经网络是一个重要的研究领域,旨在通过智能算法提高BP神经网络的性能和效率。以下是对该过程的详细解释:
BP神经网络(Back Propagation Neural Network)是一种基于误差反向传播算法的人工神经网络,由输入层、隐层和输出层组成。它通过前向传播计算输出,然后通过反向传播调整权重和阈值,以最小化输出误差。BP神经网络广泛应用于分类、回归、模式识别等领域。
智能优化算法是一类受到人类智能、生物群体社会性或自然现象规律启发的算法,用于解决复杂的优化问题。这些算法通常具有全局搜索能力,能够避免陷入局部最优解。智能优化算法包括进化类算法(如遗传算法、差分进化算法)、群智能类算法(如蚁群算法、粒子群算法)和物理法则类算法(如模拟退火算法、引力搜索算法)等。
蜣螂优化算法(Dung Beetle Optimizer, DBO)是一种新型的群智能优化算法,该算法在2022年底被提出,并发表在知名SCI期刊《The Journal of Supercomputing》上。其主要受蜣螂的滚球、跳舞、觅食、偷窃和繁殖行为的启发,具有进化能力强、搜索速度快、寻优能力强的特点。以下是对蜣螂优化算法的详细介绍:
蜣螂优化算法由Jiankai Xue和Bo Shen在2022年提出,其灵感来源于蜣螂在自然界中的多种行为习性。这些习性包括滚球导航、跳舞重新定位、觅食、偷窃以及繁殖等,这些行为被巧妙地转化为算法中的优化策略。
在蜣螂优化算法中,每只蜣螂的位置对应一个解,通过模拟蜣螂的多种行为来更新解的位置,从而找到问题的最优解。算法主要包括以下几个部分:
蜣螂优化算法通过模拟蜣螂的多种行为,实现了全局探索和局部开发的平衡,具有收敛速度快和准确率高的特点。该算法已被应用于多个领域的优化问题中,如无人机路径规划、神经网络参数优化等,并取得了良好的效果
神经网络工具箱:
网络图:
回归
预测误差
- % 清空环境
- 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);