Ⅰ 蟻群演算法是什麼
蟻群演算法,又稱螞蟻演算法,是一種用來在圖中尋找優化路徑的機率型演算法。 它由Marco Dorigo於1992年在他的博士論文中提出,其靈感來源於螞蟻在尋找食物過程中發現路徑的行為。蟻群演算法是一種模擬進化演算法,初步的研究表明該演算法具有許多優良的性質。針對PID控制器參數優化設計問題,將蟻群演算法設計的結果與遺傳演算法設計的結果進行了比較,數值模擬結果表明,蟻群演算法具有一種新的模擬進化優化方法的有效性和應用價值。
原理
設想,如果我們要為螞蟻設計一個人工智慧的程序,那麼這個程序要多麼復雜呢?首先,你要讓螞蟻能夠避開障礙物,就必須根據適當的地形給它編進指令讓他們能夠巧妙的避開障礙物,其次,要讓螞蟻找到食物,就需要讓他們遍歷空間上的所有點;再次,如果要讓螞蟻找到最短的路徑,那麼需要計算所有可能的路徑並且比較它們的大小,而且更重要的是,你要小心翼翼地編程,因為程序的錯誤也許會讓你前功盡棄。這是多麼不可思議的程序!太復雜了,恐怕沒人能夠完成這樣繁瑣冗餘的程序。
然而,事實並沒有你想得那麼復雜,上面這個程序每個螞蟻的核心程序編碼不過100多行!為什麼這么簡單的程序會讓螞蟻干這樣復雜的事情?答案是:簡單規則的涌現。事實上,每隻螞蟻並不是像我們想像的需要知道整個世界的信息,他們其實只關心很小范圍內的眼前信息,而且根據這些局部信息利用幾條簡單的規則進行決策,這樣,在蟻群這個集體里,復雜性的行為就會凸現出來。這就是人工生命、復雜性科學解釋的規律!那麼,這些簡單規則是什麼呢?
Ⅱ 蟻群演算法求解TSP問題的源程序及簡要說明
該程序試圖對具有31個城市的VRP進行求解,已知的最優解為784.1,我用該程序只能優化到810左右,應該是陷入局部最優,但我不知問題出在什麼地方。請用過蟻群演算法的高手指教。
蟻群演算法的matlab源碼,同時請指出為何不能優化到已知的最好解
%
%
% the procere of ant colony algorithm for VRP
%
% % % % % % % % % % %
%initialize the parameters of ant colony algorithms
load data.txt;
d=data(:,2:3);
g=data(:,4);
m=31; % 螞蟻數
alpha=1;
belta=4;% 決定tao和miu重要性的參數
lmda=0;
rou=0.9; %衰減系數
q0=0.95;
% 概率
tao0=1/(31*841.04);%初始信息素
Q=1;% 螞蟻循環一周所釋放的信息素
defined_phrm=15.0; % initial pheromone level value
QV=100; % 車輛容量
vehicle_best=round(sum(g)/QV)+1; %所完成任務所需的最少車數
V=40;
% 計算兩點的距離
for i=1:32;
for j=1:32;
dist(i,j)=sqrt((d(i,1)-d(j,1))^2+(d(i,2)-d(j,2))^2);
end;
end;
%給tao miu賦初值
for i=1:32;
for j=1:32;
if i~=j;
%s(i,j)=dist(i,1)+dist(1,j)-dist(i,j);
tao(i,j)=defined_phrm;
miu(i,j)=1/dist(i,j);
end;
end;
end;
for k=1:32;
for k=1:32;
deltao(i,j)=0;
end;
end;
best_cost=10000;
for n_gen=1:50;
print_head(n_gen);
for i=1:m;
%best_solution=[];
print_head2(i);
sumload=0;
cur_pos(i)=1;
rn=randperm(32);
n=1;
nn=1;
part_sol(nn)=1;
%cost(n_gen,i)=0.0;
n_sol=0; % 由螞蟻產生的路徑數量
M_vehicle=500;
t=0; %最佳路徑數組的元素數為0
while sumload<=QV;
for k=1:length(rn);
if sumload+g(rn(k))<=QV;
gama(cur_pos(i),rn(k))=(sumload+g(rn(k)))/QV;
A(n)=rn(k);
n=n+1;
end;
end;
fid=fopen('out_customer.txt','a+');
fprintf(fid,'%s %i\t','the current position is:',cur_pos(i));
fprintf(fid,'\n%s','the possible customer set is:')
fprintf(fid,'\t%i\n',A);
fprintf(fid,'------------------------------\n');
fclose(fid);
p=compute_prob(A,cur_pos(i),tao,miu,alpha,belta,gama,lmda,i);
maxp=1e-8;
na=length(A);
for j=1:na;
if p(j)>maxp
maxp=p(j);
index_max=j;
end;
end;
old_pos=cur_pos(i);
if rand(1)<q0
cur_pos(i)=A(index_max);
else
krnd=randperm(na);
cur_pos(i)=A(krnd(1));
bbb=[old_pos cur_pos(i)];
ccc=[1 1];
if bbb==ccc;
cur_pos(i)=A(krnd(2));
end;
end;
tao(old_pos,cur_pos(i))=taolocalupdate(tao(old_pos,cur_pos(i)),rou,tao0);%對所經弧進行局部更新
sumload=sumload+g(cur_pos(i));
nn=nn+1;
part_sol(nn)=cur_pos(i);
temp_load=sumload;
if cur_pos(i)~=1;
rn=setdiff(rn,cur_pos(i));
n=1;
A=[];
end;
if cur_pos(i)==1; % 如果當前點為車場,將當前路徑中的已訪問用戶去掉後,開始產生新路徑
if setdiff(part_sol,1)~=[];
n_sol=n_sol+1; % 表示產生的路徑數,n_sol=1,2,3,..5,6...,超過5條對其費用加上車輛的派遣費用
fid=fopen('out_solution.txt','a+');
fprintf(fid,'%s%i%s','NO.',n_sol,'條路徑是:');
fprintf(fid,'%i ',part_sol);
fprintf(fid,'\n');
fprintf(fid,'%s','當前的用戶需求量是:');
fprintf(fid,'%i\n',temp_load);
fprintf(fid,'------------------------------\n');
fclose(fid);
% 對所得路徑進行路徑內3-opt優化
final_sol=exchange(part_sol);
for nt=1:length(final_sol); % 將所有產生的路徑傳給一個數組
temp(t+nt)=final_sol(nt);
end;
t=t+length(final_sol)-1;
sumload=0;
final_sol=setdiff(final_sol,1);
rn=setdiff(rn,final_sol);
part_sol=[];
final_sol=[];
nn=1;
part_sol(nn)=cur_pos(i);
A=[];
n=1;
end;
end;
if setdiff(rn,1)==[];% 產生最後一條終點不為1的路徑
n_sol=n_sol+1;
nl=length(part_sol);
part_sol(nl+1)=1;%將路徑的最後1位補1
% 對所得路徑進行路徑內3-opt優化
final_sol=exchange(part_sol);
for nt=1:length(final_sol); % 將所有產生的路徑傳給一個數組
temp(t+nt)=final_sol(nt);
end;
cost(n_gen,i)=cost_sol(temp,dist)+M_vehicle*(n_sol-vehicle_best); %計算由螞蟻i產生的路徑總長度
for ki=1:length(temp)-1;
deltao(temp(ki),temp(ki+1))=deltao(temp(ki),temp(ki+1))+Q/cost(n_gen,i);
end;
if cost(n_gen,i)<best_cost;
best_cost=cost(n_gen,i);
old_cost=best_cost;
best_gen=n_gen; % 產生最小費用的代數
best_ant=i; %產生最小費用的螞蟻
best_solution=temp;
end;
if i==m; %如果所有螞蟻均完成一次循環,,則用最佳費用所對應的路徑對弧進行整體更新
for ii=1:32;
for jj=1:32;
tao(ii,jj)=(1-rou)*tao(ii,jj);
end;
end;
for kk=1:length(best_solution)-1;
tao(best_solution(kk),best_solution(kk+1))=tao(best_solution(kk),best_solution(kk+1))+deltao(best_solution(kk),best_solution(kk+1));
end;
end;
fid=fopen('out_solution.txt','a+');
fprintf(fid,'%s%i%s','NO.',n_sol,'路徑是:');
fprintf(fid,'%i ',part_sol);
fprintf(fid,'\n');
fprintf(fid,'%s %i\n','當前的用戶需求量是:',temp_load);
fprintf(fid,'%s %f\n','總費用是:',cost(n_gen,i));
fprintf(fid,'------------------------------\n');
fprintf(fid,'%s\n','最終路徑是:');
fprintf(fid,'%i-',temp);
fprintf(fid,'\n');
fclose(fid);
temp=[];
break;
end;
end;
end;
end;
我現在也在研究它,希望能共同進步.建義可以看一下段海濱的關於蟻群演算法的書.講的不錯,李士勇的也可以,還有一本我在圖書館見過,記不得名字了.
Ⅲ 求基本蟻群演算法C++代碼
if (featureData[i]!=0)
{
str1.Format("%d",featureData[i]);
pDC->TextOut( nDeltaHorz*((i-pFrm->m_startPIN)+2)+20, nDeltaVert*8, str1 );
}
Ⅳ 求教:蟻群演算法選擇最短路徑問題
這個例子其實是當初數模比賽時用來完成碎片拼接的,但其所用到原理還是求解最短路徑的原理。但這里的最短路徑和數據結構中最短路徑有一定的區別。在數據結構中,對於最短路徑的求解常用的一般有Dijkstra演算法與Floyd演算法,但對於要求出一條經過所有的點的並且要求路徑最短,這些演算法還是有一定的局限性的。而蟻群演算法則很好地滿足了這些條件。話說回來,很想吐槽一下網路流傳的一些蟻群演算法的例子,當初學習這個時候,身邊也沒有相關的書籍,只好到網上找例子。網上關於這個演算法源代碼的常見的有2個版本,都是出自博客,但是在例子都代碼是不完整的,缺失了一部分,但就是這樣的例子,居然流傳甚廣,我很好奇那些轉載這些源碼的人是否真的有去學習過這些,去調試過。當然,我下面的例子也是無法直接編譯通過的,因為涉及到圖像讀取處理等方面的東西,所以就只貼演算法代碼部分。但是對於這個問題蟻群演算法有一個比較大的缺點,就是收斂很慢,不過對於數量小的路徑,效果還是很好的。function bestqueue =aco1(nt,nc_max,m ,st, sd ,Alpha ,Beta ,Rho ,Q,gethead,getend)%參數解釋:%nt 路徑所經過的點的個數;%nc_max 迭代的次數;%m 螞蟻的個數;%st 起點序號;%sd 終點序號;%Alpha 信息素系數;�ta 啟發因子系數;%Rho 蒸發系數;% Q 信息量;%gethead getend 是用來求距離矩陣的,可根據實際情況修改
% nt = 209;%碎片個數full = zeros(nt,nt);tic;%初始化距離矩陣for i =1:nt for t = 1:nt if i ~= t full(i,t) = sum(abs(getend(:,i) - gethead(:,t))); else full(i,t) = inf; end endend% a =full(156,187)eta = 1./full;%啟發因子,取距離的倒數% eta% e = eta(4,2)tau = ones(nt,nt);%信息素矩陣% tabu = zeros(nt,nt);%禁忌矩陣,取螞蟻數量和碎片數量一致,以減少迭代次數nc =1;%初始化迭代次數;rbest=zeros(nc_max,nt);%各代最佳路線rbest(:,1) = (linspace(st,st,nc_max))';rbest(:,nt) =(linspace(sd,sd,nc_max))'; lbest=zeros(nc_max,1);%各代最佳路線的長度pathlen = 0;%臨時記錄每代最佳路線長度stime = 1;%記錄代數進度for i = 1:nc_max % 代數循環 delta_tau=zeros(nt,nt);%初始化改變數 stime for t = 1:m % 對螞蟻群體的循環, tabu=zeros(1,nt);%禁忌向量,標記已訪問的碎片,初試值設為0,訪問之後則變為1; viseted = zeros(1,nt);%記錄已訪問的元素的位置 tabu(st) = 1;%st為起點,在此表示為碎片矩陣的編號,因為已經將蟻群放在起點,故也應將禁忌向量和位置向量的狀態進行修改 tabu(sd) =1;%同上 visited(nt) = sd ;%同上; visited(1) = st;%同上; ht = 0; for r = 2:nt-1 %記錄了還沒訪問的圖片編號 vp = 1;%visited指示量 pp = [];%置空的概率向量 jc = 0; %獲取尚未訪問的位置的向量。 wv = zeros( nt -2 - ht ); for k =1 : nt if tabu(k) == 0 jc = jc +1; wv(jc) = k; end end% a =(tau(visited(end),ju(3))^Alpha)*(eta(visited(end),ju(3))^Beta)% visited(end) %計算選擇的概率 for k=1:length(wv) pp(k)=(tau(visited(vp),wv(k))^Alpha)*(eta(visited(vp),wv(k))^Beta);%下一張碎片的選擇概率計算,p =(信息素^信息素系數)*(啟發因子^啟發因子系數) end pp=pp./(sum(pp));%歸一化 pcum =cumsum(pp); psl = find(pcum >= rand);%輪盤賭法 to_visit= wv(psl(1)) ;%完成選點 tabu(to_visit) =1; visited(r) = to_visit; ht =ht +1;%已訪問碎片個數變化 vp =vp+1; end %路徑變化信息 %對單個螞蟻的路徑進行統計 sum1 =0; for pr = 1:nt -1 x = visited(pr); y = visited(pr+1) ; sum1 =sum1 + full(x,y); end% vcell{t} =visited;%元胞記錄每個螞蟻的路徑,即碎片順序;% msum(t) = sum1; %信息素變化; for ww=1:(nt-1) delta_tau(visited(ww),visited(ww+1))=delta_tau(visited(ww),visited(ww+1)) + Q/sum1; end% delta_tau(visited(end),visited(1))=delta_tau(visited(end),visited(1))+Q/(sum1/100);% if t == m & i == nc_max % bestqueue = visited% end if t == m bestqueue = visited end end tau=(1-Rho).*tau+delta_tau; %完成信息素的更新,找出現有的最新的最佳路徑,即信息素最多的路徑; stime =stime +1;end toc;
Ⅳ 蟻群優化演算法的蟻群優化演算法
開本: 16開
所屬分類: 圖書 >> 計算機/網路 >> 人工智慧
定價:¥43.00 主要內容包括蟻群演算法基本原理、蟻群演算法在TSP及其擴展問題求解中的應用、蟻群演算法在VRP及其擴展問題求解中的應用、蟻群演算法在最優樹問題求解中的應用、蟻群演算法在整數規劃問題求解中的應用、一般連續優化問題的蟻群演算法以及多目標蟻群演算法等。書中還給出了一些主要演算法的Delphi程序實現源代碼,可供參考或修改使用。
本書可供運籌學、管理科學、系統工程、計算機科學等有關專業的高校師生、科研人員和工程技術人員閱讀參考。
Ⅵ 急求蟻群演算法解決 VRPTW問題的matlab代碼,最好是ACS或者MMAS的!
function [R_best,L_best,L_ave,Shortest_Route,Shortest_Length]=ACATSP(C,NC_max,m,Alpha,Beta,Rho,Q)
%%=========================================================================
%% ACATSP.m
%% Ant Colony Algorithm for Traveling Salesman Problem
%% ChengAihua,PLA Information Engineering University,ZhengZhou,China
%% Email:[email protected]
%% All rights reserved
%%-------------------------------------------------------------------------
%% 主要符號說明
%% C n個城市的坐標,n×2的矩陣
%% NC_max 最大迭代次數
%% m 螞蟻個數
%% Alpha 表徵信息素重要程度的參數
%% Beta 表徵啟發式因子重要程度的參數
%% Rho 信息素蒸發系數
%% Q 信息素增加強度系數
%% R_best 各代最佳路線
%% L_best 各代最佳路線的長度
%% 運行可能要很久,需要耐心等待
%%=========================================================================
n=length(C); %n 為市個數
for i=1:n %坐標矩陣轉換為距離矩陣
for j=1:n
D(i,j)=sqrt((x(i,1)-x(j,1))^2+(x(i,2)-x(j,2))^2);
end
end
for i=1:n %Eta為啟發因子,這里設為距離的倒數
for j=1:n %原文作者少考慮的當D=0是MATLAB提示出錯
if i~=j
Eta(i,j)=1./D(i,j);
end
end
end
for i=1:n
Eta(i,i)=0;
end
Tau=ones(n,n); %Tau為信息素矩陣
Tabu=zeros(m,n); %存儲並記錄路徑的生成
NC=1; %迭代計數器
R_best=zeros(NC_max,n); %各代最佳路線
L_best=inf.*ones(NC_max,1); %各代最佳路線的長度
L_ave=zeros(NC_max,1); %各代路線的平均長度
while NC<=NC_max %停止條件之一:達到最大迭代次數
%%第二步:將m只螞蟻放到n個城市上
Randpos=[];
for i=1:(ceil(m/n))
Randpos=[Randpos,randperm(n)];
end
Tabu(:,1)=(Randpos(1,1:m))';
%%第三步:m只螞蟻按概率函數選擇下一座城市,完成各自的周遊
for j=2:n
for i=1:m
visited=Tabu(i,1:(j-1)); %已訪問的城市
J=zeros(1,(n-j+1)); %待訪問的城市
P=J; %待訪問城市的選擇概率分布
Jc=1;
for k=1:n
if length(find(visited==k))==0
J(Jc)=k;
Jc=Jc+1;
end
end
%下面計算待選城市的概率分布
for k=1:length(J)
P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta);
end
P=P/(sum(P));
%按概率原則選取下一個城市
Pcum=cumsum(P);
Select=find(Pcum>=rand);
to_visit=J(Select(1));
Tabu(i,j)=to_visit;
end
end
if NC>=2
Tabu(1,:)=R_best(NC-1,:);
end
%%第四步:記錄本次迭代最佳路線
L=zeros(m,1);
for i=1:m
R=Tabu(i,:);
for j=1:(n-1)
L(i)=L(i)+D(R(j),R(j+1));
end
L(i)=L(i)+D(R(1),R(n));
end
L_best(NC)=min(L);
pos=find(L==L_best(NC));
R_best(NC,:)=Tabu(pos(1),:);
L_ave(NC)=mean(L);
NC=NC+1;
%%第五步:更新信息素
Delta_Tau=zeros(n,n);
for i=1:m
for j=1:(n-1)
Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i);
end
Delta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i);
end
Tau=(1-Rho).*Tau+Delta_Tau;
%%第六步:禁忌表清零
Tabu=zeros(m,n);
end
%%第七步:輸出結果
Pos=find(L_best==min(L_best));
Shortest_Route=R_best(Pos(1),:);
Shortest_Length=L_best(Pos(1));
DrawRoute(C,Shortest_Route) %調用函數繪圖
Ⅶ VB或C++的蟻群演算法源碼
//建議使用vc2005以上的編譯器,加油!!~!
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
const int iAntCount=34;//螞蟻數量
const int iCityCount=51;//城市數量
const int iItCount=2000;//最大跌代次數
const double Q=100;
const double alpha=1;
const double beta=5;
const double rou=0.5;
int besttour[iCityCount];//最有路徑列表
double rnd(int low,double uper)//獲得隨機數
{
double p=(rand()/(double)RAND_MAX)*((uper)-(low))+(low);
return (p);
};
int rnd(int uper)
{
return (rand()%uper);
};
class GInfo//tsp地圖信息,包含了信息素,城市距離,和信息素變化矩陣
{
public:
double m_dDeltTrial[iCityCount][iCityCount];
double m_dTrial[iCityCount][iCityCount];
double distance[iCityCount][iCityCount];
};
GInfo Map;
class ant
{
private:
int ChooseNextCity();//選擇城市
double prob[iCityCount];
int m_iCityCount;
int AllowedCity[iCityCount];//沒有走過的城市
public:
void addcity(int city);
int tabu[iCityCount];
void Clear();
void UpdateResult();
double m_dLength;
double m_dShortest;
void move();
ant();
void move2last();
};
void ant::move2last()
{
int i;
for(i=0;i<iCityCount;i++)
if (AllowedCity[i]==1)
{
addcity(i);
break;
}
}
void ant::Clear()
{
m_dLength=0;
int i;
for(i=0; i<iCityCount;i++)
{
prob[i]=0;
AllowedCity[i]=1;
i=tabu[iCityCount-1];
m_iCityCount=0;
addcity(i);
}
}
ant::ant()
{
m_dLength=m_dShortest=0;
m_iCityCount=0;
int i;
for(i=0;i<iCityCount;i++) {
AllowedCity[i]=1;
prob[i]=0;
}
}
void ant::addcity(int city)
{
//add city to tabu;
tabu[m_iCityCount]=city;
m_iCityCount++;
AllowedCity[city]=0;
}
int ant::ChooseNextCity()
{
//Update the probability of path selection
//select a path from tabu[m_iCityCount-1] to next
int i;
int j=10000;
double temp=0;
int curCity=tabu[m_iCityCount-1];
for (i=0;i<iCityCount;i++) {
if((AllowedCity[i]==1))
{
temp+=pow((1.0/Map.distance[curCity][i]),beta)*pow((Map.m_dTrial[curCity][i]),alpha);
}
}
double sel=0;
for (i=0;i<iCityCount;i++) {
if((AllowedCity[i]==1))
{
prob[i]=pow((1.0/Map.distance[curCity][i]),(int)beta)*pow((Map.m_dTrial[curCity][i]),(int)alpha)/temp;
sel+=prob[i];
}
else
prob[i]=0;
}
double mRate=rnd(0,sel);
double mSelect=0;
for ( i=0;i<iCityCount;i++) {
if((AllowedCity[i]==1))
mSelect+=prob[i] ;
if (mSelect>=mRate) {j=i;break;}
}
if (j==10000)
{
temp=-1;
for (i=0;i<iCityCount;i++) {
if((AllowedCity[i]==1))
if (temp) {
temp=pow((1.0/Map.distance[curCity][i]),beta)*pow((double)(Map.m_dTrial[curCity][i]),alpha);
j=i;
}
}
}
return j;
}
void ant::UpdateResult()
{
// Update the length of tour
int i;
for(i=0;i<iCityCount-1;i++)
m_dLength+=Map.distance[tabu[i]][tabu[i+1]];
m_dLength+=Map.distance[tabu[iCityCount-1]][tabu[0]];
}
void ant::move()
{
//the ant move to next town and add town ID to tabu.
int j;
j=ChooseNextCity();
addcity(j);
}
class project
{
public:
void UpdateTrial();
double m_dLength;
void initmap();
ant ants[iAntCount];
void GetAnt();
void StartSearch();
project();
};
void project::UpdateTrial()
{
//calculate the changes of trial information
int i;
int j;
for(i=0;i<iAntCount;i++) {
for (j=0;j<iCityCount-1;j++)
{
Map.m_dDeltTrial[ants[i].tabu[j]][ants[i].tabu[j+1]]+=Q/ants[i].m_dLength ;
Map.m_dDeltTrial[ants[i].tabu[j+1]][ants[i].tabu[j]]+=Q/ants[i].m_dLength;
}
Map.m_dDeltTrial[ants[i].tabu[iCityCount-1]][ants[i].tabu[0]]+=Q/ants[i].m_dLength;
Map.m_dDeltTrial[ants[i].tabu[0]][ants[i].tabu[iCityCount-1]]+=Q/ants[i].m_dLength;
}
for (i=0;i<iCityCount;i++) {
for (j=0;j<iCityCount;j++)
{
Map.m_dTrial[i][j]=(rou*Map.m_dTrial[i][j]+Map.m_dDeltTrial[i][j] );
Map.m_dDeltTrial[i][j]=0;
}
}
}
void project::initmap()
{
int i;
int j;
for(i=0;i<iCityCount;i++)
for (j=0;j<iCityCount;j++)
{
Map.m_dTrial[i][j]=1;
Map.m_dDeltTrial[i][j]=0;
}
}
project::project()
{
//initial map,read map infomation from file . et.
initmap();
m_dLength=10e9;
ifstream in("eil51.tsp");
struct city
{
int num;
int x;
int y;
}cc[iCityCount];
for (int i=0;i<iCityCount;i++)
{
in>>cc[i].num>>cc[i].x>>cc[i].y;
besttour[i]=0;
}
int j;
for(int i=0;i<iCityCount;i++)
for (j=0;j<iCityCount;j++)
{
{
Map.distance[i][j]=sqrt(pow((double)(cc[i].x-cc[j].x),2)+pow((double)(cc[i].y-cc[j].y),2));
}
}
}
void project::GetAnt()
{
//randomly put ant into map
int i=0;
int city;
srand( (unsigned)time( NULL ) +rand());
for (i=0;i<iAntCount;i++)
{
city=rnd(iCityCount);
ants[i].addcity(city);
}
}
void project::StartSearch()
{
//begin to find best solution
int max=0;//every ant tours times
int i;
int j;
double temp;
int temptour[iCityCount];
while (max<iItCount)
{
for(j=0;j<iAntCount;j++)
{
for (i=0;i<iCityCount-1;i++)
ants[j].move();
}
for(j=0;j<iAntCount;j++)
{ ants[j].move2last();
ants[j].UpdateResult ();
}
//find out the best solution of the step and put it into temp
int t;
temp=ants[0].m_dLength;
for (t=0;t<iCityCount;t++)
temptour[t]=ants[0].tabu[t];
for(j=0;j<iAntCount;j++)
{
if (temp>ants[j].m_dLength) {
temp=ants[j].m_dLength;
for ( t=0;t<iCityCount;t++)
temptour[t]=ants[j].tabu[t];
}
}
if(temp<m_dLength){
m_dLength=temp;
for ( t=0;t<iCityCount;t++)
besttour[t]=temptour[t];
}
printf("%d : %f\n",max,m_dLength);
UpdateTrial();
for(j=0;j<iAntCount;j++)
ants[j].Clear();
max++;
}
printf("The shortest toure is : %f\n",m_dLength);
for ( int t=0;t<iCityCount;t++)
printf(" %d ",besttour[t]);
}
int main()
{
project TSP;
TSP.GetAnt();
TSP.StartSearch();
return 0;
}
Ⅷ 求「基於蟻群演算法的城市配電網規劃」的源代碼!!!!!