欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 2024数学建模国赛B题代码

2024数学建模国赛B题代码

2024/10/25 20:22:01 来源:https://blog.csdn.net/zzzzzzzxxaaa/article/details/141976335  浏览:    关键词:2024数学建模国赛B题代码

B题已经完成模型代码!详情查看文末名片

问题1:可以考虑使用统计学中的“样本量估算”方法,使用二项分布或正态近似来决定最少的样本量,并通过假设检验(如单侧检验)在95%和90%置信度下进行判断。

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt# 参数设置
p_0 = 0.10  # 标称次品率(供应商声称)
confidence_level_95 = 0.95  # 问题 (1) 的置信水平
confidence_level_90 = 0.90  # 问题 (2) 的置信水平
margin_of_error = 0.05  # 误差限# 计算Z值
Z_95 = stats.norm.ppf((1 + confidence_level_95) / 2)  # 95%置信区间
Z_90 = stats.norm.ppf((1 + confidence_level_90) / 2)  # 90%置信区间# 样本量估算公式
def sample_size(Z, p, E):"""根据Z值,次品率p,误差限E计算最少样本量"""return (Z**2 * p * (1 - p)) / (E**2)# 计算95%和90%置信度下的最少样本量
n_95 = sample_size(Z_95, p_0, margin_of_error)
n_90 = sample_size(Z_90, p_0, margin_of_error)print(f"95%置信水平下的最少样本量: {int(np.ceil(n_95))}")
print(f"90%置信水平下的最少样本量: {int(np.ceil(n_90))}")# 抽样假设检验
def hypothesis_test(p_0, n, x, confidence_level):"""根据样本量n,抽样检测到的次品数量x,以及置信水平,计算置信区间p_0: 标称次品率n: 样本量x: 次品数量confidence_level: 置信水平"""p_hat = x / n  # 样本次品率Z = stats.norm.ppf((1 + confidence_level) / 2)margin = Z * np.sqrt((p_hat * (1 - p_hat)) / n)lower_bound = p_hat - marginupper_bound = p_hat + marginreturn lower_bound, upper_bound# 模拟抽样检测
np.random.seed(42)  # 固定随机种子
n_sample = int(np.ceil(n_95))  # 使用95%置信水平下的样本量
true_defect_rate = 0.12  # 假设实际次品率为12%
sample_defects = np.random.binomial(n_sample, true_defect_rate)  # 抽样检测出的次品数量# 进行95%置信水平的假设检验
lower_95, upper_95 = hypothesis_test(p_0, n_sample, sample_defects, confidence_level_95)
# 进行90%置信水平的假设检验
lower_90, upper_90 = hypothesis_test(p_0, n_sample, sample_defects, confidence_level_90)# 打印检测结果
print(f"抽样检测得到的次品数量: {sample_defects}/{n_sample}")
print(f"95%置信区间: [{lower_95:.3f}, {upper_95:.3f}]")
print(f"90%置信区间: [{lower_90:.3f}, {upper_90:.3f}]")
完整代码:https://mbd.pub/o/bread/mbd-ZpqZl55p

问题2:基于零配件的次品率和成本数据,建立一个决策树模型或者动态规划模型,分析在每个阶段是否检测、是否拆解能使企业的总成本最小化。重点在于计算检测成本、拆解费用、调换损失等对整体利润的影响。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager# 使用SimHei字体来支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']  # 或者你系统支持的中文字体
plt.rcParams['axes.unicode_minus'] = False# 定义输入参数
# 情况1的数据
p1_defect_rate = 0.10  # 零配件1的次品率
p2_defect_rate = 0.10  # 零配件2的次品率
product_defect_rate = 0.10  # 成品的次品率
purchase_cost1 = 4  # 零配件1的购买单价
purchase_cost2 = 18  # 零配件2的购买单价
assembly_cost = 6  # 装配成本
market_price = 56  # 市场售价
replacement_loss = 6  # 调换损失
dismantling_cost = 5  # 拆解费用# 检测成本
detection_cost1 = 2  # 零配件1的检测成本
detection_cost2 = 3  # 零配件2的检测成本
product_detection_cost = 3  # 成品的检测成本# 决策1: 是否对零配件进行检测
def part_detection_decision(p_defect, detection_cost, purchase_cost):"""决定是否对零配件进行检测,基于检测成本和次品率如果检测成本低于购买并丢弃次品的期望损失,则选择检测"""expected_defect_loss = p_defect * purchase_cost  # 不检测时的期望损失if detection_cost < expected_defect_loss:return True  # 检测else:return False  # 不检测# 决策2: 是否对成品进行检测
def product_detection_decision(p_defect, detection_cost, market_price, replacement_loss):"""决定是否对成品进行检测,基于检测成本和退货损失如果检测成本低于次品退货的期望损失,则选择检测"""
完整代码:https://mbd.pub/o/bread/mbd-ZpqZl55p

问题3:扩展模型,考虑多道工序的情形。可以将每道工序看作一个子系统,递归地分析各个阶段的次品率对最终成品质量的影响,并提出最优的检测方案。

import numpy as np
import matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['SimHei']  # 中文字体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题# 决策函数:是否检测零配件
def part_detection_decision(p_defect, detection_cost, purchase_cost):"""根据次品率和检测成本,决定是否检测零配件。"""expected_defect_loss = p_defect * purchase_cost  # 不检测的期望次品损失detect = detection_cost < expected_defect_loss  # 如果检测成本低于次品损失,则检测decision = "检测" if detect else "不检测"return detect, decision# 决策函数:是否检测半成品/成品
def product_detection_decision(p_defect, detection_cost, replacement_loss):"""根据次品率和检测成本,决定是否检测半成品或成品。"""expected_defect_loss = p_defect * replacement_loss  # 不检测的期望次品损失detect = detection_cost < expected_defect_loss  # 如果检测成本低于次品损失,则检测decision = "检测" if detect else "不检测"return detect, decision# 工序中次品率的递进计算
def calculate_defect_rate(p_list):"""计算多个零配件组合后的次品率(使用联合概率公式)。p_list: 各零配件的次品率列表"""combined_defect_rate = 1 - np.prod([1 - p for p in p_list])  # 联合次品率return combined_defect_rate# 计算总成本,并输出决策依据
def total_cost(steps, dismantling_cost, replacement_loss):"""计算总期望成本,并输出决策依据。"""total_parts_cost = 0total_assembly_cost = 0total_product_cost = 0previous_defect_rate = 0  # 初始时为0,没有前序工序
完整代码:https://mbd.pub/o/bread/mbd-ZpqZl55p

问题4:结合问题1的抽样检测方法,重新优化问题2和问题3中的方案,确保抽样检测得到的次品率可以指导后续的决策。

import numpy as np
import scipy.stats as stats# 抽样次品率估计函数
def estimate_defect_rate(sample_size, defect_count):"""使用抽样检测方法估算次品率。sample_size: 样本量defect_count: 检测到的次品数"""return defect_count / sample_size# 决策函数:是否检测零配件
def part_detection_decision(p_defect, detection_cost, purchase_cost):"""根据估算次品率和检测成本,决定是否检测零配件。"""expected_defect_loss = p_defect * purchase_cost  # 不检测的期望次品损失detect = detection_cost < expected_defect_loss  # 如果检测成本低于次品损失,则检测decision = "检测" if detect else "不检测"return detect, decision# 决策函数:是否检测成品
def product_detection_decision(p_defect, detection_cost, replacement_loss):"""根据估算次品率和检测成本,决定是否检测成品。p_defect: 成品的次品率detection_cost: 检测成品的成本replacement_loss: 成品退货的损失"""expected_defect_loss = p_defect * replacement_loss  # 不检测的期望退货损失detect = detection_cost < expected_defect_loss  # 如果检测成本低于退货损失,则检测decision = "检测" if detect else "不检测"return detect, decision# 工序中次品率的递进计算
def calculate_defect_rate(p_list):"""计算多个零配件组合后的次品率(使用联合概率公式)。
完整代码:https://mbd.pub/o/bread/mbd-ZpqZl55p

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com