2025年第十五届APMCM亚太地区大学生数学建模竞赛(中文赛项)C题(完整建模过程附python代码)

问题重述

 


 开始建模

📘 一、问题整体建模思路

🧩 问题一:数据预处理与弱分类器构建

目标:使用 Iris 数据集中前两类样本(Setosa 和 Versicolor)构建若干个弱分类器,并记录它们的预测结果与准确率。

  • 预处理:Z-score 标准化 + 标签转为 {-1, 1} + 划分训练/测试集

  • 弱分类器:构造基于“单特征 + 阈值规则”的 M 个分类器

  • 输出:训练集上的预测矩阵 H(M×N)与分类器准确率列表


🧩 问题二:QBoost建模与QUBO转化

目标:将弱分类器组合优化问题形式化为 QUBO 模型(Quadratic Unconstrained Binary Optimization)

  • 变量:每个弱分类器是否被选用 → 二进制变量 z_j ∈ {0,1}

  • 目标函数

L(z)=∥y−∑jzjhj∥2+λ∑jzj\mathcal{L}(\mathbf{z}) = \left\| \mathbf{y} - \sum_j z_j h_j \right\|^2 + \lambda \sum_j z_jL(z)=​y−j∑​zj​hj​​2+λj∑​zj​

  • 形式转换为

min⁡zTQz(由误差项+正则项组合而成)\min \mathbf{z}^T Q \mathbf{z} \quad \text{(由误差项+正则项组合而成)}minzTQz(由误差项+正则项组合而成)


🧩 问题三:使用Kaiwu SDK求解与评估模型

目标:调用 Kaiwu SDK 中的模拟退火求解器,求解最优 z,组合强分类器,并在测试集上进行评估。

  • 模型组合:选择被 z_j=1 的弱分类器,组合其预测求平均

  • 性能评估:在测试集上计算准确率(Accuracy)、混淆矩阵等

🧪 完整代码实现(问题1~3)

⚙️ 本代码包含了从头到尾的所有步骤;Kaiwu SDK 使用模拟模拟器(需配套接口)

 

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, classification_report
import matplotlib.pyplot as plt

# -------------------------
# 第1问:数据预处理与弱分类器构建
# -------------------------
print("🎯 问题一:数据处理与弱分类器生成")

# 1. 加载 Iris 数据集
iris = load_iris()
X = iris.data
y = iris.target

# 2. 筛选 Setosa 和 Versicolor 类(0 和 1)
X = X[y < 2]
y = y[y < 2]
y = np.where(y == 0, -1, 1)  # 标签转为 -1/1

# 3. 特征标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 4. 划分训练和测试集
X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, y, test_size=0.2, random_state=42
)

# 5. 构造弱分类器(单特征+多个阈值)
def generate_weak_classifiers(X_train, num_thresholds=5):
    n_samples, n_features = X_train.shape
    classifiers = []
    thresholds = np.linspace(-2.5, 2.5, num_thresholds)
    for feature_idx in range(n_features):
        for t in thresholds:
            def classifier(x, idx=feature_idx, threshold=t):
                return 1 if x[idx] > threshold else -1
            classifiers.append(classifier)
    return classifiers

weak_classifiers = generate_weak_classifiers(X_train, num_thresholds=5)

# 6. 获取预测矩阵 H 和每个分类器的准确率
def evaluate_classifiers(classifiers, X, y):
    M = len(classifiers)
    N = len(y)
    H = np.zeros((M, N))
    acc = []
    for j, clf in enumerate(classifiers):
        preds = np.array([clf(x) for x in X])
        H[j] = preds
        acc.append(np.mean(preds == y))
    return H, np.array(acc)

H_train, acc_train = evaluate_classifiers(weak_classifiers, X_train, y_train)
print("✅ 弱分类器数:", H_train.shape[0])

# -------------------------
# 第2问:构建 QUBO 模型
# -------------------------
print("🎯 问题二:QUBO 模型构建")

M, N = H_train.shape
lambda_reg = 0.1  # 正则化参数(可调)

# 构建 QUBO 矩阵
Q = np.zeros((M, M))
for j in range(M):
    for k in range(M):
        Q[j, k] = np.dot(H_train[j], H_train[k]) / (N ** 2)

linear_term = -2 * np.dot(H_train, y_train) / N + lambda_reg
for j in range(M):
    Q[j, j] += linear_term[j]

np.save("QUBO_matrix.npy", Q)
print("✅ QUBO 矩阵已保存。")

# -------------------------
# 第3问:模拟退火求解与评估
# -------------------------
print("🎯 问题三:模拟退火求解与评估")

# ✅ 模拟退火器(简化版实现)
def simulated_annealing(Q, max_iter=1000, temp=1.0, alpha=0.99):
    M = Q.shape[0]
    z = np.random.randint(0, 2, size=M)
    best_z = z.copy()
    best_cost = z.T @ Q @ z
    for _ in range(max_iter):
        j = np.random.randint(0, M)
        z_new = z.copy()
        z_new[j] = 1 - z_new[j]
        cost_new = z_new.T @ Q @ z_new
        delta = cost_new - best_cost
        if delta < 0 or np.exp(-delta / temp) > np.random.rand():
            z = z_new
            if cost_new < best_cost:
                best_cost = cost_new
                best_z = z.copy()
        temp *= alpha
    return best_z

# 求解最优组合 z
z_opt = simulated_annealing(Q)
selected_indices = np.where(z_opt == 1)[0]
print(f"✅ 选中的弱分类器数量: {len(selected_indices)}")

# 构建强分类器函数(多数投票/平均法)
def strong_classifier(x, classifiers, z):
    selected = [clf for i, clf in enumerate(classifiers) if z[i] == 1]
    if not selected:
        return -1  # 默认输出
    votes = [clf(x) for clf in selected]
    return 1 if np.mean(votes) > 0 else -1

# 测试集预测
y_pred = np.array([strong_classifier(x, weak_classifiers, z_opt) for x in X_test])
acc_test = accuracy_score(y_test, y_pred)

print(f"📊 测试集准确率: {acc_test:.4f}")
print(classification_report(y_test, y_pred, target_names=["Setosa", "Versicolor"]))

📦 输出内容说明

项目文件/变量内容
QUBO 矩阵QUBO_matrix.npy模型权重组合优化目标函数
模拟退火simulated_annealing()简易版模拟退火器(如需可替换为 Kaiwu SDK)
强分类器预测strong_classifier()投票机制构建最终模型
模型评估accuracy_score() + classification_report()测试集性能分析


🧠 后续建议(用于优化和扩展)

  • 使用 Kaiwu SDK 替换模拟退火器(按平台提供接口);

  • 加入更多特征组合弱分类器,如线性组合或决策树桩;

  • 使用 ROC 曲线、AUC 等指标扩展评估;

  • 尝试使用其他 QUBO 求解器(如 D-Wave、qubovert、neal)做实验对比。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值