Error when using Sparse matrix with Gurobi variables
AnsweredHi,
I have a dense matrix H which I have transformed into a sparse matrix.
from scipy import sparse
sH = sparse.csr_matrix(H)
I have a D matrix which is an identity matrix with Gurobi variables in the diagonal <gurobi.Var z[i,j]>.
I try to multiple the following:
r = np.dot(np.transpose(sH),D)
This is the error I am getting: ( I do not get this error if my matrices are not sparse)
---------------------------------------------------------------------------
TypeError
Traceback (most recent call last)
var.pxi in gurobipy.Var.__rmul__()
TypeError: float() argument must be a string or a number, not 'csc_matrix'
During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last)
linexpr.pxi in gurobipy.LinExpr.__imul__()
TypeError: float() argument must be a string or a number, not 'csc_matrix'
During handling of the above exception, another exception occurred: GurobiError Traceback (most recent call last) <ipython-input-74-48094dbb4ef2> in <module>()
----> 1 r = np.dot(np.transpose(sH),D)
<__array_function__ internals> in dot(*args, **kwargs)
var.pxi in gurobipy.Var.__rmul__()
linexpr.pxi in gurobipy.LinExpr.__imul__()
linexpr.pxi in gurobipy.LinExpr.__mul__()
linexpr.pxi in gurobipy.LinExpr._mul()
GurobiError: Invalid argument to LinExpr multiplication
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Sagnik,
Could you provide a minimal working example producing the error you posted?
In case, you were able to solve the problem, could you provide the solution of how you achieved it?
Best regards,
Jaromił0 -
This question is old, but so as it has an answer: you can't use numpy functions on scipy sparse objects. The transpose should be sH.T and the dot product should be done via the @ operator: sH @ D .
0
Post is closed for comments.
Comments
3 comments