I need help with LinExpr
AnsweredI am trying to code a simple objective function which works fine. I read that quicksum() isn't the best way to model large objective function. I would like to know how I can model my objective function and constraints with LinExpr. The examples on the website are not very helpful.
I have 5 terms that make the objective function. I would like to write these in terms of LinExpr.
Here "O, U, Q, R, alpha" are variables created using dictionaries in pythongurobi interface
Term1 = quicksum(quicksum(O[(d,k)]*Co for k in range(1,K+1)) for d in range(1,delta+1))
Term2 = quicksum(quicksum(U[(d,k)]*Cu for k in range(1,K+1)) for d in range(1,delta+1))
Term3 = quicksum(quicksum(Q[(k,d)]*Ck for k in range(1,K+1)) for d in range(1,delta+1))
Term4 = quicksum(quicksum(R[i,r]*CR[r1] for r in range(1,Ra+1)) for i in range(1,N+1))
Term5 = quicksum(quicksum(quicksum(quicksum(quicksum(alpha[i,inst,t,d]* InstrumentType[inst1,r1] *CF[r1] for inst in range(1,M+1)) for t in range(1,T+1)) for i in range(1,N+1)) for d in range(1,delta+1)) for r in range(1,Ra+1))
I want to be able to write this in following form
Objective = Term1+Term2+Term3+Term4+Term5
If you can show me how I can model one of these with LinExpr(), I would be able to implement this for all my constraints.

Hi Amogh,
The fastest way is to use the LinExpr() constructor or the addTerms() function, where you provide 2 lists. The first list holds the coefficients and the second one holds the variables. The code
import gurobipy
from gurobipy import *
m = Model("test")
coeff = [1,2,3]
x = m.addVars(3,name="x")
lexpr = LinExpr(coeff,x.select('*'))would produce the term \(\texttt{lexpr =}\) \(1\cdot x_1 + 2 \cdot x_2 + 3 \cdot x_3\). The select() function is required to convert a tupledict to a tuplelist.
Please note that the Community can only help in a more specific way if you provide enough details on your code to make it reproducible. In best case, try to provide a minimal working example.Best regards,
Jaromił0 
Hello Jaromił,
Thank you for your response. I believe my question is very straightforward. I am not able to find any code with two dimensional list coefficient and dictionary variable.
Can you provide the same example with 2*2 coefficient matrix ?
import gurobipy
from gurobipy import *
m = Model("test")
coeff = [[1,2],[4,5]]
x = {}
for i in range(2):
for j in range(2):
print(i,j)
print(coeff[i][j])
x[(i,j)] = m.addVar(vtype = GRB.BINARY, name="x%d,%d" % (i,j))
lexpr = LinExpr(coeff,x.select('*'))The final expression does not work with error,
dict' object has no attribute 'select'
Thanks
Amogh
0 
Hi Amogh,
This will not work with multidimensional matrices. However, you can just flatten your matrix.
import gurobipy
from gurobipy import *
m = Model("test")
coeff = [[1,2],[4,5]]
I=[0,1]
J=[0,1]
x = m.addVars(I, J, vtype = GRB.BINARY, name="x")
lexpr = LinExpr([c for c1 in coeff for c in c1],x.select('*'))
m.setObjective(lexpr)
m.write("myLP.lp") # writes LP file for sanity checkThis way you keep your \(\texttt{coeff}\) object as a 2D list. You can access the variables \(\texttt{x}\) via \(\texttt{x[i,j]}\).
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
3 comments