name: linear-programming-solver description: 用于资源分配、调度和优化问题的线性规划求解技能 allowed-tools:
- Read
- Write
- Glob
- Grep
- Bash
metadata:
specialization: 决策智能
domain: 商业
category: 优化
priority: medium
shared-candidate: true
tools-libraries:
- pulp
- ortools
- pyomo
- cvxpy
线性规划求解器
概述
线性规划求解器技能提供了全面的线性优化问题建模和求解能力。它通过高效的求解器集成和解决方案分析,支持资源分配、生产计划、调度和其他商业优化挑战。
能力
- LP模型构建辅助
- 求解器集成(GLPK、CBC、CPLEX、Gurobi)
- 敏感性分析(影子价格、减少成本)
- 不可行性诊断
- 无界性检测
- 整数规划支持
- 多目标LP(目标规划)
- 解决方案解释
使用流程
- 规范性分析与优化
- 资源分配
- 供应链优化
使用方法
问题建模
# 定义LP问题
lp_problem = {
"name": "生产计划",
"sense": "maximize", # 或 "minimize"
"decision_variables": {
"product_A": {"type": "continuous", "lower_bound": 0, "upper_bound": 1000},
"product_B": {"type": "continuous", "lower_bound": 0, "upper_bound": 800},
"product_C": {"type": "integer", "lower_bound": 0} # 整数变量
},
"objective": {
"expression": "50*product_A + 40*product_B + 60*product_C",
"description": "最大化总利润"
},
"constraints": [
{
"name": "labor_hours",
"expression": "2*product_A + 3*product_B + 4*product_C <= 2400",
"description": "可用总工时"
},
{
"name": "machine_time",
"expression": "3*product_A + 2*product_B + 3*product_C <= 2000",
"description": "机器时间容量"
},
{
"name": "raw_material",
"expression": "product_A + product_B + product_C <= 1200",
"description": "原材料可用性"
},
{
"name": "demand_A",
"expression": "product_A >= 100",
"description": "产品A的最低需求"
}
]
}
求解器配置
# 求解器设置
solver_config = {
"solver": "CBC", # 或 "GLPK", "CPLEX", "GUROBI"
"time_limit": 300, # 秒
"mip_gap": 0.01, # MIP的1%最优性间隙
"threads": 4,
"presolve": True,
"cuts": "automatic"
}
敏感性分析
# 请求敏感性信息
sensitivity_config = {
"shadow_prices": True,
"reduced_costs": True,
"allowable_ranges": True,
"what_if": [
{"constraint": "labor_hours", "change": 100},
{"objective_coeff": "product_A", "change": 5}
]
}
常见LP问题类型
| 问题类型 | 目标 | 关键约束 |
|---|---|---|
| 生产计划 | 最大化利润 | 产能、需求 |
| 运输问题 | 最小化成本 | 供应、需求 |
| 分配问题 | 最小化成本/时间 | 一对一匹配 |
| 混合问题 | 最小化成本 | 质量标准、可用性 |
| 网络流 | 最小成本/最大流 | 流量平衡、容量 |
| 投资组合 | 最大化回报 | 风险、预算、分散化 |
输入模式
{
"problem_definition": {
"name": "string",
"sense": "maximize|minimize",
"decision_variables": "object",
"objective": {
"expression": "string",
"description": "string"
},
"constraints": ["object"]
},
"solver_config": {
"solver": "string",
"time_limit": "number",
"mip_gap": "number"
},
"analysis_options": {
"sensitivity": "boolean",
"what_if": ["object"],
"report_format": "string"
}
}
输出模式
{
"status": "Optimal|Infeasible|Unbounded|TimeLimit",
"objective_value": "number",
"solution": {
"variable_name": "number"
},
"sensitivity": {
"shadow_prices": {
"constraint_name": {
"value": "number",
"allowable_increase": "number",
"allowable_decrease": "number"
}
},
"reduced_costs": {
"variable_name": {
"value": "number",
"allowable_increase": "number",
"allowable_decrease": "number"
}
}
},
"infeasibility_analysis": {
"conflicting_constraints": ["string"],
"suggested_relaxations": ["object"]
},
"what_if_results": ["object"],
"solve_time": "number"
}
敏感性解释
| 指标 | 含义 | 用途 |
|---|---|---|
| 影子价格 | 约束放宽1单位带来的价值 | 优先考虑约束缓解 |
| 减少成本 | 强制非基本变量进入解决方案的成本 | 评估非最优替代方案 |
| 允许范围 | 基础保持最优的范围 | 评估解决方案的稳定性 |
最佳实践
- 使用简单测试用例验证模型构建
- 检查系数中的单位一致性
- 在手动调试前分析不可行性
- 使用影子价格指导资源获取
- 仅在必要时考虑整数规划(更难求解)
- 根据业务约束验证解决方案
- 清晰记录模型假设
不可行性处理
当模型不可行时:
- 识别不可约不可行子集(IIS)
- 使用弹性变量放宽约束
- 优先满足约束条件
- 对冲突目标使用目标规划
集成点
- 输入到优化专家代理
- 与敏感性分析器连接以增强鲁棒性
- 支持约束满足求解器处理混合问题
- 与决策可视化集成以显示解决方案