📄 elman.m
字号:
%遗传算法优化神经网络结构及其权系数的程序采用实数编码
clear all
clear all
%遗传算法优化神经网络结构及其权系数的程序采用实数编码
clear all
close all
clc
global maxjiedian
global popsize
global chromlength
global maxpop
global ny
global nx
global hh
[file1,path1]=uigetfile('*.*','打开数据文件');
filename1=strcat(path1,file1);
fid1=fopen(filename1,'r');
prompt={'除Y外的列数','样本总数','取其中用于拟合的样本个数','输入需要预测向量因子'};
def={'10','48','46','0 0 0 0 0 0 0 0 0 0'};
lineNo=1;
dlgTitle='参数输入,遗传算法集成神经网络,吴建生2005/09/10';
answer=inputdlg(prompt,dlgTitle,lineNo,def);
answer=char(answer);
ny=str2num(answer(1,:));
nx=str2num(answer(2,:));
it=str2num(answer(3,:));
wxyq=str2num(answer(4,:));
prompt={'输入节点个数','种群个数','最大进化代数','隐节点下限','隐节点上限','输出节点个数'};
def={'10','50','50','0.5','1.5','1'};
dlgTile='参数输入, 遗传算法集成神经网络的参数';
lineNo=1;
answer=inputdlg(prompt,dlgTile,lineNo,def);
answer=char(answer);
maxjiedian=str2num(answer(1,:));
popsize=str2num(answer(2,:));
maxpop=str2num(answer(3,:));
wyt=str2num(answer(4,:));
lyt=str2num(answer(5,:));
gx=str2num(answer(6,:));
prompt={'连接权下限','连接权上限','交叉概率1','交叉概率2','交叉概率3','变异概率1','变异概率2','变异概率3','变异强度'};
def={'-2.0','2.0','0.6','0.6','0.6','0.05','0.05','0.05','0.01'};
dlgTile='参数输入, 遗传算法优化神经网络权系数参数输入';
lineNo=1;
answer=inputdlg(prompt,dlgTile,lineNo,def);
answer=char(answer);
xyy1=str2num(answer(1,:));
xyy2=str2num(answer(2,:));
pc1=str2num(answer(3,:));
pc2=str2num(answer(4,:));
pc3=str2num(answer(5,:));
pm1=str2num(answer(6,:));
pm2=str2num(answer(7,:));
pm3=str2num(answer(8,:));
pm4=str2num(answer(9,:));
prompt={'最大训练的次数','目标误差','显示间隔次数','学习速率','动量因子'};
def={'200','0.01','100','0.0025','0.005'};
lineNo=1;
dlgTitle='参数输入,神经网络程序吴建生';
answer=inputdlg(prompt,dlgTitle,lineNo,def);
answer=char(answer);
cx=str2num(answer(1,:));
dx=str2num(answer(2,:));
ex=str2num(answer(3,:));
alfa=str2num(answer(4,:));
bieta=str2num(answer(5,:));
[file2,path2]=uiputfile('*.*','请选择要保存结果的文件夹名');
filename2=strcat(path2,file2);
fid2=fopen(filename2,'w');
AA=fscanf(fid1,'%f');
BB=reshape(AA,ny+gx,nx)';
[px,py]=size(BB);
fprintf(fid2,'****############*********遗传算法集成神经网络权系数和网络结构的结果*********#############****\n');
fprintf(fid2,'******#######################原始数据得结果距阵#######################################******\n');
for i=1:nx
for j=1:ny+gx
fprintf(fid2,'%12.5f',BB(i,j));
end
fprintf(fid2,'\n');
end
sw1w=it;
for TGB=1:(nx-sw1w);
BB1=BB(1:it,1:ny);
BB2=BB(1:it,ny+1:ny+gx);
BB3=BB(it+1,1:ny);
BB4=BB(it+1,ny+1:ny+gx);
BB5=wxyq;
Y1=BB2;
Y2=BB4;
Y3=[Y1;Y2];
[pn,minp,maxp,tn,mint,maxt]=premnmx(BB1',BB2');
p1=pn;
t1=tn;
p2=tramnmx(BB3',minp,maxp);
t2=tramnmx(BB4',mint,maxt);
p3=tramnmx(BB5',minp,maxp);
p4=[p1 p2];
t3=Y1';
t4=Y2';
t5=[t1 t2];
generation=1;
%编码的总串长度chromlegntg=stepwise*(maxjiedian+2)
format long
yty=floor(maxjiedian*lyt);
chromlength=(yty*(ny+2)+1);
rand('seed',78341223);
pop=UNIFRND(xyy1,xyy2,popsize,chromlength);
sqa1=(maxjiedian.*lyt);
sqa2=(maxjiedian.*wyt);
rand('seed',78341223);
you=floor((rand(popsize,1)).*(sqa1-sqa2)+sqa2);
maxn=max(you);
chromlength2=(maxn*maxn);
popo=UNIFRND(xyy1,xyy2,popsize,chromlength2);
p1_=zeros(chromlength2,chromlength2);
%解码过程
hh=ny*yty;
for i=1:popsize
n=you(i,1);%隐节点个数
c_2=popo(i,1:n*n);
p1_2=p1_(i,1:n)';
c=pop(i,1:n*ny);%从输入层到隐层的权值
d=pop(i,hh+1:hh+n)';%隐层的阈限
e=pop(i,hh+yty+1:hh+yty+n);%从隐层到输出层的权值
g=pop(i,chromlength);%输出层的阈限
f=reshape(c,n,ny);%从输入层到隐层的权值作成n*ny的矩阵
f_2=reshape(c_2,n,n);
for j=1:it
aatq=f*p1(:,j); %训练样本与从输入层到隐层的权值结合
aatq_2=f_2*p1_2;
fert=aatq+d+aatq_2;
fet=1./(1+exp(-fert));
%记忆==
p1_2=fet+(p1_2.*0.5);%将第m个样本的隐层输出作为连接层的第m+1个样本
%====
rewqt=e*fet;
dmf=rewqt+g;
dmn(i,j)=postmnmx(dmf,mint,maxt);
end
obj(i)=1/(1+mean(abs(dmn(i,:)-t3)));
end
objval=obj';
[maxfit,bestindex]=max(objval);
maxfitting(generation,1)=maxfit;
hyu1=pop(bestindex,:);
hyu1o=popo(bestindex,:);
bestpop(generation,:)=hyu1;
bestpopo(generation,:)=hyu1o;
hyu2=you(bestindex,:)
bestyou(generation,:)=hyu2;
[mifit,worstindex]=min(objval);
mifitting(generation,1)=mifit;
hyu3=pop(worstindex,:);
hyu3o=popo(worstindex,:);
worstpop(generation,:)=hyu3;
worstpopo(generation,:)=hyu3o;
hyu4=you(worstindex,:);
worstyou(generation,:)=hyu4;
fitvalue=objval;
fitvalue1=fitvalue/sum(fitvalue);
cfitness=cumsum(fitvalue1);
currentbest=bestindex;
bar=waitbar(0,'please wait....');
while (generation<maxpop)
waitbar(generation/maxpop,bar);
generation=generation+1;
[px11,py11]=size(cfitness);
rans=sort(rand(px11,1));
fitin=1;
newin=1;
lx=hh;
ly=(hh+yty);
lt=(hh+2*yty);
pop1=pop(:,1:lx);
pop2=pop(:,lx+1:ly);
pop3=pop(:,ly+1:lt);
pop4=pop(:,lt+1);
fitind=1;
newind=1;
%复制最佳个体
while newind<=px11
if(rans(newind))<cfitness(fitind)
newpop1(newind,:)=pop1(fitind,:);
newpop2(newind,:)=pop2(fitind,:);
newpop3(newind,:)=pop3(fitind,:);
newpop4(newind,:)=pop4(fitind,:);
newyou(newind,:)=you(fitind,:);
newpopo(newind,:)=popo(fitind,:);
newind=newind+1;
else
fitind=fitind+1;
end
end
copyw1=newpop1;
copyw2=newpop2;
copyw3=newpop3;
copyw4=newpop4;
copyw5=newpopo;
copyyou=newyou;
[px3,py3]=size(copyw1);
[px4,py4]=size(copyw2);
[px5,py5]=size(copyw3);
[px6,py6]=size(copyw4);
[px7,py7]=size(copyw5);
%rand('seed',78341223);
c=rand(px3,py3);
%交叉种群的第一个
for i=1:2:px3-1
if(rand<pc1)
overw1(i,:)=copyw1(i,:).*c(i,:)+copyw1(i+1,:).*(1-c(i,:));
overw1(i+1,:)=copyw1(i,:).*(1-c(i,:))+copyw1(i+1,:).*c(i,:);
else
overw1(i,:)=copyw1(i,:);
overw1(i+1,:)=copyw1(i+1,:);
end
end
overpop1=overw1;
%rand('seed',78341223);
c1=rand(px4,py4);
%交叉第二个种群
for i=1:2:px4-1
if(rand<pc2)
overw2(i,:)=copyw2(i,:).*c1(i,:)+copyw2(i+1,:).*(1-c1(i,:));
overw2(i+1,:)=copyw2(i,:).*(1-c1(i,:))+copyw2(i+1,:).*c1(i,:);
else
overw2(i,:)=copyw2(i,:);
overw2(i+1,:)=copyw2(i+1,:);
end
end
overpop2=overw2;
%rand('seed',78341223);
c2=rand(px5,py5);
%交叉第三个种群
for i=1:2:px5-1
if(rand<pc2)
overw3(i,:)=copyw3(i,:).*c2(i,:)+copyw3(i+1,:).*(1-c2(i,:));
overw3(i+1,:)=copyw3(i,:).*(1-c2(i,:))+copyw3(i+1,:).*c2(i,:);
else
overw3(i,:)=copyw3(i,:);
overw3(i+1,:)=copyw3(i+1,:);
end
end
overpop3=overw3;
%rand('seed',78341223);
c3=rand(px6,py6);
%交叉第四个种群
for i=1:2:px6-1
if(rand<pc2)
overw4(i,:)=copyw4(i,:).*c3(i,:)+copyw4(i+1,:).*(1-c3(i,:));
overw4(i+1,:)=copyw4(i,:).*(1-c3(i,:))+copyw4(i+1,:).*c3(i,:);
else
overw4(i,:)=copyw4(i,:);
overw4(i+1,:)=copyw4(i+1,:);
end
end
overpop4=overw4;
c5=rand(px7,py7);
%交叉第四个种群
for i=1:2:px7-1
if(rand<pc2)
overw5(i,:)=copyw5(i,:).*c5(i,:)+copyw5(i+1,:).*(1-c5(i,:));
overw5(i+1,:)=copyw5(i,:).*(1-c5(i,:))+copyw5(i+1,:).*c5(i,:);
else
overw5(i,:)=copyw5(i,:);
overw5(i+1,:)=copyw5(i+1,:);
end
end
overpopo=overw5;
[px99,py99]=size(copyyou);
%rand('seed',78341223);
c4=rand(px99,py99);
for i=1:2:px99-1
if(rand<pc3)
overy(i,:)=copyyou(i,:).*c3(i,:)+copyyou(i+1,:).*(1-c3(i,:));
overy(i+1,:)=copyyou(i,:).*(1-c3(i,:))+copyyou(i+1,:).*c3(i,:);
else
overy(i,:)=copyyou(i,:);
overy(i+1,:)=copyyou(i+1,:);
end
end
overyou=floor(overy);
[px7,py7]=size(overpop1);
[px8,py8]=size(overpop2);
[px9,py9]=size(overpop3);
[px10,py10]=size(overpop4);
[px11,py11]=size(overyou);
[px12,py12]=size(overpopo);
%进行变异过程
%rand('seed',78341223);
cc1=rand(px7,py7);
for i=1:px7
if(rand<pm1)
u1=min(overpop1(i,:));
u2=max(overpop1(i,:));
u3=u2-u1;
mutionpop1(i,:)=u3.*cc1(i,:)+u1;
else
mutionpop1(i,:)=overpop1(i,:);
end
end
mutpop1=mutionpop1;
%fitst2 mution
%rand('seed',78341223);
cc2=rand(px8,py8);
for i=1:px8
if(rand<pm2)
u1=min(overpop2(i,:));
u2=max(overpop2(i,:));
u3=u2-u1;
mutionpop2(i,:)=u3.*cc2(i,:)+u1;
else
mutionpop2(i,:)=overpop2(i,:);
end
end
mutpop2=mutionpop2;
%fitst3 mution
%rand('seed',78341223);
cc3=rand(px9,py9);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -