def tsp_path_planning(points): # TSP路径规划 n = len(points) dist_matrix = cdist(points, points) mst = minimum_spanning_tree(csr_matrix(dist_matrix)).toarray() graph = Graph() for i in range(n): graph.add_node(i) for i in range(n): for j in range(i+1, n): if mst[i, j] != 0: graph.add_edge(i, j, weight=dist_matrix[i, j]) vertex_cover = min_weighted_vertex_cover(graph, weight='weight') odd_vertices = list(vertex_cover) odd_edges = np.zeros((len(vertex_cover), 2)) k = 0 for i in range(n): if i in odd_vertices: odd_edges[k, 0] = i k += 1 closed_tour = np.array(list(vertex_cover) + [vertex_cover[0]]) for i in range(0, len(odd_edges), 2): u, v = int(odd_edges[i, 0]), int(odd_edges[i+1, 0]) min_dist, min_j = np.inf, -1 for j in range(n): if j not in odd_vertices: dist = dist_matrix[u, j] + dist_matrix[v, j] if dist < min_dist: min_dist, min_j = dist, j closed_tour = np.insert(closed_tour, np.where(closed_tour == u)[0][0]+1, min_j) closed_tour = np.insert(closed_tour, np.where(closed_tour == v)[0][0]+1, min_j) odd_vertices.remove(u) odd_vertices.remove(v) return closed_tour这个函数貌似没有删除重复的顶点
时间: 2024-01-19 21:04:46 浏览: 204
基于蚁群算法的二维路径规划算法.zip_path planning_tsp 障碍_网络规划_蚁群算法二维_蚁群路径
5星 · 资源好评率100%
是的,你说得没错。这个函数确实没有删除重复的顶点,因为它是用于求解TSP路径规划的,而TSP路径规划中需要经过所有的顶点,因此不需要删除重复的顶点。另外,由于TSP问题是NP问题,没有一种简单的算法可以在多项式时间内求解,因此很多TSP路径规划算法都是近似算法或者启发式算法,而不是精确算法,因此允许一定的误差和重复顶点。
阅读全文