简单的MATLAB遗传算法.doc
上传人:sy****28 上传时间:2024-09-11 格式:DOC 页数:3 大小:31KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

简单的MATLAB遗传算法.doc

简单的MATLAB遗传算法.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

老师:这次修改后比较稳定了,值都在x=0附近。%产生初始种群pop,共a个个体,每个个体编码长度为bfunctionpop=initate(a,b)pop=round(rand(a,b));%通过number函数将个体编码转化为十进制并落在区间内functionx=number(pop)[a,b]=size(pop);pop1=zeros(a,b);forj=1:bpop1(:,j)=pop(:,j).*2^(b-j);endpop1=sum(pop1,2);x=(pop1.*20)./2^b-10;%计算个体适应值functionfit=fitness(x)fit=-x.^2;%通过选择/交叉/变异产生新的种群functionnewpop=new(pop,pm,pc)[a,b]=size(pop);newpop=pop;x=number(pop);fit=fitness(x);fitvalue=fit/sum(fit);pw=fitvalue(1);ps=sort(rand(a));new=1;fori=1:aifps(i)<pwnewpop(I,:)=pop(new,:);elsenew=new+1;pw=pw+fitvalue(new);endendfori=1:aifrand<pcpoint=round(rand*(b-1)+1);newpop(i,1:point)=pop(a-i+1,1:point);newpop(a-i+1,1:point)=pop(i,1:point);endendfori=1:aifrand<pmpoint=round(rand*(b-1)+1);newpop(i,point)=1-pop(i,point);endend%主函数cleara=30;b=10;pc=0.6;pm=0.0001;pop=initate(a,b);x=number(pop);fit=fitness(x);[yy,xx]=max(fit);besty=yy;bestx=x(xx);forN=1:200newpop=new(pop,pm,pc);x=number(newpop);fit=fitness(x);[yy,xx]=max(fit);ifbesty<yybesty=yy;bestx=x(xx);endpop=newpop;endbestxbesty=-besty多次运行的结果:>Untitled2bestx=0.1172besty=0.0137>>Untitled2bestx=0besty=0>>Untitled2bestx=0besty=0>>Untitled2bestx=0besty=0>>Untitled2bestx=-0.0195besty=3.8147e-04