%GA-BP Network
clc%清屏幕
clear%清变量
close all%
rng('default')%%固定随机数
%% 第一步 读取数据
feature_index = [1,3,13,14,17];
x=xlsread("data.xlsx");
y=xlsread("10号风机标签.csv");
error_index = find(y~=1);
train_index = [error_index(1),error_index(2),error_index(4),error_index(5),error_index(7),error_index(8)]';
test_index = [error_index(3),error_index(6),error_index(9)]';
y(error_index)
%% 第二步 设置训练数据和预测数据
[m n]=size(x);
input_train =[x(1:20000,feature_index);x(train_index,feature_index)]';
output_train =[y(1:20000,:);y(train_index,:)]';
num = 50;
input_test =[x(20000:20000+num,feature_index);x(error_index,feature_index)]';
output_test =[y(20000:20000+num,:);y(error_index,:)]';
%% 第三步 训练样本数据归一化
[inputn,inputps]=mapminmax(input_train,0,1);%归一化到[-1,1]之间,inputps用来作下一次同样的归一化
outputn=output_train;
inputnum=size(input_train,1);
hiddennum=[30];%隐含层节点
outputnum=1;
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);
net=newff(inputn,outputn,hiddennum,{'tansig','purelin'},'trainlm'); %%{'tansig','purelin'}为默认的激活函数(trainlm为默认的训练算法,Levenberg-Marquart算法)
%% 遗传算法参数初始化
maxgen=30;%进化代数,即迭代次数
sizepop=10;%种群规模
pcross=0.2;%交叉概率选择,0和1之间
pmutation=0.1; %变异概率选择,0和1之间
numsum=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;
lenchrom=ones(1,numsum);
bound=[-3*ones(numsum,1) 3*ones(numsum,1)]; %数据范围
individuals=struct('fitness',zeros(1,sizepop), 'chrom',[]); %将种群信息定义为一个结构体
avgfitness=[];%每一代种群的平均适应度
bestfitness=[];%每一代种群的最佳适应度
bestchrom=[];%适应度最好的染色体
for i=1:sizepop%随机产生一个种群
individuals.chrom(i,:)=Code(lenchrom,bound);%编码
x=individuals.chrom(i,:);%计算适应度
individuals.fitness(i)=fun(x,inputnum,hiddennum,outputnum,net,inputn,outputn);%染色体的适应度
end
[bestfitness bestindex]=min(individuals.fitness);
bestchrom=individuals.chrom(bestindex,:); %最好的染色体
avgfitness=sum(individuals.fitness)/sizepop; %染色体的平均适应度
trace=[avgfitness bestfitness]; % 记录每一代进化中最好的适应度和平均适应度
for num=1:maxgen
% 选择
individuals=select(individuals,sizepop);
avgfitness=sum(individuals.fitness)/sizepop;
%交叉
individuals.chrom=Cross(pcross,lenchrom,individuals,sizepop,bound);
% 变异
individuals.chrom=Mutation(pmutation,lenchrom,individuals,sizepop,num,maxgen,bound);
% 计算适应度
for j=1:sizepop
x=individuals.chrom(j,:); %个体
individuals.fitness(j)=fun(x,inputnum,hiddennum,outputnum,net,inputn,outputn);
%找到最小和最大适应度的染色体及它们在种群中的位置
[newbestfitness,newbestindex]=min(individuals.fitness);
[worestfitness,worestindex]=max(individuals.fitness);
% 代替上一次进化中最好的染色体
if bestfitness>newbestfitness
bestfitness=newbestfitness;
bestchrom=individuals.chrom(newbestindex,:);
end
individuals.chrom(worestindex,:)=bestchrom;
individuals.fitness(worestindex)=bestfitness;
avgfitness=sum(individuals.fitness)/sizepop;
trace=[trace;avgfitness bestfitness]; %记录每一代进化中最好的适应度和平均适应度
end
end
trace= trace(1:end-1,:);
xl=1:sizepop:length(trace);
trace=trace(xl,:);
% figure
% plot(1:maxgen,trace(:,1),'r*-');
% hold on
% plot(1:maxgen,trace(:,2),'bo-');
% title(['适应度曲线 ' '终止代数=' num2str(maxgen)]);
% xlabel('进化代数');ylabel('适应度');
% legend('平均适应度','最佳适应度');
% set(gca,'FontSize',20);
%% 把最优初始阀值权值赋予网络预测
% %用遗传算法优化的BP网络进行值预测
x=bestchrom;
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}=reshape(B2,outputnum,1);
%% BP网络训练
%网络参数
net.trainParam.epochs=10;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00001;
net.divideParam.trainRatio = 70/100;%默认训练集占比 %只画训练曲线
net.divideParam.valRatio = 15/100; %默认验证集占比
net.divideParam.testRatio = 15/100;%默认测试集占比
%网络训练
[net,per2]=train(net,inputn,outputn);
%% GA-BP网络预测
%数据归一化
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);
test_simu=abs(mapminmax('reverse',an,outputps));
test_simu1=round(test_simu);
test_simu1(test_simu1<1)=1;
test_simu1(test_simu1>4)=4;
error=test_simu1-output_test; %预测值和真实值的误差
acc=length(find(test_simu1==output_test))/length(test_simu1);
%% 真实值与预测值误差比较
figure(1)
stem(output_test,'bo')
hold on
stem(test_simu1,'r*')
hold on
legend('实际故障','预测故障')
xlabel('测试样本')
set(gca,'YTick',0:4)
set(gca,'YTickLabel',{'0' '1-正常' '2-变桨控制故障' '3-叶片故障' '4-CT2故障' })
title("GABP-真实值VS预测值");
savefig("1-GABP-真实值VS预测值");
[c,l]=size(output_test);
MAE1=sum(abs(error))/l;
MSE1=error*error'/l;
RMSE1=MSE1^(1/2);
disp(['-----------------------误差计算--------------------------'])
disp(['隐含层节点数为',num2str(hiddennum),'时的误差结果如下:'])
disp(['平均绝对误差MAE为:',num2str(MAE1)])
disp(['均方误差MSE为: ',num2str(MSE1)])
disp(['均方根误差RMSE为: ',num2str(RMSE1)])
disp(['分类正确率为/%: ',num2str(acc*100)])
figure(2)
label = {'1-正常' '2-变桨控制故障' '3-叶片故障' '4-CT2故障'};
mat = cfmatrix(output_test,test_simu1);
maxcolor = [191,54,12]; % 最大值颜色
mincolor = [255,255,255]; % 最小值颜色
% 绘制坐标轴
m = length(mat);
imagesc(1:m,1:m,mat)
xticks(1:m)
xlabel('预测类别','fontsize',10.5)
xticklabels(label)
yticks(1:m)
ylabel('实际类别','fontsize',10.5)
yticklabels(label)
% 构造渐变色
mymap = [linspace(mincolor(1)/255,maxcolor(1)/255,64)',...
linspace(mincolor(2)/255,maxcolor(2)/255,64)',...
linspace(mincolor(3)/255,maxcolor(3)/255,64)'];
colormap(mymap)
colorbar()
% 色块填充数字
for i = 1:m
for j = 1:m
text(i,j,num2str(mat(j,i)),...
'horizontalAlignment','center',...
'verticalAlignment','middle',...
'fontname','Times New Roman',...
'fontsize',10);
end
end
% 图像坐标轴等宽
ax = gca;
ax.FontName = 'SimHei';
set(gca,'box','on','xlim',[0.5,m+0.5],'ylim',[0.5,m+0.5]);
set(0,'defaultTextFontName', 'TimesSimSun'); %文字
axis square
savefig("3-GABP-混淆矩阵")