Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python列表中查找点之间最短距离的更干净的方法?_Python_For Loop_While Loop_Distance_Heuristics - Fatal编程技术网

在python列表中查找点之间最短距离的更干净的方法?

在python列表中查找点之间最短距离的更干净的方法?,python,for-loop,while-loop,distance,heuristics,Python,For Loop,While Loop,Distance,Heuristics,我有一个元组列表和python中的一个点,例如[(1,2),(2,5),(6,7),(9,3)]和(2,1),我想找出由各个点的所有组合创建到点列表的最快路径(基本上我想找到从(2,1)开始到达所有点的最有效方法)。我有一个曼哈顿站函数,它可以取2个点并输出距离。然而,我的算法给出了不一致的答案(由于某种原因,启发式被关闭) 实现这一目标的正确方法是什么 这是我以前的算法: def bestPath(currentPoint,goalList): sum = 0 bestList

我有一个元组列表和python中的一个点,例如[(1,2),(2,5),(6,7),(9,3)]和(2,1),我想找出由各个点的所有组合创建到点列表的最快路径(基本上我想找到从(2,1)开始到达所有点的最有效方法)。我有一个曼哈顿站函数,它可以取2个点并输出距离。然而,我的算法给出了不一致的答案(由于某种原因,启发式被关闭)

实现这一目标的正确方法是什么

这是我以前的算法:

def bestPath(currentPoint,goalList):
    sum = 0
    bestList = []
    while len(goallist) > 0:
        for point in list:
            bestList.append((manhattanD(point,currentPoint),point))
        bestTup = min(bestList)
        bestList = []
        dist = bestTup[0]
        newP = bestTup[1]
        currentPoint = newP
        sum += dist
return sum

试着找出所有的组合,然后检查最短的距离。

因为你没有这么多的点,你可以很容易地使用一个解决方案,尝试所有的可能性

以下是您可以做的:

首先获取所有组合:

>>> list_of_points = [(1,2) , (2,5), (6,7), (9,3)]
>>> list(itertools.permutations(list_of_points))
[((1, 2), (2, 5), (6, 7), (9, 3)),
((1, 2), (2, 5), (9, 3), (6, 7)),
((1, 2), (6, 7), (2, 5), (9, 3)),
((1, 2), (6, 7), (9, 3), (2, 5)),
((1, 2), (9, 3), (2, 5), (6, 7)),
((1, 2), (9, 3), (6, 7), (2, 5)),
((2, 5), (1, 2), (6, 7), (9, 3)),
((2, 5), (1, 2), (9, 3), (6, 7)),
((2, 5), (6, 7), (1, 2), (9, 3)),
((2, 5), (6, 7), (9, 3), (1, 2)),
((2, 5), (9, 3), (1, 2), (6, 7)),
((2, 5), (9, 3), (6, 7), (1, 2)),
((6, 7), (1, 2), (2, 5), (9, 3)),
((6, 7), (1, 2), (9, 3), (2, 5)),
((6, 7), (2, 5), (1, 2), (9, 3)),
((6, 7), (2, 5), (9, 3), (1, 2)),
((6, 7), (9, 3), (1, 2), (2, 5)),
((6, 7), (9, 3), (2, 5), (1, 2)),
((9, 3), (1, 2), (2, 5), (6, 7)),
((9, 3), (1, 2), (6, 7), (2, 5)),
((9, 3), (2, 5), (1, 2), (6, 7)),
((9, 3), (2, 5), (6, 7), (1, 2)),
((9, 3), (6, 7), (1, 2), (2, 5)),
((9, 3), (6, 7), (2, 5), (1, 2))]
然后创建一个函数,为您提供组合长度:

def combination_length(start_point, combination):
    lenght = 0
    previous = start_point  
    for elem in combination:
        lenght += manhattanDistance(previous, elem)

    return length
最后是一个测试各种可能性的函数:

def get_shortest_path(start_point, list_of_point):
    min = sys.maxint
    combination_min = None
    list_of_combinations = list(itertools.permutations(list_of_points))
    for combination in list_of_combination:
        length = combination_length(start_point, combination)
        if length < min:
            min = length
            combination_min = combination

    return combination_min
def get_最短路径(起始点、列表点):
min=sys.maxint
组合_min=无
组合列表=列表(itertools.排列(点列表))
对于组合的列表中的组合:
长度=组合长度(起点,组合)
如果长度<最小值:
最小=长度
组合_min=组合
返回组合_min
最后你可以有:

import sys, itertools

def combination_length(start_point, combination):
    lenght = 0
    previous = start_point  
    for elem in combination:
        lenght += manhattanDistance(previous, elem)

    return length

def get_shortest_path(start_point, list_of_point):
    min = sys.maxint
    combination_min = None
    list_of_combinations = list(itertools.permutations(list_of_points))
    for combination in list_of_combination:
        length = combination_length(start_point, combination)
        if length < min:
            min = length
            combination_min = combination

    return combination_min

list_of_points = [(1,2) , (2,5), (6,7), (9,3)]
print get_shortest_path((2,1), list_of_points)
导入系统,itertools
def组合长度(起点、组合):
长度=0
上一个=起点
对于组合中的elem:
长度+=曼哈顿站(上一个元素)
返回长度
def get_最短路径(起点、点列表):
min=sys.maxint
组合_min=无
组合列表=列表(itertools.排列(点列表))
对于组合的列表中的组合:
长度=组合长度(起点,组合)
如果长度<最小值:
最小=长度
组合_min=组合
返回组合_min
点列表=[(1,2)、(2,5)、(6,7)、(9,3)]
打印获取最短路径((2,1),列出点)

如果这与旅行推销员问题类似,那么您需要查看python模块

你能展示函数的代码吗?你知道这是问题吗?是旅行推销员的问题吗?我认为这更多的是找到所有可能的路径组合,然后选择最佳路径。italic_这是旅行推销员的问题吗?我认为这更重要的是找到所有可能的路径组合,然后选择最佳路径,这不是你想要做的吗?是的。你是对的,事实上,这可能是旅行推销员的问题。你必须意识到这并不完全是推销员的问题,因为这个解决方案不考虑回到起点的时间长度。这将需要一个小的改变,但我不认为这是你需要的,当我读你的问题。谢谢。这应该行得通,因为列表很小。幸运的是,我不需要回来,所以这应该没问题。