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