global p global t global R % 输入神经元个数,此处是6个 global S1 % 隐层神经元个数,此处是10个 global S2 % 输出神经元个数,此处是4个 global S % 连接权值个数+阈值个数即(6*10+10*4)+(10+4) S1 = 10; p = [0.01 0.01 0.00 0.90 0.05 0.00; 0.00 0.00 0.00 0.40 0.50 0.00; 0.80 0.00 0.10 0.00 0.00 0.00; 0.00 0.20 0.10 0.00 0.00 0.10]'; t = [1.00 0.00 0.00 0.00; 0.00 1.00 0.00 0.00; 0.00 0.00 1.00 0.00; 0.00 0.00 0.00 1.00]'; P_test = [0.05 0 0.9 0.12 0.02 0.02; 0 0 0.9 0.05 0.05 0.05; 0.01 0.02 0.45 0.22 0.04 0.06; 0 0 0.4 0.5 0.1 0; 0 0.1 0 0 0 0]'; net = newff(minmax(p),[S1,4],{'tansig','purelin'},'trainlm'); net.trainParam.show = 10; net.trainParam.epochs = 2000; net.trainParam.goal = 1.0e-3; net.trainParam.lr = 0.1; [net,tr] = train(net,p,t); s_bp = sim(net,P_test) R = size(p,1); S2 = size(t,1); S = R*S1 + S1*S2 + S1 + S2; aa = ones(S,1)*[-1,1]; popu = 50; initPpp = initializega(popu,aa,'gabpEval',[],[1e-6 1]); gen = 100; [x,endPop,bPop,trace] = ga(aa,'gabpEval',[],initPpp,[1e-6 1 1],'maxGenTerm',gen,... 'normGeomSelect',[0.09],['arithXover'],[2],'nonUnifMutation',[2 gen 3]); figure(1) plot(trace(:,1),1./trace(:,3),'r-'); title( 'GA优化BP神经网络,绘制均方误差变化曲线—Jason niu') hold on plot(trace(:,1),1./trace(:,2),'b-'); xlabel('Generation'); ylabel('Sum-Squared Error'); figure(2) plot(trace(:,1),trace(:,3),'r-'); title( 'GA优化BP神经网络,绘制适应度函数变化曲线—Jason niu') hold on plot(trace(:,1),trace(:,2),'b-'); xlabel('Generation'); ylabel('Fittness'); [W1,B1,W2,B2,val] = gadecod(x); net.IW{1,1} = W1; net.LW{2,1} = W2; net.b{1} = B1; net.b{2} = B2; net = train(net,p,t); s_ga = sim(net,P_test)