I am looking for a solution to derive the shortest route that touches all the points using Python. Please note that
- This is not a Travelling salesman problem (TSP), in which the route should end where it started (salesman reaches his origin point).
- No need to consider the road availability or any such parameters.
I have tried TSP solution that makes a circular path instead of the shortest path. Also, I tried routing-py that considers the road network and finds the optimal road route rather than the shortest path. I am looking for the shortest route that can start from any point, end at any point, and touches all the points.
Can you provide any suggestions in terms of code/ theoretical possibility?
Consider the code below as the input points.
import random
from shapely.geometry import Point
import matplotlib.pyplot as plt
minx, miny, maxx, maxy = 100, 500, 200, 600
ptList = []
for _ in range(50):
pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
ptList.append(pnt)
xs = [point.x for point in ptList]
ys = [point.y for point in ptList]
plt.scatter(xs, ys)
I also tried TSP without the last point and it didn't help. The derived route isn't efficient since it moves in one direction and comes back in the same direction, making the route longer. The image below represents that scenario.
Got an update! I understand that what I am looking for is termed 'shortest Hamilton path'. It is a modified TSP algorithm, in which the cyclic coming back is not forced. I got a solution using it, and below is the code. However, the result is not effective in a few nodes. The misrouting is marked in black circles in the figure below. Correcting this misrouting could provide a better solution to this kind of problem in terms of hardcoding, speed, and effectiveness.
import networkx as nx
g = nx.Graph()
# A distance matrix is made that have distance btwn each points
# A loop that creates all the edges
for i in range(len(distance_matrix)):
source = i
curr_pt_dist = distance_matrix[i]
for j in range(len(curr_pt_dist)):
dest = j
# eucl -> Distance btwn source and destination point
eucl = curr_pt_dist[j]
g.add_edge(source, dest, weight = eucl)
# Finding the route
solved_path = nx.approximation.traveling_salesman_problem(g, weight='weight', cycle=False)
# Getting the points in the calculated order and ploting them as lines.
path = []
for item in solved_path:
path.append(ptList[item])
route_polyline = LineString([z for z in path])
route_geo = gpd.GeoSeries(route_polyline)
route_geo.plot
1, 28, {'weight': 4}), 20, 46, {'weight': 21})
. However, the library seems promising. I tried a few ways and failed. Now I am trying to understand the functions and also I have tried their google group for an answer. Let me come back to you if I could derive a solution.