資源簡介
給出了范圍是[1,16]的1600多個數據,這些數據都是隨機無規律出現,運用遺傳算法來進行數據預測。

代碼片段和文件信息
function?[in_put_newcountpredictflag]?=?GA(in_putout_put)?
%%
outerinternum?=?100?;?????%?最外層迭代次數
internum?=?50?;????????????%?選擇?交叉?迭代次數
mutation_P?=?0.03??;???????%?變異概率
for?outerinter=1:outerinternum
????%?統計1-16?每個數字出現的次數及概率
????count=zeros(116)?;???%?計數
????for?i=1:length(in_put)
?????????count(?in_put(i)?)?=?count(?in_put(i)?)?+1?;???%?每個數字出現次數
????end
????P?=?count./sum(count)???;???????%?每個數字出現的概率
????[temp_Pind_P]?=?sort(P)?;
????Cumsum_P?=?cumsum(temp_P)??;????%?計算累積概率
????Cumsum_P?=?[0?Cumsum_P]??;?
????
????X?=?[]??;???????????%?通過選擇?交叉?變異?產生的100個新數據
????X_bin?=?[]??;???????%?轉換為二進制
???for?inter?=1:internum
????????%%?輪盤賭選擇
????????rnd?=?rand()?;
????????ind?=?find(Cumsum_P>=rnd)??;
????????ind?=?ind(1)?-1?;
????????chrom1?=?ind_P(ind)???;????????%?選擇第一個染色體
????????while?1
????????????rnd?=?rand()?;
????????????ind?=?find(Cumsum_P>=rnd)??;
????????????ind?=?ind(1)?-1?;
????????????chrom2?=?ind_P(ind)???;????%?選擇第二個染色體
????????????if?chrom1~=chrom2??????????%?保證這兩個染色體不同
????????????????break?
????????????end
????????end
????????%%?交叉
????????chrom1_bin?=?‘0000‘?;??????????%?二進制長度:4??char
????????temp?=?dec2bin(chrom1-1)??;????%?轉換為二進制??char
????????chrom1_bin(?end:-1:end+1-length(temp)?)?=?temp(end:-1:1)??;??%?高位補0
????????chrom2_bin?=?‘0000‘?;??????????%?二進制長度:4??char
????????temp?=?dec2bin(chrom2-1)??;????%?轉換為二進制??char
????????chrom2_bin(?end:-1:end+1-length(temp)?)?=?temp(end:-1:1)??;??%?高位補0
????????%?0|000?交叉
????????new_chrom1_bin?=?[?chrom1_bin(1)?chrom2_bin(2:end)?]?;
????????new_chrom2_bin?=?[?chrom2_bin(1)?chrom1_bin(2:end)?]?;
????????X_bin?=?[X_bin?new_chrom1_bin?new_chrom2_bin]?;???%?保存為二進制
???end
???%%?變異
????for?i=1:4:length(X_bin)
????????if?rand()<=mutation_P
????????????X_bin(i)?=?num2str(1-?str2num(X_bin(i)))???;??????%?0-1?變異?取反
????????end
????end
????for?i=3:4:length(X_bin)
????????if?rand()<=mutation_P
????????????X_bin(i)?=?num2str(1-?str2num(X_bin(i)))???;??????%?0-1?變異?取反
????????end
????end
????%?X_bin?再轉換為十進制(最后加1)
????for?i=1:4:length(X_bin)
????????temp_bin?=?X_bin(i:i+3)?;
????????temp?=?bin2dec(temp_bin)?+?1?;???%?二進制轉十進制
????????X?=?[Xtemp]??;
????end
????
????%%?更新輸入
????in_put?=?X??;?????????%?將新產生的100個數據作為輸入
end
%%?循環多次后,最終的數據結果
in_put_new?=?in_put??;
%?統計1-16?每個數字出現的次數及概率
count=zeros(116)?;???%?計數
for?i=1:length(in_put_new)
???count(?in_put_new(i)?)?=?count(?in_put_new(i)?)?+1?;???%?每個數字出現次數
end
[max_count?ind]?=?max(count)??;
predict?=?ind(1)??;??????%?預測結果
if?predict==out_put
????flag?=?1?;
else
????flag?=?0?;
end
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-05-19?10:08??GA?1\
?????目錄???????????0??2014-05-18?15:20??GA?1\data\
?????文件????????1089??2014-05-18?15:20??GA?1\data\data.mat
?????文件???????75776??2014-05-18?15:20??GA?1\data\一維實驗數據.xls
?????文件????????2859??2014-05-22?17:01??GA?1\GA.m
?????文件????????1129??2014-05-22?17:02??GA?1\GA_main.m
?????目錄???????????0??2014-06-06?08:52??GA?1\result\
?????文件?????????478??2014-05-22?17:06??GA?1\result\實驗結果.csv
?????文件???????18944??2014-06-06?08:52??GA?1\result\實驗結果.xls
?????文件?????????749??2014-05-22?17:06??GA?1\result\實驗運行時間.csv
?????文件???????17920??2014-06-06?08:52??GA?1\result\實驗運行時間.xls
?????文件?????????884??2014-05-18?15:20??GA?1\遺傳算法.txt
- 上一篇:QShareMemory.zip
- 下一篇:office2007_visio.txt
評論
共有 條評論