Skip to main content

Extract info from Gurobi binary variables during run-time

Answered

Comments

4 comments

  • Official comment
    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?.
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Chenguang,

    These sound like modeling questions. It's common to use binary variables to ensure that certain logical conditions are satisfied.

    For (1), consider a fixed time i and operation v. Assuming y is a nonnegative "volume" variable, we can model this as follows:

    \( y_{iv} \geq 100 ( Z_{iv} - \sum_{j > i} Z_{jv} ) \)

    If Z(i,v) = 0 or operation v is performed again at a time later than i, the constraint is redundant due to the nonnegativity of y. However, if Z(i,v) = 1 and is the last such i, this constraint becomes y(i, v) >= 100, as desired.

    Perhaps you can do something similar for (2)? It's tough to say without additional information about the penalty. I imagine that this is possible to do by introducing additional "indicator" binary variables that are equal to 1 if a penalty should be incurred, and 0 otherwise. This is done by modeling a logical constraint, not unlike what we did for (1). Then, these variables are incorporated into the objective function.

    I hope this helps!

    Eli

    0
  • Chenguang Zhang
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Eli,

    Thanks for your brilliant solution! I also have another quick question to ask, or more precisely, a confirmation to make.

    Like I said, we have an $Z_{iv}$ binary array. Is there a way to quickly identify those i-index where $Z_{iv}=1$, so I can use those i-index somewhere else (in constraint, penalty, for example)? Is this a model formulation issue, or it is directly support by some Gurobi syntax?

    I ask this because Gurobi has syntax sugar to support certain things (for example, absolute value).

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Chenguang,

    This is more of a model formulation question. The idea is to exploit the fact that Z_{iv} is binary in order to enforce logical conditions. For example, if we want to add a penalty of 10 to our objective if Z_{iv} = 1, we can simply add the term 10 Z_{iv} to the objective function.

    If we want to enforce a constraint if Z_{iv} = 1, this is possible as well, but the exact approach depends on the particular constraint you want to add. Fortunately, Gurobi provides a shortcut method for doing this. For example, in our Python API, you can do something like this:

    model.addConstr((Z[i,v] == 1) >> (x + y <= 1.5))

    In this case, if Z_{iv} = 1, then the constraint x + y <= 1.5 must hold. See the documentation for the Model.addGenConstrIndicator() method for more information.

    I hope this helps. Thanks!

    Eli

    0

Post is closed for comments.