简体   繁体   English

找到一组二维点之间的最有效路径(以最短距离表示)

[英]find the most efficient path (in term of shortest distance) between a set of 2D points

I have a set of 2D points stored in a dictionary and i need to find the most efficient path to sampling all points (red traingles) in term of the shortest distance from a start-end point (yellow circle). 我在字典中存储了一组2D点,我需要找到最有效的路径来采样所有点(红色梯级),即距起点(黄圈)的最短距离。

在此处输入图片说明

dict_points = OrderedDict([(0, (0.5129102892466411, 1.2791525891782567)), (1, (1.8571436538551014, 1.3979619805011203)), (2, (2.796472292985357, 1.3021773853592946)), (3, (2.2054745567697513, 0.5231652951626251)), (4, (1.1209493135130593, 0.8220950186969501)), (5, (0.16416153316980153, 0.7241249969879273))]) dict_points = OrderedDict([[0,(0.5129102892466411,1.2791525891782567)),(1,(1.8571436538551014,1.3979619805011203)),(2,(2.796472292985357,1.3021773853592946)),(3,(2.2054745567697513,0.5231652951626251493)),(4,( ,0.8220950186969501)),(5,(0.16416153316980153,0.7241249969879273))])

where the key is the ID of the point 关键是点的ID

My strategy is very simple. 我的策略很简单。 I use all points sequence possible (720 for 6 points) and i compute the euclidean distance point-by-point starting and ending from the start-end point (yellow point). 我使用所有可能的点序列(6个点为720),并从起点到终点(黄点)逐点计算欧几里得距离。 The sequence with the shortest total distance is the most efficient. 总距离最短的序列最有效。

The problem of this approach is that get very slow for a large number of points 这种方法的问题是,对于很多点来说,它变得非常慢

import math
import itertools

base = (2.596, 2.196)

def segments(poly):
    """A sequence of (x,y) numeric coordinates pairs """
    return zip(poly, poly[1:] + [poly[0]])


 def best_path(dict_points, base=None):
    sequence_min_distance = None
    l = dict_points.keys()
    gen = itertools.permutations(l)
    min_dist = float('+inf')
    for index, i in enumerate(gen):
        seq = gen.next()
        seq_list = [dict_points[s] for s in seq]
        if base:
            seq_list.insert(0, base)
        points_paired = segments(seq_list)
        tot_dist = 0
        for points in points_paired:
            dist = math.hypot(points[1][0] - points[0][0], points[1][1] - points[0][1])
            tot_dist += dist
        if tot_dist <= min_dist:
            sequence_min_distance = seq
            min_dist = min(min_dist, tot_dist)
    return sequence_min_distance

best_seq = best_path(dict_points)
(5, 4, 3, 2, 1, 0)

您还可以查看项目tsp-solver https://github.com/dmishin/tsp-solver

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

相关问题 计算二维网格上点之间的最短距离,Python - Calculate Shortest distance between points on a 2d grid, Python 寻找最有效的方法来计算3D点集之间的所有距离矢量 - Looking for most efficient way to calc all distance vectors between set of points in 3D 查找多对点之间最接近的8连通棋盘距离:最短的m路径 - Find closest 8-connected chessboard distance between multiple pairs of points: shortest m-path 许多二维点之间的最短路径(Shapely LineString 中的旅行商?) - Shortest path between many 2D points (travelling salesman within Shapely LineString?) python中二维数组中两点之间的距离 - Distance between 2 points in a 2D Array in python 找出两点之间的最短路径 - Find the shortest path between two points 是否有一种有效的算法可以在不重复计算的情况下找到多个 2d 点之间的距离? - Is there an efficient algorithm for finding the distance between multiple 2d points without repeating a calculation? 如何找到 2D 数组和 3D 矩阵网格中的点之间的最近距离? - How can I find the nearest distance between points in a 2D array and a 3D matrix grid? 查找两个阵列之间最短距离的有效方法? - Efficient way to find the shortest distance between two arrays? 使用scipy查找两个数组的点之间的最短距离 - Find shortest distance between points of two arrays with scipy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM