You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CPLEX生产计划问题提前期建模:约束索引正确性咨询

生产计划CPLEX建模:提前期约束的索引修正

你当前代码里Xin的索引下界-4是硬编码的,问题在于它完全依赖当前LeadTime数组的最大值(现在是5,1-5=-4),如果后续LeadTime的最大值发生变化(比如改成6),就会出现索引越界的错误,而且这种写法也不够健壮。

解决方法是动态计算最大提前期,用这个值来确定Xin的索引范围:

  1. 先定义一个参数计算所有工序的最大提前期:
    int MaxLeadTime = max(l in Operations) LeadTime[l];
    
  2. 用这个动态计算的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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:12:43