Skip to main content

How to code an index which is defined as for each and in the same time to sum over its variable range?

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff 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?.
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Ibrahim,

    You did not specify which terms are variables and which are scalar values so I just assumed that \(I,q,\) and \(d\) are all variables.
    I think what you need can be achieved via the function addVars() and quicksum() with two \(\texttt{for}\)-loops

    import gurobipy
    from gurobipy import *

    model = Model("test")

    Nc = [0,1,2,3,4]
    T = [0,1,2,3]

    I = model.addVars(Nc, T, name="I")
    q = model.addVars(Nc, T, name="q")
    d = model.addVars(Nc, T, name="d")

    for t in T:
    for j in Nc:
    model.addConstr( - I[j,t] + I[j,0] + quicksum((q[j,l]-d[j,l]) for l in range(0,t+1)) == 0, "myConstr_"+str(t)+"_"+str(j))

    model.write("test.lp")

    model.dispose()

    Note that I used the write() function to write a human readable LP file and with that check whether the constructed constraints are indeed correct.

    Best regards,
    Jaromił

    0
  • Ibrahim Fikry
    • Gurobi-versary
    • First Question
    • Conversationalist

    Hi Dr.Jaromil

     

    I got it, Thanks for your time and effort. I really appreciate it 

    0

Post is closed for comments.