Skip to main content

Merging a binary decision variable with a continuous variable (semi-binary/continuous)

Answered

Comments

2 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    You can do this by setting the \( \texttt{vtype} \) keyword argument to a \( \texttt{dict} \) that maps each arc to either \( \texttt{GRB.CONTINUOUS} \) or \( \texttt{GRB.BINARY} \). For example:

    import random

    vtypes = {(i, j): GRB.CONTINUOUS if random.random() < 0.5 else GRB.BINARY for (i, j) in arcs}
    x = m.addVars(arcs, vtype=vtypes, name='x')

    If you want the continuous variables to be bounded above by \( 1 \), you should include the argument \( \texttt{ub=1} \) in your call to Model.addVars().

    Alternatively, you could:

    • Set the \( \texttt{vtype} \) argument equal to a list that's the same length as \( \texttt{arcs} \), or
    • After calling Model.addVars(), iterate over the variables and individually set their VType attributes.
    1
  • Vincent
    Gurobi-versary
    Curious
    Conversationalist

    Thanks very much for your quick and detailed response.

    0

Please sign in to leave a comment.