[英]Gurobi: how to add a constraint x1*x2*x3
I wish to add a constraint n1 = x1 * x2 *x3
in Gurobi, where x1
, x2
and x3
are variables with 0 or 1. Unfortunately, can not find the solution. 我希望在Gurobi中添加一个约束
n1 = x1 * x2 *x3
,其中x1
, x2
和x3
是具有0或1的变量。不幸的是,找不到解决方案。
Can someone help me? 有人能帮我吗?
You are trying to create a seemingly non-linear constraint on binary variables. 您正在尝试对二进制变量创建一个看似非线性的约束。 You can model this as a series of linear constraints by noting the
n
will have the value 1 if and only if x1, x2, and x3 are all 1. 您可以通过将线性建模为一系列线性约束,只要且仅当x1,x2和x3均为1时,
n
的值为1。
// only if part
model.addConstr(n, GRB.LESS_EQUAL, x1);
model.addConstr(n, GRB.LESS_EQUAL, x2);
model.addConstr(n, GRB.LESS_EQUAL, x3);
// if part
GRBLinExpr all_three;
all_three.addTerm(1.0, x1);
all_three.addTerm(1.0, x2);
all_three.addTerm(1.0, x3);
all_three.addConstrant(-2);
model.addConstr(n, GRB.GREATER_EQUAL, all_three);
This adds the constraints n >= x1 + x2 + x3 - 2
and n <= min(x1, x2, x3)
. 这将添加约束
n >= x1 + x2 + x3 - 2
和n <= min(x1, x2, x3)
。
n1<=x1
n1<=x2
n1<=x3
2+n1>=x1+x2+x3
If any of the of the x are 0, then n will be force to zero. 如果x中的任何一个为0,则n将强制为零。 If all are 1, n is forced to 1.
如果全部为1,则将n强制为1。
Edit Since Gurobi recognizes binary variables, you could just use 编辑由于Gurobi可以识别二进制变量,因此您可以使用
3*n1<=x1+x2+x3
2+n1>=x1+x2+x3
The first only allow 0 and 1 while this would allow fractional values if not for the binary requirement. 第一个只允许0和1,而如果不是二进制要求,这将允许小数。
EDIT The constraint 编辑约束
n3 = x2 * (n1 + n2 - n1 * n2) + x1 * (n1 - n2) *(n1 - n2)
appears to be trying to enforce the logic 似乎正在尝试执行逻辑
IF n1 AND n2:
n3 = x2
IF n1 XOR n2:
n3 = x1
IF (NOT n1) AND (NOT n2):
n3 = 0
Since it the standard rule for expressing boolean logic operations in zero-one integer linear programming (ILP) take the form of x1 AND x2 IMPLIES y1
, I reconstructed the above to read 由于以零一整数线性编程(ILP)表示布尔逻辑运算的标准规则采用
x1 AND x2 IMPLIES y1
的形式,因此我将上面的内容重构为
n1 AND n2 IMPLIES i1
n1 XOR n2 IMPLIES i2
(NOT n1) AND (NOT n2) IMPLIES i3
Constructing the constraints for i1, i2, i3
is given below 下面给出构造
i1, i2, i3
的约束
IF n1 AND n2, THEN i1
i1 ≥ n1 + n2 − 1
i1 ≤ n1
i1 ≤ n2
0 ≤ i1 ≤ 1
IF n1 XOR n2, THEN i2
i2 ≤ n1 + n2
i2 ≥ n1 − n2
i2 ≥ n2 − n1
i2 ≤ 2 − n1 − n2
0 ≤ i2 ≤ 1
IF NOT n1 AND NOT n2, THEN i3
i3 ≥ 1 - n1 - n2
i3 ≤ (1 - n1)
i3 ≤ (1 - n2)
0 ≤ i3 ≤ 1
This gives use three mutually exclusive indicators and the original problem can be rewritten as 这样可以使用三个互斥的指标,并且原始问题可以重写为
-(1 - i1) ≤ n3 - x2 ≤ (1 - i1)
-(1 - i2) ≤ n3 - x1 ≤ (1 - i2)
-(1 - i3) ≤ n3 ≤ (1 - i3)
Simplify way to convert binary string to integer. 简化将二进制字符串转换为整数的方法。 And, multiply it.
并且,乘以它。 Is it not OK?
这样不好吗
int x1 = Integer.parseInt("101011", 2);
int x2 = Integer.parseInt("00010", 2);
int x3 = Integer.parseInt("000101", 2);
int n1 = x1 * x2 * x3;
System.out.println(n1);
Update 更新
I don't know Gurobi
, but 我不知道
Gurobi
,但是
GRBVar x1 = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x1");
GRBVar x2 = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x2");
GRBVar x3 = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x3");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.