matlab遗传算法求最大值问题例题完整代码.docx

上传人:小飞机 文档编号:3161308 上传时间:2023-03-11 格式:DOCX 页数:9 大小:38.45KB
返回 下载 相关 举报
matlab遗传算法求最大值问题例题完整代码.docx_第1页
第1页 / 共9页
matlab遗传算法求最大值问题例题完整代码.docx_第2页
第2页 / 共9页
matlab遗传算法求最大值问题例题完整代码.docx_第3页
第3页 / 共9页
matlab遗传算法求最大值问题例题完整代码.docx_第4页
第4页 / 共9页
matlab遗传算法求最大值问题例题完整代码.docx_第5页
第5页 / 共9页
亲,该文档总共9页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《matlab遗传算法求最大值问题例题完整代码.docx》由会员分享,可在线阅读,更多相关《matlab遗传算法求最大值问题例题完整代码.docx(9页珍藏版)》请在三一办公上搜索。

1、matlab遗传算法求最大值问题例题完整代码本文件的目的是减少您打字的烦恼%主程序:用遗传算法求解y=200*exp(-0.05*x).*sin(x)在-2 2区间上的最大值clc;clear all;close all;global BitLengthglobal boundsbeginglobal boundsendbounds=-2 2; %一维自变量的取值范围precision=0.0001; %运算精度boundsbegin=bounds(:,1); boundsend=bounds(:,2);%计算如果满足求解精度至少需要多长的染色体BitLength=ceil(log2(boun

2、dsend-boundsbegin)./precision);popsize=50; %初始种群大小Generationnmax=12; %最大代数pcrossover=0.90; %交配概率pmutation=0.09; %变异概率%产生初始种群population=round(rand(popsize,BitLength);%计算适应度,返回适应度Fitvalue和累积概率cumsumpFitvalue,cumsump=fitnessfun(population);Generation=1;while Generation<Generationnmax+1for j=1:2:popsi

3、ze %选择操作seln=selection(population,cumsump);%交叉操作scro=crossover(population,seln,pcrossover);scnew(j,:)=scro(1,:);scnew(j+1,:)=scro(2,:);%变异操作smnew(j,:)=mutation(scnew(j,:),pmutation);smnew(j+1,:)=mutation(scnew(j+1,:),pmutation);endpopulation=smnew; %产生了新的种群%计算新种群的适应度Fitvalue,cumsump=fitnessfun(popul

4、ation);%记录当前代最好的适应度和平均适应度fmax,nmax=max(Fitvalue);fmean=mean(Fitvalue);ymax(Generation)=fmax;ymean(Generation)=fmean;%记录当前代的最佳染色体个体 x=transform2to10(population(nmax,:);%自变量取值范围是-2 2,需要把经过遗传运算的最佳染色体整合到-2 2区间xx=boundsbegin+x*(boundsend-boundsbegin)/(power(boundsend),BitLength)-1);xmax(Generation)=xx;Ge

5、neration=Generation+1;endGeneration=Generation-1;%显示结果Bestpoplation=xxBesttargetfunvalue=targetfun(xx)%绘制经过遗传运算后的适应度曲线。一般的,如果进化过程中种群的平均适应度与最大适应度在曲线上有相互%趋同的形态,表示算法收敛进行的很顺利,没有出现震荡;在这种前提下,最大适应度个体连续若干代都没有发生进化表明种群已经成熟%作图figure(1);hand1=plot(1:Generation,ymax);set(hand1,linestyle,-,linewidth,1.8,marker,*,

6、markersize,6)hold on;hand2=plot(1:Generation,ymean);set(hand2,color,r,linestyle,-,linewidth,1.8,marker,h,markersize,6)xlabel(进化代数);ylabel(最大/平均适应度);xlim(1 Generationnmax);legend(最大适应度,平均适应度);box off;hold off;%子程序:新种群交叉操作,函数名称存储为crossover.mfunction scro=crossover(population,seln,pc);BitLength=size(po

7、pulation,2);pcc=IfCroIfMut(pc); %根据交叉概率决定是否进行交叉操作,1则是,0则否if pcc=1chb=round(rand*(BitLength-2)+1; %在1,BitLength-1范围内随机产生一个交叉位scro(1,:)=population(seln(1),1:chb) population(seln(2),chb+1:BitLength);scro(2,:)=population(seln(2),1:chb) population(seln(1),chb+1:BitLength);elsescro(1,:)=population(seln(1)

8、,:);scro(2,:)=population(seln(2),:);end%子程序:计算适应度函数,函数名称存储为fitnessfunfunctionFitvalue,cumsump=fitnessfun(population);global BitLengthglobal boundsbeginglobal boundsendpopsize=size(population,1); %有popsize个个体for i=1:popsizex=transform2to10(population(i,:); %将二进制转化为十进制%转化为-2 2区间的实数xx=boundsbegin+x*(bo

9、undsend-boundsbegin)/(power(boundsend),BitLength)-1);Fitvalue(i)=targetfun(xx); %计算函数值,即适应度end%给适应度函数加上一个大小合理的数以便保证种群适应值为正数Fitvalue=Fitvalue+230;%计算选择概率fsum=sum(Fitvalue);Pperpopulation=Fitvalue/fsum;%计算累积概率cumsump(1)=Pperpopulation(1);for i=2:popsizecumsump(i)=cumsump(i-1)+Pperpopulation(i);endcums

10、ump=cumsump;%子程序,新种群变异操作,函数名称存储为mutation.mfunction snnew=mutation(snew,pmutation);BitLength=size(snew,2);snnew=snew;pmm=IfCroIfMut(pmutation); %根据变异概率决定是否进行变异操作,1则是,0则否if pmm=1chb=round(rand*(BitLength-1)+1; %在1,BitLength范围内随机产生一个变异位snnew(chb)=abs(snew(chb)-1);end%子程序:判断遗传运算是否进行交叉或变异,函数名称存储为IfCroIfM

11、ut.mfunction pcc=IfCroIfMut(mutORcro);test(1:100)=0;l=round(100*mutORcro);test(1:1)=1;n=round(rand*99)+1;pcc=test(n);%子程序:新种群选择操作,函数名称存储为selection.mfunction seln=selection(population,cumsump);%从种群中选择两个个体for i=1:2r=rand; %产生一个随机数prand=cumsump-r;j=1;while prand(j)<0j=j+1;endseln(i)=j; %选中个体的序号end%子程

12、序:将二进制数转换为十进制数,函数名称存储为transform2to10.mfunction x=transform2to10(Population);BitLength=size(Population,2);x=Population(BitLength);for i=1:BitLength-1x=x+Population(BitLength-i)*power(2,i);end%子程序:对于优化最大值或极大值函数问题,目标函数可以作为适应度函数%函数名称存储为targetfun.mfunction y=targetfun(x); %目标函数 y=200*exp(-0.05*x).*sin(x);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号