資源簡介
可以運(yùn)行的一種蟻群算法代碼,下載既用,方便學(xué)習(xí)。可以用此算法做一些優(yōu)化;
代碼片段和文件信息
%%?I.?清空環(huán)境變量
clear?all
clc
%%?II.?導(dǎo)入數(shù)據(jù)
load?citys_data.mat
%%?III.?計算城市間相互距離
n?=?size(citys1);
D?=?zeros(nn);
for?i?=?1:n
????for?j?=?1:n
????????if?i?~=?j
????????????D(ij)?=?sqrt(sum((citys(i:)?-?citys(j:)).^2));
????????else
????????????D(ij)?=?1e-4;??????
????????end
????end????
end
%%?IV.?初始化參數(shù)
m?=?50;??????????????????????????????%?螞蟻數(shù)量
alpha?=?1;???????????????????????????%?信息素重要程度因子
beta?=?5;????????????????????????????%?啟發(fā)函數(shù)重要程度因子
rho?=?0.1;???????????????????????????%?信息素?fù)]發(fā)因子
Q?=?1;???????????????????????????????%?常系數(shù)
Eta?=?1./D;??????????????????????????%?啟發(fā)函數(shù)
Tau?=?ones(nn);?????????????????????%?信息素矩陣
Table?=?zeros(mn);??????????????????%?路徑記錄表
iter?=?1;????????????????????????????%?迭代次數(shù)初值
iter_max?=?200;??????????????????????%?最大迭代次數(shù)?
Route_best?=?zeros(iter_maxn);??????%?各代最佳路徑???????
Length_best?=?zeros(iter_max1);?????%?各代最佳路徑的長度??
Length_ave?=?zeros(iter_max1);??????%?各代路徑的平均長度??
%%?V.?迭代尋找最佳路徑
while?iter?<=?iter_max
?????%?隨機(jī)產(chǎn)生各個螞蟻的起點(diǎn)城市
??????start?=?zeros(m1);
??????for?i?=?1:m
??????????temp?=?randperm(n);
??????????start(i)?=?temp(1);
??????end
??????Table(:1)?=?start;?
??????citys_index?=?1:n;
??????%?逐個螞蟻路徑選擇
??????for?i?=?1:m
??????????%?逐個城市路徑選擇
?????????for?j?=?2:n
?????????????tabu?=?Table(i1:(j?-?1));???????????%?已訪問的城市集合(禁忌表)
?????????????allow_index?=?~ismember(citys_indextabu);
?????????????allow?=?citys_index(allow_index);??%?待訪問的城市集合
?????????????P?=?allow;
?????????????%?計算城市間轉(zhuǎn)移概率
?????????????for?k?=?1:length(allow)
?????????????????P(k)?=?Tau(tabu(end)allow(k))^alpha?*?Eta(tabu(end)allow(k))^beta;
?????????????end
?????????????P?=?P/sum(P);
?????????????%?輪盤賭法選擇下一個訪問城市
?????????????Pc?=?cumsum(P);?????
????????????target_index?=?find(Pc?>=?rand);?
????????????target?=?allow(target_index(1));
????????????Table(ij)?=?target;
?????????end
??????end
??????%?計算各個螞蟻的路徑距離
??????Length?=?zeros(m1);
??????for?i?=?1:m
??????????Route?=?Table(i:);
??????????for?j?=?1:(n?-?1)
??????????????Length
- 上一篇:卡爾曼濾波器matlab
- 下一篇:zw_Matlab余弦相似度算法.zip
評論
共有 條評論