小波神经网络进行短期电力负荷预测的matlab代码
clear;
P=[ 0.2286 0.2090 0.0442 0.3690 0.2603 0.0359 0.0724 0.1759 0.2634;
0.1292 0.0947 0.0880 0.2222 0.1715 0.1149 0.1909 0.2347 0.2258;
0.0720 0.1393 0.1147 0.0562 0.0702 0.1230 0.1340 0.1829 0.1165;
0.1529 0.8700 0.0563 0.5157 0.2711 0.5460 0.2409 0.1811 0.1124;
0.1335 0.2558 0.3347 0.1872 0.1491 0.1977 0.2842 0.2922 0.1074;
0.0733 0.0900 0.1100 0.1614 0.1330 0.1248 0.0450 0.0655 0.0657;
0.1159 0.0771 0.1453 0.1425 0.0968 0.0624 0.0824 0.0774 0.0610;
0.0940 0.0882 0.0429 0.1506 0.1911 0.0832 0.1064 0.2237 0.2623;
0.0522 0.0393 0.1818 0.1310 0.2545 0.1640 0.1909 0.2056 0.2588;
0.1345 0.1430 0.0378 0.0500 0.0871 0.1002 0.1586 0.0925 0.1155;
0.0090 0.0126 0.0092 0.0078 0.0060 0.0059 0.0116 0.0078 0.0050;
0.1260 0.1670 0.2251 0.0348 0.1793 0.1503 0.1698 0.1852 0.0978;
0.3619 0.2450 0.1560 0.0451 0.1001 0.1837 0.3644 0.3501 0.1511;
0.0690 0.0508 0.0852 0.0707 0.0789 0.1295 0.2718 0.1680 0.2273;
0.1828 0.1328 0.0670 0.0880 0.0909 0.0700 0.2494 0.2668 0.3220];
T=[1 1 1 0 0 0 0 0 0;0 0 0 1 1 1 0 0 0;0 0 0 0 0 0 1 1 1];
threshold=[0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1];
net=newff(threshold,[15,3],{'morlet','logsig'},'traingda');
net.trainParam.epochs=500;
net.trainParam.goal=0.001;
LP.Ir=0.01;
s=15;r=15;
[r,q] = size(P);
pmin = min(P')';
pmax = max(P')'
magw =0.1*s^(1/r);
w = magw*randnr(s,r);
b = magw*rands(s,1);
b1=rands(s,1);
rng = pmax-pmin;
mid = 0.5*(pmin+pmax);
w=2*w./(ones(s,1)*rng');
b = b - w*mid;
a=w*rng/2.45;
t=w*mid;
net.IW{1,1}=w;
net.b{1,1}=b;
net.LW{2,1}=0.01*randnr(3,s);
net.b{2,1}=0.1*randnc(3,1);
net=train(net,P,T);
w1=net.IW{1,1};
b1=net.b{1,1};
w2=net.LW{2,1};
b2=net.b{2,1};
%保存网络训练结果
fww1=fopen('w11.dat','w');
fwb1=fopen('b11.dat','w');
fww2=fopen('w22.dat','w');
fwb2=fopen('b22.dat','w');
fprintf(fww1,'%6.4f %6.4f\n',w1);
fprintf(fwb1,'%6.4f %6.4f\n',b1);
fprintf(fww2,'%6.4f %6.4f\n',w2);
fprintf(fwb2,'%6.4f %6.4f\n',b2);
fclose(fww1);
fclose(fwb1);
fclose(fww2);
fclose(fwb2);
g=[ 0.2101 0.2593 0.2599;0.095 0.18 0.2235;0.1298 0.0711 0.1201;0.1359 0.2801 0.1171;
0.2601 0.1501 0.1102;0.1001 0.1298 0.0683;0.0753 0.1001 0.0621;0.089 0.1891 0.2597;
0.0389 0.2531 0.2602;0.1451 0.0875 0.1167;0.0128 0.0058 0.0048;0.159 0.1803 0.1002;
0.2452 0.0992 0.1521;0.0512 0.0802 0.2281;0.1319 0.1002 0.3205];
y=sim(net,g)
====================================================
问题来源:http://chinavib.com/forum/thread-48742-1-1.html
程序一:GA训练BP权值的主函数 function net=GABPNET(XX,YY) %-------------------------------------------------------------------------- % GABPNET.m % 使用遗传算法对BP网络权值阈值进行优化,再用BP算法训练网络 %-------------------------------------------------------------------------- %数据归一化预处理 nntwarn off XX=premnmx(XX); YY=premnmx(YY); %创建网络 net=newff(minmax(XX),[19,25,1],{'tansig','tansig','purelin'},'trainlm'); %下面使用遗传算法对网络进行优化 P=XX; T=YY; R=size(P,1); S2=size(T,1); S1=25;%隐含层节点数 S=R*S1+S1*S2+S1+S2;%遗传算法编码长度 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,[1e-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'); %下面将初步得到的权值矩阵赋给尚未开始训练的BP网络 [W1,B1,W2,B2,P,T,A1,A2,SE,val]=gadecod(x); net.LW{2,1}=W1; net.LW{3,2}=W2; net.b{2,1}=B1; net.b{3,1}=B2; XX=P; YY=T; %设置训练参数 net.trainParam.show=1; net.trainParam.lr=1; net.trainParam.epochs=50; net.trainParam.goal=0.001; %训练网络 net=train(net,XX,YY); 程序二:适应值函数 function [sol, val] = gabpEval(sol,options) % val - the fittness of this individual % sol - the individual, returned to allow for Lamarckian evolution % options - [current_generation] load data2 nntwarn off XX=premnmx(XX); YY=premnmx(YY); P=XX; T=YY; R=size(P,1); S2=size(T,1); S1=25;%隐含层节点数 S=R*S1+S1*S2+S1+S2;%遗传算法编码长度 for i=1:S, x(i)=sol(i); end; [W1, B1, W2, B2, P, T, A1, A2, SE, val]=gadecod(x); 程序三:编解码函数 function [W1, B1, W2, B2, P, T, A1, A2, SE, val]=gadecod(x) load data2 nntwarn off XX=premnmx(XX); YY=premnmx(YY); P=XX; T=YY; R=size(P,1); S2=size(T,1); S1=25;%隐含层节点数 S=R*S1+S1*S2+S1+S2;%遗传算法编码长度 % 前R*S1个编码为W1 for i=1:S1, for k=1:R, W1(i,k)=x(R*(i-1)+k); end end % 接着的S1*S2个编码(即第R*S1个后的编码)为W2 for i=1:S2, for k=1:S1, W2(i,k)=x(S1*(i-1)+k+R*S1); end end % 接着的S1个编码(即第R*S1+S1*S2个后的编码)为B1 for i=1:S1, B1(i,1)=x((R*S1+S1*S2)+i); end % 接着的S2个编码(即第R*S1+S1*S2+S1个后的编码)为B2 for i=1:S2, B2(i,1)=x((R*S1+S1*S2+S1)+i); end % 计算S1与S2层的输出 A1=tansig(W1*P,B1); A2=purelin(W2*A1,B2); % 计算误差平方和 SE=sumsqr(T-A2); val=1/SE; % 遗传算法的适应值 上述程序需要调用gaot工具箱 =============== initializega 函数的代码: function [pop] = initializega(num, bounds, evalFN,evalOps,options) % function [pop]=initializega(populationSize, variableBounds,evalFN, % evalOps,options) % initializega creates a matrix of random numbers with % a number of rows equal to the populationSize and a number % columns equal to the number of rows in bounds plus 1 for % the f(x) value which is found by applying the evalFN. % This is used by the ga to create the population if it % is not supplied. % % pop - the initial, evaluated, random population % populatoinSize - the size of the population, i.e. the number to create % variableBounds - a matrix which contains the bounds of each variable, i.e. % [var1_high var1_low; var2_high var2_low; ....] % evalFN - the evaluation fn, usually the name of the .m file for % evaluation % evalOps - any options to be passed to the eval function defaults [] % options - options to the initialize function, ie. % [type prec] where eps is the epsilon value % and the second option is 1 for float and 0 for binary, % prec is the precision of the variables defaults [1e-6 1] % Binary and Real-Valued Simulation Evolution for Matlab GAOT V2 % Copyright (C) 1998 C.R. Houck, J.A. Joines, M.G. Kay % % C.R. Houck, J.Joines, and M.Kay. A genetic algorithm for function % optimization: A Matlab implementation. ACM Transactions on Mathmatical % Software, Submitted 1996. % % This program is free software; you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation; either version 1, or (at your option) % any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. A copy of the GNU % General Public License can be obtained from the % Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. if nargin<5 options=[1e-6 1]; end if nargin<4 evalOps=[]; end if any(evalFN<48) %Not a .m file if options(2)==1 %Float GA estr=['x=pop(i,1); pop(i,xZomeLength)=', evalFN ';']; else %Binary GA estr=['x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=', evalFN ';']; end else %A .m file if options(2)==1 %Float GA estr=['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);']; else %Binary GA estr=['x=b2f(pop(i,:),bounds,bits);[x v]=' evalFN ... '(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];']; end end numVars = size(bounds,1); %Number of variables rng = (bounds(:,2)-bounds(:,1))'; %The variable ranges' if options(2)==1 %Float GA xZomeLength = numVars+1; %Length of string is numVar + fit pop = zeros(num,xZomeLength); %Allocate the new population pop(:,1:numVars)=(ones(num,1)*rng).*(rand(num,numVars))+... (ones(num,1)*bounds(:,1)'); else %Binary GA bits=calcbits(bounds,options(1)); xZomeLength = sum(bits)+1; %Length of string is numVar + fit pop = round(rand(num,sum(bits)+1)); end for i=1:num eval(estr); end

您当前的位置: