user3544665
user3544665

Reputation: 55

Gurobi: Unable to convert argument to an expression

I'm kinda new to gurobi on python

Is there someone who can explain me what I'm doing wrong? I get the error :

gurobipy.GurobiError: Unable to convert argument to an expression

when I call :

m.setObjective(obj,GRB.MINIMIZE)

My code:

m = Model("mdp")    

v=[]
for i in range(nblignes):
    for j in range(nbcolonnes): 
        v.append(m.addVar(vtype=GRB.CONTINUOUS, lb=0, name="v%d" % (i*10+j)))

m.update()

c=np.zeros((len(v),1), dtype=numpy.int)
for k in range(len(v)):
    c[k]= 1

obj = LinExpr();
obj =0
for j in range(nbcolonnes*nblignes):
    obj += c[j] * v[j]
print "OBJ",obj  

m.setObjective(obj,GRB.MINIMIZE)

Upvotes: 0

Views: 1725

Answers (1)

David Nehme
David Nehme

Reputation: 21572

Your array 'c' is a len(v) x 1 matrix, so when you add c[j] * v[j], you multiply a vector by an gurobi Var object. You can fix this by either declaring the array as 1-D with

c=np.zeros(len(v), dtype=numpy.int)

or replacing your final loop with

for j in range(nbcolonnes*nblignes):
     obj += c[j][0] * v[j]

or more succinctly

obj = quicksum(c[:,0]*v)

Upvotes: 1

Related Questions