CPLEX生产计划问题提前期建模:约束索引正确性咨询
生产计划CPLEX建模:提前期约束的索引修正
你当前代码里Xin的索引下界-4是硬编码的,问题在于它完全依赖当前LeadTime数组的最大值(现在是5,1-5=-4),如果后续LeadTime的最大值发生变化(比如改成6),就会出现索引越界的错误,而且这种写法也不够健壮。
解决方法是动态计算最大提前期,用这个值来确定Xin的索引范围:
- 先定义一个参数计算所有工序的最大提前期:
int MaxLeadTime = max(l in Operations) LeadTime[l]; - 用这个动态计算的
MaxLeadTime替换硬编码的-4,修改Xin的变量定义:dvar int+ Xin[Products][-MaxLeadTime..NbPeriods];
这样不管LeadTime里的最大值怎么变,Xin的索引范围都能覆盖所有可能的t - LeadTime[l]取值(因为t最小是1,LeadTime[l]最大是MaxLeadTime,所以t - LeadTime[l]的最小值是1 - MaxLeadTime,而-MaxLeadTime比这个值还要小,完全能覆盖)。
修改后的完整代码如下:
Data: Products = {P1 P2 P3}; NbOperations = 10; NbPeriods = 3; LeadTime = [1,1,3,5,0,0,3,0,0,0]; {string} Products = ...; int NbOperations = ...; range Operations = 1..NbOperations; int NbPeriods = ...; range Periods = 1..NbPeriods; int LeadTime[Operations]= ...; // 动态计算所有工序的最大提前期 int MaxLeadTime = max(l in Operations) LeadTime[l]; dvar int+ Y[Products][0..NbOperations][Periods]; // 基于最大提前期动态设置Xin的索引范围 dvar int+ Xin[Products][-MaxLeadTime..NbPeriods]; forall(p in Products,l in Operations,t in Periods) ct3: Xin[p][t-LeadTime[l]] == Y[p][l][t];
额外说明:如果业务逻辑中,t - LeadTime[l] < 1的周期没有实际输入需求,可以给这些Xin变量添加约束,比如:
forall(p in Products, idx in -MaxLeadTime..0) Xin[p][idx] == 0;
具体可根据你的生产计划规则调整。
内容的提问来源于stack exchange,提问作者Lew Xin Yan
相关产品推荐
相关产品推荐

