资源描述:
《遗传算法matlab实现源程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、word资料下载可编辑附页:一.遗传算法源程序:clc;clear;population;%评价目标函数值foruim=1:popsize vector=population(uim,:); obj(uim)=hanshu(hromlength,vector,phen);end%obj%min(obj)专业技术资料word资料下载可编辑clearuim;objmin=min(obj);forsequ=1:popsize ifobj(sequ)==objmin opti=population(sequ,:); en
2、dendclearsequ;fmax=22000;%==forgen=1:maxgen%选择操作%将求最小值的函数转化为适应度函数forindivi=1:popsize obj1(indivi)=1/obj(indivi);endclearindivi;%适应度函数累加总合total=0;forindivi=1:popsize total=total+obj1(indivi);endclearindivi;%每条染色体被选中的几率专业技术资料word资料下载可编辑forindivi=1:popsize fitness1(indi
3、vi)=obj1(indivi)/total;endclearindivi;%各条染色体被选中的范围forindivi=1:popsize fitness(indivi)=0; forj=1:indivi fitness(indivi)=fitness(indivi)+fitness1(j); endendclearj;fitness;%选择适应度高的个体forranseti=1:popsize ran=rand; while(ran>1
4、
5、ran<0) ran=rand; end ran
6、; ifran<=fitness(1) newpopulation(ranseti,:)=population(1,:); else forfet=2:popsize if(ran>fitness(fet-1))&&(ran<=fitness(fet))专业技术资料word资料下载可编辑 newpopulation(ranseti,:)=population(fet,:); end end endendclearran;new
7、population;%交叉forint=1:2:popsize-1 popmoth=newpopulation(int,:); popfath=newpopulation(int+1,:); popcross(int,:)=popmoth; popcross(int+1,:)=popfath; randnum=rand; if(randnum
cpoint1=round(rand*hromlength);
8、 cpoint2=round(rand*hromlength); while(cpoint2==cpoint1) cpoint2=round(rand*hromlength); end ifcpoint1>cpoint2 tem=cpoint1; cpoint1=cpoint2; cpoint2=tem;专业技术资料w
9、ord资料下载可编辑 end cpoint1; cpoint2; forterm=cpoint1+1:cpoint2 forss=1:hromlength ifpopcross(int,ss)==popfath(term) tem1=popcross(int,ss); popcross(int,ss)=popcross(int,term)
10、; popcross(int,term)=tem1; end end cl