当前位置: 首页 > news >正文

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 探索非局部均值滤波在椒盐噪声去除中的应用
  • C# 删除Word文档中的段落
  • uniapp写的一个年月日时分秒时间选择功能
  • Linux-进程管理【重点】
  • elementUI之不会用
  • 《C Primer Plus》第 11 章复习题和编程练习
  • STM32 RTC实时时钟
  • 如何看待IBM中国研发部裁员!
  • 惩罚矩阵?动态规划是如何爱上矩阵的
  • rman 备份尽量使用 backup database plus archivelog
  • 数据库进阶:2.索引
  • 【 html+css 绚丽Loading 】 000045 太极旋流轮
  • 【SpringCloud】 实现负载均衡
  • 【C++ 09】继承
  • Path系统环境变量和CLASSPATH环境变量
  • css属性的继承、初识值、计算值、当前值、应用值
  • flask接收请求并推入栈
  • js继承的实现方法
  • OSS Web直传 (文件图片)
  • Python爬虫--- 1.3 BS4库的解析器
  • React16时代,该用什么姿势写 React ?
  • SAP云平台里Global Account和Sub Account的关系
  • Service Worker
  • vue-loader 源码解析系列之 selector
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • (11)MSP430F5529 定时器B
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (bean配置类的注解开发)学习Spring的第十三天
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (一) storm的集群安装与配置
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .Net core 6.0 升8.0
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .net Signalr 使用笔记
  • .net中的Queue和Stack
  • .pyc文件是什么?
  • ?php echo ?,?php echo Hello world!;?
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [.net] 如何在mail的加入正文显示图片
  • [ASP]青辰网络考试管理系统NES X3.5
  • [BZOJ]4817: [Sdoi2017]树点涂色
  • [C# 基础知识系列]专题十六:Linq介绍
  • [C#]winform使用onnxruntime部署LYT-Net轻量级低光图像增强算法
  • [CSS]CSS 字体属性
  • [excel与dict] python 读取excel内容并放入字典、将字典内容写入 excel文件