<p>谢谢@jsiirola。在</p>
<p>我用大M松弛实现了约束。它起作用了。在</p>
<p>此外,我简化了expr-and-return语句。在</p>
<p>对于每个f(原f1,f2),(b型[S,f,1,M]*model.x[S,f,1,M])的S和M上的和应等于0或模型.af[f] (前模型.aff[f1,f2])。在</p>
<p>在模型.af[f] 也用作绑定M</p>
<pre><code>## Big-M Relaxation
def P1_constraint_rule (model, f):
expr = 0
for (s,m) in model.index_sm:
expr += model.b[s,f,1,m] * model.x[s,f,1,m]
return expr <= model.af[f] * model.xf[f]
model.P1Constraint = Constraint(model.F, rule=P1_constraint_rule)
def P2_constraint_rule (model, f):
expr = 0
for (s,m) in model.index_sm:
expr += model.b[s,f,1,m] * model.x[s,f,1,m]
return expr >= model.af[f] * model.xf[f]
model.P2Constraint = Constraint(model.F, rule=P2_constraint_rule)
def P3_constraint_rule (model, f):
expr = 0
for (s,m) in model.index_sm:
expr += model.b[s,f,1,m] * model.x[s,f,1,m]
return expr - model.af[f] <= model.af[f] * (1- model.xf[f])
model.P3Constraint = Constraint(model.F, rule=P3_constraint_rule)
def P4_constraint_rule (model, f):
expr = 0
for (s,m) in model.index_sm:
expr += model.b[s,f,1,m] * model.x[s,f,1,m]
return model.af[f] - expr <= model.af[f] * (1- model.xf[f])
model.P4Constraint = Constraint(model.F, rule=P4_constraint_rule)
</code></pre>