Skip to main content

addGenConstrOr - does the list need to just be 0 and 1 values?

Answered

Comments

3 comments

  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    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?

    1. 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.
    2. 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
  • Summer Purschke
    First Comment
    First Question

    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
  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    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.