简体   繁体   中英

Finding the Shortest Path Between Certain Points in the Coordinate System in Python

I wrote a code that produces the desired number of points in a certain width and length range in the coordinate system. It calculates and tabulate the distance matrix of these points I produced using the Euclidean method.

My code is here:

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)

Output is:

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

I want to create an algorithm that goes around all the points in the shortest path, starting from a random one of the entered points. (The nearest neighbor method continues by finding the closest point to the starting point according to the Euclidean distance. Then it goes to the closest point to this new point among the unentangled points. This process continues until all points are traversed and the round is completed). How can I repeat this process 10 different times at 10 different points and get an output like this:

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
.
...

Thanks a lot for the help.

If I have understood your problem correctly, this should do the job for a single path.

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)

Output

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

From that you should be able to adapt the code to run 10 times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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