运筹学练习Python精解——网络计划技术

news/2024/10/7 20:34:15

练习1

某新产品研制项目的各项工序、所需时间及相互关系如下表所示,试画出该项目的网络图,分别用和双代号、单代号方法以及表上计算法计算事项时间参数,并求出关键路线。

工序 工序代号 所需时间 紧后工序
产品及工艺设计 A 60 B, C, D, E
外购配套件 B 45 K
下料、锻件 C 10 F
工装制造1 D 20 G, H
木模、铸件 E 40 H
机械加工1 F 18 K
工装制造2 G 30 I
机械加工2 H 15 K
机械加工3 I 25 K
装配调试 K 35 -

1.1 绘制网络计划图

1.2 Python求解

#网络计划的最长路算法
import networkx as nx# Define the edges with the new data structure
edges = {'A': {'nodes': ('1', '2'), 'weight': 60},'B': {'nodes': ('2', '7'), 'weight': 45},'C': {'nodes': ('2', '3'), 'weight': 10},'D': {'nodes': ('2', '4'), 'weight': 20},'E': {'nodes': ('2', '5'), 'weight': 40},'F': {'nodes': ('3', '7'), 'weight': 18},'G': {'nodes': ('4', '6'), 'weight': 30},'L': {'nodes': ('4', '5'), 'weight': 0},'H': {'nodes': ('5', '7'), 'weight': 15},'I': {'nodes': ('6', '7'), 'weight': 25},'K': {'nodes': ('7', '8'), 'weight': 35}
}# Initialize a directed graph
G = nx.DiGraph()# Add edges to the graph using the new data structure
for edge_name, edge_data in edges.items():start, end = edge_data['nodes']weight = edge_data['weight']G.add_edge(start, end, weight=weight, name=edge_name)# Compute the longest path using the networkx library
length, path = nx.algorithms.dag.dag_longest_path_length(G, weight='weight'), nx.algorithms.dag.dag_longest_path(G, weight='weight')# Extract the names of the edges in the critical path
critical_path_edges = []
for i in range(len(path) - 1):critical_path_edges.append(G[path[i]][path[i + 1]]['name'])# Format the critical path output
formatted_critical_path = " -> ".join(critical_path_edges) + "."print(f"Length of the critical path: {length}")
print(f"Critical path nodes: {path}")
print(f"Critical path edges: {formatted_critical_path}")
Length of the critical path: 170
Critical path nodes: ['1', '2', '4', '6', '7', '8']
Critical path edges: A -> D -> G -> I -> K.

练习2

import networkx as nx
import matplotlib.pyplot as plt
from matplotlib import rcParams# 设置字体
rcParams['font.sans-serif'] = ['Arial']  # 使用Arial字体
rcParams['axes.unicode_minus'] = False  # 解决坐标轴负号显示问题# 定义任务、紧前任务和任务时间
tasks = {'A': {'pre': ['G', 'M'], 'time': 3},'B': {'pre': ['H'], 'time': 4},'C': {'pre': [], 'time': 7},'D': {'pre': ['L'], 'time': 3},'E': {'pre': ['C'], 'time': 5},'F': {'pre': ['A', 'E'], 'time': 5},'G': {'pre': ['B', 'C'], 'time': 2},'H': {'pre': [], 'time': 5},'I': {'pre': ['A', 'L'], 'time': 2},'K': {'pre': ['F', 'L'], 'time': 1},'L': {'pre': ['B', 'C'], 'time': 7},'M': {'pre': ['C'], 'time': 9}
}# 构建有向图
G = nx.DiGraph()# 添加任务节点和时间
for task, details in tasks.items():G.add_node(task, time=details['time'])# 添加任务依赖关系
for task, details in tasks.items():for pre in details['pre']:G.add_edge(pre, task)# 计算最早开始时间和最晚完成时间
def calculate_earliest_latest_times(G):earliest_start = {}latest_finish = {}for node in nx.topological_sort(G):es = max([earliest_start.get(pred, 0) + G.nodes[pred]['time'] for pred in G.predecessors(node)], default=0)earliest_start[node] = esfor node in reversed(list(nx.topological_sort(G))):lf = min([latest_finish.get(succ, float('inf')) - G.nodes[node]['time'] for succ in G.successors(node)], default=earliest_start[node] + G.nodes[node]['time'])latest_finish[node] = lfreturn earliest_start, latest_finish# 计算关键路径
def calculate_critical_path(G, earliest_start, latest_finish):critical_path = []for node in nx.topological_sort(G):if earliest_start[node] == latest_finish[node] - G.nodes[node]['time']:critical_path.append(node)return critical_path# 计算并显示结果
earliest_start, latest_finish = calculate_earliest_latest_times(G)
critical_path = calculate_critical_path(G, earliest_start, latest_finish)print("Earliest Start Times:", earliest_start)
print("Latest Finish Times:", latest_finish)
print("Critical Path:", critical_path)# 按时间线组织节点布局
pos = {}
layer = 0
for node in nx.topological_sort(G):pos[node] = (earliest_start[node], layer)layer += 1plt.figure(figsize=(12, 8))# 绘制所有节点和边
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=2000, font_size=10, font_weight='bold', arrowsize=20, font_family='sans-serif')# 添加节点标签
node_labels = {node: f"{node}\n{G.nodes[node]['time']} days" for node in G.nodes}
nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=10, font_family='sans-serif')# 添加边标签
edge_labels = {(pre, succ): f"ES: {earliest_start[succ]}" for pre, succ in G.edges}
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color='red', font_size=8, font_family='sans-serif')# 高亮关键路径
critical_edges = [(critical_path[i], critical_path[i + 1]) for i in range(len(critical_path) - 1)]
nx.draw_networkx_edges(G, pos, edgelist=critical_edges, edge_color='r', width=2)plt.title("Network Diagram")
plt.show()
Length of the critical path: 170
Critical path nodes: ['1', '2', '4', '6', '7', '8']
Critical path edges: A -> D -> G -> I -> K.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/42712.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

树上前缀和与差分

树上前缀和 设 \(sum_i\) 表示根节点到节点 \(i\) 的权值总和。 则有:对于点权,\(x,y\) 路径上的和为 \(sum_x + sum_y - sum_{lca} - sum_{fa_{lca}}\)。 对于边权,\(x,y\) 路径上的和为 \(sum_x + sum_y - 2 \times sum_{lca}\)。习题:P4427 [BJOI2018] 求和解题思路 预处…

SynProject 介绍---(synopse理解的版本控制和文档自动化生成)

SynProject 介绍---(synopse理解的版本控制和文档自动化生成) Synopse SynProject是一个用于Delphi项目的源代码版本控制和自动化文档生成的开源应用程序。它在GPL许可下发布。 有关其全部功能的完整列表,请参阅SynProject功能。源代码可从本源代码存储库获取。请选择上方的…

品牌全域人群资产增长飞轮

品牌人群资产是指品牌通过一系列营销活动和用户互动所积累的、对品牌有一定认知、情感和忠诚度的用户群体。这些资产是品牌的无形资产,对于品牌的长期成功和市场份额的扩张至关重要。 品牌人群资产可以模拟成一个多层次、多维度的球形结构体,其中核心用户群位于中心,随着用户…

TCP传输控制协议

网络编程 TCP传输控制协议 传输层中最为常见的两个协议分别是传输控制协议TCP(Transmission Control Protocol)和用户数据报协议UDP(User Datagram Protocol) 一、TCP协议的特点:TCP是面向连接的,端对端(一对一)的可靠的协议,可修复损坏数据,全双工的连接。 1.TCP协议判…

NPU与超异构计算杂谈

NPU与超异构计算杂谈 NPU 基础 近年来,随着人工智能技术的飞速发展,AI 专用处理器如 NPU(Neural Processing Unit)和 TPU(Tensor Processing Unit)也应运而生。这些处理器旨在加速深度学习和机器学习任务,相比传统的 CPU 和 GPU,它们在处理 AI 任务时表现出更高的效率和…

植物大战僵尸杂交版 官方版(作者:b站潜艇伟伟迷) 植物大战僵尸杂交版v2.0.88安装程序.exe

植物大战僵尸杂交版 官方版(作者:b站潜艇伟伟迷) 植物大战僵尸杂交版v2.0.88安装程序.exe阿里云盘链接: https://www.alipan.com/s/HgmmLFjHzj7 提取码: 15yc 【点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。】夸克网盘…

网络编程知识点

网络编程 两台主机的进程实现通信的方式 同一台主机中的实现进程间通信的方式有很多,比如管道、信号、消息队列、信号量集、共享内存等,如果现在需要两台主机间的进程实现数据传输,则想要用到套接字文件(socket)的,它的作用则是用于实现不同主机中的进程间通信的。 IP协议 …

MySQL存储

MySQL存储关系型数据库是基于关系模型的数据库,而关系模型是通过二维表来保存的,所以它的存储方式就是行列组成的表,每一列是一个字段,每一行是一条记录。表可以看作某个实体的集合,实体之间存在联系需要表与表之间的关联关系来体现,如主键外键的关联关系。多个表组成一个…