Beispiel #1
0
def get_up(CityNum, Dist):
    """
		函数名:get_up(CityNum,Dist)
		函数功能:	通过贪心算法求取目标函数的上界
			输入	1 	CityNum:城市数量
				2	Dist:城市间距离矩阵
			输出	1 Path_Up:分支界限法的上界
		其他说明:无
	"""
    Path_Up = Greedy.GreedyMethond(CityNum, Dist)[0]
    return Path_Up
Beispiel #2
0
		for i in range(layer,CityNum):
			#判断是否符合剪枝条件,不符合则继续执行
			if IsPrun(layer,i):
				continue
			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