GAMS调度模型代码报错排查及结果异常求助
GAMS调度模型报错排查请求
我的GAMS调度模型运行时报错,无法输出预期结果,以下是模型代码:
SETS i beds /1,2,3,4,5,6,7,8,9,10/ w weaving machines /W1,W2,W3,W4/ h handle machines /H1,H2,H3,H4/ e embriodery machines /E1,E2/ ; PARAMETERS demand(i) demand of bed i /1 545, 2 266, 3 179, 4 170, 5 137, 6 88, 7 83, 8 80, 9 67, 10 64/ max_work_time maximum work time for each machine /5850/ D(i,w) duration of bed i for operation o in machine w /1.W3 2.212, 1.W4 2.459, 2.W3 1.336, 2.W4 1.336, 3.W3 1.406, 3.W4 1.406, 4.W1 2.554, 4.W2 2.554, 5.W3 2.413, 5.W4 2.413, 6.W3 2.691, 6.W4 2.691, 7.W3 2.398, 7.W4 2.398, 8.W3 1.204, 8.W4 1.204, 9.W1 2.624, 9.W2 2.624, 10.W3 0.238, 10.W4 0.238/ S(i,e) duration of bed i for operation o in machine e / 1.E1 2.113, 1.E2 2.113, 5.E1 2.150, 5.E2 2.150, 6.E1 2.485, 6.E2 2.485, 7.E1 2.150, 7.E2 2.150/ F(i,h) duration of bed i for operation o in machine h /1.H3 0.154, 1.H4 0.154, 2.H1 0.162, 2.H2 0.162, 3.H1 0.154, 3.H2 0.154, 4.H3 0.154, 4.H4 0.154, 5.H3 0.154, 5.H4 0154, 6.H3 0.154, 6.H4 0.154, 7.H1 0.162, 7.H2 0.162, 8.H1 0.154, 8.H2 0.154, 9.H1 0.154, 9.H2 0.154, 10.H1 1.846, 10.H2 1.846/ ; Table R(w,i) relation between machine j and bed i 1 2 3 4 5 6 7 8 9 10 W1 0 0 0 1 0 0 0 0 1 0 W2 0 0 0 1 0 0 0 0 1 0 W3 1 1 1 0 1 1 1 1 0 1 W4 1 1 1 0 1 1 1 1 0 1 ; Table P(h,i) 1 2 3 4 5 6 7 8 9 10 H1 0 1 1 0 0 0 1 1 1 1 H2 0 1 1 0 0 0 1 1 1 1 H3 1 0 0 1 1 1 0 0 0 0 H4 1 0 0 1 1 1 0 0 0 0 ; Table U(e,i) 1 2 3 4 5 6 7 8 9 10 E1 1 0 0 0 1 1 1 0 0 0 E2 1 0 0 0 1 1 1 0 0 0 ; ; VARIABLES X(i,w) binary variable indicating if bed i is produced in machine j for operation o(weaving) Y(i,h) binary variable indicating if bed i is produced in machine j for operation o Z(i,e) binary variable indicating if bed i is produced in machine j for operation o T(i,w) starting time of operation o for bed i on machine w L(i,h) starting time of operation o for bed i on machine h J(i,e) starting time of operation o for bed i on machine e Comp(i) completion time of each i z total completion time to be minimized ; BINARY VARIABLES X(i,w) Y(i,h) Z(i,e) ; EQUATIONS objective objective function to minimize total completion time a(i,w) b(i,h) v(i,e) priority_weaving(i,w,h) ensures that weaving is completed before handle priority_handle(i,h,e) ensures that handle is completed before embroidery e1(w) e2(h) e3(e) c5(i) c6(i,w) c7(i,h) c8(i,e) balance(i) ensures that production balanced for every machine balance2(i) c2(i) ensures that the production meets the demand c4(i) ensures that the completion time is correctly calculated h1 h2 h3 h4 ; objective.. z =e= sum(i, Comp(i) * demand(i)); c4(i).. sum(e,J(i,e)+ S(i,e)) =e= Comp(i); a(i,w).. X(i,w) =l= R(w,i); b(i,h).. Y(i,h) =l= P(h,i); v(i,e).. Z(i,e) =l=U(e,i); e1(w)..sum(i,X(i,w)*D(i,w)) =l= max_work_time; e2(h)..sum(i,Y(i,h)*F(i,h)) =l= max_work_time; e3(e)..sum(i,Z(i,e)*S(i,e)) =l= max_work_time; priority_weaving(i,w,h).. T(i,w)+D(i,w)*X(i,w) =l= L(i,h); priority_handle(i,h,e).. L(i,h)+ F(i,h)*Y(i,h) =l= J(i,e); c2(i)..sum(h,Y(i,h)) =g= demand(i); balance(i).. sum(w,X(i,w)) =e= sum(h,Y(i,h))/4; balance2(i).. sum(h,Y(i,h))/8 =e= sum(e,Z(i,e)); h1..T("1","W3") =e= 0.000; h2..T("1","W4") =e= 0.000; h3..T("4","W1") =e= 0.000; h4..T("4","W2") =e= 0.000; c5(i).. Comp(i) =g= 0; c6(i,w).. T(i,w) =g= 0; c7(i,h).. L(i,h) =g= 0; c8(i,e).. J(i,e) =g= 0; MODEL Scheduling /all /; SOLVE Scheduling USING RMINLP MINIMIZING z;
代码中的关键问题排查
1. 参数语法错误
参数F(i,h)中,5.H4的取值为0154,缺少小数点,应为0.154。这个语法错误会直接导致GAMS解析失败。
2. 变量重复定义
在VARIABLES块中已经定义了X(i,w)、Y(i,h)、Z(i,e),又在BINARY VARIABLES块中重复定义这三个变量。GAMS不允许重复定义变量,需删除VARIABLES块中的这三个变量定义,仅保留BINARY VARIABLES块的定义。
3. 完工时间计算逻辑错误
方程c4(i)的写法sum(e,J(i,e)+ S(i,e)) =e= Comp(i)存在逻辑错误:
J(i,e)是刺绣工序在机器e上的开始时间,只有当Z(i,e)=1时该工序才实际执行,当前写法会将所有机器的时间累加,导致完工时间计算错误。- 正确写法应仅计算选中机器的完工时间,可改为线性化的大M约束,或调整为:
同时补充约束确保c4(i).. Comp(i) =g= J(i,e) + S(i,e)*Z(i,e);Comp(i)等于实际执行工序的最大完工时间(需结合大M实现)。
4. 工序优先级约束缺失大M,导致模型不可行
priority_weaving(i,w,h):当前约束T(i,w)+D(i,w)*X(i,w) =l= L(i,h)会强制所有h的handle工序开始时间晚于weaving完工时间,但如果Y(i,h)=0(该床品不在h上生产handle),L(i,h)可能为0,导致约束冲突。需添加大M常数:priority_weaving(i,w,h).. T(i,w)+D(i,w)*X(i,w) =l= L(i,h) + M*(1-Y(i,h));priority_handle(i,h,e)同理,需修改为:
其中priority_handle(i,h,e).. L(i,h)+ F(i,h)*Y(i,h) =l= J(i,e) + M*(1-Z(i,e));M为足够大的常数(例如大于max_work_time)。
5. 平衡约束导致整数冲突
balance(i):sum(w,X(i,w)) =e= sum(h,Y(i,h))/4,sum(h,Y(i,h))等于需求demand(i)(整数),除以4后可能得到非整数,但sum(w,X(i,w))是整数(二进制变量求和),导致约束无法满足,模型不可行。balance2(i):sum(h,Y(i,h))/8 =e= sum(e,Z(i,e))存在同样问题,整数除以8得到非整数,与整数求和结果矛盾。- 需重新梳理工序逻辑:每个床品应仅经过一次weaving、一次handle、一次刺绣(若需),因此正确约束应为:
同时将balance(i).. sum(w,X(i,w)) =e= demand(i); balance2(i).. sum(e,Z(i,e)) =e= demand(i);c2(i)改为sum(h,Y(i,h)) =e= demand(i)(确保生产数量恰好满足需求,而非大于等于)。
6. 强制开始时间约束可能限制可行性
h1-h4强制指定部分床品在特定机器的开始时间为0,若模型最优解不需要在这些机器上生产对应床品,该约束虽不冲突,但会限制模型的灵活性,可根据实际需求决定是否保留。
内容的提问来源于stack exchange,提问作者xtray nezuko
相关产品推荐
相关产品推荐

