1、螞蟻在路徑上釋放信息素。
2、碰到還沒走過的路口,就隨機挑選一條路走。同時,釋放與路徑長度有關的信息素。
3、信息素濃度與路徑長度成反比。后來的螞蟻再次碰到該路口時,就選擇信息素濃度較高路徑。
4、最優路徑上的信息素濃度越來越大。
5、最終蟻群找到最優尋食路徑。
人工蟻群與真實蟻群對比:

基於TSP問題的基本蟻群算法:
TSP求解中,假設蟻群算法中的每只螞蟻是具有以下特征的簡單智能體:
每次周游,每只螞蟻在其經過的支路(i,j)上都留下信息素。
‚螞蟻選擇城市的概率與城市之間的距離和當前連接支路上所包含的信息素余量有關。
ƒ為了強制螞蟻進行合法的周游,直到一次周游完成后,才允許螞蟻游走已訪問過的城市(這可由禁忌表來控制)。
基本蟻群的兩個過程:
(1)狀態轉移
(2)信息素更新
(1)狀態轉移
為了避免殘留信息素過多而淹沒啟發信息,在每只螞蟻走完一步或者完成對所有n個城市的遍歷(也即一個循環結束)后,要對殘留信息進行更新處理。
由此,t+n時刻在路徑(i,j)上的信息量可按如下規則進行調整:

(2)信息素更新模型
蟻周模型(Ant-Cycle模型)
蟻量模型(Ant-Quantity模型)
蟻密模型(Ant-Density模型)
區別:
1.蟻周模型利用的是全局信息,即螞蟻完成一個循環后更新所有路徑上的信息素;
2.蟻量和蟻密模型利用的是局部信息,即螞蟻完成一步后更新路徑上的信息素。

蟻群算法基本流程:

蟻群算法中主要參數的選擇:

蟻群算法中主要參數的理想選擇如下:

國內外,對於離散域蟻群算法的改進研究成果很多,例如自適應蟻群算法、基於信息素擴散的蟻群算法等,這里僅介紹離散域優化問題的自適應蟻群算法。
自適應蟻群算法:對蟻群算法的狀態轉移概率、信息素揮發因子、信息量等因素采用自適應調節策略為一種基本改進思路的蟻群算法。
自適應蟻群算法中兩個最經典的方法:蟻群系統(AntColony System, ACS)和最大-最小蟻群系統(MAX-MINAnt System, MMAS)。
蟻群系統對基本蟻群算法改進:①螞蟻的狀態轉移規則不同;

②全局更新規則不同;

③新增了對各條路徑信息量調整的局部更新規則

下面是Python實現求50個城市之間最短距離的代碼
# -*- coding: utf-8 -*- import random import copy import time import sys import math import tkinter #//GUI模塊 import threading from functools import reduce # 參數 ''' ALPHA:信息啟發因子,值越大,則螞蟻選擇之前走過的路徑可能性就越大 ,值越小,則蟻群搜索范圍就會減少,容易陷入局部最優 BETA:Beta值越大,蟻群越就容易選擇局部較短路徑,這時算法收斂速度會 加快,但是隨機性不高,容易得到局部的相對最優 ''' (ALPHA, BETA, RHO, Q) = (1.0,2.0,0.5,100.0) # 城市數,蟻群 (city_num, ant_num) = (50,50) distance_x = [ 178,272,176,171,650,499,267,703,408,437,491,74,532, 416,626,42,271,359,163,508,229,576,147,560,35,714, 757,517,64,314,675,690,391,628,87,240,705,699,258, 428,614,36,360,482,666,597,209,201,492,294] distance_y = [ 170,395,198,151,242,556,57,401,305,421,267,105,525, 381,244,330,395,169,141,380,153,442,528,329,232,48, 498,265,343,120,165,50,433,63,491,275,348,222,288, 490,213,524,244,114,104,552,70,425,227,331] #城市距離和信息素 distance_graph = [ [0.0 for col in range(city_num)] for raw in range(city_num)] pheromone_graph = [ [1.0 for col in range(city_num)] for raw in range(city_num)] #----------- 螞蟻 ----------- class Ant(object): # 初始化 def __init__(self,ID): self.ID = ID # ID self.__clean_data() # 隨機初始化出生點 # 初始數據 def __clean_data(self): self.path = [] # 當前螞蟻的路徑 self.total_distance = 0.0 # 當前路徑的總距離 self.move_count = 0 # 移動次數 self.current_city = -1 # 當前停留的城市 self.open_table_city = [True for i in range(city_num)] # 探索城市的狀態 city_index = random.randint(0,city_num-1) # 隨機初始出生點 self.current_city = city_index self.path.append(city_index) self.open_table_city[city_index] = False self.move_count = 1 # 選擇下一個城市 def __choice_next_city(self): next_city = -1 select_citys_prob = [0.0 for i in range(city_num)] #存儲去下個城市的概率 total_prob = 0.0 # 獲取去下一個城市的概率 for i in range(city_num): if self.open_table_city[i]: try : # 計算概率:與信息素濃度成正比,與距離成反比 select_citys_prob[i] = pow(pheromone_graph[self.current_city][i], ALPHA) * pow((1.0/distance_graph[self.current_city][i]), BETA) total_prob += select_citys_prob[i] except ZeroDivisionError as e: print ('Ant ID: {ID}, current city: {current}, target city: {target}'.format(ID = self.ID, current = self.current_city, target = i)) sys.exit(1) # 輪盤選擇城市 if total_prob > 0.0: # 產生一個隨機概率,0.0-total_prob temp_prob = random.uniform(0.0, total_prob) for i in range(city_num): if self.open_table_city[i]: # 輪次相減 temp_prob -= select_citys_prob[i] if temp_prob < 0.0: next_city = i break # 未從概率產生,順序選擇一個未訪問城市 # if next_city == -1: # for i in range(city_num): # if self.open_table_city[i]: # next_city = i # break if (next_city == -1): next_city = random.randint(0, city_num - 1) while ((self.open_table_city[next_city]) == False): # if==False,說明已經遍歷過了 next_city = random.randint(0, city_num - 1) # 返回下一個城市序號 return next_city # 計算路徑總距離 def __cal_total_distance(self): temp_distance = 0.0 for i in range(1, city_num): start, end = self.path[i], self.path[i-1] temp_distance += distance_graph[start][end] # 回路 end = self.path[0] temp_distance += distance_graph[start][end] self.total_distance = temp_distance # 移動操作 def __move(self, next_city): self.path.append(next_city) self.open_table_city[next_city] = False self.total_distance += distance_graph[self.current_city][next_city] self.current_city = next_city self.move_count += 1 # 搜索路徑 def search_path(self): # 初始化數據 self.__clean_data() # 搜素路徑,遍歷完所有城市為止 while self.move_count < city_num: # 移動到下一個城市 next_city = self.__choice_next_city() self.__move(next_city) # 計算路徑總長度 self.__cal_total_distance() #----------- TSP問題 ----------- class TSP(object): def __init__(self, root, width = 800, height = 600, n = city_num): # 創建畫布 self.root = root self.width = width self.height = height # 城市數目初始化為city_num self.n = n # tkinter.Canvas self.canvas = tkinter.Canvas( root, width = self.width, height = self.height, bg = "#EBEBEB", # 背景白色 xscrollincrement = 1, yscrollincrement = 1 ) self.canvas.pack(expand = tkinter.YES, fill = tkinter.BOTH) self.title("TSP蟻群算法(n:初始化 e:開始搜索 s:停止搜索 q:退出程序)") self.__r = 5 self.__lock = threading.RLock() # 線程鎖 self.__bindEvents() self.new() # 計算城市之間的距離 for i in range(city_num): for j in range(city_num): temp_distance = pow((distance_x[i] - distance_x[j]), 2) + pow((distance_y[i] - distance_y[j]), 2) temp_distance = pow(temp_distance, 0.5) distance_graph[i][j] =float(int(temp_distance + 0.5)) # 按鍵響應程序 def __bindEvents(self): self.root.bind("q", self.quite) # 退出程序 self.root.bind("n", self.new) # 初始化 self.root.bind("e", self.search_path) # 開始搜索 self.root.bind("s", self.stop) # 停止搜索 # 更改標題 def title(self, s): self.root.title(s) # 初始化 def new(self, evt = None): # 停止線程 self.__lock.acquire() self.__running = False self.__lock.release() self.clear() # 清除信息 self.nodes = [] # 節點坐標 self.nodes2 = [] # 節點對象 # 初始化城市節點 for i in range(len(distance_x)): # 在畫布上隨機初始坐標 x = distance_x[i] y = distance_y[i] self.nodes.append((x, y)) # 生成節點橢圓,半徑為self.__r node = self.canvas.create_oval(x - self.__r, y - self.__r, x + self.__r, y + self.__r, fill = "#ff0000", # 填充紅色 outline = "#000000", # 輪廓白色 tags = "node", ) self.nodes2.append(node) # 顯示坐標 self.canvas.create_text(x,y-10, # 使用create_text方法在坐標(302,77)處繪制文字 text = '('+str(x)+','+str(y)+')', # 所繪制文字的內容 fill = 'black' # 所繪制文字的顏色為灰色 ) # 順序連接城市 #self.line(range(city_num)) # 初始城市之間的距離和信息素 for i in range(city_num): for j in range(city_num): pheromone_graph[i][j] = 1.0 self.ants = [Ant(ID) for ID in range(ant_num)] # 初始蟻群 self.best_ant = Ant(-1) # 初始最優解 self.best_ant.total_distance = 1 << 31 # 初始最大距離 self.iter = 1 # 初始化迭代次數 # 將節點按order順序連線 def line(self, order): # 刪除原線 self.canvas.delete("line") def line2(i1, i2): p1, p2 = self.nodes[i1], self.nodes[i2] self.canvas.create_line(p1, p2, fill = "#000000", tags = "line") return i2 # order[-1]為初始值 reduce(line2, order, order[-1]) # 清除畫布 def clear(self): for item in self.canvas.find_all(): self.canvas.delete(item) # 退出程序 def quite(self, evt): self.__lock.acquire() self.__running = False self.__lock.release() self.root.destroy() print (u"\n程序已退出...") sys.exit() # 停止搜索 def stop(self, evt): self.__lock.acquire() self.__running = False self.__lock.release() # 開始搜索 def search_path(self, evt = None): # 開啟線程 self.__lock.acquire() self.__running = True self.__lock.release() while self.__running: # 遍歷每一只螞蟻 for ant in self.ants: # 搜索一條路徑 ant.search_path() # 與當前最優螞蟻比較 if ant.total_distance < self.best_ant.total_distance: # 更新最優解 self.best_ant = copy.deepcopy(ant) # 更新信息素 self.__update_pheromone_gragh() print (u"迭代次數:",self.iter,u"最佳路徑總距離:",int(self.best_ant.total_distance)) # 連線 self.line(self.best_ant.path) # 設置標題 self.title("TSP蟻群算法(n:隨機初始 e:開始搜索 s:停止搜索 q:退出程序) 迭代次數: %d" % self.iter) # 更新畫布 self.canvas.update() self.iter += 1 # 更新信息素 def __update_pheromone_gragh(self): # 獲取每只螞蟻在其路徑上留下的信息素 temp_pheromone = [[0.0 for col in range(city_num)] for raw in range(city_num)] for ant in self.ants: for i in range(1,city_num): start, end = ant.path[i-1], ant.path[i] # 在路徑上的每兩個相鄰城市間留下信息素,與路徑總距離反比 temp_pheromone[start][end] += Q / ant.total_distance temp_pheromone[end][start] = temp_pheromone[start][end] # 更新所有城市之間的信息素,舊信息素衰減加上新迭代信息素 for i in range(city_num): for j in range(city_num): pheromone_graph[i][j] = pheromone_graph[i][j] * RHO + temp_pheromone[i][j] # 主循環 def mainloop(self): self.root.mainloop() #----------- 程序的入口處 ----------- if __name__ == '__main__': print (u""" -------------------------------------------------------- 程序:蟻群算法解決TPS問題程序 作者:許彬 日期:2015-12-10 語言:Python 2.7 -------------------------------------------------------- """) TSP(tkinter.Tk()).mainloop()
轉自:https://blog.csdn.net/fanxin_i/article/details/80380733
