天天看點

python_最基礎的實作——蟻群算法

為了更好地展現算法性能,采用Python來簡單模拟TSP(旅行商問題),進而分析。已知34個城市、32隻螞蟻和兩兩城市間的距離,确定一條經過所有城市且僅一次的最短路徑。

初始值設定:ρ = 0.5,Q = 100,α = 1.0,β = 2.0,🏙 = 34,🐜 = 32;

作業系統:Windows 10;

實作語言:Python 3.7 ;

運作工具:Anaconda/Pycharm;

最終疊代最優路徑:3419 🐱‍🏍

附:基本蟻群算法流程圖(圖是照着書上畫的哈哈哈哈)

python_最基礎的實作——蟻群算法

附:算法簡單實作效果圖(實在過于粗糙hhh)

算法初始——>疊代一次——>疊代100次(其實此實作代碼疊代40-50次左右就達到最優了,,)

python_最基礎的實作——蟻群算法
python_最基礎的實作——蟻群算法
python_最基礎的實作——蟻群算法

咳咳,,,附上這不講究顆粒度的代碼!嘿嘿😄

```python
"""
Created on Wed May 15 18:50:04 2019
@author: hp
"""
import random
import copy
import sys
import tkinter  # //GUI子產品(引用tk子產品)
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) = (34,32)
distance_x = [
    688,805,176,654,600,499,267,703,408,437,491,74,532,
    416,666,100,251,359,685,508,229,576,777,560,35,714,
    757,517,800,314,675,680,900,625]
distance_y = [
    300,325,198,500,242,600,57,401,305,421,222,105,525,
    381,244,350,395,169,625,380,153,442,268,329,232,40,
    498,265,100,120,165,50,180,550]

#城市距離和資訊素,建立二維數組(矩陣)
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):
            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蟻群算法(i:初始化 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("i", 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="#0000FF",      # 填充藍色
                    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蟻群算法(i:随機初始 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""" 
--------------------------------------------------------
    程式:蟻群算法解決簡單TSP問題 
    作者:_jiao 
    日期:2019-05-15
    語言:Python 
-------------------------------------------------------- 
    """)
    TSP(tkinter.Tk()).mainloop()

           

最後,該🐜問題疊代的最優路徑為:3419。

🆗 半年後發出來這麼簡單的,,,emm…僅供參考~

雖說是上半年看了幾個月的算法,找了好多篇文章,寫了篇論文,但還是感覺自己腦子一片空白😆

好了,不瞎扯了🤐