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 - margin
    upper_bound = p_hat + margin
    return 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 plt

plt.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 = 0
    total_assembly_cost = 0
    total_product_cost = 0

    previous_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
相关推荐
2601_9539880712 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·后端·物联网·tcp/ip·数学建模·前端框架
数模竞赛Paid answer1 天前
2026年华东杯数学建模C题收益率预测问题解题全过程文档及程序
算法·数学建模·数据分析·华东杯
数模竞赛Paid answer2 天前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
AI Dog2 天前
MathHub:数学建模学习社区,内置数学建模智能体Modulus
学习·数学建模·智能体·modulus·数学建模智能体
统计学小王子3 天前
分类模型评价指标——R语言(数学建模常用)
数学建模·分类·r语言
数模竞赛Paid answer3 天前
2026年华东杯数学建模B题医药物流安排问题解题全过程文档及程序
算法·数学建模·数据分析·华东杯
数模竞赛Paid answer3 天前
2020年深圳杯数学建模A题关于国家“先行示范区”建设中的医疗和养老保障问题解题全过程论文及程序
算法·数学建模·数据分析·深圳杯
数模竞赛Paid answer3 天前
2021年深圳杯数学建模C题配电网可靠性和故障软自愈研究解题全过程论文及程序
算法·数学建模·数据分析·深圳杯
数模竞赛Paid answer4 天前
2026年华数杯数学建模A题微构体中填充导电介质的仿真优化解题全过程及程序
数学建模·数据分析·华数杯
数模竞赛Paid answer4 天前
2021年深圳杯数学建模B题背向瑞利散射光纤激光传感器设计解题全过程论文及程序
数学建模·数据分析·深圳杯