|
赠MATLAB代码与有缘人- 遗传算法
- function error = fun(x,inputnum,hiddennum,hiddennum,outputnum,net,inputn,outputn)
- x1 = x(1:inputnum*hiddennum);
- B1 = x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
- w2 = x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
- B2 = x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
- net.iw{1,1}=reshape(w1,hiddennum,inputnum);
- net.lw{2,1}=reshape(w2,outputnum,hiddennum);
- net.b{1}=reshape(B1,hiddennum,1);
- net.b{2}=B2;
- net=newff(inputn,outputn,hiddennum);
- net.trainParam.epochs=20;
- net.trainParam.lr=0.1;
- net.trainParam.goal=0.00001;
- net.trainParam.show=100;
- net.trainParam.showWindow=0;
- net=train(net,inputn,outputn);
- an=sim(net,inputn);
- error=sum(abs(an-outputn));
- 选择操作
- function ret=select(individuals,sizepop)
- fitness1=10./individuals.fitness;
- sumfitness=sum(fitness1);
- sumf=fitmessl./sumfitness;
- index=[];
- for i=1:sizepop
- pick=rand;
- while pick==0
- pick=rand;
- end
- for i=1:sizepop
- pick=pick-sumf(i);
- if pick<0
- index=[index i];
- break;
- end
- end
- end
- individuals.chrom=individuals.chrom(index,:);
- individuals.fitness=individuals.fitness(index);
- ret=individuals;
-
- 交叉操作
- function ret=Cross(pcross,lenchrom,chrom,sizepop,bound)
- for i=1:sizepop
- pick=rand(1,2);
- while prod(pick)==0
- pick=rand(1,2);
- end
- index=ceil(pick.*sizepop);
- pick=rand;
- while pick==0
- pick=rand;
- end
- if pick>pcross
- continue;
- end
- flag=0;
- while flag==0
- pick=rand;
- end
- pos=ceil(pick.*sum(lenchrom));
- pick=rand;
- v1=chrom(index(1),pos);
- v2=chrom(index(2),pos);
- chrom(index(1),pos)=pick*v2+(1-pick)*v1;
- chrom(index(2),pos)=pick*v1+(1-pick)*v2;
- flag1=test(lenchrom,bound,chrom(index(1),:));
- flag2=test(lenchrom,bound,chrom(index(2),:));
- if flag1*flag2==0
- flag=0;
- else flag=1;
- end
- end
- end
- ret=chrom;
-
- 变异操作
- function ret=Mutation(pmutation,lenchrom,chrom,sizepop,num,maxgen,bound)
- for i=1:sizepop
- pick=rand;
- while pick==0
- pick=rand;
- end
- index=ceil(pick*sizepop);
- pick=rand;
- if pick>pmutation
- continue;
- end
- flag=0;
- while flag==0
- pick=rand;
- while pick==0
- pick=rand;
- end
- pos=ceil(pick*sum(lenchrom));
- v=chrom(i,pos);
- v1=v-bound(pos,1);
- v2=bound(pos,2)-v;
- pick=rand;
- fg=(rand*(1-num/maxgen))^2;
- if pick>0.5
- chrom(i,pos)=chrom(i,pos)+(bound(pos,2)-chrom(i,pos))*fg;
- else
- chrom(i,pos)=chrom(i,pos)-(bound(i,pos)-bound(pos,1))*fg;
- end
- flag=test(lenchrom,boun,chrom(i,;));
- end
- end
- ret=chrom;
-
-
-
- 遗传算法主函数
- clc
- clear
- load data input output
- inputnum=2;
- hiddennum=5;
- outputnum=1;
- input_train=input(1:1900,:)';
- input_test=input(1901:2000,:)';
- output_train=output(1:1900)';
- output_test=output(1901:2000)';
- [inputn,inputps]=mapminmax(input_train);
- [outputn,outputps]=mapminmax(output_train);
- net=newff(inputn,outputn,hiddennum);
- maxgen=50;
- sizepop=10;
- pcross=[0.4];
- pmutation=[0.2];
- numsum=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;
- lenchrom=ones(1,numsum);
- bound=[-3*ones(numsum,1)3*ones(numsum,1)];
- individuals=struct('fitness',zeros(1,sizepop),'chrom',[]);
- avgfitness=[];
- bestfitness=[];
- bestchrom=[];
- for i=1:sizepop
- individuals.chrom(i,:)=Code(lenchrom,bound);
- x=individuals.chrom(i,:);
- individuals.fitness(i)=fun(x,inputnum,hiddennum,outputnum,net,inputn,outputn);
- end
- for i=1:maxgen
- individuals=Select(individuals,sizepop);
- individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,i,maxgen,bound);
- for j=1:sizepop
- x=individuals.chrom(j,:);
- individuals.fitness(j)=fun(x,inputnum,hiddennum,outputnum,net,inputn,output n);
- end
- [newbestfitness,newbestindex]=min(individuals.fitness);
- [worestfitness,worestindex]=max(individuals.fitness);
- if bestfitness>newbestfitness
- bestfitness=newbestfitness;
- bestchrom=individuals.chrom(newbestindex,:);
- end
- individuals.chrom(worestindex,:)=bestchrom;
- individuals.fitness(worestindex,:)=bestfitness;
- avgfitness=sum(individuals.fitness)/sizepop;
- trace=[trace;avgfitness bestfitness];
- end
- 遗传算法优化的BP神经网络函数拟合
- x=bestchrom
- x1=x(1:inputnum*hiddennum);
- B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
- w2=x(inputnum*hiddennum+hiddennum+1;inputnum*hiddennum+hiddennum+hiddennum*outputnum);
- B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1;inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
- net.iw{1,1}=reshape(w1,hiddennum,inputnum);
- net.lw{2,1}=reshape(w2,outputnum,hiddennum);
- net.b{1}=reshape(B1,hiddennum,1);
- net.b(2)=B2;
- net.trainParam.epochs=100;
- net.trainParam.lr=0.1;
- [net,per2]=trian(net,inputn,outputn);
- inputn_test=mapminmax('apply',input_test,inputps);
- an=sim(net,inputn_test);
- test_simu=mapminmax('reverse',an,outputps);
复制代码
|
-
-
遗传算法.zip
13.91 KB, 下载次数: 17, 下载积分: 黑币 -5
遗传算法
|