Skip to main content

Summation

Answered

Comments

1 comment

  • Alison Cozad
    • Gurobi Staff 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

Please sign in to leave a comment.