CPLEX OPL多产品多周期库存路径问题约束5索引越界求助
解决CPLEX中多产品多周期库存路径模型的Constraint5索引越界问题
错误原因分析
报错Index out of bound for array "r": 1的核心原因是:
- 库存变量
r的时间维度定义为Time = 1..T(即1到12),但Constraint5中使用了t-1作为时间索引。当DemandSet中存在t=1的记录时,t-1=0超出了Time的合法范围,触发索引越界。
解决方案
针对初始库存(t=0时刻)的处理,提供两种可行方案:
方案1:扩展库存变量的时间范围
直接将库存变量r的时间维度从Time扩展为0..T,包含初始时刻(t=0):
dvar float r[ProductSet][CustomerSupplierSet][0..Time];//Amount of on-hand inventory of product p by customer I
同时补充初始库存约束(示例为初始库存设为0,也可从数据文件读取):
// 初始库存约束 forall(p in ProductSet, i in CustomerSupplierSet) { r[p][i][0] == 0; }
修改后Constraint5无需调整,t-1在t=1时对应t=0,属于合法索引。
方案2:单独定义初始库存参数
若不想扩展时间维度,可定义初始库存参数,在Constraint5中对t=1的情况单独处理:
- 添加初始库存参数:
float initial_r[ProductSet][CustomerSupplierSet] = ...;// 可设为0或从数据文件读取
- 修改Constraint5:
//Constraint 5 forall (<p,i,t,d> in DemandSet) { if (t == 1) { initial_r[p][i] - r[p][i][t] + sum (v in Vehicle) (sum(j in CustomerSupplierSet) y[p][v][<j,i>][t] + sum(k in CustomerSupplierSet) y[p][v][<i,k>][t]) == d; } else { r[p][i][t-1] - r[p][i][t] + sum (v in Vehicle) (sum(j in CustomerSupplierSet) y[p][v][<j,i>][t] + sum(k in CustomerSupplierSet) y[p][v][<i,k>][t]) == d; } }
额外检查建议
- 确认DemandSet中的
t值均在1..T范围内,避免出现其他非法时间值 - 检查
ProductSet和CustomerSupplierSet是否包含DemandSet中所有的p和i,防止其他维度的索引越界
内容的提问来源于stack exchange,提问作者Jay Park
相关产品推荐
相关产品推荐

