繁体   English   中英

计算二维网格上点之间的最短距离,Python

[英]Calculate Shortest distance between points on a 2d grid, Python

给出以下数据框。

names = ['a', 'b', 'c', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
x = [3, 6, 6, 10, 12, 15, 3, 6, 13, 9, 13, 12]
y = [9, 12, 9, 9, 12, 9, 6, 3, 8, 3, 1, 3]

目标:我的目标是找到起点'a'到最后一点之间的最短路径,按照我的算法,我取第一个点'a'并找到它最近的3个点,例如'b,c和d'并像这样计算 a 和 d 之间的距离 dist1 = abcd 和 dist2 = acbd 如果 acbd 短于我将点'c'作为下一跳如果不是那么 b 是下一跳,那么我移动到下一个点' b' 假设 'b' 最接近 a。

我已经非常接近解决它,但似乎我有一些逻辑错误,因为具有跳跃点的最终列表具有重复点。

我的代码:

def distance(p1, p2):
    return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)


def get_hops():
    hopes = []
    for i in range(len(x)):
        for j in range(len(y)):
            dist = distance((x[i], y[i]), (x[j], y[j]))
            hopes.append({'from': names[i], 'to': names[j], 'dist': dist})
    return hopes


def get_points(points):
    point_1 = points[0]
    point_2 = points[0]
    point_3 = points[0]
    for point in points:
        if point['dist'] < point_1['dist']:
            point_1 = point

        if point['dist'] < point_2['dist'] and point['to'] != point_1['to']:
            point_2 = point

        if point['dist'] < point_3['dist'] and point['to'] != point_1['to'] and point['to'] != point_2['to']:
            point_3 = point

    return point_1, point_2, point_3


def get_optimal_path(names_list):
    backup_list = []
    for elm in names_list:
        backup_list.append(elm)
    points_list = [names_list[0]]
    backup_list.remove(names_list[0])
    hops = get_hops()
    for elm in itertools.cycle(names_list):
        if len(points_list) != len(backup_list)-3:
            print(elm, points_list[-1])
            if elm == points_list[-1]:

                # get closet 3 points to the last point in path
                hop_1 = {'from': 'qdsofhvcl', 'to': 'qjf', 'dist': 10000000000000}
                hop_2 = {'from': 'qdsofhvcl', 'to': 'qjf', 'dist': 10000000000000}
                hop_3 = {'from': 'qdsofhvcl', 'to': 'qjf', 'dist': 10000000000000}
                for hop in hops:
                    if hop['from'] == elm and hop['to'] != elm and hop['dist'] < hop_1['dist']:
                        hop_1 = hop

                for hop in hops:
                    if hop['from'] == elm and hop['to'] != elm and hop['dist'] < hop_2['dist'] and hop['to'] != hop_1['to']:
                        hop_2 = hop

                for hop in hops:
                    if hop['from'] == elm and hop['to'] != elm and hop['dist'] < hop_3['dist'] and hop['to'] != hop_1['to'] and hop['to'] != hop_2['to']:
                        hop_3 = hop

                # calculate dist_1 and dist_2 to determine optimal hoping point
                dist_1 = 0
                dist_2 = 0
                for hop in hops:
                    if hop['from'] == elm and hop['to'] == hop_1['to']:
                        dist_1 += hop['dist']
                    if hop_1['from'] == hop['from'] and hop_2['to'] == hop['to']:
                        dist_1 += hop['dist']
                    if hop_2['to'] == hop['from'] and hop['to'] == hop_3['to']:
                        dist_1 += hop['dist']

                    if hop['from'] == elm and hop['to'] == hop_2['to']:
                        dist_2 += hop['dist']
                    if hop_2['to'] == hop['from'] and hop_1['to'] == hop['to']:
                        dist_2 += hop['dist']
                    if hop_1['to'] == hop['from'] and hop['to'] == hop_3['to']:
                        dist_2 += hop['dist']

                # compare 2 distances and determine the best hoping point
                if dist_1 < dist_2:
                    points_list.append(hop_1['to'])
                    print(backup_list)
                    # backup_list.remove(hop_1['to'])
                else:
                    points_list.append(hop_2['to'])
                    print(backup_list)
                    # backup_list.remove(hop_2['to'])
        else:
            print('exit')
            break
        print(points_list)

非常感谢任何建议,如果有新的排序方法,我愿意接受,但我的算法是优先考虑的。

谢谢。

无需在这里重新发明轮子。 根据您的描述,我有点不确定您到底需要什么,但这可能有效: Dijkstra's Algorithm 您还可以查找更多图形算法并找到适合您需求的算法。

编辑:是的,它将返回一条将所有点以最短距离连接到源顶点的路径

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM