免疫算法matlab程序_第1頁
免疫算法matlab程序_第2頁
免疫算法matlab程序_第3頁
免疫算法matlab程序_第4頁
免疫算法matlab程序_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、免疫算法matlab程序這是免疫算法。這個(gè)算法幾乎與遺傳算法一樣,只是多用了一個(gè)免疫函數(shù)免疫算法是遺傳算法的變體,它不用雜交,而是采用注入疫苗的方法。疫苗是優(yōu) 秀染色體中的一段基因,把疫苗接種到其它染色體中。注意:標(biāo)準(zhǔn)遺傳算法的一個(gè)重要概念是,染色體是可能解的2進(jìn)制順序號,由這個(gè)序號在可能解的集合(解空間)中找到可能解。這是免疫算法的主程序,它需要調(diào)用的函數(shù)如下接種疫苗函數(shù):%functioninoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)%parameter:1,隨機(jī)制取染色體接種。2,每個(gè)染

2、色體都接種。3,每個(gè)染色體都接種,但接種的位置是隨機(jī)的% 這個(gè)函數(shù)實(shí)現(xiàn)對染色體的疫苗接種%由染色體(可能解的2進(jìn)制)順序號找到可能解:%x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);吩巴解代入非線性方程組計(jì)算誤差函數(shù):functionError=nonLinearSumError1(x);判定程是否得解函數(shù):solution,isTrue=isSolution(x,funtionError,solutionSumError);%選擇最優(yōu)染色體函數(shù):%bestChromosome,leastFunctionEr

3、ror=best_worstChromosome(fatherChromosom eGroup,functionError);%誤差比較函數(shù):從兩個(gè)染色體中,選出誤差較小的染色體%holdBestChromosome,holdLeastFunctionError.% =compareBestChromosome(holdBestChromosome,holdLeastFunctionError,.% bestChromosome,leastFuntionError)%為染色體定義概率函數(shù),好的染色體概率高,壞染色體概率低%p=chromosomeProbability(functionErro

4、r);%按概率選擇染色體函數(shù):%slecteChromosomeGroup=selecteChromome(fatherChromosomeGroup,p);%父代染色體雜交產(chǎn)生子代染色體函數(shù)%sonChrmosomeGroup=crossChromosome(slecteChromosomeGroup,2);%防止染色體超出解空間的函數(shù)%chromosomeGroup=checkSequence(chromosomeGroup,solutionSum)%變異函數(shù)%fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.8,solutionN

5、);%通過實(shí)驗(yàn)有如下結(jié)果:%1。染色體應(yīng)當(dāng)多一些%2。通過概率選擇染色體,在迭代早期會有效選出優(yōu)秀的染色體,使解的誤差 迅速降低,%旦隨著迭代的進(jìn)行,概率選擇也會導(dǎo)致某種染色體在基因池中迅速增加,使染 色體趨同,%這就減少了物種的多樣性,反而難以逼近解%&不用概率選擇,僅采用染色體雜交,采用保留優(yōu)秀染色體,也可以得到解%4單純免疫效果不好,雜交+免疫效果比較好程序開始運(yùn)行clear,clc;%f理內(nèi)存,活屏circleN=200;%4 代次數(shù)format long%構(gòu)殖可能解的空間,確定染色體的個(gè)數(shù)、長度solutionSum=4;leftBoundary=-10;rightBound

6、ary=10;distance=1;chromosomeSum=500;solutionSumError=0.1;%solutionSum:非線性方程組的元數(shù)(待解變量的個(gè)數(shù));leftBoundary:可能解的左 邊界;%rightBoundary:可能解的右邊界;distance:可能解的間隔,也是解的精度%chromosomeSum染色體的個(gè)數(shù);solveSumError解的誤差 oneDimensionSet=leftBoundary:distance:rightBoundary;%oneDimensionSet可能解在一個(gè)數(shù)軸(維)上的集合oneDimensionSetN=size(

7、oneDimensionSet,2)隧回 oneDimensionSet 中的元素個(gè) 數(shù)solutionN=oneDimensionSetN”olutionSum;%牟空間(解集合)中可能解的總數(shù) binSolutionN=dec2bin(solutionN);%JE可能解的總數(shù)轉(zhuǎn)換成二進(jìn)制數(shù) chromosomeLength=size(binSolutionN,2);臨解空間中可能解的總數(shù)(二進(jìn)制數(shù))計(jì) 算染色體的長度程序初始化涮機(jī)生成初始可能解的順序號,+1是為了防止出現(xiàn)0順序號solutionSequence=fix(rand(chromosomeSum,1)*solutionN)+1;

8、for i=1:chromosomeSum%止解的順序號超出解的個(gè)數(shù)if solutionSequence(i)>solutionN;solutionSequence(i)=solutionN;endend%染色體是解集合中的序號,它對應(yīng)一個(gè)可能解%m解的十進(jìn)制序號轉(zhuǎn)成二進(jìn)制序號fatherChromosomeGroup=dec2bin(solutionSequence,chromosomeLength); holdLeastFunctionError=Inf;%可能解的最小誤差的初值holdBestChromosome=0;%對應(yīng)最小誤差的染色體的初值開始計(jì)算compute=1;cir

9、cle=0;while compute%開始迭代求解%M能解的序號尋找解本身(關(guān)鍵步驟)x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);% %把解代入非線,性方程計(jì)算誤差functionError=nonLinearSumError1(x);%G 解代入方程計(jì)算誤差solution,minError,isTrue=isSolution(x,functionError,solutionSumError);%isSolution函數(shù)根據(jù)誤差functionError判定方程是否已經(jīng)解開,isTrue=1方程得 解。

10、solution是方程的解if isTrue=1'方程得解'solutionminErrorreturn%結(jié)束程序end% %3擇最好解對應(yīng)的最優(yōu)染色體bestChromosome,leastFunctionError=best_worstChromosome(fatherChromosome Group,functionError);% %4留每次迭代產(chǎn)生的最好的染色體%本次最好解與上次最好解進(jìn)行比較,如果上次最好解優(yōu)于本次最好解,保留上次最好解;%反之,保留本次最好解。保留的最好染色體放在holdBestChromosome中holdBestChromosome,holdL

11、eastFunctionError.=compareBestChromosome(holdBestChromosome,holdLeastFunctionError,.bestChromosome,leastFunctionError);circle=circle+1%minError%solutionholdLeastFunctionErrorif circle>circleNreturnend%翩留的最好的染色體 holdBestChromosome加入到染色體群中order=round(rand(1)*chromosomeSum);if order=0order=1;endfath

12、erChromosomeGroup(order,:)=holdBestChromosome;functionError(order)=holdLeastFunctionError;%每一條染色體(即可能解的序號)定義一個(gè)概率(關(guān)鍵 步驟)%染色體概率高,壞的概率低。依據(jù)誤差functionError 計(jì)算概率p,trueP=chromosomeProbability(functionError);if trueP ='Fail''可能解嚴(yán)重不適應(yīng)方程,請重新開始'return%結(jié)束程序end按照概率篩選染色體(關(guān)鍵步驟)%fa=bin2dec(fatherChr

13、omosomeGroup)3# 示父染色體% 從父染體中選擇優(yōu)秀染色體%selecteChromosomeGroup=selecteChromosome(fatherChromosomeGroup,p);%尷色體雜交(關(guān)鍵步驟)%sle=bin2dec(selecteChromosomeGroup)囑示選擇出來的解的序號(染色體)%用概率篩選出的染色體 selecteChromosomeGroup進(jìn)行雜交,產(chǎn)生子代染色體%sonChromosomeGroup=crossChromosome(selecteChromosomeGroup,2);%不用概率篩選出的染色體 selecteChromo

14、someGroup進(jìn)行雜交,而直接用上一代(父代)的sonChromosomeGroup=crossChromosome(fatherChromosomeGroup,2);%sonChromosomeGroup=immunity(fatherChromosomeGroup,holdBestChromosome ,3);%ffi疫苗接種到其它染色體中sonChromosomeGroup=immunity(sonChromosomeGroup,holdBestChromosome,3);%cro=bin2dec(sonChromosomeGroup)% 示雜交后 的子代染色體sonChromoso

15、meGroup=checkSequence(sonChromosomeGroup,solutionN);% 查雜交后的染色體是否越界%9異%不雜交直接變異%fatherChromosomeGroup=varianceCh(fatherChromosomeGroup,0.1,solutionN);%雜交后變異fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.5,solutionN);fatherChromosomeGroup=checkSequence(fatherChromosomeGroup,solutionN);%t 查變異后的染色

16、體是否越界end接種疫苗函數(shù),這是和遺傳算法唯一不同的函數(shù),可以用它代替染色體的交義操 作。%chromosomeGroup 染色體組%bachterinChromosome疫苗染色體,即最好的染色體。從這個(gè)染色體上取疫苗%parameter:接種疫苗的參數(shù),即用什么方法接種%inoculateChromosome接種疫苗后的染色體functioninoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)chromosomeGroupSum,chromosomeLength=size(chromosome

17、Group);row,bacterinChromosomeLength=size(bacterinChromosome);%chromosomeGroupSum色體的條數(shù);chromosomeLength:染色體的長度switch parametercase 1%®機(jī)選擇染色體進(jìn)行接種for i=1:chromosomeGroupSum%苗染色體上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上左邊的點(diǎn)位if headDot=0潮止出現(xiàn)0點(diǎn)位headDot=1;endtailDot=fix(rand(1)*bacte

18、rinChromosomeLength);%疫苗在染色體上右邊的點(diǎn)位if tailDot=0%防止出現(xiàn)0點(diǎn)位tailDot=1;endif tailDot>headDot%防止右邊的點(diǎn)位大于左邊的點(diǎn)位dot=headDot;headDot=tailDot;tailDot=dot;end接種randChromosomeSequence=round(rand(1)*chromosomeGroupSum);%隨機(jī)產(chǎn)生1條染色體的序號,對這條染色體進(jìn)行接種if randChromosomeSequence=0(®止產(chǎn)生 0 序號randChromosomeSequence=1;endi

19、noculateChromosome(i,:).%先把輸入染色體傳給輸出=chromosomeGroup(randChromosomeSequence,:);%執(zhí)行免疫,即從疫苗染色體上取出一段基因做疫苗,再注入到其它染色體中inoculateChromosome(i,headDot:tailDot).=bacterinChromosome(1,headDot:tailDot);endcase 2 %W有染色體挨個(gè)接種for i=1:chromosomeGroupSum%苗染色體上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體

20、上左邊的點(diǎn)位if headDot=0潮止出現(xiàn)0點(diǎn)位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上右邊的點(diǎn)位if tailDot=0%防止出現(xiàn)0點(diǎn)位tailDot=1;endif tailDot>headDot%防止右邊的點(diǎn)位大于左邊的點(diǎn)位dot=headDot;headDot=tailDot;tailDot=dot;end%»inoculateChromosome(i,:)=chromosomeGroup(i,:);%fe 把輸入染色體傳給輸 出 %執(zhí)行免疫,即從疫苗染色體上取出一段基因做疫

21、苗,再注入到其它染色體中 inoculateChromosome(i,headDot:tailDot).=bacterinChromosome(1,headDot:tailDot);endcase 3 %1種位置是隨機(jī)的for i=1:chromosomeGroupSum%苗染色體上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上左邊的點(diǎn)位if headDot=0潮止出現(xiàn)0點(diǎn)位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上右邊的點(diǎn)位if tailDot=0%防止出現(xiàn)0點(diǎn)位tailDot=1;endif tailDot>headDot%防止右邊的點(diǎn)位大于左邊的點(diǎn)位dot=headDot;headDot=tailDot;tailDot=dot;end在染色體上隨機(jī)定位接種位置inoculateDot=fix(rand(1)*chromosomeL

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論