cumcm-b
数学建模国赛 B 题(优化/运筹/规划)解题方法。当题目涉及资源调度、路径规划、最优决策、选址、排班、分配、组合优化、生产决策、线性/整数/非线性/动态/多目标规划、图论、排队论时使用。核心套路:定义决策变量 → 构建目标函数 → 梳理约束 → 判定模型类型 → 求解 →…
от Chen-Jin-Han★ 24 звезды на GitHub
Установить cumcm-b в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
数模国赛 B 题:优化 / 运筹 / 规划
B 题核心是运筹优化:在可行域里找"最好"的方案。注意近年 B/C 边界模糊(2024 B=生产决策、C=农作物种植都是优化+不确定性),所以 B 题方法也可能出现在 C 题里,反之亦然。
一、通用解题流程(6 步)
- 审题:反复精读,明确"要决定什么"(决策变量的来源)
- 定义决策变量:题目要你"决定"的东西 →
x(选不选、放几个、走哪条、何时开始、测不测……) - 构建目标函数:最大利润 / 最小成本 / 最短时间 / 最小风险 / 最大收益
- 梳理约束条件:所有限制翻译成等式/不等式(资源上限、需求下限、逻辑关系、整数要求、0-1 互斥)
- 判定模型类型:线性?整数?0-1?非线性?动态?多目标?随机?
- 选择算法求解 + 灵敏度分析
二、规划模型五大家族
1. 线性规划(LP)
目标与约束都线性,最优解在可行域顶点。
from scipy.optimize import linprog
res = linprog(c, A_ub=A, b_ub=b, bounds=[(0, None)]*n, method="highs")
x_opt, f_opt = res.x, res.fun
2. 整数规划(IP)/ 0-1 规划
决策变量取整数;整数最优解不能由实数解简单取整。0-1:变量 ∈ {0,1}。
from scipy.optimize import milp, LinearConstraint, Bounds
import numpy as np
res = milp(c=c, integrality=np.ones(n), # 1=整数
constraints=LinearConstraint(A, ub=b), bounds=Bounds(0, 1))
更灵活的建模用 pulp(支持 0-1、混合整数、多目标转单目标):
import pulp
prob = pulp.LpProblem("p", pulp.LpMinimize)
x = [pulp.LpVariable(f"x{i}", cat="Binary") for i in range(n)] # Binary/Integer/Continuous
prob += pulp.lpSum(c[i]*x[i] for i in range(n)) # 目标
prob += pulp.lpSum(a[i]*x[i] for i in range(n)) <= b # 约束
prob.solve()
3. 非线性规划(NLP)
目标或约束含非线性,无通用解法。
from scipy.optimize import minimize
res = minimize(f, x0, method="SLSQP", constraints=cons, bounds=bnds) # 带约束
res = minimize(f, x0, method="L-BFGS-B", bounds=bnds) # 带界
4. 动态规划(DP)
按时间/阶段划分的多阶段决策;核心是状态转移方程 + 最优子结构。
dp = [0]*(n+1)
for j in range(1, n+1):
dp[j] = max(dp[i] + cost(i, j) for i in range(j)) # 一维,前缀和可优化 O(N²)→O(N)
# 多阶段二维:dp[k][j] = min_i dp[k-1][i] + (j-i)*R(...)
5. 多目标规划
转化为单目标:加权法 min Σ w_k f_k(x);序贯法(先优化第一目标,再在其最优集里优化第二);ε-约束法(其余目标转约束)。
三、现代优化算法(完整可运行代码)
原则:能精确解(LP/IP/DP)就别用启发式;启发式用于规模大/非凸/NP 难,且要跑多次取最好并说明随机性。
遗传算法 GA(离散 + 连续通用)
import numpy as np
def genetic_algorithm(fitness, n_vars, n_pop=100, n_gen=200,
bounds=(0, 1), integer=False, p_cross=0.8, p_mut=0.1):
"""fitness: 越大越好(求最小传-f);integer=True 表离散决策变量"""
lo, hi = bounds
rng = np.random.default_rng(42)
pop = rng.integers(lo, hi+1, (n_pop, n_vars)) if integer \
else rng.uniform(lo, hi, (n_pop, n_vars))
for _ in range(n_gen):
fit = np.array([fitness(ind) for ind in pop])
prob = fit - fit.min() + 1e-9; prob /= prob.sum() # 轮盘赌
parents = pop[rng.choice(n_pop, n_pop, p=prob)]
child = parents.copy()
for i in range(0, n_pop, 2):
if rng.random() < p_cross:
k = rng.integers(1, n_vars)
child[i, k:], child[i+1, k:] = child[i+1, k:].copy(), child[i, k:].copy()
mask = rng.random((n_pop, n_vars)) < p_mut
child[mask] = rng.integers(lo, hi+1, mask.sum()) if integer \
else rng.uniform(lo, hi, mask.sum())
pop = child
fit = np.array([fitness(ind) for ind in pop])
return pop[np.argmax(fit)], fit.max()
模拟退火 SA(连续优化,跳出局部最优)
def simulated_annealing(f, x0, bounds, T0=1000., T_min=1e-3, alpha=0.95, n_iter=200):
"""f: 求最小;bounds: [(lo,hi),...]"""
rng = np.random.default_rng(42)
lo = np.array([b[0] for b in bounds]); hi = np.array([b[1] for b in bounds])
x = np.array(x0, float); T = T0
best_x, best_f = x.copy(), f(x)
while T > T_min:
for _ in range(n_iter):
xn = np.clip(x + rng.normal(0, T/100, x.shape), lo, hi)
d = f(xn) - f(x)
if d < 0 or rng.random() < np.exp(-d/T): # 接受劣解概率
x = xn
if f(x) < best_f: best_x, best_f = x.copy(), f(x)
T *= alpha
return best_x, best_f
粒子群 PSO(连续优化,收敛快)
def pso(f, n_vars, bounds, n_p=50, n_iter=200, w=0.7, c1=1.5, c2=1.5):
rng = np.random.default_rng(42)
lo = np.array([b[0] for b in bounds]); hi = np.array([b[1] for b in bounds])
X = rng.uniform(lo, hi, (n_p, n_vars)); V = rng.uniform(-1, 1, (n_p, n_vars))
pb = X.copy(); pbf = np.array([f(x) for x in X])
gb = pb[np.argmin(pbf)]; gbf = pbf.min()
for _ in range(n_iter):
V = w*V + c1*rng.random((n_p,n_vars))*(pb-X) + c2*rng.random((n_p,n_vars))*(gb-X)
X = np.clip(X + V, lo, hi)
fv = np.array([f(x) for x in X])
m = fv < pbf; pb[m] = X[m]; pbf[m] = fv[m]
if fv.min() < gbf: gbf = fv.min(); gb = X[np.argmin(fv)]
return gb, gbf
也可直接用
scipy.optimize.dual_annealing(退火)、differential_evolution(差分进化)。
四、常见 B 题问题模式(建模公式速查)
| 问题模式 | 决策变量 | 目标 | 典型约束 | 解法 |
|---|---|---|---|---|
| 选址 p-median | x_ij 是否服务,y_j 是否建 | min ΣΣ d_ij x_ij | Σx_ij=1,Σy_j=p | 整数规划 / 启发式 |
| 覆盖选址 | y_j 是否建 | max 覆盖需求 / min 建点数 | 覆盖半径约束 | 0-1 规划 |
| TSP 旅行商 | x_ij 是否走 i→j | min Σ d_ij x_ij | 每点进出各一次,消子环 | 精确(小)/遗传·蚁群 |
| VRP 车辆路径 | x_ijv 车辆 v 走 i→j | min 总里程 | 容量、时间窗、车辆数 | 启发式/邻域搜索 |
| 指派/分配 | x_ij 0-1 | min Σ c_ij x_ij | 每行每列恰一个 1 | 匈牙利 `linear |
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл cumcm-b?
数学建模国赛 B 题(优化/运筹/规划)解题方法。当题目涉及资源调度、路径规划、最优决策、选址、排班、分配、组合优化、生产决策、线性/整数/非线性/动态/多目标规划、图论、排队论时使用。核心套路:定义决策变量 → 构建目标函数 → 梳理约束 → 判定模型类型 → 求解 → 灵敏度分析。内含完整遗传/退火/粒子群代码与常见问题模式。
Как установить скилл cumcm-b?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл cumcm-b запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
AnthropicToolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
access
AnthropicManage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
configure
AnthropicSet up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
access
AnthropicManage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…