简体   繁体   English

Gurobi:如何添加约束x1 * x2 * x3

[英]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 ,其中x1x2x3是具有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 - 2n <= 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");

Example

暂无
暂无

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

相关问题 如何重新格式化 GetVertices 以返回 (x1,y1,0), (x2,y2,0), (x3,y3,0);? - How to re format GetVertices to return (x1,y1,0), (x2,y2,0), (x3,y3,0);? 返回方程 x1 + x2 + x3 = num 的解数 - return number of solutions to the equation x1 + x2 + x3 = num 给定一个整数数组[x0 x1 x2],如何计算从[0 0 0]到[x0 x1 x2]的所有可能排列? - Given an array of integers [x0 x1 x2], how do you calculate all possible permutations from [0 0 0] to [x0 x1 x2]? e ^ x = 1 + x + x2 / 2! + x3 / 3! + x4 / 4! &lt;&lt;使用Java方法给x的力量 - e^x = 1 + x + x2/2! + x3/3! + x4/4! << e to the x power using java method 如何划分字符串(x1,y1)(x2,y2)格式 - How to divide string (x1,y1)(x2,y2) format 给定两个点 A(x1,y1) &amp; B(x2,y2),我想在球面上找到第三点 C(x3,y3) 和线 AB 之间的距离和 AD 的长度 - Given two points A(x1,y1) & B(x2,y2), I want to find the distance between the third point C(x3,y3) and line AB and length of AD on spherical surface 按钮无法绘制具有坐标(x1,x2,y1,y2)的折线图 - Button not working to draw line graph having its coordinate (x1, x2, y1, y2) 如果我们知道距离x1,y1,y2,则计算x2 - Calculate x2 if we know distance, x1, y1, y2 Firefox Webdriver 实例“打开 x2 窗口”而不是仅“x1 窗口” - Firefox Webdriver Instance 'Opens x2 Windows' instead of only 'x1 Window' 仅使用增量,减量,循环和= 0来制作pow(x1,x2)函数 - Making a pow(x1, x2) function using only increments, decrements, loops, and =0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM