資源描述:
《遺傳算法matlab實(shí)現(xiàn)源程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、WORD格式可編輯附頁(yè):一.遺傳算法源程序: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)專(zhuān)業(yè)知識(shí)整理分享WORD格式可編輯clearuim;objmin=min(obj);forsequ=1:popsize???ifobj(sequ)==objmin???????opti=population(sequ,:);???endendclearsequ;fmax=22000;%==
2、forgen=1:maxgen%選擇操作%將求最小值的函數(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;%每條染色體被選中的幾率專(zhuān)業(yè)知識(shí)整理分享WORD格式可編輯forindivi=1:popsize???fitness1(indivi)=obj1(indivi)/total;endclearindivi;%各條染色體被選中的范圍fori
3、ndivi=1:popsize???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????
6、???forfet=2:popsize???????????if(ran>fitness(fet-1))&&(ran<=fitness(fet))專(zhuān)業(yè)知識(shí)整理分享WORD格式可編輯???????????????newpopulation(ranseti,:)=population(fet,:);???????????end???????end???endendclearran;newpopulation;%交叉forint=1:2:popsize-1???popmoth=newpopulation(int,:);????????????????????????popfath=newpopul
7、ation(int+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*hromle
8、ngth);???????end???????ifcpoint1>cpoint2????????????????????????????????tem=cpoint1;???????????cpoint1=cpoint2;???????????cpoint2=tem;專(zhuān)業(yè)知識(shí)整理分享WORD格式可編輯???????end???????cpoint1;???????cpoint2;???????forterm=cpoint1+1:cpoint2??????????????????????????????forss=1:hromlength???????????????ifpopcross(in
9、t,ss)==popfath(term)???????????????????tem1=popcross(int,ss);???????????????????popcross(int,ss)=popcross(int,term);???????????????????popcross(int,term)=tem1;???????????????end???????????end???????????clea