【MATLAB第59期】基于MATLAB的混沌退火粒子群CSAPSO-BP、SAPSO-BP、PSO-BP优化BP神经网络非线性函数拟合预测/回归预测对比
注意事项
不同版本matlab 不同电脑 加上数据集随机,BP权值阈值随机,进化算法种群随机,所以运行结果不一定和我运行的一致 。其次, 也会存在CSAPSO 比SAPSO / PSO差的情况。
一、效果展示
二、代码展示
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行%% 导入数据
res = xlsread('数据集.xlsx');%% 划分训练集和测试集
%temp =1:size(res,1);
temp =randperm(size(res,1));
save temp temp
P_train = res(temp(1: 80), 1: 7)';
T_train = res(temp(1: 80), 8)';
MM = size(P_train, 2);P_test = res(temp(81: end), 1: 7)';
T_test = res(temp(81: end), 8)';
NN = size(P_test, 2);%% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input);[t_train, ps_output] = mapminmax(T_train, 0, 1);
t_test = mapminmax('apply', T_test, ps_output);%节点个数
inputnum=size(p_train,1); % 输入层神经元个数
outputnum=size(t_train,1); % 输出层神经元个数
hiddennum=10;
% 创建网络;
net1 = newff(p_train,t_train,hiddennum);
net2 = newff(p_train,t_train,hiddennum);
net3 = newff(p_train,t_train,hiddennum);
%节点总数 2*5 + 5 + 5 + 1 = 21
numsum=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;%% 粒子群算法求权值和阈值
%粒子群算法参数设置
N = 20;
c1 = 2;
c2 = 2;
w = 0.6;
M = 100;
D = numsum;
x = zeros(1,D);%% 把最优初始阀值权值赋予网络预测
% 用粒子群算法优化的BP网络进行值预测
w1_1=xm1(1:inputnum*hiddennum);
B1_1=xm1(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2_1=xm1(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2_1=xm1(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);net1.iw{1,1}=reshape(w1_1,hiddennum,inputnum);
net1.lw{2,1}=reshape(w2_1,outputnum,hiddennum);
net1.b{1}=reshape(B1_1,hiddennum,1);
net1.b{2}=reshape(B2_1,outputnum,1);% % 用模拟退火粒子群算法优化的BP网络进行值预测
w1_2=xm2(1:inputnum*hiddennum);
B1_2=xm2(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2_2=xm2(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2_2=xm2(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);net2.iw{1,1}=reshape(w1_2,hiddennum,inputnum);
net2.lw{2,1}=reshape(w2_2,outputnum,hiddennum);
net2.b{1}=reshape(B1_2,hiddennum,1);
net2.b{2}=reshape(B2_2,outputnum,1);% 用混沌模拟退火粒子群算法优化的BP网络进行值预测
w1_3=xm3(1:inputnum*hiddennum);
B1_3=xm3(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2_3=xm3(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2_3=xm3(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);net3.iw{1,1}=reshape(w1_3,hiddennum,inputnum);
net3.lw{2,1}=reshape(w2_3,outputnum,hiddennum);
net3.b{1}=reshape(B1_3,hiddennum,1);
net3.b{2}=reshape(B2_3,outputnum,1);%% BP网络训练
%粒子群网络进化参数
net1.trainParam.epochs=100;
net1.trainParam.lr = 0.1;
net1.trainParam.goal=1e-3; % 训练目标误差
%
%模拟退火粒子群网络进化参数
net2.trainParam.epochs=100;
net2.trainParam.lr=0.1;
net2.trainParam.goal=1e-6;%混沌模拟退火粒子群网络进化参数
net3.trainParam.epochs=100;
net3.trainParam.lr=0.1;
net3.trainParam.goal=1e-3;% 训练网络
net1 = train(net1,p_train,t_train); % 粒子群
net2 = train(net2,p_train,t_train); % 模拟退火粒子群
net3 = train(net3,p_train,t_train); % 混沌模拟退火粒子群%% 仿真测试
%% 训练集
test_sim11 = sim(net1,p_train); % 粒子群
test_sim22 = sim(net2,p_train); % 模拟退火粒子群
test_sim33 = sim(net3,p_train); % 混沌模拟退火粒子群% 输出数据反归一化,Test_sim为测试数据通过神经网络的预测输出值
Test_sim11 = mapminmax('reverse',test_sim11,ps_output); % 粒子群
Test_sim22 = mapminmax('reverse',test_sim22,ps_output); % 模拟退火粒子群
Test_sim33 = mapminmax('reverse',test_sim33,ps_output); % 混沌模拟退火粒子群
%% 测试集
test_sim1 = sim(net1,p_test); % 粒子群
test_sim2 = sim(net2,p_test); % 模拟退火粒子群
test_sim3 = sim(net3,p_test); % 混沌模拟退火粒子群% 输出数据反归一化,Test_sim为测试数据通过神经网络的预测输出值
Test_sim1 = mapminmax('reverse',test_sim1,ps_output); % 粒子群
Test_sim2 = mapminmax('reverse',test_sim2,ps_output); % 模拟退火粒子群
Test_sim3 = mapminmax('reverse',test_sim3,ps_output); % 混沌模拟退火粒子群%% 算法结果分析
%% 均方根误差
%MM=size(T_train,2);
%NN=size(T_test,2);
error11 = sqrt(sum((Test_sim11 - T_train).^2) ./ MM);
error22 = sqrt(sum((Test_sim22 - T_train).^2) ./ MM);
error33 = sqrt(sum((Test_sim33 - T_train).^2) ./ MM);
error1 = sqrt(sum((Test_sim1 - T_test ).^2) ./ NN);
error2 = sqrt(sum((Test_sim2 - T_test ).^2) ./ NN);
error3 = sqrt(sum((Test_sim3 - T_test ).^2) ./ NN);
%% 查看网络结构
%analyzeNetwork(net)%% 相关指标计算
% R2
disp(['PSO-BP训练集数据的RMSE为:', num2str(error11)])
disp(['SAPSO-BP训练集数据的RMSE为:', num2str(error22)])
disp(['CSAPSO-BP训练集数据的RMSE为:', num2str(error33)])
disp(['PSO-BP测试集数据的RMSE为:', num2str(error1)])
disp(['SAPSO-BP测试集数据的RMSE为:', num2str(error2)])
disp(['CSAPSO-BP测试集数据的RMSE为:', num2str(error3)])R11 = 1 - norm(T_train - Test_sim11)^2 / norm(T_train - mean(T_train))^2;
R22 = 1 - norm(T_train - Test_sim22)^2 / norm(T_train - mean(T_train))^2;
R33 = 1 - norm(T_train - Test_sim33)^2 / norm(T_train - mean(T_train))^2;
R1 = 1 - norm(T_test - Test_sim1)^2 / norm(T_test - mean(T_test ))^2;
R2 = 1 - norm(T_test - Test_sim2)^2 / norm(T_test - mean(T_test ))^2;
R3 = 1 - norm(T_test - Test_sim3)^2 / norm(T_test - mean(T_test ))^2;
disp(['PSO-BP训练集数据的R2为:', num2str(R11)])
disp(['SAPSO-BP训练集数据的R2为:', num2str(R22)])
disp(['CSAPSO-BP训练集数据的R2为:', num2str(R33)])
disp(['PSO-BP测试集数据的R2为:', num2str(R1)])
disp(['SAPSO-BP测试集数据的R2为:', num2str(R2)])
disp(['CSAPSO-BP测试集数据的R2为:', num2str(R3)])% MAE
mae11 = sum(abs(Test_sim11 - T_train)) ./ MM ;
mae22 = sum(abs(Test_sim22 - T_train)) ./ MM ;
mae33 = sum(abs(Test_sim33 - T_train)) ./ MM ;
mae1 = sum(abs(Test_sim1 - T_test )) ./ NN ;
mae2 = sum(abs(Test_sim2 - T_test )) ./ NN ;
mae3 = sum(abs(Test_sim3 - T_test )) ./ NN ;disp(['PSO-BP训练集数据的MAE为:', num2str(mae11)])
disp(['SAPSO-BP训练集数据的MAE为:', num2str(mae22)])
disp(['CSAPSO-BP训练集数据的MAE为:', num2str(mae33)])
disp(['PSO-BP测试集数据的MAE为:', num2str(mae1)])
disp(['SAPSO-BP测试集数据的MAE为:', num2str(mae2)])
disp(['CSAPSO-BP测试集数据的MAE为:', num2str(mae3)])% MAPE mape = mean(abs((YReal - YPred)./YReal));mape11 = mean(abs((T_train - Test_sim11)./T_train));
mape22 = mean(abs((T_train - Test_sim22)./T_train));
mape33 = mean(abs((T_train - Test_sim33)./T_train));
mape1 = mean(abs((T_test - Test_sim1 )./T_test));
mape2 = mean(abs((T_test - Test_sim2 )./T_test));
mape3 = mean(abs((T_test - Test_sim3)./T_test)); disp(['PSO-BP训练集数据的MAPE为:', num2str(mape11)])
disp(['SAPSO-BP训练集数据的MAPE为:', num2str(mape22)])
disp(['CSAPSO-BP训练集数据的MAPE为:', num2str(mape33)])
disp(['PSO-BP测试集数据的MAPE为:', num2str(mape1)])
disp(['SAPSO-BP测试集数据的MAPE为:', num2str(mape2)])
disp(['CSAPSO-BP测试集数据的MAPE为:', num2str(mape3)])save resultfigure()
t = 1:M;
plot(t,Pbest1,'b',t,Pbest2,'g',t,Pbest3,'r');
title('算法收敛过程');
xlabel('进化代数');
ylabel('最小均方误差值(MSE值)');
legend('基本粒子群算法','模拟退火粒子群算法','混沌模拟退火粒子群算法');%% 绘图
%[0.00,0.45,0.74] //蓝
%[0.85,0.33,0.10] //橙红
%[0.93,0.69,0.13] //橙黄
%[0.72,0.27,1] //淡紫
%[0.47,0.67,0.19] //淡绿
figure()plot( 1: MM, T_train, 'k-*', 'LineWidth', 1.5)
hold on
plot( 1: MM, Test_sim11, 'Color', [0.93,0.69,0.13],'LineWidth', 1.5)
hold on
plot( 1: MM, Test_sim22, 'Color',[0.85,0.33,0.10], 'LineWidth', 1.5)
hold on
plot( 1: MM, Test_sim33,'Color',[0.00,0.45,0.74], 'LineWidth', 1.5)
legend('真实值', 'PSO-BP预测值', 'SAPSO-BP预测值', 'CSAPSO-BP预测值')
xlabel('训练样本')
ylabel('预测结果')
string = {'训练集预测结果对比'};
title(string)
xlim([1, MM])
gridfigure()
plot( 1: NN, T_test, 'k-*', 'LineWidth', 1.5)
hold on
plot( 1: NN, Test_sim1, 'Color', [0.93,0.69,0.13],'LineWidth', 1.5)
hold on
plot( 1: NN, Test_sim2, 'Color',[0.85,0.33,0.10], 'LineWidth', 1.5)
hold on
plot( 1: NN, Test_sim3,'Color',[0.00,0.45,0.74], 'LineWidth', 1.5)
legend('真实值', 'PSO-BP预测值', 'SAPSO-BP预测值', 'CSAPSO-BP预测值')
xlabel('测试样本')
ylabel('预测结果')
string = {'测试集预测结果对比'};
title(string)
xlim([1, NN])
gridsave result
三、代码获取
获取细则详见主页置顶文章。
私信回复“59期”即可获取下载链接。