Skip to main content

How to define a hybrid variable or define two separate set?

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • 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

    It would be easier to define the variables with a single call to Model.addVars(), then modify the upper bounds afterwards. You can modify the upper bounds by individually setting the UB attribute of each Var object, or by using Model.setAttr() to modify multiple variable bounds at once. E.g., with this code:

    import gurobipy as gp
    from gurobipy import GRB

    # Dummy data
    V = list(range(4))
    E = [(i, j) for i in V for j in V if i != j]

    m = gp.Model()

    # Create all variables as integer with domain [0, 1]
    delta = m.addVars(E, vtype=GRB.INTEGER, ub=1, name='delta')

    # Modify upper bounds of variables for edges emanating from node 0
    m.setAttr('UB', delta.select(0, '*'), 2)

    # Display bounds
    m.update()
    for v in delta.values():
        print(f'{v.VarName}.UB = {v.UB}')

    we only increase the upper bounds of variables corresponding to edges emanating from node 0:

    delta[0,1].UB = 2.0
    delta[0,2].UB = 2.0
    delta[0,3].UB = 2.0
    delta[1,0].UB = 1.0
    delta[1,2].UB = 1.0
    delta[1,3].UB = 1.0
    delta[2,0].UB = 1.0
    delta[2,1].UB = 1.0
    delta[2,3].UB = 1.0
    delta[3,0].UB = 1.0
    delta[3,1].UB = 1.0
    delta[3,2].UB = 1.0
    0

Post is closed for comments.