GurobiError: Invalid argument to LinExpr multiplication

2,267 views
Skip to first unread message

Camille

unread,
Apr 7, 2016, 9:29:24 AM4/7/16
to Gurobi Optimization
I am using Spyder (Python 2.7). 
I am getting the following error: GurobiError: Invalid argument to LinExpr multiplication
I am still quite new to programming in general. Is there someone who can explain me what I am doing wrong ? 

Thanks,
Camille

from gurobipy import *
kostenInputFile = open("INFLAKOSTEN.txt");
scoresInputFile = open("OPBRENGST.txt");
kostenArray = kostenInputFile.read().split();
scoresArray = scoresInputFile.read().split();
print "Kosten::"
print str(kostenArray)
print "Scores::"
print str(scoresArray)
 
m = Model("mip1")
 
projectEnabledVars = [];
for i in xrange(0, 600):
    projectEnabledVars.append(m.addVar(vtype=GRB.BINARY, name="X_"+str(i)))
m.update();
 
objective = 0;
for i in xrange(0, len(scoresArray)):
    objective+=projectEnabledVars[i]*scoresArray[i]
m.setObjective(objective, GRB.MAXIMIZE)
m.update();
 
constraint = 0;
for i in xrange(0, len(scoresArray)):
    constraint += projectEnabledVars[i] * kostenArray[i]
m.addConstr(constraint <= 30000000, "Budget")
m.update();
 
m.optimize();
 
print('Obj: %g' % m.objVal)
for v in m.getVars():
    print('%s %g' % (v.varName, v.x))

Kostja Siefen

unread,
Apr 11, 2016, 8:34:33 AM4/11/16
to Gurobi Optimization
Hi Camille,

Make sure you convert your input data to a proper data type (e.g. convert values of kostenArray and scoresArray to float). 

To create linear expressions (objective / left-hand side of constraints) you can use the quicksum() function, e.g.

objective = quicksum([ projectEnabledVars[i] * scoresArray[i] for i in xrange(len(scoresArray)) ])
m.setObjective(objective, GRB.MAXIMIZE)

Best wishes,
Kostja
Reply all
Reply to author
Forward
0 new messages