對《禁忌搜索(Tabu Search)算法及python實現》的修改


這個算法是在聽北大人工智能mooc的時候,老師講的一種局部搜索算法,可是舉得例子不太明白。搜索網頁后,發現《禁忌搜索(Tabu Search)算法及python實現》(https://blog.csdn.net/adkjb/article/details/81712969) 已經做了好詳細的介紹,仔細看了下很有收獲。於是想泡泡代碼,看前面還好,后邊的代碼有些看不懂了,而且在函數里定義函數,這種做法少見,並且把函數有當作類來用,為什么不直接用類呢。還有就是,可能對禁忌搜索不太了解,可能具體算法在代碼里有問題,歡迎提出。

 

import random 

class Tabu:
    def __init__(self,tabulen=100,preparelen=200):
        self.tabulen=tabulen
        self.preparelen=preparelen
        self.city,self.cityids,self.stid=self.loadcity2()  #我直接把他的數據放到代碼里了
        
        self.route=self.randomroute()
        self.tabu=[]
        self.prepare=[]
        self.curroute=self.route.copy()
        self.bestcost=self.costroad(self.route)
        self.bestroute=self.route
    def loadcity(self,f="d:/Documents/code/aiclass/tsp.txt",stid=1):
        city = {} 
        cityid=[]
                     
        for line in open(f): 
            place,lon,lat = line.strip().split(" ") 
            city[int(place)]=float(lon),float(lat) #導入城市的坐標 
            cityid.append(int(place))
        return city,cityid,stid
    def loadcity2(self,stid=1):
        city={1: (1150.0, 1760.0), 2: (630.0, 1660.0), 3: (40.0, 2090.0), 4: (750.0, 1100.0), 
              5: (750.0, 2030.0), 6: (1030.0, 2070.0), 7: (1650.0, 650.0), 8: (1490.0, 1630.0), 
              9: (790.0, 2260.0), 10: (710.0, 1310.0), 11: (840.0, 550.0), 12: (1170.0, 2300.0), 
              13: (970.0, 1340.0), 14: (510.0, 700.0), 15: (750.0, 900.0), 16: (1280.0, 1200.0), 
              17: (230.0, 590.0), 18: (460.0, 860.0), 19: (1040.0, 950.0), 20: (590.0, 1390.0), 
              21: (830.0, 1770.0), 22: (490.0, 500.0), 23: (1840.0, 1240.0), 24: (1260.0, 1500.0), 
              25: (1280.0, 790.0), 26: (490.0, 2130.0), 27: (1460.0, 1420.0), 28: (1260.0, 1910.0),
              29: (360.0, 1980.0)}  #原博客里的數據
        cityid=list(city.keys())
        return city,cityid,stid
    def costroad(self,road):
        #計算當前路徑的長度 與原博客里的函數功能相同
        d=-1
        st=0,0
        cur=0,0
        city=self.city
        for v in road:
            if d==-1:
                st=city[v]
                cur=st
                d=0
            else:
                d+=((cur[0]-city[v][0])**2+(cur[1]-city[v][1])**2)**0.5 #計算所求解的距離,這里為了簡單,視作二位平面上的點,使用了歐式距離
                cur=city[v]
        d+=((cur[0]-st[0])**2+(cur[1]-st[1])**2)**0.5
        return d
    def randomroute(self):
        #產生一條隨機的路
        stid=self.stid
        rt=self.cityids.copy()
        random.shuffle(rt)
        rt.pop(rt.index(stid))
        rt.insert(0,stid)
        return rt
    def randomswap2(self,route):
        #隨機交換路徑的兩個節點
        route=route.copy()
        while True:
            a=random.choice(route)
            b=random.choice(route)
            if a==b or a==1 or b==1:
                continue
            ia=route.index(a)
            ib=route.index(b)
            route[ia]=b
            route[ib]=a
            return route
    def step(self):
        #搜索一步路找出當前下應該搜尋的下一條路
        rt=self.curroute
        
        i=0
        while i<self.preparelen:     #產生候選路徑
            prt=self.randomswap2(rt)
            if int(self.costroad(prt)) not in self.tabu:    #產生不在禁忌表中的路徑
                self.prepare.append(prt.copy())
                i+=1
        c=[]
        for r in self.prepare:
            c.append(self.costroad(r))     
        mc=min(c)
        mrt=self.prepare[c.index(mc)]     #選出候選路徑里最好的一條
        if mc<self.bestcost:    
            self.bestcost=mc
            self.bestroute=mrt.copy()      #如果他比最好的還要好,那么記錄下來
        self.tabu.append(mc)#int(mrt))    #這里本來要加 mrt的 ,可是mrt是路徑,要對比起來麻煩,這里假設每條路是由長度決定的
                                    #也就是說 每個路徑和他的長度是一一對應,這樣比對起來速度快點,當然這樣可能出問題,更好的有待研究
        self.curroute=mrt   #用候選里最好的做下次搜索的起點
        self.prepare=[]
        if len(self.tabu)>self.tabulen:
            self.tabu.pop(0)

下面跑跑看:

import timeit
t=Tabu()print('ok') print(t.city) print(t.route) print(t.bestcost) print(t.curroute) for i in range(1000): t.step() if i%50==0: print(t.bestcost) print(t.bestroute) print(t.curroute) print('ok') #print(timeit.timeit(stmt="t.step()", number=1000,globals=globals())) print('ok')

ok
{1: (1150.0, 1760.0), 2: (630.0, 1660.0), 3: (40.0, 2090.0), 4: (750.0, 1100.0), 5: (750.0, 2030.0), 6: (1030.0, 2070.0), 7: (1650.0, 650.0), 8: (1490.0, 1630.0), 9: (790.0, 2260.0), 10: (710.0, 1310.0), 11: (840.0, 550.0), 12: (1170.0, 2300.0), 13: (970.0, 1340.0), 14: (510.0, 700.0), 15: (750.0, 900.0), 16: (1280.0, 1200.0), 17: (230.0, 590.0), 18: (460.0, 860.0), 19: (1040.0, 950.0), 20: (590.0, 1390.0), 21: (830.0, 1770.0), 22: (490.0, 500.0), 23: (1840.0, 1240.0), 24: (1260.0, 1500.0), 25: (1280.0, 790.0), 26: (490.0, 2130.0), 27: (1460.0, 1420.0), 28: (1260.0, 1910.0), 29: (360.0, 1980.0)}
[1, 6, 2, 12, 9, 28, 15, 21, 8, 22, 29, 19, 26, 24, 20, 18, 17, 7, 4, 16, 27, 11, 14, 25, 10, 3, 13, 23, 5]
24651.706120672443
[1, 6, 2, 12, 9, 28, 15, 21, 8, 22, 29, 19, 26, 24, 20, 18, 17, 7, 4, 16, 27, 11, 14, 25, 10, 3, 13, 23, 5]
21567.36269967159
[1, 6, 2, 12, 9, 28, 15, 21, 8, 22, 29, 3, 26, 24, 20, 18, 17, 7, 4, 16, 27, 11, 14, 25, 10, 19, 13, 23, 5]
[1, 6, 2, 12, 9, 28, 15, 21, 8, 22, 29, 3, 26, 24, 20, 18, 17, 7, 4, 16, 27, 11, 14, 25, 10, 19, 13, 23, 5]
9701.867337779715
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 8, 27, 23, 7, 25, 15, 4, 13, 24]
9701.867337779715
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 12, 6, 5, 9, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 16, 27, 8, 23, 7, 25, 19, 4, 13, 24]
9701.867337779715
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 12, 6, 5, 9, 26, 29, 3, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
9701.867337779715
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 12, 6, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
9667.242844275002
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 6, 12, 9, 3, 29, 26, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 16, 8, 27, 23, 7, 25, 19, 4, 13, 24]
9667.242844275002
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
9667.242844275002
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 6, 12, 9, 5, 26, 29, 3, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
9667.242844275002
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 16, 27, 8, 23, 7, 25, 15, 4, 13, 24]
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 16, 27, 8, 23, 7, 25, 19, 4, 13, 24]
9248.522952771107
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 9, 5, 29, 3, 26, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 19, 15, 4, 13, 16, 25, 7, 23, 8, 27, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 8, 27, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 5, 9, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 6, 12, 9, 5, 29, 3, 26, 21, 2, 20, 10, 18, 17, 14, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 9, 26, 29, 3, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 8, 27, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 9, 5, 26, 29, 3, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 12, 6, 5, 9, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
9213.898459266395
[1, 28, 6, 12, 9, 26, 3, 29, 5, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]
[1, 28, 6, 12, 9, 5, 26, 3, 29, 21, 2, 20, 10, 18, 14, 17, 22, 11, 15, 4, 13, 16, 19, 25, 7, 23, 27, 8, 24]

看到最小路徑是9213.89 如果我們把timeit去掉,跑1000步我的電腦是不到4秒大概

為了直觀把路徑畫下來:

from matplotlib import pyplot 

x=[]
y=[]
print("最優路徑長度:",t.bestcost)
for i in t.bestroute:
    x0,y0=t.city[i]
    x.append(x0)
    y.append(y0)
x.append(x[0])
y.append(y[0])
pyplot.plot(x,y)
pyplot.scatter(x,y)

貌似找到最好的了。。。。

 再跑一次:

9760.12

再來一次:

10212

10500

10100

 

10080

發現有些地方總有不變的地方,是不是可以把多條線路給疊加起來,做個鏈接的加權圖,按照路徑的權再來啟發,是否能得到更好的結果呢?

比如右下角,一直都是一個形狀,是否說明,這幾個點的連接狀況固定了呢?

這樣可以把總是連續的點給合並成一個整體再來搜索是否也是個辦法?

CSDN我的博客不知道怎么給禁用了,不能留言給博主,只能這樣了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM