帮我修改一下这段代码的错误import networkx as nx G = nx.DiGraph() start = [1, 3, 5, 7] to = [2, 4, 6, 8] for j in range(len(start)): G.add_edge(start[j], to[j]) nx.draw(G, with_lables=True)

时间: 2023-06-16 21:02:30 浏览: 55
import networkx as nx G = nx.DiGraph() start = [1, 3, 5, 7] to = [2, 4, 6, 8] for j in range(len(start)): G.add_edge(start[j], to[j]) nx.draw(G, with_labels=True) # 你在原来的代码中写成了 with_lables,正确的参数名称应该是 with_labels。此外,你需要在绘图之前先添加边。
相关问题

import pandas as pd import numpy as np import networkx as nx import matplotlib.pyplot as plt df = pd.read_excel(r"C:\Users\li'yi'jie\Desktop\运筹学网络规划数据.xlsx") edges = [] for i in range(len(df)): edge = { "id": df.loc[i, "边的编号"], "tail": df.loc[i, "边的尾节点"], "head": df.loc[i, "边的头节点"], "length": df.loc[i, "长度"], "capacity": df.loc[i, "容量"] } edges.append(edge) plt.figure(figsize=(15,15)) G = nx.DiGraph() for edge in edges: G.add_edge(edge["tail"], edge["head"], weight=edge["length"]) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) labels = nx.get_edge_attributes(G, "weight") nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) #nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, label_pos=0.3) plt.show() all_pairs = dict(nx.all_pairs_dijkstra_path_length(G)) rows = [] for start_node, dist_dict in all_pairs.items(): for end_node, dist in dist_dict.items(): rows.append({'起始节点': start_node, '终止节点': end_node, '最短路径长度': dist}) df_result = pd.DataFrame(rows) df_result.to_excel('all_pairs.xlsx', index=False)请问在写入excel的时候,能否把经过的边的编号一起写入呢?麻烦给下修改后的代码,谢谢你!

可以在遍历最短路径的时候记录下经过的边的编号,然后将其存储到结果表格中。以下是修改后的代码: ``` import pandas as pd import numpy as np import networkx as nx import matplotlib.pyplot as plt df = pd.read_excel(r"C:\Users\li'yi'jie\Desktop\运筹学网络规划数据.xlsx") edges = [] for i in range(len(df)): edge = { "id": df.loc[i, "边的编号"], "tail": df.loc[i, "边的尾节点"], "head": df.loc[i, "边的头节点"], "length": df.loc[i, "长度"], "capacity": df.loc[i, "容量"] } edges.append(edge) plt.figure(figsize=(15,15)) G = nx.DiGraph() for edge in edges: G.add_edge(edge["tail"], edge["head"], weight=edge["length"]) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) labels = nx.get_edge_attributes(G, "weight") nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) #plt.draw() plt.show() all_pairs = dict(nx.all_pairs_dijkstra_path(G)) rows = [] for start_node, paths in all_pairs.items(): for end_node, path in paths.items(): path_edges = [] for i in range(len(path)-1): edge_id = G[path[i]][path[i+1]]['id'] path_edges.append(edge_id) row = { '起始节点': start_node, '终止节点': end_node, '最短路径长度': nx.dijkstra_path_length(G, start_node, end_node), '经过的边编号': path_edges } rows.append(row) df_result = pd.DataFrame(rows) df_result.to_excel('all_pairs.xlsx', index=False) ``` 修改后的代码中,我们在遍历最短路径时,记录下了经过的边的编号,并将其存储到结果表格的“经过的边编号”一列中。

Traceback (most recent call last): File "C:\Users\ying\Desktop\pythonProject5\main.py", line 84, in <module> end_times = nx.dag_late_finish(G, start_times) AttributeError: module 'networkx' has no attribute 'dag_late_finish'如何解决

这个错误是因为 `networkx` 模块中没有 `dag_late_finish` 属性。这个属性在较早的版本中可能存在,但在最新版本中已被删除。 你可以使用 `networkx` 中的 `topological_sort` 函数来实现类似的功能。下面是一个示例: ```python import networkx as nx G = nx.DiGraph() # 添加节点和边 start_times = nx.topological_sort(G) end_times = {node: max(end_times.get(child, 0) for child in G.successors(node)) + weight for node, weight in G.nodes(data="weight")} ``` 在这个示例中,我们使用了 `topological_sort` 函数来获取拓扑排序后的节点列表,然后使用一个字典推导式计算每个节点的最晚完成时间。 你可以根据你的具体需求进行修改。

相关推荐

import networkx as nx import matplotlib.pyplot as plt # 输入数据 locations = [[125.330802,125.401931,125.326444,125.332284,125.322837,125.32563,125.334942,125.378548,125.386251,125.426883,125.42665,125.437111,125.453763,125.431396,125.430705,125.41968,125.437906,125.404171,125.385772,125.341942,125.341535,125.300812,125.307316,125.345642,125.331492,125.330322,125.284474,125.334851,125.30606,125.377211,125.381077,125.417041,125.41427,125.416371,125.432283,125.401676,125.403855,125.38582,125.426733,125.291], [43.917542,43.919075,43.905821,43.90266,43.900238,43.89703,43.888187,43.904508,43.892574,43.907904,43.896354,43.894605,43.889122,43.88774,43.882928,43.887149,43.8789,43.879647,43.883112,43.873763,43.861505,43.854652,43.876513,43.850479,43.833745,43.825044,43.812019,43.803154,43.793054,43.788869,43.824152,43.816805,43.801673,43.82893,43.83235,43.843713,43.854322,43.868372,43.871792,43.8306]] num_flights = 4 flight_capacity = [10, 10, 10, 10] # 将坐标转化为图 G = nx.Graph() for i in range(len(locations[0])): G.add_node(i+1, pos=(locations[0][i], locations[1][i])) for i in range(len(locations[0])): for j in range(i+1, len(locations[0])): dist = ((locations[0][i]-locations[0][j])**2 + (locations[1][i]-locations[1][j])**2)**0.5 G.add_edge(i+1, j+1, weight=dist) # 添加起点和终点 start_node = len(locations[0])+1 end_node = len(locations[0])+2 G.add_node(start_node, pos=(0, 0)) G.add_node(end_node, pos=(0, 0)) # 添加边和边权 for i in range(len(locations[0])): G.add_edge(start_node, i+1, weight=0) G.add_edge(i+1, end_node, weight=0) for f in range(num_flights): for i in range(len(locations[0])): G.add_edge(i+1, len(locations[0])+f*len(locations[0])+i+1, weight=0) G.add_edge(len(locations[0])+f*len(locations[0])+i+1, end_node, weight=0) # 添加航班容量的限制 for f in range(num_flights): for i in range(len(locations[0])): G.add_edge(len(locations[0])+f*len(locations[0])+i+1, len(locations[0])+f*len(locations[0])+len(locations[0])+1, weight=-flight_capacity[f]) #创造路径规划模型 path_model = nx.DiGraph() for i in range(len(locations[0])): for f in range(num_flights): for j in range(len(locations[0])): if i != j: path_model.add_edge(len(locations[0])+flen(locations[0])+i+1, len(locations[0])+flen(locations[0])+j+1, weight=G[i+1][j+1]['weight']+G[len(locations[0])+flen(locations[0])+i+1][len(locations[0])+f*len(locations[0])+j+1]['weight']) 添加航班时间的限制 for f in range(num_flights): for i in range(len(locations[0])): for j in range(len(locations[0])): if i != j: path_model.add_edge(len(locations[0])+f*len(locations[0])+i+1, len(locations[0])+((f+1)%num_flights)*len(locations[0])+j+1, weight=G[i+1][j+1]['weight']) 求解最短路径 path = nx.bellman_ford_path(path_model, source=start_node, target=end_node, weight='weight') 绘制路径图 pos = nx.get_node_attributes(G, 'pos') nx.draw_networkx_nodes(G, pos, node_size=50, node_color='w') nx.draw_networkx_labels(G, pos) nx.draw_networkx_edges(G, pos, edgelist=G.edges(), width=0.5) for f in range(num_flights): start = len(locations[0])+f*len(locations[0])+1 end = len(locations[0])+(f+1)*len(locations[0])+1 nx.draw_networkx_edges(G, pos, edgelist=path[start:end], edge_color='r', width=2.0, alpha=0.7) plt.axis('off') plt.show()找出错误并修改

import networkx as nx import matplotlib.pyplot as plt # 输入数据 locations = [ [125.330802,125.401931,125.326444,125.332284,125.322837,125.32563,125.334942,125.378548,125.386251,125.426883,125.42665,125.437111,125.453763,125.431396,125.430705,125.41968,125.437906,125.404171,125.385772,125.341942,125.341535,125.300812,125.307316,125.345642,125.331492,125.330322,125.284474,125.334851,125.30606,125.377211,125.381077,125.417041,125.41427,125.416371,125.432283,125.401676,125.403855,125.38582,125.426733,125.291], [43.917542,43.919075,43.905821,43.90266,43.900238,43.89703,43.888187,43.904508,43.892574,43.907904,43.896354,43.894605,43.889122,43.88774,43.882928,43.887149,43.8789,43.879647,43.883112,43.873763,43.861505,43.854652,43.876513,43.850479,43.833745,43.825044,43.812019,43.803154,43.793054,43.788869,43.824152,43.816805,43.801673,43.82893,43.83235,43.843713,43.854322,43.868372,43.871792,43.8306] ] num_flights = 4 flight_capacity = [10, 10, 10, 10] # 将坐标转化为图 G = nx.Graph() for i in range(len(locations[0])): G.add_node(i+1, pos=(locations[0][i], locations[1][i])) for i in range(len(locations[0])): for j in range(i+1, len(locations[0])): dist = ((locations[0][i]-locations[0][j])**2 + (locations[1][i]-locations[1][j])**2)**0.5 G.add_edge(i+1, j+1, weight=dist) # 添加起点和终点 start_node = len(locations[0])+1 end_node = len(locations[0])+2 G.add_node(start_node, pos=(0, 0)) G.add_node(end_node, pos=(0, 0)) # 添加边和边权 for i in range(len(locations[0])): G.add_edge(start_node, i+1, weight=0) G.add_edge(i+1, end_node, weight=0) for f in range(num_flights): for i in range(len(locations[0])): G.add_edge(i+1, len(locations[0])+flen(locations[0])+i+1, weight=0) G.add_edge(len(locations[0])+flen(locations[0])+i+1, end_node, weight=0) # 添加航班容量的限制 for f in range(num_flights): for i in range(len(locations[0])): G.add_edge(len(locations[0])+flen(locations[0])+i+1, len(locations[0])+flen(locations[0])+len(locations[0])+1, weight=-flight_capacity[f]) # 创造路径规划模型 path_model = nx.DiGraph() for i in range(len(locations[0])): for f in range(num_flights): for j in range(len(locations[0])): if i != j: path_model.add_edge(len(locations[0])+flen(locations[0])+i+1, len(locations[0])+flen(locations[0])+j+1, weight=G[i+1][j+1]['weight']+G[len(locations[0])+flen(locations[0])+i+1][len(locations[0])+flen(locations[0])+j+1]['weight']) # 添加航班时间的限制 for f in range(num_flights): for i in range(len(locations[0])): for j in range(len(locations[0])): if i != j: path_model.add_edge(len(locations[0])+f*len(locations[0])+i+1, len(locations[0])+((f+1)%num_flights)len(locations[0])+j+1, weight=G[i+1][j+1]['weight']) # 求解最短路径 path = nx.bellman_ford_path(path_model, source=start_node, target=end_node, weight='weight') # 绘制路径图 pos = nx.get_node_attributes(G, 'pos') nx.draw_networkx_nodes(G, pos, node_size=50, node_color='w') nx.draw_networkx_labels(G, pos) nx.draw_networkx_edges(G, pos, edgelist=G.edges(), width=0.5) for f in range(num_flights): start = len(locations[0])+flen(locations[0])+1 end = len(locations[0])+(f+1)*len(locations[0])+1 nx.draw_networkx_edges(G, pos, edgelist=path[start:end], edge_color='r', width=2.0, alpha=0.7) plt.axis('off') plt.show()找出错误并改正

# 输入数据 locations = [[125.330802,125.401931,125.326444,125.332284,125.322837,125.32563,125.334942,125.378548,125.386251,125.426883,125.42665,125.437111,125.453763,125.431396,125.430705,125.41968,125.437906,125.404171,125.385772,125.341942,125.341535,125.300812,125.307316,125.345642,125.331492,125.330322,125.284474,125.334851,125.30606,125.377211,125.381077,125.417041,125.41427,125.416371,125.432283,125.401676,125.403855,125.38582,125.426733,125.291], [43.917542,43.919075,43.905821,43.90266,43.900238,43.89703,43.888187,43.904508,43.892574,43.907904,43.896354,43.894605,43.889122,43.88774,43.882928,43.887149,43.8789,43.879647,43.883112,43.873763,43.861505,43.854652,43.876513,43.850479,43.833745,43.825044,43.812019,43.803154,43.793054,43.788869,43.824152,43.816805,43.801673,43.82893,43.83235,43.843713,43.854322,43.868372,43.871792,43.8306]] num_flights = 4 flight_capacity = [10, 10, 10, 10] # 将坐标转化为图 G = nx.Graph() for i in range(len(locations[0])): G.add_node(i+1, pos=(locations[0][i], locations[1][i])) for i in range(len(locations[0])): for j in range(i+1, len(locations[0])): dist = ((locations[0][i]-locations[0][j])**2 + (locations[1][i]-locations[1][j])**2)**0.5 G.add_edge(i+1, j+1, weight=dist) # 添加起点和终点 start_node = len(locations[0])+1 end_node = len(locations[0])+2 G.add_node(start_node, pos=(0, 0)) G.add_node(end_node, pos=(0, 0)) # 添加边和边权 for i in range(len(locations[0])): G.add_edge(start_node, i+1, weight=0) G.add_edge(i+1, end_node, weight=0) for f in range(num_flights): for i in range(len(locations[0])): G.add_edge(i+1, len(locations[0])+f*len(locations[0])+i+1, weight=0) G.add_edge(len(locations[0])+f*len(locations[0])+i+1, end_node, weight=0) # 添加航班容量的限制 for f in range(num_flights): for i in range(len(locations[0])): G.add_edge(len(locations[0])+f*len(locations[0])+i+1, len(locations[0])+f*len(locations[0])+len(locations[0])+1, weight=-flight_capacity[f]) #创造路径规划模型 path_model = nx.DiGraph() for i in range(len(locations[0])): for f in range(num_flights): for j in range(len(locations[0])): if i != j: path_model.add_edge(len(locations[0])+f*len(locations[0])+i+1, len(locations[0])+f*len(locations[0])+j+1, weight=G[i+1][j+1]['weight']+G[len(locations[0])+f*len(locations[0])+i+1][len(locations[0])+f*len(locations[0])+j+1]['weight'])检查错误并修改

最新推荐

recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

matlab建立计算力学课程的笔记和文件.zip

matlab建立计算力学课程的笔记和文件.zip
recommend-type

FT-Prog-v3.12.38.643-FTD USB 工作模式设定及eprom读写

FT_Prog_v3.12.38.643--FTD USB 工作模式设定及eprom读写
recommend-type

matlab基于RRT和人工势场法混合算法的路径规划.zip

matlab基于RRT和人工势场法混合算法的路径规划.zip
recommend-type

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip

matlab基于matlab的两步定位软件定义接收机的开源GNSS直接位置估计插件模块.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。