1- #!/usr/bin/env python3
21"""
32使用 Google OR-Tools 解决最短路径问题的示例
43
54这个示例展示了如何使用 OR-Tools 的图算法来解决城市间的最短路径问题。
65"""
76
8- from ortools .graph .python import min_cost_flow
97import time
8+
109import matplotlib .pyplot as plt
1110import networkx as nx
1211import pandas as pd
12+ from ortools .graph .python import min_cost_flow
13+
1314from oxygent import oxy
1415
15- shortest_path_tools = oxy .FunctionHub (
16- name = "shortest_path_tools" )
16+ shortest_path_tools = oxy .FunctionHub (name = "shortest_path_tools" )
1717column_data = {}
1818
1919
20- @shortest_path_tools .tool (description = "Update city and distance information based on Excel." )
20+ @shortest_path_tools .tool (
21+ description = "Update city and distance information based on Excel."
22+ )
2123async def info_update (file_path , sheet_name = 0 ):
2224 # 读取 Excel 文件
2325 df = pd .read_excel (file_path , sheet_name = sheet_name )
@@ -35,16 +37,18 @@ async def info_update(file_path, sheet_name=0):
3537 return "File is Empty"
3638
3739
38- @shortest_path_tools .tool (description = "A tool that can calculate the shortest path between different points" )
40+ @shortest_path_tools .tool (
41+ description = "A tool that can calculate the shortest path between different points"
42+ )
3943async def shortest_path (start_city : str , end_city ):
4044 # 城市列表
41- city_to_index = {city : i for i , city in enumerate (column_data [' cities' ])}
45+ city_to_index = {city : i for i , city in enumerate (column_data [" cities" ])}
4246 print (start_city , end_city )
4347
44- cities = column_data [' cities' ]
45- start_cities = column_data [' start_cities' ]
46- end_cities = column_data [' end_cities' ]
47- distances = column_data [' distances' ]
48+ cities = column_data [" cities" ]
49+ start_cities = column_data [" start_cities" ]
50+ end_cities = column_data [" end_cities" ]
51+ distances = column_data [" distances" ]
4852 # 转换城市名称为索引
4953 start_nodes = [city_to_index [city ] for city in start_cities ]
5054 end_nodes = [city_to_index [city ] for city in end_cities ]
@@ -54,9 +58,11 @@ async def shortest_path(start_city: str, end_city):
5458 # 添加每条边到图中 (注意:我们需要添加双向边,因为城市之间的道路是双向的)
5559 for i in range (len (start_nodes )):
5660 sp_func .add_arc_with_capacity_and_unit_cost (
57- start_nodes [i ], end_nodes [i ], 1 , distances [i ])
61+ start_nodes [i ], end_nodes [i ], 1 , distances [i ]
62+ )
5863 sp_func .add_arc_with_capacity_and_unit_cost (
59- end_nodes [i ], start_nodes [i ], 1 , distances [i ])
64+ end_nodes [i ], start_nodes [i ], 1 , distances [i ]
65+ )
6066
6167 # 设置起点和终点的供应/需求
6268 sp_func .set_node_supply (city_to_index [start_city ], 1 ) # 起点
@@ -133,20 +139,24 @@ def visualize_city_path(cities, start_cities, end_cities, distances, path):
133139 for u , v in path :
134140 path_edges .append ((cities [u ], cities [v ]))
135141
136- nx .draw_networkx_edges (G , city_positions , edgelist = path_edges , width = 3 , edge_color = 'r' )
142+ nx .draw_networkx_edges (
143+ G , city_positions , edgelist = path_edges , width = 3 , edge_color = "r"
144+ )
137145
138146 # 绘制节点
139- nx .draw_networkx_nodes (G , city_positions , node_size = 700 , node_color = ' lightblue' )
147+ nx .draw_networkx_nodes (G , city_positions , node_size = 700 , node_color = " lightblue" )
140148
141149 # 绘制节点标签
142- nx .draw_networkx_labels (G , city_positions , font_size = 12 , font_family = ' SimHei' )
150+ nx .draw_networkx_labels (G , city_positions , font_size = 12 , font_family = " SimHei" )
143151
144152 # 绘制边权重
145- edge_labels = nx .get_edge_attributes (G , 'weight' )
146- nx .draw_networkx_edge_labels (G , city_positions , edge_labels = edge_labels , font_size = 8 )
153+ edge_labels = nx .get_edge_attributes (G , "weight" )
154+ nx .draw_networkx_edge_labels (
155+ G , city_positions , edge_labels = edge_labels , font_size = 8
156+ )
147157
148- plt .title ("中国城市间最短路径" , fontsize = 16 , fontfamily = ' SimHei' )
149- plt .axis (' off' )
158+ plt .title ("中国城市间最短路径" , fontsize = 16 , fontfamily = " SimHei" )
159+ plt .axis (" off" )
150160 plt .tight_layout ()
151161 plt .savefig ("city_shortest_path.png" )
152162 print ("\n 城市路径图已保存为 'city_shortest_path.png'" )
0 commit comments