您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 人事档案/员工关系 > 基于模糊神经网络的水质评价代码
该代码为基于模糊神经网络的水质评价代码清空环境变量参数初始化网络训练网络预测嘉陵江实际水质预测清空环境变量clcclear参数初始化xite=0.001;alfa=0.05;%网络节点I=6;%输入节点数M=12;%隐含节点数O=1;%输出节点数%系数初始化p0=0.3*ones(M,1);p0_1=p0;p0_2=p0_1;p1=0.3*ones(M,1);p1_1=p1;p1_2=p1_1;p2=0.3*ones(M,1);p2_1=p2;p2_2=p2_1;p3=0.3*ones(M,1);p3_1=p3;p3_2=p3_1;p4=0.3*ones(M,1);p4_1=p4;p4_2=p4_1;p5=0.3*ones(M,1);p5_1=p5;p5_2=p5_1;p6=0.3*ones(M,1);p6_1=p6;p6_2=p6_1;%参数初始化c=1+rands(M,I);c_1=c;c_2=c_1;b=1+rands(M,I);b_1=b;b_2=b_1;maxgen=100;%进化次数%网络测试数据,并对数据归一化loaddata1input_trainoutput_traininput_testoutput_test%选连样本输入输出数据归一化[inputn,inputps]=mapminmax(input_train);[outputn,outputps]=mapminmax(output_train);[n,m]=size(input_train);网络训练%循环开始,进化网络foriii=1:maxgeniii;fork=1:mx=inputn(:,k);%输出层结算fori=1:Iforj=1:Mu(i,j)=exp(-(x(i)-c(j,i))^2/b(j,i));endend%模糊规则计算fori=1:Mw(i)=u(1,i)*u(2,i)*u(3,i)*u(4,i)*u(5,i)*u(6,i);endaddw=sum(w);fori=1:Myi(i)=p0_1(i)+p1_1(i)*x(1)+p2_1(i)*x(2)+p3_1(i)*x(3)+p4_1(i)*x(4)+p5_1(i)*x(5)+p6_1(i)*x(6);endaddyw=yi*w';%网络预测计算yn(k)=addyw/addw;e(k)=outputn(k)-yn(k);%计算p的变化值d_p=zeros(M,1);d_p=xite*e(k)*w./addw;d_p=d_p';%计算b变化值d_b=0*b_1;fori=1:Mforj=1:Id_b(i,j)=xite*e(k)*(yi(i)*addw-addyw)*(x(j)-c(i,j))^2*w(i)/(b(i,j)^2*addw^2);endend%更新c变化值fori=1:Mforj=1:Id_c(i,j)=xite*e(k)*(yi(i)*addw-addyw)*2*(x(j)-c(i,j))*w(i)/(b(i,j)*addw^2);endendp0=p0_1+d_p+alfa*(p0_1-p0_2);p1=p1_1+d_p*x(1)+alfa*(p1_1-p1_2);p2=p2_1+d_p*x(2)+alfa*(p2_1-p2_2);p3=p3_1+d_p*x(3)+alfa*(p3_1-p3_2);p4=p4_1+d_p*x(4)+alfa*(p4_1-p4_2);p5=p5_1+d_p*x(5)+alfa*(p5_1-p5_2);p6=p6_1+d_p*x(6)+alfa*(p6_1-p6_2);b=b_1+d_b+alfa*(b_1-b_2);c=c_1+d_c+alfa*(c_1-c_2);p0_2=p0_1;p0_1=p0;p1_2=p1_1;p1_1=p1;p2_2=p2_1;p2_1=p2;p3_2=p3_1;p3_1=p3;p4_2=p4_1;p4_1=p4;p5_2=p5_1;p5_1=p5;p6_2=p6_1;p6_1=p6;c_2=c_1;c_1=c;b_2=b_1;b_1=b;endE(iii)=sum(abs(e));endfigure(1);plot(outputn,'r')holdonplot(yn,'b')holdonplot(outputn-yn,'g');legend('实际输出','预测输出','误差','fontsize',12)title('训练数据预测','fontsize',12)xlabel('样本序号','fontsize',12)ylabel('水质等级','fontsize',12)Warning:Ignoringextralegendentries.网络预测%数据归一化inputn_test=mapminmax('apply',input_test,inputps);[n,m]=size(inputn_test)fork=1:mx=inputn_test(:,k);%计算输出中间层fori=1:Iforj=1:Mu(i,j)=exp(-(x(i)-c(j,i))^2/b(j,i));endendfori=1:Mw(i)=u(1,i)*u(2,i)*u(3,i)*u(4,i)*u(5,i)*u(6,i);endaddw=0;fori=1:Maddw=addw+w(i);endfori=1:Myi(i)=p0_1(i)+p1_1(i)*x(1)+p2_1(i)*x(2)+p3_1(i)*x(3)+p4_1(i)*x(4)+p5_1(i)*x(5)+p6_1(i)*x(6);endaddyw=0;fori=1:Maddyw=addyw+yi(i)*w(i);end%计算输出yc(k)=addyw/addw;end%预测结果反归一化test_simu=mapminmax('reverse',yc,outputps);%作图figure(2)plot(output_test,'r')holdonplot(test_simu,'b')holdonplot(test_simu-output_test,'g')legend('实际输出','预测输出','误差','fontsize',12)title('测试数据预测','fontsize',12)xlabel('样本序号','fontsize',12)ylabel('水质等级','fontsize',12)n=6m=50Warning:Ignoringextralegendentries.嘉陵江实际水质预测loaddata2hgscgjhydxg%-----------------------------------红工水厂-----------------------------------zssz=hgsc;%数据归一化inputn_test=mapminmax('apply',zssz,inputps);[n,m]=size(zssz);fork=1:1:mx=inputn_test(:,k);%计算输出中间层fori=1:Iforj=1:Mu(i,j)=exp(-(x(i)-c(j,i))^2/b(j,i));endendfori=1:Mw(i)=u(1,i)*u(2,i)*u(3,i)*u(4,i)*u(5,i)*u(6,i);endaddw=0;fori=1:Maddw=addw+w(i);endfori=1:Myi(i)=p0_1(i)+p1_1(i)*x(1)+p2_1(i)*x(2)+p3_1(i)*x(3)+p4_1(i)*x(4)+p5_1(i)*x(5)+p6_1(i)*x(6);endaddyw=0;fori=1:Maddyw=addyw+yi(i)*w(i);end%计算输出szzb(k)=addyw/addw;endszzbz1=mapminmax('reverse',szzb,outputps);fori=1:mifszzbz1(i)=1.5szpj1(i)=1;elseifszzbz1(i)1.5&&szzbz1(i)=2.5szpj1(i)=2;elseifszzbz1(i)2.5&&szzbz1(i)=3.5szpj1(i)=3;elseifszzbz1(i)3.5&&szzbz1(i)=4.5szpj1(i)=4;elseszpj1(i)=5;endend%%-----------------------------------高家花园-----------------------------------zssz=gjhy;inputn_test=mapminmax('apply',zssz,inputps);[n,m]=size(zssz);fork=1:1:mx=inputn_test(:,k);%计算输出中间层fori=1:Iforj=1:Mu(i,j)=exp(-(x(i)-c(j,i))^2/b(j,i));endendfori=1:Mw(i)=u(1,i)*u(2,i)*u(3,i)*u(4,i)*u(5,i)*u(6,i);endaddw=0;fori=1:Maddw=addw+w(i);endfori=1:Myi(i)=p0_1(i)+p1_1(i)*x(1)+p2_1(i)*x(2)+p3_1(i)*x(3)+p4_1(i)*x(4)+p5_1(i)*x(5)+p6_1(i)*x(6);endaddyw=0;fori=1:Maddyw=addyw+yi(i)*w(i);end%计算输出szzb(k)=addyw/addw;endszzbz2=mapminmax('reverse',szzb,outputps);fori=1:mifszzbz2(i)=1.5szpj2(i)=1;elseifszzbz2(i)1.5&&szzbz2(i)=2.5szpj2(i)=2;elseifszzbz2(i)2.5&&szzbz2(i)=3.5szpj2(i)=3;elseifszzbz2(i)3.5&&szzbz2(i)=4.5szpj2(i)=4;elseszpj2(i)=5;endend%%-----------------------------------大溪沟水厂-----------------------------------zssz=dxg;inputn_test=mapminmax('apply',zssz,inputps);[n,m]=size(zssz);fork=1:1:mx=inputn_test(:,k);%计算输出中间层fori=1:Iforj=1:Mu(i,j)=exp(-(x(i)-c(j,i))^2/b(j,i));endendfori=1:Mw(i)=u(1,i)*u(2,i)*u(3,i)*u(4,i)*u(5,i)*u(6,i);endaddw=0;fori=1:Maddw=addw+w(i);endfori=1:Myi(i)=p0_1(i)+p1_1(i)*x(1)+p2_1(i)*x(2)+p3_1(i)*x(3)+p4_1(i)*x(4)+p5_1(i)*x(5)+p6_1(i)*x(6);endaddyw=0;fori=1:Maddyw=addyw+yi(i)*w(i);end%计算输出szzb(k)=addyw/addw;endszzbz3=mapminmax('reverse',szzb,outputps);fori=1:mifszzbz3(i)=1.5szpj3(i)=1;elseifszzbz3(i)1.5&&szzbz3(i)=2.5szpj3(i)=2;elseifszzbz3(i)2.5&&szzbz3(i)=3.5szpj3(i)=3;elseifszzbz3(i)3.5&&szzbz3(i)=4.5szpj3(i)=4;elseszpj3(i)=5;endendfigure(3)plot(szzbz1,'o-
本文标题:基于模糊神经网络的水质评价代码
链接地址:https://www.777doc.com/doc-6618241 .html