資源簡介
遺傳算法、禁忌搜索、模擬退火、蟻群算法 解決三十個城市的旅行商問題python實現
代碼片段和文件信息
import?random
import?numpy?as?np
import?math
num_city=30#城市總數0-29
num_total=100#隨機生成的初始解的總數
copy_num=70#保留的解的個數
cross_num=20#交叉解的個數
var_num=10#變異解的個數
location=np.loadtxt(‘city_location.txt‘)
#print(location)
#隨機生成初始解[[][][]...]
def?generate_initial():
????initial=[]
????city=list(range(num_city))
????for?i?in?range(num_total):
????????random.shuffle(city)
????????p=city.copy()
????????while?(p?in?initial):
????????????#print(‘2333‘)#隨機了一個重復的解
????????????random.shuffle(city)
????????????p=city.copy()
????????initial.append(p)
????return?initial
#對稱矩陣,兩個城市之間的距離
def?distance_p2p_mat():
????dis_mat=[]
????for?i?in?range(30):
????????dis_mat_each=[]
????????for?j?in?range(30):
????????????dis=math.sqrt(pow(location[i][0]-location[j][0]2)+pow(location[i
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-01-31?15:47??TSP-master\
?????文件????????4815??2018-01-31?15:47??TSP-master\GA.py
?????文件????????1065??2018-01-31?15:47??TSP-master\LICENSE
?????文件?????????386??2018-01-31?15:47??TSP-master\README.md
?????文件????????2392??2018-01-31?15:47??TSP-master\SA.py
?????文件????????3709??2018-01-31?15:47??TSP-master\SI.py
?????文件????????3050??2018-01-31?15:47??TSP-master\TS.py
評論
共有 條評論