使用Pyomo求解带存储约束的线性规划(LP)运输问题
运输问题新增存储约束的Pyomo实现方案
核心逻辑梳理
加入存储约束后,核心要解决节点(工厂/仓库/需求点)的流入、流出与存储量的动态平衡,核心关系为:
- 当期期初存储量 + 当期流入量 = 当期流出量 + 当期期末存储量
- 同时需配套存储容量上限、期初/期末存储边界(比如给定初始存储、期末最低存储要求等)约束
关键约束的Pyomo代码示例
假设模型包含以下基础集合与变量:
model.F:工厂集合model.W:仓库集合model.C:客户集合model.T:时间周期集合(多周期场景)model.x[f, w, t]:t周期工厂f运往仓库w的运输量model.y[w, c, t]:t周期仓库w运往客户c的运输量model.s[w, t]:t周期末仓库w的存储量
1. 仓库流量平衡约束
def storage_balance_rule(model, w, t): # 期初存储:首周期用初始值,其余周期用上一期末存储 initial_s = model.init_s[w] if t == model.T.first() else model.s[w, t-1] # 仓库当期总流入 inflow = sum(model.x[f, w, t] for f in model.F) # 仓库当期总流出 outflow = sum(model.y[w, c, t] for c in model.C) # 平衡方程 return initial_s + inflow == outflow + model.s[w, t] model.StorageBalance = Constraint(model.W, model.T, rule=storage_balance_rule)
2. 仓库存储容量约束
若仓库有最大存储上限:
def storage_cap_rule(model, w, t): return model.s[w, t] <= model.cap_s[w] model.StorageCapacity = Constraint(model.W, model.T, rule=storage_cap_rule)
3. 期末存储边界约束(可选)
若要求最后一期保留最低存储量:
def end_storage_rule(model, w): return model.s[w, model.T.last()] >= model.min_end_s[w] model.EndStorage = Constraint(model.W, rule=end_storage_rule)
调试技巧
- 先单独验证平衡约束:固定存储量变量,检查流入流出的计算逻辑是否匹配
- 打印约束表达式:用
model.StorageBalance.pprint()输出约束的具体形式,确认变量索引、系数是否正确 - 检查变量定义域:确保存储量变量
s设置为within=NonNegativeReals,避免出现负存储的不合理解
内容的提问来源于stack exchange,提问作者DudeWah
相关产品推荐
相关产品推荐

