自己编的函数,大神莫喷
基于GEAT tool Box
GEATbx
function [BestError,BestW1,BestW2,BestB1,BestB2,SampleyFit,...
GlobalError,GlobalW1,GlobalW2,GlobalB1,GlobalB2,GlobalSampleyFit]=GEATNewNetworkfun(Samplex,Sampley,MM)
%%%%%%%%Numberofindividuals=50;%%染色体的数目.............举例设置参数值,这拥有九个自变量
%%%%%%%%Maxgeneration=500;%%%最大的迭代数目...........举例设置参数值
%%%%%%%%IndependentNum=5;%%自变量的个数............举例设置参数值
%%%%%%%%SelectProbability=0.9;%%选择概率..........举例设置参数值
%%%%%%%%RecombinationProbability=0.7;%%重组概率...........举例设置参数值
%%%%%%%%PrecisionofVariables=15;%%每个染色体到底有多长..........举例设置参数值
%%%%%%%%Mutationprobability=0.01; %%变异的概率..........举例设置参数值
%%%%%%%%vlb=[-5 -5 -5 -5 -5];vub=[5 5 5 5 5];函数自变量的上下界......举例参数设置
%%%%W1=0.5*rand(H,M)-0.1;
%%%%W2=0.5*rand(N,H)-0.1;
%%%%B1=0.5*rand(H,1)-0.1;
%%%%B2=0.5*rand(N,1)-0.1;
[M,number]=size(Samplex);
[N,number]=size(Sampley);
HiddenNum=ceil(sqrt(M+N))+3;%%隐藏层的个数
H=HiddenNum;
Numberofindividuals=50;
Maxgeneration=200;
IndependentNum=H*M+N*H+H+N;
SelectProbability=0.9;
RecombinationProbability=0.7;
Mutationprobability=0.01;
PrecisionofVariables=10;
vlb=repmat(-0.5,1,IndependentNum);
vub=repmat(0.5,1,IndependentNum);
ub=[vlb;vub];
trace=zeros(Maxgeneration,2);%遗传算法的性能跟踪
Field=[REP(PrecisionofVariables,[1,IndependentNum]);ub;REP([1;0;1;1],[1,IndependentNum])];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%以上为基础参数的设置
Chorome=CRTBP(Numberofindividuals,PrecisionofVariables*IndependentNum);
%%(Numberofindividuals*(PrecisionofVariables*(H*M+N*H+H+N)))的初始染色体矩阵Chorome
generation=0;
P=BS2RV(Chorome,Field);
ObjV=NewNNWorkfun(P,Samplex,Sampley,MM);
TraceBariables=[];
while generation<Maxgeneration
f=RANKING(ObjV);
FitV=GEATfitfun(f,P);
SelCh=SELECT('SUS',Chorome,FitV,SelectProbability);
SelCh=RECOMBIN('XOVSP',SelCh,RecombinationProbability);
SelCh=MUT(SelCh,Mutationprobability);
P=BS2RV(SelCh,Field);
ObjSelV=NewNNWorkfun(P,Samplex,Sampley,MM);
[Chorome,ObjV]=REINS(Chorome,SelCh,1,1,ObjV,ObjSelV);
P=BS2RV(Chorome,Field);
generation=generation+1;
trace(generation,1)=min(ObjV);
trace(generation,2)=sum(ObjV)/length(ObjV);
[~,Index]=min(ObjV);
TraceBariables=[TraceBariables;P(Index,:)];
end
figure(1)
plot(trace(:,1),'b','linewidth',2)
hold on
plot(trace(:,1),'r*','markersize',10)
plot(trace(:,2),'g','linewidth',2)
plot(trace(:,2),'r*','markersize',10)
legend('Error的变化','Error均值的变化');
hold off
[BestError,index]=min(ObjV);
BestP=P(index,:);
W11=BestP(1:H*M);
BestW1=reshape(W11,M,H)';
W22=BestP(H*M+1:H*M+N*H);
BestW2=reshape(W22,H,N)';
B11=BestP(H*M+N*H+1:H*M+N*H+H);
BestB1=B11';
B22=BestP(H*M+N*H+H+1:H*M+N*H+H+N);
BestB2=B22';
[examplein,pmin,pmax,exampleout,tmin,tmax]=premnmx(Samplex,Sampley);
Hiddenin=BestW1*examplein+repmat(BestB1,1,number);
switch MM(1)
case 1
Hiddenout=logsig(Hiddenin);
case 2
Hiddenout=tansig(Hiddenin);
case 3
Hiddenout=purelin(Hiddenin);
end
Netin=BestW2*Hiddenout+repmat(BestB2,1,number);
switch MM(2)
case 1
Netout=logsig(Netin);
case 2
Netout=tansig(Netin);
case 3
Netout=purelin(Netin);
end
SampleyFit=postmnmx(Netout,tmin,tmax);
[GlobalError,Indexx]=min(trace(:,1));
GlobalP=TraceBariables(Indexx,:);
GW11=GlobalP(1:H*M);
GlobalW1=reshape(GW11,M,H)';
GW22=GlobalP(H*M+1:H*M+N*H);
GlobalW2=reshape(GW22,H,N)';
GB11=GlobalP(H*M+N*H+1:H*M+N*H+H);
GlobalB1=GB11';
GB22=GlobalP(H*M+N*H+H+1:H*M+N*H+H+N);
GlobalB2=GB22';
HiddeninG=GlobalW1*examplein+repmat(GlobalB1,1,number);
switch MM(1)
case 1
HiddenoutG=logsig(HiddeninG);
case 2
HiddenoutG=tansig(HiddeninG);
case 3
HiddenoutG=purelin(HiddeninG);
end
NetinG=GlobalW2*HiddenoutG+repmat(GlobalB2,1,number);
switch MM(2)
case 1
NetoutG=logsig(NetinG);
case 2
NetoutG=tansig(NetinG);
case 3
NetoutG=purelin(NetinG);
end
GlobalSampleyFit=postmnmx(NetoutG,tmin,tmax);
end
function MSError=NewNNWorkfun(P,Samplex,Sampley,MM)
M=size(P,1);
Error=zeros(M,1);
[M,number]=size(Samplex);
[N,number]=size(Sampley);
HiddenNum=ceil(sqrt(M+N))+3;%%隐藏层的个数,IndependentNum=H*M+N*H+H+N;
H=HiddenNum;
for i=1:M
PP=P(i,:);
W11=PP(1:H*M);
W1=reshape(W11,M,H)';
W22=PP(H*M+1:H*M+N*H);
W2=reshape(W22,H,N)';
B11=PP(H*M+N*H+1:H*M+N*H+H);
B1=B11';
B22=PP(H*M+N*H+H+1:H*M+N*H+H+N);
B2=B22';
Errors=NewNNWork(Samplex,Sampley,MM,W1,W2,B1,B2);
Error(i)=Errors;
end
MSError=Error;
end
function [MSError,pmin,pmax,tmin,tmax]=NewNNWork(Samplex,Sampley,MM,W1,W2,B1,B2)
%%%%MM的第一个数是隐藏层的函数选择,MM的第二个数是输出层的函数选择
%%%1、2、3分别代表logsig和tansig、purelin函数
%%%Samplex为M*number的矩阵,Sampley为N*number的矩阵
[~,number1]=size(Samplex);
[N,number2]=size(Sampley);
if (number1~=number2)
error('BP Network is wrong!');
end
number=number1;
%%%%W1=0.5*rand(H,M)-0.1;
%%%%W2=0.5*rand(N,H)-0.1;
%%%%B1=0.5*rand(H,1)-0.1;
%%%%B2=0.5*rand(N,1)-0.1;
[examplein,pmin,pmax,exampleout,tmin,tmax]=premnmx(Samplex,Sampley);
Hiddenin=W1*examplein+repmat(B1,1,number);
switch MM(1)
case 1
Hiddenout=logsig(Hiddenin);
case 2
Hiddenout=tansig(Hiddenin);
case 3
Hiddenout=purelin(Hiddenin);
end
Netin=W2*Hiddenout+repmat(B2,1,number);
switch MM(2)
case 1
Netout=logsig(Netin);
case 2
Netout=tansig(Netin);
case 3
Netout=purelin(Netin);
end
Error=exampleout-Netout;
MSError=sumsqr(Error)/(N*number);
end
function z=GEATfitfun(x,y)
%%%%%%%x是函数值,y是种群的十进制值
N=length(x);
for i=1:length(N)
if abs(sum(y(i,:)-500))>5%%%%%按照y的每一行测度自变量的总和是不是大于500,如果大于500则x(i)=0.001*x(i);
x(i)=0.001*x(i);
end
end
z=x;
end