Skip to main content

Efficient of defining decision variables

Answered

Comments

2 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    You can provide a list of tuples holding all indices of interest to the addVars method instead of just all possible combinations. For example

    import gurobipy as gp
    from gurobipy import GRB

    m = gp.Model()

    I = [1,2,3]
    J = [1,2,3,4]
    K = [1,2,3,4,5]

    x = m.addVars(I,J,K) # defines I x J x K = 3 * 4 * 5 variables
    # define indices of interest
    indices = [(1,2,3),(1,3,4),(2,1,5),(3,4,2),(3,1,5)]
    y = m.addVars(indices, name="y") # defines only 5 variables of interest
    m.update()

    for i in indices:
    print(y[i]) # access y variables via tuples defined in indices list

    # Console output:
    # <gurobi.Var y[1,2,3]>
    # <gurobi.Var y[1,3,4]>
    # <gurobi.Var y[2,1,5]>
    # <gurobi.Var y[3,4,2]>
    # <gurobi.Var y[3,1,5]>

    The hard part which is up to you is to efficiently construct the indices list. This should be possible, given the fact, that you have the \(\texttt{ts_j}\) dictionary.

    Best regards, 
    Jaromił

    0
  • whwhwh55
    Gurobi-versary
    Conversationalist
    First Question

    Thank you very much for the prompt reply. This seems to be a very good approach. I will try. Sometimes I don’t have the right keywords searching for the answers. I hope these examples may be present somewhere in the online help or documentation.

    0

Please sign in to leave a comment.