【问题标题】:Finding the Shortest Path Between Certain Points in the Coordinate System in Python在 Python 中找到坐标系中某些点之间的最短路径
【发布时间】:2022-01-06 04:08:17
【问题描述】:

我编写了一个代码,可以在坐标系中的某个宽度和长度范围内生成所需数量的点。它计算我使用欧几里得方法生成的这些点的距离矩阵并将其制成表格。

我的代码在这里:

import pandas as pd
from scipy.spatial import distance_matrix, distance

import random

npoints = int(input("Type the npoints:"))
width = float(input("Enter the Width you want:"))
height = float(input("Enter the Height you want:"))

sample = []
for _ in range(npoints):
    sample.append((width * random.random(), height * random.random()))
print(*[f"({w:.2f}, {h:.2f})" for w, h in sample], sep=', ')

mat_dist = distance.cdist(sample, sample, 'euclidean')
df_mat_dist = pd.DataFrame(mat_dist)
print(df_mat_dist)

输出是:

Type the npoints:5
Enter the Width you want:6
Enter the Height you want:7
(3.25, 3.55), (5.51, 6.47), (5.87, 5.31), (2.27, 3.20), (0.96, 3.83)
          0         1         2         3         4
0  0.000000  3.690201  3.153510  1.047022  2.305800
1  3.690201  0.000000  1.209096  4.608588  5.257688
2  3.153510  1.209096  0.000000  4.176733  5.123103
3  1.047022  4.608588  4.176733  0.000000  1.450613
4  2.305800  5.257688  5.123103  1.450613  0.000000

Process finished with exit code 0

我想创建一个算法,从输入的随机点开始,围绕最短路径中的所有点。 (最近邻法继续根据欧几里得距离找到离起点最近的点,然后去未纠缠的点中离这个新点最近的点。这个过程一直持续到遍历完所有点,完成一轮)。如何在 10 个不同点重复此过程 10 次并获得如下输出:

Tour Number:1
Number of points visited in order in the relevant round: 0-7-3-8-2...
Total route length of the tour: 18,75755

Tour Number:2
The number of the points visited in order in the relevant round: 6-9-11-2-7...
Total route length of the tour: 14,49849
.
...

非常感谢您的帮助。

【问题讨论】:

  • 这里的“10 个不同的点”是什么——你的意思是你总共产生了 10 个点并且想从每个点开始?
  • 不,兄弟,10 个不同的起点——我的意思是青少年不同的道路。
  • 像这样:你输入npoints的值为20。代码生成20个点。你从任何一点开始,你以最短的方式绕过这 20 个点。你做了 10 次这项工作。

标签: python for-loop data-structures shortest-path nearest-neighbor


【解决方案1】:

如果我正确理解了您的问题,这应该可以为单一路径完成工作。

import random
import pandas as pd
from scipy.spatial import distance_matrix, distance

npoints = int(input("Type the npoints: "))
width = float(input("Enter the Width you want: "))
height = float(input("Enter the Height you want: "))

sample = []
for _ in range(npoints):
    sample.append((width * random.random(), height * random.random()))
print(*[f"({w:.2f}, {h:.2f})" for w, h in sample], sep=', ')

mat_dist = distance.cdist(sample, sample, 'euclidean')
df_mat_dist = pd.DataFrame(mat_dist)
print(df_mat_dist)

#Randomly select the first point
closest_idx = random.randrange(npoints)
path_points = [closest_idx]

#Find the closest point to the starting point, different from diagonal and save results
path_length = 0

for _ in range(npoints-1):
    closest_dist = df_mat_dist.loc[closest_idx, ~df_mat_dist.index.isin(path_points)].min()
    closest_idx = df_mat_dist.loc[closest_idx, ~df_mat_dist.index.isin(path_points)].idxmin()
    path_points.append(closest_idx)
    path_length += closest_dist

print(path_points, path_length)

输出

Type the npoints: 5
Enter the Width you want: 6
Enter the Height you want: 7
(2.45, 6.66), (3.01, 3.94), (5.06, 0.51), (5.89, 1.04), (1.37, 5.03)
          0         1         2         3         4
0  0.000000  2.775327  6.677550  6.587089  1.950042
1  2.775327  0.000000  3.993631  4.086550  1.970787
2  6.677550  3.993631  0.000000  0.988898  5.834766
3  6.587089  4.086550  0.988898  0.000000  6.030719
4  1.950042  1.970787  5.834766  6.030719  0.000000
[1, 4, 0, 3, 2] 11.49681560383563

您应该能够调整代码以运行 10 次。

【讨论】:

  • 非常感谢朋友。
最近更新 更多