What is the alternate for @ operator in optimal transport problem in gurobi in python 2.7?
AnsweredBelow is my code for optimal transport. As I am going to use python 2.7 for running my complete code, the code below does not work in the python 2.7 version as the code involves @ symbol. Can someone help me with the alternate for the @ symbol in python 2.7 and I am using Gurobi 9.5. I cannot switch to python 3 as some of the other modules are not supported in python 3.
def gurobi_minimize(cost,m,n,tempr):
#cost: cost array
#m,n: size of the two input distributions
#concatenating two input distributions into one.
print("m,n=",m,n)
sz=m*n
#calling Short function to generate the sparse LP matrix for optimal transport
# pi_mat is the LP sparse matrix for optimal transport
pi_mat=Short_PI(m,n)
m=gpy.Model('ot')
pi=m.addMVar(sz,lb=0.0,ub=1.0)
m.setObjective(cost @ pi,GRB.MINIMIZE)
m.addConstr(pi_mat @ pi==tempr)
m.optimize()
m.printQuality()
#m.printstats()
allvars = m.getVars()
gurobi_pi=[]
obj1 = m.getObjective()
print("OBJECTIVE VALUE=",obj1.getValue())
for v in allvars:
gurobi_pi.append(v.X)
gurobi_pi=np.array(gurobi_pi)
return gurobi_pi

Hi,
I think the @ symbol was introduced in Python 3.5 and indicates matrix multiplication.
I would say the easiest way around this is using the standard API i.e. model.addVars instead of addMVar and so on.
Also, please note that Gurobi 10.0.0 has dropped support for Python 2.7.
Cheers,
David0 
David Torres Sanchez Thank you so much! But I also need to add constraints and there again I encounter
@ symbol. How do I replace the @ symbol in model.addConstr() method in the code.
Edit: One more thing, LP matrix that I have used here "pi_mat" is actually a sparse matrix and Hence I need to multiply this LP sparse matrix with "pi" and equating it to the constraints on the RHS which is the array "tempr"
0 
Well, I am not sure about the dimensions of your data.
But you just need to explicitly write each term by term operation.
For example, assuming they are all the same dimensions:pi = m.addVars(sz, lb=0.0, ub=1.0, obj=cost)
# Use obj keyword argument above or expression below
# m.setObjective(sum(pi[i] * cost[i] for i range(sz)))
m.addConstrs((pi_mat[i] * pi[i] == tempr[i] for i in range(sz)))0 
David Torres Sanchez Thanks but I still face the issues. Even after modifications as the LP matrix pi_mat is actually a sparse matrix and hence I couldn't multiply as you mentioned in the above correction. It throws the following error message. How could I multiply a LP sparse matrix pi_mat with constraint (unknown) variable pi in the code ?
TypeError Traceback (most recent call last) <ipythoninput80563a2da9eb8d> in <module> 1 #print(Kcost.flatten()[0][3]) > 2 gurobi_minimize(Kcost.flatten(),3,3,np.append(rho_1,rho_2)) <ipythoninput78a85c2017206d> in gurobi_minimize(cost, m, n, tempr) 19 m.setObjective(gpy.quicksum(cost[i]*pi[i] for i in range(sz))) 20 > 21 m.addConstrs((pi_mat[i] * pi[i] == tempr[i] for i in range(sz))) 22 #m.addConstrs((pi_mat[i] * pi[i] == tempr[i] for i in range(sz))) This line copied from gurobi community guide line 23 src\gurobipy\model.pxi in gurobipy.Model.addConstrs() <ipythoninput78a85c2017206d> in <genexpr>(.0) 19 m.setObjective(gpy.quicksum(cost[i]*pi[i] for i in range(sz))) 20 > 21 m.addConstrs((pi_mat[i] * pi[i] == tempr[i] for i in range(sz))) 22 #m.addConstrs((pi_mat[i] * pi[i] == tempr[i] for i in range(sz))) This line copied from gurobi community guide line 23 TypeError: 'coo_matrix' object is not subscriptable
0 
Alternatively to the termwise translation, you actually also use the Model.setMObjective() and Model.addMConstr() instead of the @ operator:
m.setMObjective(None, cost, 0.0, None, None, pi, GRB.MINIMIZE)
m.addMConstr(pi_mat, pi, '=', tempr)0 
David Torres Sanchez Thank you so much. You solved my problem I ll read further about the difference between all the methods you a recommended
0 
No worries, props to Jaromił Najman!
0
Please sign in to leave a comment.
Comments
7 comments