数据处理
day=[0.9363 -0.9698 -0.9907 -0.9562 -0.9507 0.9363 -0.9164 0.9045 0.8918;
-0.9358 -0.9751 0.9821 -0.9544 -0.9469 0.9426 0.9182 0.8967 -0.8841;
0.9516 -0.9781 -0.9744 -0.9525 0.9509 0.9368 0.9082 -0.8903 -0.8665;
-0.9480 -0.9795 -0.9796 -0.9507 0.9509 0.9300 -0.9075 -0.8902 -0.8671;
-0.9433 -0.9923 -0.9812 -0.9596 -0.9406 -0.9230 0.9071 -0.8864 -0.8547;
-0.9424 1.0000 -0.9800 -0.9514 0.9349 -0.9089 0.9206 -0.8780 -0.8414;
0.9355 -0.9878 -0.9737 -0.9499 0.9337 0.9084 -0.9072 -0.8745 -0.8332]
dayh=day;
[dayhn,mindayh,maxdayh]=premnmx(dayh);
m=dayhn;
%输入和输出样本
p=m(:,1:8);
t=dayhn(:,2:9);
%测试样本
k=[0.9435 0.9796 -0.9706 -0.9552 -0.9298 -0.9130 -0.9003 0.8708 0.8234;
-0.9358 -0.9751 0.9821 -0.9544 -0.9469 0.9426 0.9182 0.8967 -0.8841;
0.9516 -0.9781 -0.9744 -0.9525 0.9509 0.9368 0.9082 -0.8903 -0.8665;
-0.9480 -0.9795 -0.9796 -0.9507 0.9509 0.9300 -0.9075 -0.8902 -0.8671;
-0.9433 -0.9923 -0.9812 -0.9596 -0.9406 -0.9230 0.9071 -0.8864 -0.8547;
-0.9424 1.0000 -0.9800 -0.9514 0.9349 -0.9089 0.9206 -0.8780 -0.8414;
-0.9496 -0.9778 -0.9693 -0.9536 -0.9352 -0.9111 -0.9076 0.8797 -0.8227]
kn=tramnmx(k,mindayh,maxdayh);
%新建BP网络
S1=25
net=newff(minmax(p),[S1,7,7],{'logsig','logsig','purelin'},'trainlm');
%BP网络参数设置
net.trainParam.show=1;
net.trainParam.epochs=2000;
net.trainParam.goal=1.0e-28;
net.trainParam.max.fail=5;
net.trainParam.lr=0.3;
%BP网络初次训练
net=init(net);
[net,tr,Y,E]=train(net,p,t)
%BP网络初次训练后的预测
s=sim(net,kn);
plot(s,'r+-');
hold on;
plot(kn,'b*-')
%遗传算法对BP网络进行优化主程序
P=p;
T=t;
R=size(P,1);
S2=size(T,1);
S1=25;%隐含层节点数
S=R*S1+Sl*S2+Sl+S2;%遗传算法编码长度(MATLAB提示该处有错误!)
aa=ones(S,1)*[-1,1];
popu=50;%种群规模
initPpp=initializega(popu,aa,'gabpEval');%初始化种群
gen=100;%遗传代数
%调用GAOT工具箱,其中目标函数定义为gabpEval
[x,endPop,bPop,trace]=ga(aa,'gabpEval',[],initPpp,[1 e-6 1 1],'maxGenTerm',gen,...
'normGeomSelect',[0.09],['arithXover'],[2],'nonUnifMutation',[2 gen 3]);
%绘收敛曲线图
figure(1)
plot(trace(:,1),1./trace(:,3),'r-');
hold on
plot(trace(:,1),1./trace(:,2),'b-');
xlabel('Generation');
ylabel('Sum-Squared Error');
figure(2)
plot(trace(:,1),trace(:,3),'r-');
hold on
plot(trace(:,1),trace(:,2),'b-');
xlabel('Generation');
ylabel('Fittness');
评论0