Python线性规划

Python线性规划 # Python线性规划 - 完整代码示例# 线性规划在约束条件下优化线性目标函数广泛应用于运筹学import numpy as npfrom scipy import optimize# 1. 基础线性规划scipy.optimize.linprog# 标准形式: min c^T x, s.t. A_ub x b_ub, A_eq x b_eq, bounds x# 注意linprog 默认求最小值约束为 # 示例 1生产计划优化# 某工厂生产两种产品 A 和 B# 目标最大化利润 40*A 30*B# 约束# 原材料 1: 2A 1B 100# 原材料 2: 1A 2B 80# A 0, B 0# 转为最小化min -(40*A 30*B)c [-40, -30] # 目标函数系数负号转换求最大# 不等式约束 A_ub x b_ubA_ub [[2, 1],[1, 2]]b_ub [100, 80]# 变量边界bounds [(0, None), (0, None)] # A 0, B 0result optimize.linprog(c, A_ubA_ub, b_ubb_ub, boundsbounds, methodhighs)print(生产计划优化问题:)print(f优化状态: {result.message})print(f产品 A 产量: {result.x[0]:.2f})print(f产品 B 产量: {result.x[1]:.2f})print(f最大利润: {-result.fun:.2f})# 2. 含等式约束的线性规划# 示例 2投资组合分配# 总投资 100 万元三种投资选项# 目标最小化风险 5*A 4*B 6*C# 约束# A B C 100 (总投资额)# B 20 (固定收益最低配置)# A 40 (某类资产上限)# A 0, C 0c_risk [5, 4, 6] # 风险系数# 等式约束 A_eq x b_eqA_eq [[1, 1, 1]]b_eq [100]# 不等式约束A_ub_risk [[0, -1, 0], # -B -20 B 20[1, 0, 0]] # A 40b_ub_risk [-20, 40]bounds_risk [(0, None), (None, None), (0, None)]result_risk optimize.linprog(c_risk, A_ubA_ub_risk, b_ubb_ub_risk,A_eqA_eq, b_eqb_eq, boundsbounds_risk,methodhighs)print(f\n投资组合优化问题:)print(f优化状态: {result_risk.message})print(f资产 A: {result_risk.x[0]:.2f} 万元)print(f资产 B: {result_risk.x[1]:.2f} 万元)print(f资产 C: {result_risk.x[2]:.2f} 万元)print(f最小风险值: {result_risk.fun:.2f})# 3. 运输问题# 两个仓库向三个客户配送最小化运输成本# 仓库容量: W150, W260# 客户需求: C130, C240, C340# 运输成本矩阵:# C1 C2 C3# W1 2 4 5# W2 3 1 6# 决策变量: x11, x12, x13, x21, x22, x23c_transport [2, 4, 5, 3, 1, 6] # 成本向量化# 供应约束每个仓库的总出货量 容量A_supply [[1, 1, 1, 0, 0, 0], # W1[0, 0, 0, 1, 1, 1]] # W2b_supply [50, 60]# 需求约束每个客户的总接收量 需求A_demand [[-1, 0, 0, -1, 0, 0], # C1 (负号因 linprog 用 )[0, -1, 0, 0, -1, 0], # C2[0, 0, -1, 0, 0, -1]] # C3b_demand [-30, -40, -40]A_ub_trans A_supply A_demand # 合并所有不等式b_ub_trans b_supply b_demandbounds_trans [(0, None)] * 6 # 所有变量非负result_trans optimize.linprog(c_transport, A_ubA_ub_trans, b_ubb_ub_trans,boundsbounds_trans, methodhighs)print(f\n运输优化问题:)print(f优化状态: {result_trans.message})if result_trans.success:x result_trans.xprint(fW1-C1: {x[0]:.0f}, W1-C2: {x[1]:.0f}, W1-C3: {x[2]:.0f})print(fW2-C1: {x[3]:.0f}, W2-C2: {x[4]:.0f}, W2-C3: {x[5]:.0f})print(f最小运输成本: {result_trans.fun:.2f})# 4. 灵敏度分析观察约束变化对最优解的影响# 分析原材料 1 的约束放宽对利润的影响print(f\n灵敏度分析 - 原材料 1 约束影响:)for rhs in [80, 90, 100, 110, 120]:result_sens optimize.linprog(c, A_ubA_ub, b_ub[rhs, 80], boundsbounds, methodhighs)if result_sens.success:print(f 原材料 1 {rhs:3d}: 利润 {-result_sens.fun:7.2f} (A{result_sens.x[0]:.2f}, B{result_sens.x[1]:.2f}))# 5. 不同求解方法对比print(f\n不同求解器对比:)methods [highs, highs-ds, highs-ipm, interior-point]for method in methods:try:res optimize.linprog(c, A_ubA_ub, b_ubb_ub, boundsbounds, methodmethod)print(f {method:15s}: 状态{res.status}, 目标值{-res.fun:.2f}, 迭代{res.nit if hasattr(res, nit) else N/A})except Exception as e:print(f {method:15s}: 失败 - {str(e)[:30]})# 6. 混合整数线性规划使用 pulp 模拟仅演示思路# 如果某些变量必须是整数可以使用整数规划# 此处用四舍五入展示整数解与连续解的差异print(f\n整数解近似 (连续解取整):)x_cont result.xx_int np.round(x_cont).astype(int)# 验证整数解的可行性constraints_satisfied (x_int[0] 0 and x_int[1] 0 and2*x_int[0] x_int[1] 100 andx_int[0] 2*x_int[1] 80)profit_int 40 * x_int[0] 30 * x_int[1]print(f 连续解: A{x_cont[0]:.2f}, B{x_cont[1]:.2f}, 利润{-result.fun:.2f})print(f 整数近似: A{x_int[0]}, B{x_int[1]}, 利润{profit_int}, 可行{constraints_satisfied})# 7. 多目标线性规划权重法# 将多个目标加权求和转化为单目标# 同时考虑利润最大化和风险最小化profit_coeff [40, 30]risk_coeff [5, 4] # 简化风险系数# 组合目标: max (profit - lambda * risk)lam 0.1 # 风险厌恶系数combined_c [-(profit_coeff[0] - lam * risk_coeff[0]),-(profit_coeff[1] - lam * risk_coeff[1])]result_combined optimize.linprog(combined_c, A_ubA_ub, b_ubb_ub,boundsbounds, methodhighs)profit_val 40 * result_combined.x[0] 30 * result_combined.x[1]risk_val 5 * result_combined.x[0] 4 * result_combined.x[1]print(f\n多目标优化 (λ{lam}):)print(f A{result_combined.x[0]:.2f}, B{result_combined.x[1]:.2f})print(f 利润{profit_val:.2f}, 风险{risk_val:.2f})print(\n线性规划总结linprog 提供高效的大规模线性规划求解)print(关键在于将实际问题转化为标准形式并理解对偶变量影子价格)