Skip to main content

Summation

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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Alison Cozad
    • Gurobi Staff

    There are a few ways you can do this.  Here is one way to accomplish the nested summations using quicksum():

    import gurobipy as gp
    from gurobipy import GRB
    import numpy as np

    # Create a new model
    model = gp.Model('Nested Summation')

    # Define sets
    H = range(0,2)
    I = range(0,3)
    J = range(0,4)
    K = range(0,5)
    T = range(0,6)

    # Define cost[i,j,h]
    cost = np.random.random((len(I), len(J), len(H)))

    # Create flow[i,j,t,h,k] variable
    flow = model.addVars(I, J, T, H, K,
                         vtype=GRB.CONTINUOUS, 
                         name="flow")

    # Set objective
    model.setObjective(gp.quicksum(cost[i][j][h]*flow[i,j,t,h,k]
                                   for t in T
                                   for k in K
                                   for j in J
                                   for i in I
                                   for h in H
                                ))

    I have assumed that cost is constant here.

     

    Here are some materials that build on this:

    0

Post is closed for comments.