python无人机航点使用旅行商算法优化
·
1.将python版本升级到3.9以上
安装networkx 3.x以上版本
import networkx as nx
import networkx.algorithms.approximation as nx_app
# 使用最短路径规划算法
G = nx.random_geometric_graph(len(uav_point_list), radius=0.4, seed=3)
# Calculating the distances between the nodes as edge's weight.
for i in range(len(uav_point_list)):
for j in range(i + 1, len(uav_point_list)):
# print(i, j)
dist = math.hypot(uav_point_list[i]["x"] - uav_point_list[j]["x"], uav_point_list[i]["y"] - uav_point_list[j]["y"])
dist = dist
G.add_edge(i, j, weight=dist)
cycle = nx_app.christofides(G, weight="weight")
uav_point_list_travle = [uav_point_list[_] for _ in cycle[0:-1]]
results_json = json.dumps(uav_point_list_travle).encode("utf-8")
更多推荐
所有评论(0)