Skip to main content

Binary variable ignored at multiplication

Answered

Comments

3 comments

  • Riley Clement
    • Gurobi Staff

    Hi Zheren,

    It's not quite clear to me what you mean by the value of "a".

    Consider the following minimum reproducible example

    import guropbipy

    m = gp.Model()

    n=1
    M=5

    x = m.addVar(vtype=gp.GRB.BINARY)

    a = n*M*(1-x)


    In this case the variable "a" is assigned a linear expression

    >> type(a)
    gurobipy.LinExpr

    and

    >> a.getValue()
    AttributeError: Index out of range for attribute 'X'

    fails because we have not optimized and found a solution.

    So when you say that "a", or any variable or expression in the model, has a value (prior to model update, and more importantly model optimization) it is not clear what you are referring to.

    Are you able to elaborate more, and use the code above to explain what you mean?

    Thanks

    - Riley

    0
  • Zheren Zhang
    • Gurobi-versary
    • Curious
    • Conversationalist

    Hi Riley

    Thank you for the reply and sorry for my unclear discription. In this case the variable "a" is an gurobipy linear expression which is a sum that every summand of it is expected to contain a gurobi variable.

    I´ve inspected my code once more and I realize that the gurobi variable x of this linear expression is not ignored, just the constant term (n*M) is pushed to the beginning of the expression and the term -n*m*x is at the last position. There must be other wrong implementations that make my code not work as expected.

    Sorry for taking your time because of my carelessness.

    Best regards 

    Zheren

    0
  • Riley Clement
    • Gurobi Staff

    Hi Zheren,

    No need to apologize, it's all part of learning, and happens to the best of us :)

    - Riley

    0

Please sign in to leave a comment.