我正在使用PYOMO,我想在“P_constraint_rule”中实现逻辑OR,但我无法实现。
我的模型中有一些部分:
model.s = Param(within=NonNegativeIntegers)
model.S = RangeSet(1, model.s)
model.f = Param(within=NonNegativeIntegers)
model.F = RangeSet(1, model.f)
model.p = Param(within=NonNegativeIntegers)
model.P = RangeSet(0, model.p)
model.m = Param(within=NonNegativeIntegers)
model.M = RangeSet(1, model.m)
model.g = Param(model.M, default=5.0)
model.b2 = Param(model.F,model.F, within=Binary, default=0)
model.xf1f2 = Var(model.F, model.F, within=Binary)
model.y = Var(model.M, within=Binary)
model.xf = Var(model.F, within=Binary)
model.aff = Param(model.F,model.F, within=NonNegativeIntegers, default=0)
...
model.x = Var(model.index_sfpm, within=Binary)
model.b1 = Param(model.index_sfpm, within=Binary, default=0)
def Obj_rule(model):
expr = 0.0
for (s,f,p,m) in model.index_sfpm:
expr += model.g[m] * model.xf[f] * model.b1[s,f,p,m] * model.x[s,f,p,m]
for m in model.M:
expr += model.g[m] * model.y[m]
return expr
model.OBJ = Objective(rule=Obj_rule, sense=maximize)
def P_constraint_rule (model, f1, f2):
expr = 0.0
for (s,m) in model.index_sm:
expr += model.b2[f1,f2] * model.xf[f1] * model.b1[s,f1,1,m] * model.x[s,f1,1,m]
expr += model.xf[f2] * model.b1[s,f2,1,m] * model.x[s,f2,1,m]
e.g。在我的.dat:param aff:= 1 7 10
return expr == model.aff[f1,f2] | expr == 0
model.PConstraint = Constraint(model.index_f1f2, rule=P_constraint_rule)
当我使用“|”时,我收到以下错误:
ERROR: Rule failed when generating expression for constraint PConstraint with index (7, 3):
TypeError: unsupported operand type(s) for |: 'int' and '_SumExpression' ERROR: Constructing component 'PConstraint' from data=None failed:
TypeError: unsupported operand type(s) for |: 'int' and '_SumExpression' [ 1.72] Pyomo Finished ERROR: Unexpected exception while running model:
unsupported operand type(s) for |: 'int' and '_SumExpression'
当我使用“||”
时ERROR: Unexpected exception while loading model:
invalid syntax
当评论该约束时,模型和gurobi运行良好。
有人可以帮我解决这些错误吗?
还有其他可能使用量词吗? 不等式P1.constraint应该对model.index_f1f2有效 方程P2Constraint应该对model.F的2个元素或model.index_f1f2的1个元素有效 像这样的东西:
def P1_constraint_rule (model, f1, f2):
expr = 0.0
for (s,m) in model.index_sm:
expr += model.b2[f1,f2] * model.xf[f1] * model.b1[s,f1,1,m] * model.x[s,f1,1,m]
expr += model.xf[f2] * model.b1[s,f2,1,m] * model.x[s,f2,1,m]
return expr <= model.aff[f1,f2]
model.P1Constraint = Constraint(model.index_f1f2, rule=P1_constraint_rule)
def P2_constraint_rule (model, f1, f2):
expr = 0.0
for (s,m) in model.index_sm:
expr += model.b2[f1,f2] * model.xf[f1] * model.b1[s,f1,1,m] * model.x[s,f1,1,m]
expr += model.xf[f2] * model.b1[s,f2,1,m] * model.x[s,f2,1,m]
#this equation should be valid for 2 elements of model.F or 1 element of model.index_f1f2
return expr == model.aff[f1,f2]
model.P2Constraint = Constraint(model.index_f1f2, rule=P2_constraint_rule)
提前谢谢你,拉拉
答案 0 :(得分:3)
错误是因为您试图指定非代数约束。从概念上讲,以下内容将定义逻辑分离:
expr == model.aff[f1,f2] | expr == 0
针对具体的句法问题:
|
是二进制OR。它比关系运算符更紧密,因此不会做你想要的。||
无效的Python语法or
实现。这将是很好的语法支持 - 但是,Pyomo目前不支持它。您有两个选项来指定这样的约束:或者(1)使用pyomo.gdp
扩展名将其指定为析取,然后利用pyomo.gdp
中的转换将析取程序放回MIP ,或(2)使用例如Big-M松弛明确地放松分离。要做前者,你需要定义两个析取然后分离:
from pyomo.gdp import *
def P_disjunct_rule (b, f1, f2, i):
model = b.model()
expr = 0.0
for (s,m) in model.index_sm:
expr += model.b2[f1,f2] * model.xf[f1] * model.b1[s,f1,1,m] * model.x[s,f1,1,m]
expr += model.xf[f2] * model.b1[s,f2,1,m] * model.x[s,f2,1,m]
if i:
return expr == model.aff[f1,f2]
else:
return expr == 0
model.PDisjunct = Disjunct(model.index_f1f2, [0,1], rule=P_constraint_rule)
def P_disjunction_rule(m,f1,f2):
return [ m.PDisjunct[f1,f2,i] for i in [0,1] ]
model.PDisjunction = Disjunction(model.index_f1f2, rule=P_Disjunction_rule)
然后,您需要调用转换以将析取转换回代数约束。注意:转换需要你的Pyomo变量都有下限和上限,或者你需要通过模型上的BigM
后缀指定有效的“Big-M”值。你可以:
--transform=gdp.bigm
或--transform=gdp.chull
)在BuildAction
def xfrm(m):
TransformationFactory('gdp.bigm').apply_to(m)
model.xfrm = BuildAction(rule=xfrm)
显式调用转换作为自定义驱动程序脚本的一部分。
pyomo.gdp
的替代方法是自己明确地实现放松。您需要添加一个二进制变量(让我们称之为y
),指示析取的哪一侧必须为True,然后使用该二进制变量显式放宽两个析取。从概念上讲,你会转向
expr == model.aff[f1,f2] | expr == 0
到
expr - model.aff[f1.f2] <= M1 * y
model.aff[f1.f2] - expr <= M2 * y
expr <= M3 * (1-y)
expr >- M4 * (1-y)
请注意,根据expr
和aff
的界限,其中一些约束可能是多余的。此外,四个“Big-M”(大常数)可能不一定需要不同,尽管如果你能确定每个M的最小有效值,问题会更好地解决。
答案 1 :(得分:0)
我用Big-M-relaxation实现了约束。它有效。
此外,我简化了expr-和return-statement。
对于每个f(前f1,f2),(model.b [s,f,1,m] * model.x [s,f,1,m])的S和M之和应等于0 OR model.af [f](以前的model.aff [f1,f2])。
model.af [f]也用作绑定M。
## 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)