Skip to main content

Indexing over specific sets depending on index of variable

Awaiting user input

Comments

1 comment

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Johanna,

    I think something like the following should work

    import gurobipy as gp

    m = gp.Model()

    T = [7,8,9] 
    V = [0,1,2,3] 
    U = [[0,1,2], [1,2], [2,3,4], [3,4]] #set of indices

    b = m.addVars(T,5,name="b") # here 5 is set as second dimension because the indices in U go up to 4

    for v in V:
      L = U[v] # get index list U[v]
        for t in T[:-1]: # don't use last index in T to avoid key errors
            m.addConstr(b[t,v] <= gp.quicksum(b[t+1, l] for l in L), name="con_v%d_t%d"%(v,t))

    m.write("myLP.lp") # write human readable LP file to analyze

    Is this what you were looking for? You can have a look at the generated \(\texttt{myLP.lp}\) file to check whether the constraints look as they should.

    Best regards, 
    Jaromił

    0

Please sign in to leave a comment.