メインコンテンツへスキップ

Very weird output of binary variable?

回答済み

コメント

7件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Sonja Mars
    • Gurobi Staff Gurobi Staff

    Hi,

    Can you show us some sample code with outputs of what you are doing? Also what programming language are you using? Are you sure you are using the same precision for these values/comparisons?

    Best regards,

      Sonja

    0
  • Bradley Ning
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Sonja:

    Thanks for your reply, I have copied a screen shot here. From the screen shot, we can find that:

    print(ttt['A']['A'], x_ij['B']['C'])

    the output is: <gurobi.Var T_A_A (value 0.0)> <gurobi.Var x_ij_B_C_ (value 1.0)>

    Both are BINARY variables, one var's value is 0, another is 1, while when I calculate the 'or' of these two variable, the output value is still 0?

    x = ttt['A']['A'] or x_ij['B']['C']
    print(x)

    output is: <gurobi.Var T_A_A (value 0.0)>

    Why is it not 1, as '0 or 1' should be 1?

    And if I print(ttt['A']['A'].x or x_ij['B']['C'].x), it is 1 as we expected.

    Thanks

    Bradley

    0
  • Bradley Ning
    • Gurobi-versary
    • First Comment
    • First Question

    My logic experssion and operation is: T[i][j] = T[i][j] or ( T[i][k] and T[k][j] ) with i, j, k in T.keys() loops

    T[i][j] are binary variables, it looks like Guroby does not support such logic operation?

    If not, how to implement such operation? many thanks.

    Bradley

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Bradley,

    As you noticed, you need to use the X attributes of Var objects to retrieve their values after optimizing. If you don't use this attribute, you are instead comparing the Python objects themselves. Based on how Python handles these object comparisons, the statement

    x = ttt['A']['A'] or x_ij['B']['C']

    will assign the Var object ttt['A']['A'] to the Python variable x (as long as ttt['A']['A'] is not None).

    Note that this statement does not assign a value of 0 or 1 to x. Rather, it assigns the entire Var object ttt['A']['A'] to x. When you print a Var object after optimizing, the solution value (X attribute) is included in the print statement for convenience.

    I hope this helps. Thanks!

    Eli

    0
  • Bradley Ning
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Eli,

    I understand how to get the value of a variable, while what I want is add constraint to T[i][j], T[i][j] are binary variables, and T[i][j] = T[i][j] or ( T[i][k] and T[k][j] ) with i, j, k in T.keys() loops.

    Any suggestions on how to implement such logic operation in Gurobi? thanks.

    Bradley

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Bradley,

    Can you clarify what you are trying to do? It sounds like you want to add a constraint that T[i][j] is equal to one of two values: either T[i][j], or T[i][k]*T[k][j].

    If this is your intent, note that it is not meaningful to have a constraint that would set a variable equal to itself. This constraint is always satisfied.

    The following linear constraints set a binary variable x equal to the product of two binary variables y and z:

    x >= y + z - 1
    x <= y
    x <= z

    If y and/or z are 0, these constraints ensure x equals 0. If y and z both equal 1, then x also equals 1. In your case, this would look like:

    model.addConstr(T[i][j] >= T[i][k] + T[k][j])
    model.addConstr(T[i][j] <= T[i][k])
    model.addConstr(T[i][j] <= T[k][j])

    However, if you add these constraints for every combination of i, j, and k, you are saying you want T[i][j] to equal the product T[i][k]*T[k][j] for all k; I'm not sure this is what you want.

    Thanks!

    Eli

    0

投稿コメントは受け付けていません。