您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 投融资/租赁 > 投影寻踪-遗传算法MATLAB程序
clcclearcloseallAlpha=0.1;X=load('d:\data.txt');fork=1:21eval(sprintf('symsa%d',k));enda=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21];[n,p]=size(X);x=zeros(n,p);Xjmax=max(X);Xjmin=min(X);fori=1:nx(i,:)=(X(i,:)-Xjmin)./(Xjmax-Xjmin);end%构造投影指标fori=1:nZ(i)=sum(a.*x(i,:));end%计算投影指标函数meanZ=mean(Z);Sa=0;fork=1:nsa=(Z(i)-meanZ).^2;Sa=Sa+sa;Sa=sqrt(Sa/n);endR=Alpha*Sa;Da=0;fork=1:nrik=abs(Z(i)-Z(k));%ifRrikDa=Da+rik;%endendQa=Sa*Da;%选择操作functionret=select(individuals,sizepop)individuals.fitness=1./(individuals.fitness);sumfitness=sum(individuals.fitness);sumf=individuals./fitness;index=[];fori=1:sizepoppick=rand;whilepick==0pick=rand;endforj=1:sizepoppick=pick-sumf(j);ifpick0index=[indexj];break;endendendindivlduals.chrom=indivuduals.chrom(index,:);individuals.fitness=individuals.fitness(index);ret=individuals;%交叉操作functionret=Cross(pcross,lenchrom,chrom,sizepop,bound)fori=1:sizepoppick=rand(1,2);whileprod(pick)==0pick=rand(1,2);endindex=ceil(pick.*sizepop);pick=rand;whilepick==0pick=rand;endifpickpcrosscontinue;endflag=0;whileflag==0pick=rand;whilepick==0pick=rand;endpos=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),:),fcode);flag2=test(lenchrom,bound,chrom(index(2),:),fcode);ifflag1*flag2==0flag=0;elseflag=1;endendendret=chrom;%变异操作functionret=Mutation(pmutation,lenchrom,chrom,sizepoppop,bound)pick=rand;whilepick==0pick=rand;endindex=ceil(pick*sizepop);pick=rand;ifpickpmutationcontinue;endflag=0;whileflag==0pick=rand;whilepick==0pick=rand;endpos=ceil(pick*sum(lenchrom));v=chrom(i,pos);v1=v-bound(pos,1);v2=bound(pos,2)-v;pick=rand;ifpick0.5delta=v2*(1-pick^((1-pop(1)/pop(2))^2));chrom(i,pos)=v+delta;elsedelta=v1*(1-pick^((1-pop(1)/pop(2))^2));chrom(i,pos)=v-delta;endflag=test(lenchrom,bound,chrom(1,:),fcode);endendendret=chrom;endend主函数clcclearmaxgen=200;sizepop=20;pcross=[0.6];pmutation=[0.01];lenchrom=[11111];bound=[00.9*pi;00.9*pi;00.9*pi;00.9*pi;00.9*pi;];individuals=struct('fitness',zeros(1,sizepop),'chrom',[]);avgfitness=[];bestfitness=[];bestchrom=[];fori=1:sizepopindividuals.chrom(1,:)=code(lenchrom,bound);x=individuals.chrom(i,:);individuals,fitness(i)=fun(x);end[bestfitness,bestindex]=max(individuals.fitness);bestchrom=individuals.chrom(bestindex,:);avgfitness=sum(individuals.fitness)/sizepop;trace=[];fori=1:maxgenindividuals=Select(individuals,sizepop);avggitness=sum(individuals.fitness)/sizepop;individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,bound);individuals.chrom=Mutation(pmutation,lenchrom,individuals.chrom,sizepop,[imaxgen],bound);ifmod(i,10)==0individuals.chrom=nonlinear(individuals.chrom,sizepop);endforj=1:sizepopx=individuals.chrom(j,:);individuals.fitness(j)=fun(x);end[newbestfitness,newbestindex]=max(individuals.fitness);ifbestfitnessnewbestfitness;bestfitness=newbestfitness;bestchrom=individuals.chrom(newbestindex,:);endavgfitness=suindividuals.fitness/sizepop;trace=[trace;avgfitnessbestfitness];end
本文标题:投影寻踪-遗传算法MATLAB程序
链接地址:https://www.777doc.com/doc-4138927 .html