Affected versions: v10.0.x, v11.0.0, v11.0.1, v11.0.2
Resolved in version: Gurobi v11.0.3
The matrix-friendly API in gurobipy is not yet compatible with numpy version 2.0. If you have installed gurobipy alongside numpy version 2.0, you may encounter the following error for some model-building operations:
ValueError: Unable to avoid copy while creating an array as requested.
To resolve the error, users should downgrade their numpy version to the latest version before 2.0 (currently this is numpy version 1.26.4).
Example case
This error can arise in several different situations. One example is adding together the results of indexing into an MVar:
>>> import gurobipy as gp
>>> env = gp.Env()
>>> model = gp.Model(env=env)
>>>
>>> x = model.addMVar((3,))
>>> x[0] + x[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/gurobipy/mvar.pxi", line 357, in gurobipy.MVar.__add__
...
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
Workaround
We recommend that users ensure they using numpy version 1.x until Gurobi 11.0.3 is released. This is best achieved by providing a version constraint in your package manager.
Using pip:
pip install gurobipy[matrixapi] "numpy<2"
Using conda:
conda install gurobi "numpy<2" scipy
Using a requirements.txt file with the following content:
gurobipy[matrixapi]
numpy<2
Best practice: pinning specific compatible versions
Note that best practice when deploying an application into production is to pin all dependencies to known working versions and review periodically (with careful testing) before making any changes. For example the following requirements.txt file will ensure a compatible environment:
gurobipy[matrixapi]==11.0.2
numpy==1.26.4
scipy==1.13.1
Comments
0 comments
Article is closed for comments.