简体   繁体   English

Pyomo中的依赖二进制变量

[英]dependent binary variables in Pyomo

I'm having problems formulating a constraint in Pyomo where I want to determine a dependency between two variables.我在 Pyomo 中制定约束时遇到问题,我想确定两个变量之间的依赖关系。 In my model I have 3 binary variables in total:在我的 model 中,我总共有 3 个二进制变量:

model.x = pyo.Var(model.M, domain=pyo.Binary)
model.y = pyo.Var(model.M, domain=pyo.Binary)
model.z = pyo.Var(model.M, domain=pyo.Binary)

My objective function with the parameter model.l would be:我的目标 function 与参数model.l将是:

obj_expr = sum(model.l[i] * model.x[i] + model.l[i] * model.y[i] for i in model.M)
model.obj = pyo.Objective(sense=pyo.maximize, expr=obj_expr)

I would like model.z to be the opposite of model.x , so that a 1 in model.x[i] would be a 0 in model.z[i]. I would like model.z to be the opposite of model.x , so that a 1 in model.x[i] would be a 0 in model.z[i].

I tried to do this with the following constraint:我尝试使用以下约束来做到这一点:

    def dependent_var_con(model, i):
        lhs = model.z[i]
        rhs = model.x[i]
        return lhs != rhs 
    model.con = pyo.Constraint(model.M, expr=dependent_var_con)

Unfortunately this doesn't work and I'm getting the following error:不幸的是,这不起作用,我收到以下错误:

Cannot convert non-constant Pyomo expression (z[0] == x[0]) to bool.无法将非常量 Pyomo 表达式 (z[0] == x[0]) 转换为布尔值。 This error is usually caused by using a Var, unit, or mutable Param in a Boolean context such as an "if" statement, or when checking container membership or equality.此错误通常是由在 Boolean 上下文(例如“if”语句)中使用 Var、单位或可变参数引起的,或者在检查容器成员资格或相等性时引起。

Does anyone have an idea how to deal with this issue?有谁知道如何处理这个问题? Thank you!谢谢!

You cannot use the "not equal" logical operator in a linear program.您不能在线性程序中使用“不等于”逻辑运算符。 All you get are inequalities and equal.你得到的只是不平等和平等。 :) :)

However, if you just want to invert a binary variable, it is pretty straightforward with a little algebra.然而,如果你只是想反转一个二进制变量,用一点代数就很简单了。 In your case:在你的情况下:

z[i] == 1 - x[i]

pop that in a constraint for all i为所有i弹出一个约束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM