四、螞蟻演算法範例
1. Initialize:
Set t:=0 {t is the time counter}
Set NC:=0 {NC is the cycles counter}
For every edge (i,j) set an initial value ?ij(t)=c for trail intensity and ??ij= 0
Place the m ants on the n nodes
2. Set s:=1 {s is the tabu list index}
For k:=1 to m do
Place the starting town of the k-th ant in tabuk(s)
3. Repeat until tabu list is full {this step will be repeated (n-1) times}
Set s:=s+1
For k:=1 to m do
Choose the town j to move to, with probability pijk (t) given by equation (4)
{at time t the k-th ant is on town i=tabuk(s-1)}
Move the k-th ant to the town j
Insert town j in tabuk(s)
4. For k:=1 to m do
Move the k-th ant from tabuk(n) to tabuk(1)
Compute the length Lk of the tour described by the k-th ant
Update the shortest tour found
For every edge (i,j)
For k:=1 to m do
??ki , j ? Q/ Lk ,if (i, j) ??tour described by tabuk
0 ,otherwise
??k ij : ????ij ????ij
5. For every edge (i,j) compute ?ij(t+n) according to equation ?ij(t+n)=?.?ij(t)+??ij
Set t:=t+n
Set NC:=NC+1
For every edge (i,j) set ??ij:=0
6. If (NC < NCMAX)
then
Empty all tabu lists
Goto step 2
else
Print shortest tour
Stop
若在該演算法執行NC圈後停止,這個演算法的時間複雜度會為O(NC.n2m),事實上,步驟1 is O(n2+m),步驟2 is O(m),步驟3 is O(n2.m),步驟4 is O(n2.m),步驟5 is O(n2),步驟6 is O(n.m)。既然已經利用實驗找到蟻穴跟螞蟻數量的線性關係,所以該演算法的時間複雜度為??NC.n3)。
旅行推銷員問題 (TSP)
一地區有若干的城市,城市之間都有道路相連,但卻有長短之分,有一位推銷員從某一城市出發,途中須經過所有城市,最後回到原點,問這位推銷員要怎麼走,才能讓這條路徑最短?
Loop
randomly position num_ants on nun_city cities
for step=1 to run city
for k=1 to run ant
Choose the next city to move to applying
a probabilistic state transition rule
end for
end for
Up date pheromone trails
Until End_condition