#当前点可以向下扩展的点加入队列中 for i in range(CityNum): if cur_node.visited[i] == False: next_node = create_node(cur_node, i) pri_queue.put(next_node) BestPath.append(0) return Min_Path, BestPath ##############################程序入口######################################### if __name__ == "__main__": Position, CityNum, Dist = GetData("./data/TSP10cities.tsp") start = time.clock() #程序计时开始 Min_Path, BestPath = BaBMethod(CityNum, Dist) #调用分支限定法 end = time.clock() #程序计时结束 print() ResultShow(Min_Path, BestPath, CityNum, "穷举法的宽度优先搜索策略") print("程序的运行时间是:%s" % (end - start)) draw(BestPath, Position, "Breadth First Search Method") """ 结果: 基于穷举法的宽度优先搜索策略求得最短旅行商经过所有城市回到原城市的最短路径为: 0->4->6->7->1->3->2->5->8->9->0 总路径长为:10127.552143541276 程序的运行时间是:12.336137899 """
for i in range(layer, CityNum): Curpath[i], Curpath[layer] = Curpath[layer], Curpath[i] # 路径交换一下 DFSMethod(Dist, CityNum, layer + 1) Curpath[i], Curpath[layer] = Curpath[layer], Curpath[i] # 路径交换回来 ##############################程序入口######################################### if __name__ == "__main__": Position, CityNum, Dist = GetData("./data/TSP10cities.tsp") Curpath = np.arange(CityNum) Min_Path = 0 BestPath = [] Cur_Min_Path = math.inf start = time.clock() #程序计时开始 DFSMethod(Dist, CityNum, 1) #调用深度优先搜索核心算法 end = time.clock() #程序计时结束 print() ResultShow(Min_Path, BestPath, CityNum, "穷举法之深度优先搜索策略") print("程序的运行时间是:%s" % (end - start)) draw(BestPath, Position, "DFS Method") """ 结果: 穷举法之深度优先搜索策略求得最短旅行商经过所有城市回到原城市的最短路径为: 0->4->6->7->1->3->2->5->8->9->0 总路径长为:10127.552143541276 程序的运行时间是:4.845021001 """
2 generate:种群迭代的代数 输出 1 self.ant.bestantunit.length:最小路程 2 self.ga.best.path:最好路径 3 distance_list:每一代的最好路径列表 其他说明:无 """ distance_list = [] while generate > 0: self.ant.nextGeneration() distance = self.ant.bestantunit.length distance_list.append(distance) generate -= 1 return self.ant.bestantunit.length, self.ant.bestantunit.path, distance_list ##############################程序入口######################################### if __name__ == '__main__': Position, CityNum, Dist = GetData("./data/TSP25cities.tsp") tsp = TSP(Position, Dist, CityNum) generate = 500 Min_Path, BestPath, distance_list = tsp.run(generate) print(BestPath) print(tsp.distance(BestPath)) # 结果打印 BestPath.append(BestPath[0]) ResultShow(Min_Path, BestPath, CityNum, "GA") draw(BestPath, Position, "GA", True, range(generate), distance_list)
changepheromonetable[pathtable[i, j]][pathtable[ i, j + 1]] += Q / length[i] changepheromonetable[pathtable[i, j + 1]][pathtable[i, 0]] += Q / length[i] pheromonetable = (1 - rho) * pheromonetable + changepheromonetable # 迭代次数指示器+1 iters += 1 path_tmp = pathbest[-1] BestPath = [] for i in path_tmp: BestPath.append(int(i)) BestPath.append(BestPath[0]) return BestPath, lengthbest[-1] ##############################程序入口######################################### if __name__ == "__main__": Position, CityNum, Dist = GetData("./data/TSP25cities.tsp") start = time.clock() # 程序计时开始 BestPath, Min_Path = ant() end = time.clock() # 程序计时结束 print() ResultShow(Min_Path, BestPath, CityNum, "蚁群算法") draw(BestPath, Position, "Ant Method")
for i in range(CityNum): if cur_node.visited[i] == False: next_node = create_node(cur_node, i) if next_node.lb >= Path_Up: continue pri_queue.put(next_node) BestPath.append(0) return Min_Path, BestPath ##############################程序入口######################################### if __name__ == "__main__": Position, CityNum, Dist = GetData("./data/TSP10cities.tsp") start = time.clock() #程序计时开始 Min_Path, BestPath = BaBMethod(CityNum, Dist) #调用分支限定法 end = time.clock() #程序计时结束 print() ResultShow(Min_Path, BestPath, CityNum, "分支限定法") print("程序的运行时间是:%s" % (end - start)) draw(BestPath, Position, "Branch And Bound Method") """ 结果: 贪心法求得最短旅行商经过所有城市回到原城市的最短路径为: 0->4->6->7->1->3->2->5->8->9->0 总路径长为:10127.552143541276 程序的运行时间是:0.10620661 """
#Cur_Min_City:代表离当前城市距离最小的未经历的城市 Cur_Min_Dist = Dist[Already_Visited_City[i - 1]][j] Already_Visited_City.append(Cur_Min_City) Cumulative_Path += Cur_Min_Dist Cumulative_Path += Dist[0][Cur_Min_City] #将从最后一个城市回到出发城市的距离 Already_Visited_City.append(0) return Cumulative_Path, Already_Visited_City ##############################程序入口######################################### if __name__ == "__main__": Position, CityNum, Dist = GetData("./data/TSP25cities.tsp") start = time.clock() #程序计时开始 Min_Path, BestPath = GreedyMethond(CityNum, Dist) #调用贪心算法 end = time.clock() #程序计时结束 print() ResultShow(Min_Path, BestPath, CityNum, "贪心算法") print("程序的运行时间是:%s" % (end - start)) draw(BestPath, Position, "Greedy Methond") """ 结果: 贪心法求得最短旅行商经过所有城市回到原城市的最短路径为: 0->9->8->5->3->2->1->7->4->6->0 总路径长为:10464.1834865 程序的运行时间是:0.000508957 """
##############################程序入口######################################### if __name__ == "__main__": Position, CityNum, Dist = GetData("./data/TSP10cities.tsp") city_init = 0 #起始城市 #dp_path[city][citylists]代表citylists内与city相连的城市信息 dp_path = np.ones((CityNum, 2**CityNum)) #dp_dist[city,citylist]代表从城市city出发经过citylists内走过的城市后返回到city_start的最短距离矩阵 dp_dist = np.ones((CityNum, 2**CityNum)) * -1 start = time.clock() #程序计时开始 Min_Path, BestPath = DPMethond(CityNum, Dist, city_init) #调用动态规划算法 end = time.clock() #程序计时结束 print() ResultShow(Min_Path, BestPath, CityNum, "动态规划法") print("程序的运行时间是:%s" % (end - start)) draw(BestPath, Position, "Dynamic Programming Method") """ 结果: 贪心法求得最短旅行商经过所有城市回到原城市的最短路径为: 0->4->6->7->1->3->2->5->8->9->0 总路径长为:10127.552143541276 程序的运行时间是:0.064915142 """
else: Curpath[i],Curpath[layer] = Curpath[layer],Curpath[i] # 路径交换一下 BackTrackingMethod(Dist, CityNum, layer+1) Curpath[i],Curpath[layer] = Curpath[layer],Curpath[i] # 路径交换回来 ##############################程序入口######################################### if __name__ == "__main__": Position,CityNum,Dist = GetData("./data/TSP10cities.tsp") Curpath = np.arange(CityNum) Min_Path=0 BestPath=[] Cur_Min_Path = Greedy.GreedyMethond(CityNum,Dist)[0] start = time.clock() #程序计时开始 BackTrackingMethod(Dist,CityNum,1) #调用回溯法 end = time.clock() #程序计时结束 print() ResultShow(Min_Path,BestPath,CityNum,"回溯法") print("程序的运行时间是:%s"%(end-start)) draw(BestPath,Position,"BackTracking Method") """ 结果: 回溯法求得最短旅行商经过所有城市回到原城市的最短路径为: 0->4->6->7->1->3->2->5->8->9->0 总路径长为:10127.552143541276 程序的运行时间是:0.245802962 """