|
楼主 |
发表于 2019-10-28 21:44:11
|
显示全部楼层
一个将人工蜂群算法优化的BP神经网络封装为一个函数的代码,具体如下:
- %% 人工蜂群算法优化的BP神经网络
- function [net,w1,B1,w2,B2,per]=...
- ABC_BP_Fun(inputn,outputn, inputnum,hiddennum,outputnum )
- % 初始化网络结构
- nntwarn off
- net=newff(inputn,outputn,hiddennum,{'logsig','tansig'},'trainlm');
- net.trainParam.epochs=50;
- net.trainParam.lr=0.01;
- net.trainParam.goal=1e-6;
- net.trainParam.min_grad = 1e-6;
- net.trainParam.show=50;
- net.trainParam.showWindow=0;
- net.trainParam.max_fail=20;
- %% ABC的算法参数
- popmin = -5; % Lb
- popmax = 5; % Ub
- nvar = inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum; % 待求变量个数
- itermax = 50; % 迭代次数
- sizepop = 20; % 种群>=2
- trail(1:sizepop,1) = 0; % 未找到更优解的迭代次数
- limit = sizepop; % 拖尾最大次数
- %% 初始化种群
- for i=1:sizepop
- pop(i,:) = popmin + (popmax-popmin).*rand(1,nvar); % 实数编码
- [fitness(i), pop(i,:)]= fun_ABC_Improved(pop(i,:),inputnum,hiddennum,outputnum,net,inputn,outputn,inputn,outputn); % 目标函数值
- end
- [bestfitness,index] = min(fitness); % 极小值
- zbest = pop(index,:); % 最佳个体
- fitnesszbest = bestfitness; % 全局最优
- %% 迭代寻优
- for i=1:itermax
- disp(['当前迭代次数:', num2str(i), ' , 总的迭代次数:', num2str(itermax)])
- % 采蜜峰开始工作
- for j=1:sizepop
- % 选择采蜜的个体
- % x2y = randi(nvar); % nvar个未知数
- Disordered = randperm(nvar);
- x2y = Disordered(1:randi(nvar)); % nvar个未知数
- % 选择相连的种群
- neighbour = randi(sizepop);
- while( neighbour==j )
- neighbour = randi(sizepop);
- end
-
- % 种群更新(解)
- tempx = pop(j,:); % 当前的解
- tempx(x2y) = pop(j,x2y) + (pop(j,x2y)-pop(neighbour,x2y))*(rand-0.5)*2;
- % tempx(x2y) = pop(j,x2y) + (pop(j,x2y)-pop(neighbour,x2y))*rand;
-
- % 越界限制
- for ij = 1:nvar
- if tempx(ij)>popmax
- tempx(ij)=popmax;
- end
- if tempx(ij)<popmin
- tempx(ij)=popmin;
- end
- end
-
- % 适应度更新
- [temp_fitness, tempx]= fun_ABC_Improved( tempx, inputnum,hiddennum,outputnum,net,inputn,outputn,inputn,outputn ); % 当前的适应度函数值--目标函数值--最小目标函数值
- % 比较 个体间比较
- if temp_fitness<fitness(j)
- fitness(j) = temp_fitness;
- pop(j,:) = tempx;
- end
- if temp_fitness<bestfitness
- bestfitness = temp_fitness;
- zbest = tempx;
- end
- end
-
- % 观察峰
- % 计算概率
- prob = 0.9*fitness./max(fitness) + 0.1;
- for j=1:sizepop
- if(rand<prob(j))
-
- % 选择采蜜的个体
- % x2y = randi(nvar); % nvar个未知数
- Disordered = randperm(nvar);
- x2y = Disordered(1:randi(nvar)); % nvar个未知数
- % 选择相连的种群
- neighbour = randi(sizepop);
- while( neighbour==j )
- neighbour = randi(sizepop);
- end
-
- % 种群更新(解)
- tempx = pop(j,:); % 当前的解
- tempx(x2y) = pop(j,x2y) + (pop(j,x2y)-pop(neighbour,x2y))*(rand-0.5)*2;
- % tempx(x2y) = pop(j,x2y) + (pop(j,x2y)-pop(neighbour,x2y))*rand;
-
- % 越界限制
- for ij = 1:nvar
- if tempx(ij)>popmax
- tempx(ij)=popmax;
- end
- if tempx(ij)<popmin
- tempx(ij)=popmin;
- end
- end
-
- % 适应度更新
- [temp_fitness, tempx]= fun_ABC_Improved( tempx, inputnum,hiddennum,outputnum,net,inputn,outputn,inputn,outputn ); % 当前的适应度函数值--目标函数值--最小目标函数值
- % 比较 个体间比较
- if temp_fitness<fitness(j)
- fitness(j) = temp_fitness;
- pop(j,:) = tempx;
- end
- if temp_fitness<bestfitness
- bestfitness = temp_fitness;
- zbest = tempx;
- else
- trail(j) = trail(j)+1;
- end
- end
- end
-
- % 侦察峰开始工作
- [maxTrial, index] = max(trail);
- index = index(1);
- if(maxTrial(1)>limit)
- pop(index,:) = popmin + (popmax-popmin).*rand(1,nvar);
- [fitness(index), pop(index,:)]= fun_ABC_Improved( pop(index,:), inputnum,hiddennum,outputnum,net,inputn,outputn,inputn,outputn ); % 适应度函数值--目标函数值--最小目标函数值
- end
-
- fitness_iter(i) = bestfitness;
- end
- % figure,
- % plot(fitness_iter,'ro-','linewidth',2)
- % xlabel('迭代次数'); ylabel('适应度值--验证样本均方根误差')
- % time = toc
- % disp(['最优解 ', num2str(zbest)] )
- %% 将最优个体zbest代入BP神经网络进行训练预测
- [net,w1,B1,w2,B2,per] = netfun(zbest,inputnum,hiddennum,outputnum,net,inputn,outputn);
- % figure(55)
- % semilogy(per.perf,'b.-');
- % xlabel('迭代次数'); ylabel('均方差mse');
- % hold off
- %% BP网络预测
- % 训练样本--预测输出
- % yc_train = sim(net,inputn);
- % error_train = abs(yc_train-outputn);
- % disp(['训练样本均方根误差 = ',num2str(mse(error_train))])
- % train_simu=mapminmax('reverse',yc_train,outputps);
- %
- % % 验证样本--预测输出
- % yc_test = sim(net,inputn_test);
- % error_test = abs(yc_test-outputn_test);
- % disp(['验证样本均方根误差 = ',num2str(mse(error_test))])
- % test_simu=mapminmax('reverse',yc_test,outputps);
- %
- % save ABC_BP_pop.mat pop fitness
- % %% 显示显示
- % figure(1)
- % plot(output_train,'r.-');
- % hold on
- % plot(train_simu,'b.-');
- % legend('训练样本--实际值','训练样本--BP预测值')
- % xlabel('样本数'); ylabel('采暖季热负荷指标W/m2(输出值)');
- % hold off
- %
- % figure(2)
- % plot(error_train,'r.-');
- % xlabel('样本数'); ylabel('采暖季热负荷指标W/m2--误差值');
- % hold off
- %
- % figure(3)
- % plot(output_test,'r.-');
- % hold on
- % plot(test_simu,'b.-');
- % legend('测试样本--实际值','测试样本--BP预测值')
- % xlabel('样本数'); ylabel('采暖季热负荷指标W/m2(输出值)');
- % hold off
- %
- % figure(4)
- % plot(error_test,'r.-');
- % xlabel('样本数'); ylabel('采暖季热负荷指标W/m2--误差值');
- % hold off
复制代码 适应度函数如下:
- function [error, x_iter] = fun_ABC_Improved(x,inputnum,hiddennum,outputnum,net,inputn,outputn,inputn_test,outputn_test)
- % 该函数用来计算适应度值
- % 输入:
- % x input 个体
- % inputnum input 输入层节点数
- % outputnum input 隐含层节点数
- % net input 网络
- % inputn input 训练输入数据
- % outputn input 训练输出数据
- % 输出:
- % error output 个体适应度值
- % 提取 阈值和权值
- 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=newff(inputn,outputn,hiddennum);
- % 网络进化参数
- % net.trainParam.epochs=500;
- % net.trainParam.lr=0.01;
- % net.trainParam.goal=0.0001;
- % net.trainParam.show=100;
- % net.trainParam.showWindow=0;
- % net.trainParam.max_fail=200;
- % 网络权值、阈值赋值
- 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}=reshape(B2,outputnum,1);
- % 网络训练
- net=train(net,inputn,outputn);
- % an=sim(net,inputn);
- % error=sum(sum(abs(an-outputn)));
- % error = mse(abs(an-outputn));
- an=sim(net,inputn_test);
- error = sum(sum(abs(an-outputn_test)));
- x=[];
- w1=[];
- B1=[];
- w2=[];
- B2=[];
- % 反向取值
- w1 = net.iw{1,1};
- B1 = net.b{1};
- w2 = net.lw{2,1};
- B2 = net.b{2};
- w11 = reshape(w1,1, hiddennum*inputnum);
- w22 = reshape(w2,1, outputnum*hiddennum);
- x_iter = [];
- x_iter(1:inputnum*hiddennum) = w11;
- x_iter(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum) = B1;
- x_iter(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum) = w22;
- x_iter(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum) = B2;
复制代码
|
|