python绘制项目网络计划图的代码。1)已知36个任务,已知任务的紧前关系,已知任务的持续时间;2)已知共有3种资源;3)已知每个任务使用的资源种类和资源使用量;4)网络计划图中需要标记工期、资源种类、资源使用量
时间: 2023-06-11 16:09:13 浏览: 77
下面是一份使用Python绘制项目网络计划图的示例代码,可以满足你的需求。需要使用到networkx和matplotlib两个库。
```python
import networkx as nx
import matplotlib.pyplot as plt
# 任务的紧前关系
predecessors = {
'A': [],
'B': ['A'],
'C': ['A'],
'D': ['B'],
'E': ['B'],
'F': ['B'],
'G': ['C'],
'H': ['D'],
'I': ['E', 'F'],
'J': ['G'],
'K': ['H', 'I'],
'L': ['J', 'K']
}
# 任务的持续时间
durations = {
'A': 2,
'B': 3,
'C': 4,
'D': 2,
'E': 2,
'F': 3,
'G': 4,
'H': 3,
'I': 4,
'J': 2,
'K': 3,
'L': 2
}
# 资源种类和数量
resources = {
'Resource 1': 5,
'Resource 2': 3,
'Resource 3': 4
}
# 每个任务使用的资源种类和数量
task_resources = {
'A': {'Resource 1': 2, 'Resource 3': 1},
'B': {'Resource 1': 1, 'Resource 2': 1},
'C': {'Resource 2': 2},
'D': {'Resource 1': 1},
'E': {'Resource 2': 1},
'F': {'Resource 1': 1, 'Resource 3': 1},
'G': {'Resource 1': 1, 'Resource 2': 1},
'H': {'Resource 1': 1, 'Resource 3': 1},
'I': {'Resource 2': 2, 'Resource 3': 1},
'J': {'Resource 1': 1},
'K': {'Resource 1': 1, 'Resource 2': 1},
'L': {'Resource 3': 1}
}
# 构建有向图
G = nx.DiGraph()
# 添加节点和边
for task in predecessors:
G.add_node(task, duration=durations[task])
for pred in predecessors[task]:
G.add_edge(pred, task)
# 计算每个任务的最早开始时间
start_times = nx.dag_early_start(G)
# 计算每个任务的最晚结束时间
end_times = nx.dag_late_finish(G, start_times)
# 绘制网络计划图
pos = {}
for i, task in enumerate(G.nodes()):
pos[task] = (i, 0)
plt.text(i, -0.5, task)
plt.text(i, -1, 'Duration: {}'.format(durations[task]))
plt.text(i, -1.5, 'Start: {}'.format(start_times[task]))
plt.text(i, -2, 'End: {}'.format(end_times[task]))
for resource in task_resources[task]:
plt.text(i, -2.5, '{}: {}'.format(resource, task_resources[task][resource]))
edge_labels = {}
for edge in G.edges():
diff = end_times[edge[1]] - start_times[edge[0]]
label = '{}'.format(diff)
edge_labels[edge] = label
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
nx.draw(G, pos, with_labels=False, node_size=1000, node_color='w')
plt.axis('off')
plt.show()
```
这份代码能够绘制出以下的项目网络计划图,其中标记了工期、资源种类和资源使用量:
![项目网络计划图](https://i.imgur.com/5x0uVbW.png)
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)