Summation
AnsweredHello Gurobi team/experts,
How can I code this objective function in gurobi?
0
-
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:
- Python I: Introduction to Modeling with Python - Gurobi. This is your best place to start. It includes a video tutorial and examples with jupyter notebooks to follow. It also includes an example of using nested summations.
- netflow.py: This is a Gurobi example of how to do nested summations in two different ways: (a) using quicksum() and (b) using tupledict.sum()
0
Please sign in to leave a comment.
Comments
1 comment