Affected versions: v9.0.x, v9.1.x, v9.5.0, v9.5.1, v9.5.2, v10.0.0, v1 0.0.1 , v10.0.2
Resolved in version: Gurobi v10.0.3
Issue with sparse array incompatibility with the matrixfriendly API for scipy >= 1.11
For scipy >= 1.11, the sparse array classes in scipy are incompatible with gurobipy matrixfriendly objects and methods. For example, you may encounter the following error:
>>> import gurobipy as gp
>>> import scipy.sparse as sp
>>>
>>> data = [1.0, 1.0, 1.0, 1.0]
>>> indices = [2, 0, 2, 1]
>>> indptr = [0, 2, 4]
>>> A = sp.csr_array((data, indices, indptr))
>>>
>>> model = gp.Model()
>>> x = model.addMVar((3,))
>>> A @ x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/gurobipy/mvar.pxi", line 388, in gurobipy.MVar.__rmatmul__
File "src/gurobipy/mlinexpr.pxi", line 333, in gurobipy.MLinExpr._from_matmul_right
File "src/gurobipy/mlinexpr.pxi", line 292, in gurobipy.MLinExpr._from_compact_affine_expr
File "src/gurobipy/mlinexpr.pxi", line 59, in gurobipy.MLinExpr.__init__
AssertionError
Similarly, directly adding matrix constraints will fail:
>>> model.addMConstr(A, x, '=', np.zeros((2,)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/gurobipy/model.pxi", line 3863, in gurobipy.Model.addMConstr
ValueError: Buffer dtype mismatch, expected 'int' but got 'long'
Workaround
To fix the issue with Gurobi 10.0.2 and prior versions, use the older style sparse matrix objects from scipy instead, i.e.
>>> A = sp.csr_matrix((data, indices, indptr))
Alternatively, you can downgrade to scipy < 1.11. Upgrading to Gurobi 10.0.3 or later will resolve the issue and allow the sparse array types to be used with the latest scipy version.
Further details can be found in the scipy 1.11.0 release notes.
Deprecation warnings from numpy 1.25
For numpy >= 1.25, you may see the following warning:
DeprecationWarning: Conversion of an array with ndim > 0 to a scalar
is deprecated, and will error in future. Ensure you extract a single
element from your array before performing this operation. (Deprecated
NumPy 1.25.)
There are several cases where this can occur:
 When working with size1 objects in the matrixfriendly API, for example:
>>> import gurobipy as gp
>>> import numpy as np
>>>
>>> model = gp.Model()
>>> x = model.addMVar((1,))
>>> model.update()
>>> x.lb = np.ones((1,))
<stdin>:1: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)This is correct code and you do not need to make any changes. The warning will no longer occur with Gurobi versions 10.0.3 and later.

When using size1 numpy objects as scalar values in the termbased API, for example:
>>> import gurobipy as gp
>>> import numpy as np
>>>
>>> ub = np.ones(shape=(1,))
>>> model.addVar(ub=ub)
<stdin>:1: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
<gurobi.Var *Awaiting Model Update*>In this case, you should adjust your code to convert size1 numpy arrays to scalars before calling gurobipy method. This is done by calling .item() on size1 arrays, as follows:
>>> model.addVar(ub=ub.item())
<gurobi.Var *Awaiting Model Update*>or by indexing appropriately to create a zero dimensional object:
>>> model.addVar(ub=ub[0])
<gurobi.Var *Awaiting Model Update*>
Further details can be found in the numpy 1.25 release notes.
Comments
0 comments
Article is closed for comments.