Skip to main content

What is the alternate for @ operator in optimal transport problem in gurobi in python 2.7?

Answered

Comments

7 comments

  • David Torres Sanchez
    • Gurobi Staff Gurobi Staff

    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, 
    David

    0
  • vigneshwaran kannan
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • David Torres Sanchez
    • Gurobi Staff Gurobi Staff

    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
  • vigneshwaran kannan
    • Gurobi-versary
    • Curious
    • Conversationalist

    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)
    <ipython-input-80-563a2da9eb8d> in <module>
          1 #print(Kcost.flatten()[0][3])
    ----> 2 gurobi_minimize(Kcost.flatten(),3,3,np.append(rho_1,rho_2))
    
    <ipython-input-78-a85c2017206d> 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()
    
    <ipython-input-78-a85c2017206d> 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
  • David Torres Sanchez
    • Gurobi Staff Gurobi Staff

    Alternatively to the term-wise 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
  • vigneshwaran kannan
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • David Torres Sanchez
    • Gurobi Staff Gurobi Staff

    No worries, props to Jaromił Najman!

    0

Please sign in to leave a comment.