资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,2013-6-24,#,遗传,算法(,GA,)实例讲解,求函数,f(x)=10*sin(5x)+7*cos(4x,),,,x0,10,的,最大,值:,函数最大值,f(x,)=10*sin(5x)+7*cos(4x,),的,最大值,f,1,(x)=-,f(x,),-10*sin(5x)-7*cos(4x),的,最小值,x,fval=fminbnd(-10*sin(5*x)-7*cos(4*x),0,10,),x=1.5708,fval=-17.0000,x=7.8540,fval=-17.0000,f(x)=10*sin(5x)+7*cos(4x),,,x0,10,的最大值为:,x1=1.5708,x2,=7.8540,F,(x1)=f(x2),=17.0000,主函数,Matlab,仿真主程序,将,x,的值用一个,10,位的二值形式表示为二值问题(编码),一个,10,位的二值数提供的分辨率,是,(10-0)/(210-1)0.01,。,群体初始化,将,x,的值用一个,10,位的二值形式表示为二值,问题(编码),,一个,10,位的二值数提供的分辨率是每为,(10-0)/(210-1)0.01,。,计算目标函数,值,function objvalue=calobjvalue(pop,),temp1=decodechrom(pop,1,10,);,x=temp1*10/1023;,objvalue=10*sin(5*x)+7*cos(4*x);,end,计算个体适应值,function fitvalue=calfitvalue(objvalue),global,Cmin;,Cmin=0;,px,py=size(objvalue);,for i=1:px,if objvalue(i)+Cmin0,temp=Cmin+objvalue(i);,else,temp=0.0;,end,fitvalue(i)=temp;,end,fitvalue=fitvalue;,end,选择,function newpop =selection(pop,fitvalue),totalfit=sum(fitvalue,);,fitvalue=fitvalue/totalfit;,fitvalue=cumsum(fitvalue);,px,py=size(pop);,ms=sort(rand(px,1);,fitin=1;,newin=1;,while newin=px,if(ms(newin)fitvalue(fitin),newpop(newin)=pop(fitin);,newin=newin+1;,else,fitin=fitin+1;,end,end,end,交叉,function newpop =crossover(pop,pc),px,py=size(pop);,newpop=ones(size(pop);,for i=1:2:px-1,if(randpc),cpoint=round(rand*py);,newpop(i,:)=pop(i,1:cpoint),pop(i+1,cpoint+1:py);,newpop(i+1,:)=pop(i+1,1:cpoint),pop(i,cpoint+1:py);,else,newpop(i,:)=pop(i);,newpop(i+1,:)=pop(i+1);,end,end,end,变异,function newpop =mutation(pop,pm,),px,py=size(pop);,for i=1:px,if(randpm),mpoint=round(rand*py);,if mpointbestfit,bestfit=fitvalue(i);,end,end,end,
展开阅读全文