




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、免疫算法matlab程序這是免疫算法。這個算法幾乎與遺傳算法一樣,只是多用了一個免疫函數(shù)免疫算法是遺傳算法的變體,它不用雜交,而是采用注入疫苗的方法。疫苗是優(yōu) 秀染色體中的一段基因,把疫苗接種到其它染色體中。注意:標準遺傳算法的一個重要概念是,染色體是可能解的2進制順序號,由這個序號在可能解的集合(解空間)中找到可能解。這是免疫算法的主程序,它需要調(diào)用的函數(shù)如下接種疫苗函數(shù):%functioninoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)%parameter:1,隨機制取染色體接種。2,每個染
2、色體都接種。3,每個染色體都接種,但接種的位置是隨機的% 這個函數(shù)實現(xiàn)對染色體的疫苗接種%由染色體(可能解的2進制)順序號找到可能解:%x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);吩巴解代入非線性方程組計算誤差函數(shù):functionError=nonLinearSumError1(x);判定程是否得解函數(shù):solution,isTrue=isSolution(x,funtionError,solutionSumError);%選擇最優(yōu)染色體函數(shù):%bestChromosome,leastFunctionEr
3、ror=best_worstChromosome(fatherChromosom eGroup,functionError);%誤差比較函數(shù):從兩個染色體中,選出誤差較小的染色體%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、);%通過實驗有如下結(jié)果:%1。染色體應當多一些%2。通過概率選擇染色體,在迭代早期會有效選出優(yōu)秀的染色體,使解的誤差 迅速降低,%旦隨著迭代的進行,概率選擇也會導致某種染色體在基因池中迅速增加,使染 色體趨同,%這就減少了物種的多樣性,反而難以逼近解%&不用概率選擇,僅采用染色體雜交,采用保留優(yōu)秀染色體,也可以得到解%4單純免疫效果不好,雜交+免疫效果比較好程序開始運行clear,clc;%f理內(nèi)存,活屏circleN=200;%4 代次數(shù)format long%構(gòu)殖可能解的空間,確定染色體的個數(shù)、長度solutionSum=4;leftBoundary=-10;rightBound
6、ary=10;distance=1;chromosomeSum=500;solutionSumError=0.1;%solutionSum:非線性方程組的元數(shù)(待解變量的個數(shù));leftBoundary:可能解的左 邊界;%rightBoundary:可能解的右邊界;distance:可能解的間隔,也是解的精度%chromosomeSum染色體的個數(shù);solveSumError解的誤差 oneDimensionSet=leftBoundary:distance:rightBoundary;%oneDimensionSet可能解在一個數(shù)軸(維)上的集合oneDimensionSetN=size(
7、oneDimensionSet,2)隧回 oneDimensionSet 中的元素個 數(shù)solutionN=oneDimensionSetN”olutionSum;%牟空間(解集合)中可能解的總數(shù) binSolutionN=dec2bin(solutionN);%JE可能解的總數(shù)轉(zhuǎn)換成二進制數(shù) chromosomeLength=size(binSolutionN,2);臨解空間中可能解的總數(shù)(二進制數(shù))計 算染色體的長度程序初始化涮機生成初始可能解的順序號,+1是為了防止出現(xiàn)0順序號solutionSequence=fix(rand(chromosomeSum,1)*solutionN)+1;
8、for i=1:chromosomeSum%止解的順序號超出解的個數(shù)if solutionSequence(i)>solutionN;solutionSequence(i)=solutionN;endend%染色體是解集合中的序號,它對應一個可能解%m解的十進制序號轉(zhuǎn)成二進制序號fatherChromosomeGroup=dec2bin(solutionSequence,chromosomeLength); holdLeastFunctionError=Inf;%可能解的最小誤差的初值holdBestChromosome=0;%對應最小誤差的染色體的初值開始計算compute=1;cir
9、cle=0;while compute%開始迭代求解%M能解的序號尋找解本身(關(guān)鍵步驟)x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);% %把解代入非線,性方程計算誤差functionError=nonLinearSumError1(x);%G 解代入方程計算誤差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ōu)染色體bestChromosome,leastFunctionError=best_worstChromosome(fatherChromosome Group,functionError);% %4留每次迭代產(chǎ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;%每一條染色體(即可能解的序號)定義一個概率(關(guān)鍵 步驟)%染色體概率高,壞的概率低。依據(jù)誤差functionError 計算概率p,trueP=chromosomeProbability(functionError);if trueP ='Fail''可能解嚴重不適應方程,請重新開始'return%結(jié)束程序end按照概率篩選染色體(關(guān)鍵步驟)%fa=bin2dec(fatherChr
13、omosomeGroup)3# 示父染色體% 從父染體中選擇優(yōu)秀染色體%selecteChromosomeGroup=selecteChromosome(fatherChromosomeGroup,p);%尷色體雜交(關(guān)鍵步驟)%sle=bin2dec(selecteChromosomeGroup)囑示選擇出來的解的序號(染色體)%用概率篩選出的染色體 selecteChromosomeGroup進行雜交,產(chǎn)生子代染色體%sonChromosomeGroup=crossChromosome(selecteChromosomeGroup,2);%不用概率篩選出的染色體 selecteChromo
14、someGroup進行雜交,而直接用上一代(父代)的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疫苗染色體,即最好的染色體。從這個染色體上取疫苗%parameter:接種疫苗的參數(shù),即用什么方法接種%inoculateChromosome接種疫苗后的染色體functioninoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)chromosomeGroupSum,chromosomeLength=size(chromosome
17、Group);row,bacterinChromosomeLength=size(bacterinChromosome);%chromosomeGroupSum色體的條數(shù);chromosomeLength:染色體的長度switch parametercase 1%®機選擇染色體進行接種for i=1:chromosomeGroupSum%苗染色體上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上左邊的點位if headDot=0潮止出現(xiàn)0點位headDot=1;endtailDot=fix(rand(1)*bacte
18、rinChromosomeLength);%疫苗在染色體上右邊的點位if tailDot=0%防止出現(xiàn)0點位tailDot=1;endif tailDot>headDot%防止右邊的點位大于左邊的點位dot=headDot;headDot=tailDot;tailDot=dot;end接種randChromosomeSequence=round(rand(1)*chromosomeGroupSum);%隨機產(chǎn)生1條染色體的序號,對這條染色體進行接種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有染色體挨個接種for i=1:chromosomeGroupSum%苗染色體上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體
20、上左邊的點位if headDot=0潮止出現(xiàn)0點位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上右邊的點位if tailDot=0%防止出現(xiàn)0點位tailDot=1;endif tailDot>headDot%防止右邊的點位大于左邊的點位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種位置是隨機的for i=1:chromosomeGroupSum%苗染色體上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上左邊的點位if headDot=0潮止出現(xiàn)0點位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色體上右邊的點位if tailDot=0%防止出現(xiàn)0點位tailDot=1;endif tailDot>headDot%防止右邊的點位大于左邊的點位dot=headDot;headDot=tailDot;tailDot=dot;end在染色體上隨機定位接種位置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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度高級管理人員競業(yè)禁止合同
- 農(nóng)業(yè)生產(chǎn)資金投入與財務管理手冊
- 開幕式致辭與未來發(fā)展展望報告
- 員工年終工作總結(jié)報告模板集萃
- 互聯(lián)網(wǎng)廣告投放及推廣合作協(xié)議
- 農(nóng)業(yè)生產(chǎn)投入品減量增效技術(shù)指導手冊
- 農(nóng)業(yè)產(chǎn)業(yè)扶貧政策及項目申報指導手冊
- 智能家居技術(shù)研發(fā)推廣合作協(xié)議
- 健身房客戶服務手冊
- 健身房健身器材租賃合同
- 2.0MWp屋頂太陽能分布式光伏發(fā)電項目監(jiān)理大綱2
- 小學夢想開《去遠方》教學設計
- Q∕SY 06349-2019 油氣輸送管道線路工程施工技術(shù)規(guī)范
- CEO自戀及其經(jīng)濟后果研究:以格力電器為例
- 紅土鎳礦濕法冶煉技術(shù)綜述
- 六鑫伺服刀塔說明書LS系列
- 19.骨折術(shù)后內(nèi)固定取出臨床路徑
- 水利水電工程金屬結(jié)構(gòu)與機電設備安裝安全技術(shù)規(guī)程
- 腎內(nèi)科臨床診療規(guī)范(南方醫(yī)院)
- 珍愛生命 安全第一 中小學主題教育班會
- 二十八星宿(課堂PPT)
評論
0/150
提交評論