addGenConstrOr - does the list need to just be 0 and 1 values?
AnsweredHey all - looking for an answer to a quick question. I have variable that I want to set to 1 if two other related variables are also 1. My idea is to apply addGenConstrOr to the following list. Variables a and b have multiple sub-variables if you will. If for any of the sub-variables if both a and b ==1 i want x to equal 1. this list howver will result in some 0,1,and 0.5 values. I want to achieve this without adding new variables, as I am aiming to simplify my LP with this constraint.
[(a_1+ b_1) / 2, (a_2+ b_2) / 2, ... (a_n+ b_n) / 2]
-
Hi Summer,
I have variable that I want to set to 1 if two other related variables are also 1
Could you specify the value the binary variable \(x\) should assume if at least one of the connected binary variables is 0?
- If your goal is to model the binary variable \(x\) such that it equals 1 if and only if both related binary variables \(a\) and \(b\) are 1, then the Gurobi Python API Model.addGenConstrAnd() is what you need. This approach effectively employs a logical AND constraint, mirroring the addition of the linear constraints \(x \leq a\), \(x \leq b\), and \(x \geq a + b - 1\) to your model.
- To model the binary variable \(x\) as 1 when both binary variables \(a\) and \(b\) are 1, adding the constraint \(x \geq a + b - 1\) is sufficient. This allows the binary variable \(x\) to be 0 or 1 whenever either the binary variables \(a\) or \(b\) is 0.
It appears you are focusing on the second case. By applying this logic, you can add the constraints \(x \geq a_i + b_i - 1\) for every \(i=1, \cdots, n\), ensuring the variable \(x\) equals 1 if, for any subset \(i\), both \(a_i\) and \(b_i\) are 1. Otherwise, the variable \(x\) may be 0 or 1.
Note that as a commercial customer, you have access to one-on-one support. Feel free to raise a ticket through our Help Center for quicker assistance.
Best regards,
Maliheh
0 -
Hi Maliheh,
Thank you for the quick response! I'm trying to model the condition if any values in a list ==1, that x must equal 1, where the values in the list are calculated by [(a_1+ b_1) / 2, (a_2+ b_2) / 2, ... (a_n+ b_n) / 2]. If any of these values ==1 (meaning that a and b are both ==1 for a specific subset (1,2,...n), then I want x to be ==1.
For context, I am modeling a roundtable meeting schedule where there are multiple tables(1,2 ... n) , and I want to know if two people (a,b) sit at the same table using the variable x. Previously I created variables for each pair (a,b) of people for each table, and then used addGenConstrOr with that list, however I am attempting to remove the variables for each table and and pair by having only the variable x showing if they are ever at the same table.0 -
Thanks for the clarification.
Adding the constraints \(x \geq a_i + b_i - 1\) for every \(i\) ensures the binary variable \(x\) equals 1 if person \(a\) and person \(b\) are both assigned to table \(i\). It does not guarantee that the variable \(x\) equals 0 if the person \(a\) is, for example, assigned to table \(i\) and the person \(b\) is assigned to a different table \(j\). If there is no incentive in the objective function to force the variable \(x\) to 0 if people \(a\) and \(b\) are assigned to different tables, you need to use auxiliary variables as you described.
Best regards,
Maliheh
0
Please sign in to leave a comment.
Comments
3 comments