Skip to main content

Indexing over specific sets depending on index of variable

Awaiting user input

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • 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

Post is closed for comments.