資源描述:
《遺傳算法matlab實(shí)現(xiàn)源程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、附頁:一.遺傳算法源程序:clc;clear;population;%評(píng)價(jià)目標(biāo)函數(shù)值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;%==forgen=1:maxg
2、en%選擇操作%將求最小值的函數(shù)轉(zhuǎn)化為適應(yīng)度函數(shù)forindivi=1:popsize???obj1(indivi)=1/obj(indivi);endclearindivi;%適應(yīng)度函數(shù)累加總合total=0;forindivi=1:popsize???total=total+obj1(indivi);endclearindivi;%每條染色體被選中的幾率forindivi=1:popsize???fitness1(indivi)=obj1(indivi)/total;endclearindivi;%各條染色體被選中的范圍forindivi=1:popsize??
3、?fitness(indivi)=0;???forj=1:indivi???????fitness(indivi)=fitness(indivi)+fitness1(j);???endendclearj;fitness;%選擇適應(yīng)度高的個(gè)體forranseti=1:popsize???ran=rand;???while(ran>1
4、
5、ran<0)???????ran=rand;???end???ran;???ifran<=fitness(1)???????newpopulation(ranseti,:)=population(1,:);???else???????f
6、orfet=2:popsize???????????if(ran>fitness(fet-1))&&(ran<=fitness(fet))???????????????newpopulation(ranseti,:)=population(fet,:);???????????end???????end???endendclearran;newpopulation;%交叉forint=1:2:popsize-1???popmoth=newpopulation(int,:);????????????????????????popfath=newpopulation(in
7、t+1,:);????????????????????popcross(int,:)=popmoth;???popcross(int+1,:)=popfath;???randnum=rand;???if(randnum
???????cpoint1=round(rand*hromlength);????????????????cpoint2=round(rand*hromlength);??????????????????while(cpoint2==cpoint1)????????????????????????????cpoint2=round(rand*h
8、romlength);???????end???????ifcpoint1>cpoint2????????????????????????????????tem=cpoint1;???????????cpoint1=cpoint2;???????????cpoint2=tem;???????end???????cpoint1;???????cpoint2;???????forterm=cpoint1+1:cpoint2??????????????????????????????forss=1:hromlength???????????????ifpopcross(i
9、nt,ss)==popfath(term)???????????????????tem1=popcross(int,ss);???????????????????popcross(int,ss)=popcross(int,term);???????????????????popcross(int,term)=tem1;???????????????end???????????end???????????cleartem1;???????end???????forterm=cpoint1+1:cpoint2?????????????????????????????